static variables?

Torbjorn Tornkvist <>
Tue Aug 22 16:03:42 CEST 2006


I should probably not show you this but...
--------------------------------------------
# cat test.erl
-module(test).
-export([test/0]).

-fields(["a","b"]).


test() -> test.

# erl
Erlang (BEAM) emulator version 5.5 [source] [async-threads:0] [hipe]
Eshell V5.5  (abort with ^G)
1> element(2, lists:keysearch(fields, 1, test:module_info(attributes))).
{fields,["a","b"]}


--Tobbe




Yariv Sadan wrote:
> Hi,
> 
> Strangely, I find myself at a crossroads where I would like to have
> static (i.e. per-module) variables. Yes -- I know -- this would be a
> gross violation of the shared-nothing principle -- but then I would be
> just as happy if all static variables were "final" so there's no way
> that two processes would trample on the same data.
> 
> The situation is actually pretty simple: I have a 'person' module that
> corresponds to a database table. This module has a (static) list of
> fields. I would like to be able to access this static list from the
> module's functions without having to reconstruct it inside the
> function every time by calling a function such as
> 
> fields() ->
>  ["name", "age"].
> 
> Here's basically what I want to have:
> 
> -module(person).
> -compile(export_all).
> 
> fields = ["name", "age"].
> 
> get_fields() ->
>  fields.
> 
> Is there any way to do this in Erlang???
> 
> (I looked at the process dictionary, but it is per process, not per
> module).
> 
> Thanks,
> Yariv
> 




More information about the erlang-questions mailing list