file:eval/2
对文件里的 Erlang 表达式进行求值解析
用法:
eval(Filename, Bindings) -> ok | {error, Reason}
内部实现:
-spec eval(Filename, Bindings) -> ok | {error, Reason} when
Filename :: name_all(),
Bindings :: erl_eval:binding_struct(),
Reason :: posix() | badarg | terminated | system_limit
| {Line :: integer(), Mod :: module(), Term :: term()}.
eval(File, Bs) ->
case open(File, [read]) of
{ok, Fd} ->
R = eval_stream(Fd, ignore, Bs),
close(Fd),
R;
Error ->
Error
end.
从文件 Filename 里读取并求值解析以 '.'(或者是 ',' 隔开的一系列表达式) 隔开的 Erlang 表达式。参数 Bindings 是存储的是之前已经定义过的变量。真是的求值解析结果不会返回;文件里的任何表达式序列必须有其效用。返回的结果有以下:
- ok:文件被读取并求值解析
- {error, atom()}:打开或读取文件的时候发生了一个错误
- {error, {Line, Mod, Term}}:解析文件里的 Erlang 表达式时发生了一个错误。可以使用 file:format_error/1 函数来获取更详细直观的错误描述
file:eval("./test/test_file_path_eval.erl", erl_eval:new_bindings()).