Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

file:consult without the file part

Tags:

erlang

I'm writing an escript that needs as its input a normal proplist :

script "[{error_string, \"This is broken\"}]"

all I wanna do now is parse arbitrary strings into actual proplists, normally I would write this to a file, and then go on to use file:consult on the file to get the values - that does however seem a little over the top - so my question is, how do I do the same, that is parse the input string into a proplist without sending the the data to a file?

like image 573
Martin Kristiansen Avatar asked Dec 08 '25 19:12

Martin Kristiansen


2 Answers

M1 = "[{error_string, \"This is broken\"}].",
{ok, S1, _} = erl_scan:string(M1),
erl_parse:parse_term(S1).

returns

{ok,[{error_string,"This is broken"}]}
like image 63
Odobenus Rosmarus Avatar answered Dec 13 '25 01:12

Odobenus Rosmarus


Easiest way to do it is to use erl_eval, just like in Odobenus Rosmarus answer, even if it seems little complicated. Just have to remember to put . at the end of your expression. You can read a little more on this topic on Erlang Central Wiki.

But if you are building escript to populate your application with options i would look into full-blown argument parser. One I've used before is https://github.com/jcomellas/getopt . Project is stable, with good enough documentation, and does exactly what is should. It requires little bit configuration, but created code stays readable.

like image 38
mpm Avatar answered Dec 13 '25 00:12

mpm



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!