escript exception error, undefined function, why can't use fun in escript script?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

escript exception error, undefined function, why can't use fun in escript script?

lib nl

Hi,

 

On Linux Mint 18.2 + Erlang/OTP 20.0, there are two escript scripts: a and b.

 

1.      a

#!/usr/bin/env escript

main(_) ->

       L = lists:map(fun(N) -> 2*N end, lists:seq(1,10)),

       io:format("~p~n", [L]).

 

 

2.      b

#!/usr/bin/env escript

main(_) ->

       L = lists:map(fun f2/1, lists:seq(1,10)),

       io:format("~p~n", [L]).

 

f2(N) -> 2 * N.

 

 

$ chmod u+x a

$ ./a

[2,4,6,8,10,12,14,16,18,20]

 

$ chmod u+x b

$ ./b

escript: exception error: undefined function erl_eval:f2/1

 

 

So why cant use fun in escript script? Is this a limitation or known issue? Are there any references that can give more info regarding this behavior? Thanks!

 

 

 

Thanks!

 

 


_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: escript exception error, undefined function, why can't use fun in escript script?

Karl Velicka
Hi,

I've run into this myself once - it seems that you can't have
functions other than main/1 in escripts, although this doesn't appear
to be documented.

The way around this is to make your scripts compiled, these can have
multiple functions defined in them. You'll need to add this line
somewhere in your .escript file:

-mode(compile).


Cheers,
Karl

On 6 August 2017 at 12:44, lib nl <[hidden email]> wrote:

> Hi,
>
>
>
> On Linux Mint 18.2 + Erlang/OTP 20.0, there are two escript scripts: a and
> b.
>
>
>
> 1.      a
>
> #!/usr/bin/env escript
>
> main(_) ->
>
>        L = lists:map(fun(N) -> 2*N end, lists:seq(1,10)),
>
>        io:format("~p~n", [L]).
>
>
>
>
>
> 2.      b
>
> #!/usr/bin/env escript
>
> main(_) ->
>
>        L = lists:map(fun f2/1, lists:seq(1,10)),
>
>        io:format("~p~n", [L]).
>
>
>
> f2(N) -> 2 * N.
>
>
>
>
>
> $ chmod u+x a
>
> $ ./a
>
> [2,4,6,8,10,12,14,16,18,20]
>
>
>
> $ chmod u+x b
>
> $ ./b
>
> escript: exception error: undefined function erl_eval:f2/1
>
>
>
>
>
> So why can’t use fun in escript script? Is this a limitation or known issue?
> Are there any references that can give more info regarding this behavior?
> Thanks!
>
>
>
>
>
>
>
> Thanks!
>
>
>
>
>
>
> _______________________________________________
> erlang-questions mailing list
> [hidden email]
> http://erlang.org/mailman/listinfo/erlang-questions
>
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: escript exception error, undefined function, why can't use fun in escript script?

Håkan Mattsson
Yes, you may have other functions than main/1 in escripts. But the escript must be compiled.
​​
Either compiled in run-time
​(using -mode(compile)
​)​
​or pre-compiled ​(using a beam file or archive).

/Håkan

On Sun, Aug 6, 2017 at 3:44 PM, Karl Velicka <[hidden email]> wrote:
Hi,

I've run into this myself once - it seems that you can't have
functions other than main/1 in escripts, although this doesn't appear
to be documented.

The way around this is to make your scripts compiled, these can have
multiple functions defined in them. You'll need to add this line
somewhere in your .escript file:

-mode(compile).


Cheers,
Karl

On 6 August 2017 at 12:44, lib nl <[hidden email]> wrote:
> Hi,
>
>
>
> On Linux Mint 18.2 + Erlang/OTP 20.0, there are two escript scripts: a and
> b.
>
>
>
> 1.      a
>
> #!/usr/bin/env escript
>
> main(_) ->
>
>        L = lists:map(fun(N) -> 2*N end, lists:seq(1,10)),
>
>        io:format("~p~n", [L]).
>
>
>
>
>
> 2.      b
>
> #!/usr/bin/env escript
>
> main(_) ->
>
>        L = lists:map(fun f2/1, lists:seq(1,10)),
>
>        io:format("~p~n", [L]).
>
>
>
> f2(N) -> 2 * N.
>
>
>
>
>
> $ chmod u+x a
>
> $ ./a
>
> [2,4,6,8,10,12,14,16,18,20]
>
>
>
> $ chmod u+x b
>
> $ ./b
>
> escript: exception error: undefined function erl_eval:f2/1
>
>
>
>
>
> So why can’t use fun in escript script? Is this a limitation or known issue?
> Are there any references that can give more info regarding this behavior?
> Thanks!
>
>
>
>
>
>
>
> Thanks!
>
>
>
>
>
>
> _______________________________________________
> erlang-questions mailing list
> [hidden email]
> http://erlang.org/mailman/listinfo/erlang-questions
>
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions


_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: escript exception error, undefined function, why can't use fun in escript script?

Stanislaw Klekot
On Sun, Aug 06, 2017 at 04:22:33PM +0200, Håkan Mattsson wrote:
> Yes, you may have other functions than main/1 in escripts. But the escript
> must be compiled.
> ​​
> Either compiled in run-time
> ​(using -mode(compile)
> ​)​
> ​or pre-compiled ​(using a beam file or archive).

Actually no, you don't need the escript to be compiled to have functions
in there. They work just fine, it's just that they cannot be referenced
with `fun foo/X' syntax if the escript script is not marked as compiled.

--
Stanislaw Klekot
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: escript exception error, undefined function, why can't use fun in escript script?

lib nl
In reply to this post by lib nl

 

After adding the following line

 

       -mode(compile).

 

It works like a charm.

 

 

Thanks!

 


_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions