[erlang-questions] Escript doc mismatch?

Håkan Mattsson hakan@REDACTED
Fri Sep 25 11:41:29 CEST 2009


On Fri, Sep 25, 2009 at 4:52 AM, Michael Richter <ttmrichter@REDACTED> wrote:
> Quoting from http://www.erlang.org/doc/man/escript.html:
>
> The header of an Erlang script differs from a normal Erlang module. The
>> first line is intended to be the interpreter line, which invokes escript.
>> However if you invoke escript like this
>>
>> $ *escript factorial 5*
>>
>> *the contents of the first line does not matter, but it cannot contain
>> Erlang code as it will be ignored. *

The documentation needs to be updated.

If the escript has a header, the first line in the file must start with #!.
If you run the escript by explicitly invoking the escript executable,
e.g. like "escript factorial 5", the characters after #! on the first line
are ignored.

As the header is optional you can in fact  "execute" an .erl, .beam or
.zip file as long as escript can find the main/1 function.

# cat factorial.erl
-module(factorial).
-export([main/1, fac/1]).

main([String]) ->
    try
        N = list_to_integer(String),
        F = fac(N),
        io:format("factorial ~w = ~w\n", [N,F])
    catch
        _:_ ->
            usage()
    end;
main(_) ->
    usage().

usage() ->
    io:format("usage: factorial integer\n"),
    halt(1).

fac(0) -> 1;
fac(N) -> N * fac(N-1).

# escript factorial.erl 5
factorial 5 = 120

# erlc factorial.erl
# escript factorial.beam 4
factorial 4 = 24

# zip factorial.zip factorial.erl factorial.beam
  adding: factorial.erl (deflated 44%)
  adding: factorial.beam (deflated 29%)
# rm factorial.erl
# rm factorial.beam
# escript factorial.zip 7
factorial 7 = 5040

/Håkan


More information about the erlang-questions mailing list