如何在启动Erlang shell / node时运行自定义函数?(即,一个在`.erl`文件中的函数)

10

我可以通过命令行或Bash脚本启动Erlang文件:

exec erl file.erl

但是,我似乎找不到如何直接在这个文件中启动一个函数。

例如:

exec erl file.erl -f function()

非常感谢任何建议...

2个回答

19
您可能想要的是 erl -s 模块名 函数名。请注意,在示例中您指定了erlang文件,但在erl命令中不需要这样做。Erlang虚拟机会加载代码路径中的所有模块,包括本地目录。根据http://www.erlang.org/doc/man/erl.html所述:
-run Mod [Func [Arg1, Arg2, ...]] (init flag)使init调用指定的函数。如果未提供参数,则默认为start函数。否则,假设该函数的arity为1,将列表[Arg1,Arg2,...]作为参数传递。所有参数都作为字符串传递。请参阅init(3)。
-s Mod [Func [Arg1, Arg2, ...]] (init flag)使init调用指定的函数。如果未提供参数,则默认为start函数。否则,假设该函数的arity为1,将列表[Arg1,Arg2,...]作为参数传递。所有参数都作为原子传递。请参阅init(3)。

谢谢Jon!像往常一样,你总是能够帮忙 :) - Ted Karmel
5
另外,如果你正在制作命令行工具,请查看escript。它是一个很不错的工具,可以加入你的工具库中。http://www.erlang.org/doc/man/escript.html - Jon Gretar
7
如果你需要以除了字符串或原子列表之外的参数开始该函数,也可以使用-eval。另外,你可能想要添加-noshell来防止交互式shell打开,并且在函数完成后可能需要添加-s init stop来真正退出Erlang。 - ndim
Jon和Ndim...感谢你们的额外提示。绝对有用且符合我尝试做的事情。 - Ted Karmel

0

erl man page][1] 显示了所有命令行选项,但是 init(3) 似乎有更好的示例和描述。这篇文章 也有一些很好的示例。

此外,以下选项不是互斥的。 -run-s-eval 开关可以混合使用。

选项 1:erl -runerl -s

erl man page 描述了 -s-run 开关(文本相同),但示例在 init(3) 中(见下面的引用块)。


注意事项:
被调用的模块必须已经编译完成,否则Erlang运行时将在初始化时崩溃,产生一个晦涩的错误信息(指出函数未定义)。


-run Mod [Func [Arg1, Arg2, ...]]

Evaluates the specified function call during system initialization. Func defaults to start. If no arguments are provided, the function is assumed to be of arity 0. Otherwise it is assumed to be of arity 1, taking the list [Arg1,Arg2,...] as argument. All arguments are passed as strings. If an exception is raised, Erlang stops with an error message.

Example:

% erl -run foo -run foo bar -run foo bar baz 1 2

This starts the Erlang runtime system and evaluates the following functions:

foo:start()
foo:bar()
foo:bar(["baz", "1", "2"]).

The functions are executed sequentially in an initialization process, which then terminates normally and passes control to the user. This means that a -run call that does not return blocks further processing; to avoid this, use some variant of spawn in such cases.

选项二:erl -eval

如上面的部分所述,模块必须编译后才能与-run-s一起使用,因此要么在之前调用erlc,要么将-eval添加到混合中。假设amod.erl与执行erl的文件夹相同。

$ erl -eval 'compile:file(amod)' -run amod

这将会进入Erlang shell提示符。如果只需要启动Erlang运行时,请参考-noshell({{link1:erl man page}})。

来自{{link2:init(3)}}:

-eval Expr

Scans, parses, and evaluates an arbitrary expression Expr during system initialization. If any of these steps fail (syntax error, parse error, or exception during evaluation), Erlang stops with an error message. In the following example Erlang is used as a hexadecimal calculator:

% erl -noshell -eval 'R = 16#1F+16#A0, io:format("~.16B~n", [R])' \\
-s erlang halt
BF

If multiple -eval expressions are specified, they are evaluated sequentially in the order specified. -eval expressions are evaluated sequentially with -s and -run function calls (this also in the order specified). As with -s and -run, an evaluation that does not terminate blocks the system initialization process.


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接