A simple one pass "load and go"
Erlang scripting interface
Joe Armstrong
Robert Virding
3 - April - 2001
Last revised 20 - February - 2004 - tested with R11-B3 on Solaris with a bash shell.
./fibc abc Usage fibThe fibi script is as follows:> ./factorial abc Usage factorial <Int> > ./fibc 20 fib 20 = 10946
#!/usr/bin/env escript %% Example of an interpreted script %% Usage: %% fib-export([main/1]). main([X]) -> case (catch list_to_integer(X)) of {'EXIT', _} -> usage(); J -> N = fib(J), io:format("fib ~w = ~w~n",[J, N]) end; main(_) -> usage(). usage() -> io:format("Usage fib ~n"). fib(0) -> 1; fib(1) -> 1; fib(N) -> fib(N-1) + fib(N-2).
The default mode of escript is to interpret the code. By adding the attribute:
-mode(compile).
Will cause the code in the script to be compiled, instead of interpreted. Note that for many scripts interpreting the code is much faster than compiling the code. We can time the code as follows:
$ time fibc 20 fib 20 = 10946 real 0m1.041s user 0m0.540s sys 0m0.280s
Here is a table comparing fibi (fibonacci interpreted) with fibc (compiled):
EXpression | time (secs) |
fibi 20 | 0.93 |
fibc 20 | 1.041 |
fibc 30 | 1.36 |
fibi 30 | 38.90 |
You have to decide for yourself which is faster.
The origonal version was writen by Joe Armstrong and Robert Virding probably on 3 april 2001.
Up to version-3.0 needed a slightly hacked version of erl_eval.erl. These changes are now integrated into the official distribution.