Erlang Central

String Eval

Revision as of 21:55, 3 September 2006 by Bfulgham (Talk | contribs)


You want to evaluate Erlang code stored in a string.


Use erl_scan:string to convert the string into a list of tokens, then use erl_parse:parse_exprs to generate the Erlang intermediate representation, then finally use erl_eval:exprs to generate the final output:

1> {ok,A,B} = erl_scan:string("A = 1 + 2.").
2> A.
3> {C,D} = erl_parse:parse_exprs(A).
4> D.
5> erl_eval:exprs(D,[]).

Now, this is an admittedly baroque way to determine the value of 1 + 2, but it does give you interesting access to the inner workings of the Erlang interpreter.