A Prolog source file can be used directly as a Unix program using the
#! magic start. The same mechanism is useful for
specifying additional parameters for running a Prolog file on Windows.
#! magic is allowed because if the first letter of a Prolog
#, the first line is treated as a comment.11The
can be the legal start of a normal Prolog clause. In the unlikely case
this is required, leave the first line blank or add a header comment.
To create a Prolog script, use one of the two alternatives below as
first line. The first can be used to bind a script to a specific Prolog
installation, while the latter uses the default prolog installed in
\verb$#!/path/to/swipl$ \verb$#!/usr/bin/env swipl$
The interpretation of arguments to the executable in the
HashBang line differs between Unix-derived systems. For
#! must be followed immediately with an
absolute path to the executable and should have none or one argument.
Neither the executable path, nor the argument shall use quotes or
spaces. When started this way, the Prolog flag argv
contains the command line arguments that follow the script invocation.
Below is a simple script doing expression evaluation:
#!/usr/bin/env swipl :- initialization main. eval :- current_prolog_flag(argv, Argv), concat_atom(Argv, ' ', SingleArg), term_to_atom(Term, SingleArg), Val is Term, format('~w~n', [Val]). main :- catch(eval, E, (print_message(error, E), fail)), halt. main :- halt(1).
And here are two example runs:
% ./eval 1+2 3 % ./eval foo ERROR: is/2: Arithmetic: `foo/0' is not a function %
The Windows version simply ignores the
versions extracted command line arguments from the HashBang
line. As of version 5.9 all relevant setup can be achieved using directives.
Due to the compatibility issues around HashBang line
processing, we decided to remove it completely.