[erlang-questions] Calling external modules from a fun

Robert Virding rvirding@REDACTED
Wed Apr 28 22:17:34 CEST 2010


On 28 April 2010 17:19, Raimo Niskanen
<raimo+erlang-questions@REDACTED> wrote:
> Try this on any two nodes (substitute Node2):
>
>    (Node1)1> rpc:call(Node2, erl_eval, exprs,
>                       [[{call,1,{remote,1,{atom,1,io},{atom,1,format}},
>                          {string,1,"~p~n"},
>                          {cons,1,
>                           {call,1,{remote,1,{atom,1,erlang},{atom,1,nodes}},
>                            []},
>                           {nil,1}}]}],
>                       []]).
>    [Node1]
>    {value,ok,[]}
>    (Node1)2>
>
> Where I got the long awful list (Parsed) from:
>
>    {ok,Scanned,_} = erl_scan:string("io:format(\"~p~n\", [erlang:nodes()]).").
>    {ok,Parsed} = erl_parse:parse_exprs(Scanned).
>    Bindings = erl_eval:new_bindings().
>
> and then
>
>    rpc:call(Node2, erl_eval, exprs, Parsed, Bindings).

Of course if you are going to be doing a lot of sending of source code
to be evaluated on a remote machine then it would be easier and
shorter to use LFE. The equivalent to the above in vanilla erlang is:

Expr = [':',io,format,[quote,"~p~n"],[list,[':',erlang,nodes]]],
rpc:call(Node2, lfe_eval, expr, [Expr]).

Doing it all in LFE if course even nicer:

(let ((e '(: io format '"~p~n" (list (: erlang nodes)))))
  (: rpc call node 'lfe_eval 'expr (list e)))

Robert


More information about the erlang-questions mailing list