ocamlscript compiles scripts, i.e. one-file programs, into natively-compiled binaries, and executes them.
Basically, a script named
alpha is compiled into
alpha.exe is more recent
alpha.exe is executed directly,
alpha is recompiled first.
Compilation commands reside in the first section of the script. They are written in OCaml and consist normally in setting one or two variables.
The ocamlscript tool is made of two essential components: a command named ocamlscript and a library which is made available in the compilation section of the scripts.
The basic usage of the ocamlscript command was designed to be intuitive:
Script files do not have to follow the conventions of standard OCaml
file names. They can also be read from stdin or directly from the command-line.
Full details on the command-line options are returned by
An ocamlscript script differs from a standard OCaml file at its beginning. There is an optional first section that instructs ocamlscript on how to compile the script. Basically, variables that hold package names and additional preprocessing or compilation option can be set. A simple script that fetches a web page using the HTTP client provided by the netclient package could be:
#! /usr/bin/env ocamlscript Ocaml.packs := [ "netclient" ] -- match Sys.argv with [| _; url |] -> print_string (Http_client.Convenience.http_get url) | _ -> prerr_string "Usage: getpage URL\n"; exit 1
For complete details, see the API of the ocamlscript compilation library.
For simple examples of original uses of ocamlscript, you can browse the collection of examples extracted from the distribution of ocamlscript.
More documentation, although somewhat incomplete, is given on the wiki site.
The development version of Ocamlscript is hosted on GitHub.