records in text files or configuration

Richard Carlsson <>
Thu Aug 5 11:37:54 CEST 2004


On Thu, 5 Aug 2004, Fredrik Thulin wrote:

> Is there a way to define a record in my application and then use that
> record in my 'erl -config' file or in a plain text file that I read
> using file:consult() or something similar? Something like this :
>
> -module(test).
> -record(db, {key, value}).
>
> read_file(File) ->
>     Db = file:consult(File),
>     io:format("Result :~n~p~n", [Db]).
>
> and then have a File containing
>
> [#db{key=one, value=foo},
>  #db{key=two, value=bar}].

The #name{f1=X1,...,fn=Xn} notation is just syntactic sugar for
tuples on the form {name, X1, ..., Xn}, so even if you use record
syntax to create the entries and write them to a file, that file
will simply contain
 [{db, one, foo},
  {db, two, bar}].

If you read this back with consult(File), you can of course
again use the record syntax for working with the read entries
- as long as you haven't modified the record definition since
you wrote them to the file. It could be a good idea to specify
an explicit, stable, external data format, that is separated
from the internal representation, if this is to be used for
something serious.

	/Richard



Richard Carlsson ()   (This space intentionally left blank.)
E-mail: 	WWW: http://user.it.uu.se/~richardc/
 "Having users is like optimization: the wise course is to delay it."
   -- Paul Graham



More information about the erlang-questions mailing list