skruger / Surrogate

Proxy server written in erlang. Supports reverse proxy load balancing and forward proxy with http (including CONNECT), socks4, socks5, and transparent proxy modes.
67 stars 14 forks source link

Reading erlang terms from a string. #3

Open skruger opened 13 years ago

skruger commented 13 years ago

After reading the source to file:consult() this is how to read terms directly from a string.

ErlTerms = "{a,b,c,d}.".

{ok,Tokens,_} = erl_scan:string(ErlTerms).

{ok,Term} = erl_parse:parse_term(Tokens).

Term -> {a,b,c,d}.

skruger commented 13 years ago

To parse a fun()

StrTerm = "fun() -> result_atom end.".

{ok,Tokens,_} = erl_scan:string(StrTerm).

{ok,[Expr]} = erl_parse:parse_exprs(Tokens).

erl_eval:expr(Expr,[]).

resulted in: {value,#Fun,[]}