当使用Erlang程序与其它语言程序通讯时,可能需要把一个字符串转换成为Erlang的Term,可以这样实现
{ok, Tokens, _} = erl_scan:string(String),
{ok, Term} = erl_parse:parse_term(Tokens).
注意这里的String需要以句号结尾。
例如在erlang shell下:
5> {ok, Tokens, _} = erl_scan:string("{1, {2}, [3]}.").
{ok,[{'{',1},
{integer,1,1},
{',',1},
{'{',1},
{integer,1,2},
{'}',1},
{',',1},
{'[',1},
{integer,1,3},
{']',1},
{'}',1},
{dot,1}],
1}
6> {ok, {X, Y, Z}} = erl_parse:parse_term(Tokens).
{ok,{1,{2},[3]}}
7> X.
1
8> Y.
{2}
9> Z.
[3]
10>
{ok, Tokens, _} = erl_scan:string(String),
{ok, Term} = erl_parse:parse_term(Tokens).
注意这里的String需要以句号结尾。
例如在erlang shell下:
5> {ok, Tokens, _} = erl_scan:string("{1, {2}, [3]}.").
{ok,[{'{',1},
{integer,1,1},
{',',1},
{'{',1},
{integer,1,2},
{'}',1},
{',',1},
{'[',1},
{integer,1,3},
{']',1},
{'}',1},
{dot,1}],
1}
6> {ok, {X, Y, Z}} = erl_parse:parse_term(Tokens).
{ok,{1,{2},[3]}}
7> X.
1
8> Y.
{2}
9> Z.
[3]
10>