Comments on: Managing Application Configuration http://spawnlink.com/articles/managing-application-configuration/ Linking You to Erlang Sun, 22 May 2011 00:00:11 +0000 http://wordpress.org/?v=2.6.1 By: Witold Baryluk http://spawnlink.com/articles/managing-application-configuration/#comment-10959 Witold Baryluk Wed, 12 Aug 2009 14:03:09 +0000 http://spawnlink.com/?p=163#comment-10959 It is very cool thing. But sometimes file:consult/1 isn't enaugh, for example if you want to use, expression (like multiply something, or append list to another, or use list comprehension). This snipet will do this: eval(Filename) -> {ok, B} = file:read_file(Filename), T = binary_to_list(B), {ok, Tokens, _EndLocation} = erl_scan:string(T), {ok, [Expression]} = erl_parse:parse_exprs(Tokens), Bindings = erl_eval:new_bindings(), {value, Value, _NewBindings} = erl_eval:expr(Expression, Bindings), {ok, Value}. Use like eval("file.cfg"); example file.cfg: {hosts, [ {active, Host, 100} || X <- [a1,a2,a3,a4,a5] ] ++ [ {passive, Host, 200} || X <- [p1, p2, p3, p4] ] }. {acl, [ {joe, md5("password') } ]}. this is just example. You need to consider that users can write any expressions there (like erlang:halt() ). It is very cool thing.

But sometimes file:consult/1 isn’t enaugh, for example if you want to use, expression (like multiply something, or append list to another, or use list comprehension).

This snipet will do this:

eval(Filename) ->
{ok, B} = file:read_file(Filename),
T = binary_to_list(B),
{ok, Tokens, _EndLocation} = erl_scan:string(T),
{ok, [Expression]} = erl_parse:parse_exprs(Tokens),
Bindings = erl_eval:new_bindings(),
{value, Value, _NewBindings} = erl_eval:expr(Expression, Bindings),
{ok, Value}.

Use like eval(”file.cfg”);

example file.cfg:
{hosts,
[ {active, Host, 100} || X <- [a1,a2,a3,a4,a5] ] ++
[ {passive, Host, 200} || X <- [p1, p2, p3, p4] ]
}.
{acl, [ {joe, md5("password') } ]}.

this is just example. You need to consider that users can write any expressions there (like erlang:halt() ).

]]>
By: grantmichaels http://spawnlink.com/articles/managing-application-configuration/#comment-9677 grantmichaels Thu, 09 Jul 2009 22:13:59 +0000 http://spawnlink.com/?p=163#comment-9677 It's awesome to see that you are planning to start blogging again! It’s awesome to see that you are planning to start blogging again!

]]>
By: Mitchell http://spawnlink.com/articles/managing-application-configuration/#comment-9644 Mitchell Thu, 09 Jul 2009 02:39:53 +0000 http://spawnlink.com/?p=163#comment-9644 Ferd T-H, Awesome! After I read about it more and find some time I'll update the post to reflect these findings. Thank you very much. Ferd T-H,

Awesome! After I read about it more and find some time I’ll update the post to reflect these findings. Thank you very much.

]]>
By: Ferd T-H http://spawnlink.com/articles/managing-application-configuration/#comment-9640 Ferd T-H Thu, 09 Jul 2009 00:18:34 +0000 http://spawnlink.com/?p=163#comment-9640 To read form a proplist ([{key,Value}]), see the proplist module with the functions get_value/2 and get_value/3. They're going to do what you want without you needing to write anything. 1> Conf = [{appname, "Hello"}, {user,"Ferd"}]. [{appname,"Hello"},{user,"Ferd"}] 2> proplists:get_value(user,Conf). "Ferd" 3> proplists:get_value(password, Conf, "Default if not found"). "Default if not found" Check around the module for some other helpful functions to deal with proplists rather than writing them yourself. To read form a proplist ([{key,Value}]), see the proplist module with the functions get_value/2 and get_value/3. They’re going to do what you want without you needing to write anything.

1> Conf = [{appname, "Hello"}, {user,"Ferd"}].
[{appname,"Hello"},{user,"Ferd"}]
2> proplists:get_value(user,Conf).
“Ferd”
3> proplists:get_value(password, Conf, “Default if not found”).
“Default if not found”

Check around the module for some other helpful functions to deal with proplists rather than writing them yourself.

]]>
By: Brendon Murphy http://spawnlink.com/articles/managing-application-configuration/#comment-9629 Brendon Murphy Wed, 08 Jul 2009 18:14:33 +0000 http://spawnlink.com/?p=163#comment-9629 Thanks for sharing this. Having spent most my time recently in Rails (or even just plain Ruby) I'm addicted to being able to use YAML for reading a config. As I'm new to Erlang, this'll help fill the gap I'm sure. Thanks for sharing this. Having spent most my time recently in Rails (or even just plain Ruby) I’m addicted to being able to use YAML for reading a config. As I’m new to Erlang, this’ll help fill the gap I’m sure.

]]>
By: Dew Drop – July 8, 2009 | Alvin Ashcraft's Morning Dew http://spawnlink.com/articles/managing-application-configuration/#comment-9624 Dew Drop – July 8, 2009 | Alvin Ashcraft's Morning Dew Wed, 08 Jul 2009 14:56:27 +0000 http://spawnlink.com/?p=163#comment-9624 [...] Managing Application Configuration (Mitchell Hashimoto) [...] [...] Managing Application Configuration (Mitchell Hashimoto) [...]

]]>
By: Anselmo Silva http://spawnlink.com/articles/managing-application-configuration/#comment-9612 Anselmo Silva Wed, 08 Jul 2009 10:20:28 +0000 http://spawnlink.com/?p=163#comment-9612 Tks, great stuff as usual. Tks, great stuff as usual.

]]>
By: links for 2009-07-08 « Bloggitation http://spawnlink.com/articles/managing-application-configuration/#comment-9605 links for 2009-07-08 « Bloggitation Wed, 08 Jul 2009 07:04:43 +0000 http://spawnlink.com/?p=163#comment-9605 [...] Managing Application Configuration (tags: erlang programming) Leave a Comment [...] [...] Managing Application Configuration (tags: erlang programming) Leave a Comment [...]

]]>
By: Harish Mallipeddi http://spawnlink.com/articles/managing-application-configuration/#comment-9602 Harish Mallipeddi Wed, 08 Jul 2009 06:26:44 +0000 http://spawnlink.com/?p=163#comment-9602 Good to see you writing again! I learnt a lot from your series of blog posts on OTP. Good to see you writing again! I learnt a lot from your series of blog posts on OTP.

]]>
By: James Lee http://spawnlink.com/articles/managing-application-configuration/#comment-9600 James Lee Wed, 08 Jul 2009 04:26:37 +0000 http://spawnlink.com/?p=163#comment-9600 Thanks for this. I'm working on my first little project in Erlang and was looking for some tips on how to store configuration. I've been spoiled by http://java.sun.com/j2se/1.4.2/docs/guide/lang/preferences.html, but I think I can whip up a few more functions to add to your examples to get something comparable. Thanks for this. I’m working on my first little project in Erlang and was looking for some tips on how to store configuration. I’ve been spoiled by http://java.sun.com/j2se/1.4.2/docs/guide/lang/preferences.html, but I think I can whip up a few more functions to add to your examples to get something comparable.

]]>