[erlang-questions] The best way to convert exprs to string that represents this exprs

Caoyuan dcaoyuan@REDACTED
Sun May 20 20:14:43 CEST 2007


Hi,

I need to convert some expressions to code string, then modify it,
then erl_eval the new expressions. Currently the code looks like:

match_test() ->
    PatternStr = "{item, _, [{title, Title, _}, {link, Link, _}]}",
    Item = {item, [], [{title, "TitleTest", []}, {link, "LinkTest", []}]},
    %% The new expression I want is something like:
    %% {item, _, [{title, Title, _}, {link, Link, _}]} =  Item
    ExprsStr = PatternStr ++ " = " ++
erl_prettypr:format(erl_syntax:abstract(Item)) ++ ".",
    {ok, ExprsTokens, _} = erl_scan:string(ExprsStr),
    {ok, Exprs} = erl_parse:parse_exprs(ExprsTokens),
    {value, Value, _} = erl_eval:exprs(Exprs, []),
    io:format("~p~n", [Value])
    .


I don't think my code is the best way. Would you please give me some advices?

Thanks



More information about the erlang-questions mailing list