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, make the first line start like this:
Prolog recognises this starting sequence and causes the interpreter to receive the following argument list:
Instead of -s, the user may use -f to stop Prolog from looking for a personal initialisation file.
Here is a simple script doing expression evaluation:
#!/usr/bin/swipl -q -t main -f eval :- current_prolog_flag(argv, Argv), append(_, [--|Args], Argv), concat_atom(Args, ' ', 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: Arithmetic: `foo/0' is not a function %
The Windows version supports the
too, but here it serves a rather different role. The Windows shell
already allows the user to start Prolog source files directly through
the Windows file-type association. However, Windows makes it rather
complicated to provide additional parameters for opening an individual
Prolog file. If the file starts with
#!, the first line is
analysed to obtain additional command line arguments. The example below
runs the system in `quiet' mode.
#!/usr/bin/swipl -q -s ....
Note the use of
/usr/bin/swipl, which specifies the
interpreter. This argument is ignored in the Windows version, but must
be present to ensure best cross-platform compatibility.