[erlang-questions] escript question

Garrett Smith <>
Mon Sep 19 16:22:01 CEST 2011


On Sun, Sep 18, 2011 at 4:15 PM, Chris Hicks
<> wrote:
> I was thinking about this some more, and doing some more reading, and if I
> have a process which uses os:cmd on a unix box to run the escript and if
> doing so starts up a full Erlang node, is there any reason that the Pid of
> the calling process couldn't be passed as one of the arguments? Couldn't the
> script simply send a message to that Pid and once the script exits and
> returns a value to the process that called os:cmd, that process would then
> do a receive to retrieve the message?
>
> On a related note, can an escript be run as a text string instead of a file
> residing on the system? Basically, I'm trying to figure out if I can just
> hold all my scripts in a DB, pull them out as needed and execute them rather
> than actually creating a file that is run.

By running as an escript, you're forcing yourself to go through stdio.
IMO the only reason you'd want to do this would be run the escript
from a shell or other programs.

If all you want to do is ship around your code to be used dynamically,
check out erl_parse and erl_eval (as suggested by Tim).

E.g. store a function in your database that increments a number by 1:

FStr = "fun(N) -> N + 1 end."

When you're ready to use it:

{ok, Tokens, _} = erl_scan:string(FStr),
{ok, Exprs} = erl_parse:parse_exprs(Tokens),
{value, F, []} = erl_eval:exprs(Exprs, []).

Now:

2 = F(1).



More information about the erlang-questions mailing list