Loading application variables

Ulf Wiger <>
Sun Jun 11 16:25:03 CEST 2006


Den 2006-06-11 14:42:43 skrev Pupeno <>:

> I understand it can be written, but since nobody has written it yet I
> suppose people work in other ways, if so, I want to learn that instead
> of writing a function (that doesn't belong to my project anyway, but to
> Erlang itself).

What I normally do is (possibly) use environment variables to store
defaults, and then providing an API for setting the desired values
at runtime.

Mnesia, for example, relies on environment variables for various settings,  
but you can also call mnesia:start(Options), and set the same values that  
way. What mnesia does at startup is to read the options - either passed as  
arguments to the start function, or given as environment variables - and  
then storing them as temporary objects in the schema.

The module proplists is a convenient way to handle {Key, Value} options.  
You can, for example, do:

start(StartOpts) ->
   Options = StartOpts ++ application:get_all_env(?APP_NAME),
   Port = proplists:get_value(port, Options),
   ...


The proplists module will fetch the first instance of a given key, so all  
you need to do to override the defaults is to prepend the new values to  
the list.

Regards,
Ulf W
-- 
Ulf Wiger



More information about the erlang-questions mailing list