[erlang-questions] Reading records with file:consult/1 or erl_eval/2?

Michael McDaniel erlangy@REDACTED
Fri Nov 21 21:27:35 CET 2008


 I know using a priori knowledge of record format is not
 long-term safe, sometimes I cannot help myself so do
 things like ...


file.conf
--------
% -record(buddies, { list = ["buddy", "buddy1", ... "buddyN"] } )
{buddies,  [
	    member_one
	    , member_two
	    , member_three
	   ]
}.
--------

and use it thusly ...

-record(buddies,  { list = [] }).

%% From::atom()
member(From) -> 

  {ok, TermList} = file:consult( "file.conf" ) ,

  {value, ConfRecBuddies} = lists:keysearch(buddies, 1, TermList) ,

  Members = ConfRecBuddies#buddies.list ,

  lists:member(From, Members)

. %member/1



~Michael



On Fri, Nov 21, 2008 at 09:29:21AM -0500, Rusty Klophaus wrote:
> Hi all,
> 
> I'm working on a small utility that reads Erlang terms from a file,  
> processes them, and spits out another file.
> 
> I've hit an unanticipated snag on step one. I can read basic terms  
> just fine, but I can't make file:consult/1 or erl_eval:expr/2  
> recognize records. Any suggestions? Is this just a known limitation?
> 
> Output and code are below.
> 
> Thanks,
> Rusty
> 
> 
> *** OUTPUT ***
> 
> Eshell V5.6.5  (abort with ^G)
> 1> reader:read_with_consult().
> {error,{1,erl_parse,"bad term"}}
> 2> reader:read_with_eval().
> ** exception error: {undefined_record,document}
> 
> Here is the code and supporting files:
> 
> *** READER.ERL CONTAINS ***
> 
> -module (reader).
> -include ("records.hrl").
> -compile(export_all).
> 
> read_with_consult() ->
> 	file:consult("terms.txt").
> 
> read_with_eval() ->
> 	{ok, B} = file:read_file("terms.txt"),
> 	S = binary_to_list(B),
> 	{ok, Tokens, _} = erl_scan:string(S),
> 	{ok, [Form]} = erl_parse:parse_exprs(Tokens),
> 	erl_eval:expr(Form, []).
> 
> 
> 
> *** RECORDS.HRL CONTAINS ***
> 
> -record(document, {title, description}).
> 
> 
> 
> *** TERMS.TXT CONTAINS ***
> 
> #document {}.
> 
> 
> 
> 
> 
> 
> 
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://www.erlang.org/mailman/listinfo/erlang-questions

-- 
Michael McDaniel
Portland, Oregon, USA
http://autosys.us




More information about the erlang-questions mailing list