Erlang Central

Difference between revisions of "String Eval"

From ErlangCentral Wiki

m (missed an argument to my own function, ow.)
m
Line 19: Line 19:
  
 
[[Category:CookBook]][[Category:StringRecipes]]
 
[[Category:CookBook]][[Category:StringRecipes]]
 +
 +
 +
 +
[http://www.casinos-go.com/online-casino-tips/index.html online casino tips]
 +
[http://www.casino-theory.com/online-casino-royale/play-free-online-casino.html play free online casino]
 +
[http://www.fortune-slots.com/ slots]
 +
[http://www.slots-wiki.com/index.php/slots_online_bonus slots online bonus]
 +
[http://www.casino-theory.com/bingo-online/gambling-online-bingo.html gambling online bingo]
 +
[http://www.gambling-online-theory.com/casinos/on-line-casinos.html on line casinos]
 +
[http://www.online-casino-wiki.com/index.php/online_casino_tips online casino tips]
 +
[http://www.casino-games-wiki.com/index.php/casino_games_online casino games online]
 +
[http://www.casino-theory.com/online-casino-bonus/online-casino-net.html online casino net]
 +
[http://www.gambling-online-theory.com/online-casino/bet-online-casino.html bet online casino]

Revision as of 19:43, 21 November 2006

Problem

You want to evaluate Erlang code stored in a string.

Solution

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

eval(S,Environ) ->
    {ok,Scanned,_} = erl_scan:string(S),
    {ok,Parsed} = erl_parse:parse_exprs(Scanned),
    erl_eval:exprs(Parsed,Environ).

1> eval("A = 1 + 2.",[]).
{value,3,[{'A',3}]}

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.


online casino tips play free online casino slots slots online bonus gambling online bingo on line casinos online casino tips casino games online online casino net bet online casino