[erlang-questions] Storing funs in term files

Gaspar Chilingarov <>
Sun Apr 5 20:47:04 CEST 2009


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





-- 
Gaspar Chilingarov

tel +37493 419763 (mobile - leave voice mail message)
icq 63174784
skype://gasparch
e mailto: mailto:
w http://gasparchilingarov.com/



More information about the erlang-questions mailing list