[erlang-questions] escript vs erl

Paul Davis paul.joseph.davis@REDACTED
Thu Feb 24 00:00:34 CET 2011


On Wed, Feb 23, 2011 at 5:45 PM, Andrew Pennebaker
<andrew.pennebaker@REDACTED> wrote:
> When I run my Erlang scripts with escript, threads throw an undef error when
> trying to call themselves recursively.
>
> E.g.
>
> loop() ->
>   io:format("Looping~n"),
>
>   receive
>   after
>      3 -> loop()
>   end.
>
> main(_) ->
>   spawn(loop, loop, []).
>
> $ escript loop.erl
>
> =ERROR REPORT==== 23-Feb-2011::17:18:05 ===
> Error in process <0.30.0> with exit value: {undef,[{loop,loop,[]}]}
>
> The mitigation is to run erl and compile loop.erl into loop.beam before
> running.
>
> $ erl
> 1> c(loop).
> 2> q().
> $ escript loop.erl
> Looping
> Looping
> Looping
> ...
>
> Could Erlang be modified so that recursive functions work interactively with
> escript, rather than requiring explicit compilation first?
>
> Specs:
>
> Erlang R14B01
> MacPorts 1.9.2
> Mac OS X 10.6.6
> MacBook Pro 5,1
>
> Cheers,
> Andrew Pennebaker
>

Your particular case is a bit misleading. Escript does not evaluate in
the context of a module named after the script file. In your
particular case when you compile the loop.erl into a beam file, the
module will be found, but its not using the version in your .erl file.
A way to demonstrate this would be to compile your loop.erl and then
change the definition of the loop function without recompiling.

In other news, you can just write your spawn using an anonymous
function that calls loop like such:

    spawn(fun() -> loop() end).

HTH,
Paul Davis


More information about the erlang-questions mailing list