tags:

views:

55

answers:

2

I have some configuration data in a config file that I read off disk when the app starts. I need to make that configuration data available to other functions/modules in the application. I started down the path of looking into ets/mnesia to store the data on startup to make it shared among all processes, but then my inner voice cautioned me that there must be a more functional, erlang-y way to do this. The only alternate approach I've come up with so far is setting up a module that has an actor loop that reads the data on startup and responds to messages like {Key, From} and responds by From ! {ok,Value}. Then, I gave up and decided to ask... Thanks, --tim

+3  A: 

If what you need are just some configuration parameters, you might want to include them as environment variables (in Erlang terms) in one of your Erlang applications. The way to do this is to include them into the .app (or .app.src) file of your app, in the env tuple:

Something like:

{application, ch_app,
 [{description, "Channel allocator"},
  {vsn, "1"},
  {modules, [ch_app, ch_sup, ch3]},
  {registered, [ch3]},
  {applications, [kernel, stdlib, sasl]},
  {mod, {ch_app,[]}},
  {env, [{file, "/usr/local/log"}]}
 ]}.

SOURCE: http://www.erlang.org/doc/design_principles/applications.html

As you can see, the file is the config variable. You can access the variable by:

application:get_env(ch_app, file).

If what you need is something more complex, you might want to create a gen_server process that answer to all config requests (getter and setter methods).

Roberto Aloi
Brilliant, thanks Roberto, I've got a simple gen_server now answering my config calls thanks to a ridiculously simple tutorial[1] that simplified it enough that even I could understand...[1] - http://20bits.com/articles/erlang-a-generic-server-tutorial/
williamstw
A: 

Not to mention that any simple self-written solution involves reading the config file before starting the main supervisor as you may be needing these variables in there. Just a thought, but I ran into the same problem in my own code.

Weasel