[erlang-questions] Storing funs in term files

Steve Davis <>
Mon Apr 6 00:01:47 CEST 2009


Hi Gaspar,

Noted!

I'm usually pretty careful about file I/O. In this case the initial 
file:script read will parameterize a module which will be used by the 
application for the actual "work". So it's an on-create function only :)

Thanks and regards,
Steve


Gaspar Chilingarov wrote:
> Steve Davis wrote:
>> Given that:
>> 1> A = {echo, fun(X) -> X end}.
>> {echo,#Fun<erl_eval.6.13229925>}
>>
>> I have a scenario where it would be useful to store funs in term
>> files, for example:
>>
>> % contents of the term file
>> {echo, fun(X) -> X end}.
>>
>> 2>{ok, B} = file:consult("fun.term").
>> ** exception error: no match of right hand side value
>>                     {error,{2,erl_parse,"bad term"}}
>>
> 
> use file:script and not file:consult :)
> 
> I'd stuck in the same place several years ago :)
> 
> @aldan ~/tmp> cat > a.term
> {echo, fun(X) -> X end}.
> @aldan ~/tmp> erl
> Erlang (BEAM) emulator version 5.6.3 [source] [64-bit] [async-threads:0] 
> [hipe] [kernel-poll:false]
> 
> Eshell V5.6.3  (abort with ^G)
> 1> file:consult("a.term").
> {error,{1,erl_parse,"bad term"}}
> 2> file:script("a.term").
> {ok,{echo,#Fun<erl_eval.6.13229925>}}
> 3>
> 
> NOTICE: file:script is VERY uneffective/time consuming, so if you are 
> going to read many files or read them several times - write some simple 
> caching process around file:script .
> If you repeatedly call it on the same file - it will be main bottleneck, 
> caching may improve performance 10000-100000 times ;).
> 
> /Gaspar
> 
> 
> 
> 
> 




More information about the erlang-questions mailing list