如何在启动 Erlang shell/节点时运行自定义函数? (即“.erl”文件中的函数)

发布于 2024-10-04 12:29:48 字数 203 浏览 1 评论 0原文

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

exec erl file.erl

但是,我似乎无法找出如何直接启动此文件中的函数。

例如

exec erl file.erl -f function()

任何建议表示赞赏...

I can start an Erlang file either via the command line or bash script:

exec erl file.erl

But, I cannot seem to find out how to directly start a function within this file.

e.g.

exec erl file.erl -f function()

Any suggestions appreciated...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

看春风乍起 2024-10-11 12:29:48

您可能想要的是 erl -s module_name function_name

请注意,您永远不会像在示例中那样在 erl 命令中指定 erlang 文件。 Erlang VM 加载代码路径中的所有模块。这包括本地目录。

来自 http://www.erlang.org/doc/man/erl.html

-run Mod [Func [Arg1, Arg2, ...]]
(init 标志) 使 init 调用指定的
功能。 Func 默认启动。如果
没有提供参数,则
假设函数的元数为 0。
否则它被认为是有数的
1、将列表 [Arg1,Arg2,...] 视为
争论。所有参数都传递为
字符串。请参阅 init(3)。

-s Mod [Func [Arg1, Arg2, ...]]
(init 标志)使 init 调用指定的
功能。 Func 默认启动。如果
没有提供参数,则
假设函数的元数为 0。
否则它被认为是有数的
1、将列表 [Arg1,Arg2,...] 视为
争论。所有参数都传递为
原子。请参阅 init(3)。

what you probably want is erl -s module_name function_name

Note that you never specify the erlang file in the erl command like you did there in your example. The Erlang VM loads all modules in the codepath. That includes local directory.

From http://www.erlang.org/doc/man/erl.html:

-run Mod [Func [Arg1, Arg2, ...]]
(init flag) Makes init call the specified
function. 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. See init(3).

-s Mod [Func [Arg1, Arg2, ...]]
(init flag) Makes init call the specified
function. 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
atoms. See init(3).

﹎☆浅夏丿初晴 2024-10-11 12:29:48

erl 手册页][1] 显示了所有命令行选项,但 [init(3)` 似乎有示例,有时还有更好的描述。 这篇文章也有一些很好的例子。

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

选项 1:erl -runerl -s

erl 手册页 描述了 -s-run 开关(
文本相同),但示例位于 init(3) (参见下面的块引用)。


警告
被调用的模块必须已经编译,否则 Erlang 运行时将在 init 时崩溃,产生一条神秘的错误消息(表明该函数未定义)。


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

在系统初始化期间评估指定的函数调用。
Func 默认为start。如果没有提供参数,该函数
假设其数量为 0。否则假设其数量为 1,
将列表 [Arg1,Arg2,...] 作为参数。所有参数都是
作为字符串传递。如果引发异常,Erlang 会停止并显示
错误消息。

示例:

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

这将启动 Erlang 运行时系统并评估以下内容
功能:

<前><代码>foo:start()
富:酒吧()
foo:bar(["baz", "1", "2"]).

这些函数在初始化过程中顺序执行,
然后正常终止并将控制权传递给用户。这
意味着不返回的 -run 调用会进一步阻塞
加工;为了避免这种情况,请在这种情况下使用spawn的某些变体。

选项 2:erl -eval

如上一节所述,必须编译该模块才能与 -run-s 一起使用,因此,要么先调用 erlc ,要么添加 -eval 。假设 amod.erl 与执行 erl 的文件夹位于同一文件夹中,

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

这将显示 Erlang shell 提示符。请参阅 -noshell (erl 手册页< /a>) 如果只需要启动 Erlang 运行时。

来自 init(3)

-eval 表达式

扫描、解析和计算任意表达式 Expr
系统初始化。如果这些步骤中的任何一个失败(语法错误,解析
错误,或评估期间出现异常),Erlang 因错误而停止
信息。在下面的示例中,Erlang 被用作十六进制
计算器:

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

如果指定了多个 -eval 表达式,则会对它们进行求值
按照指定的顺序依次进行。计算 -eval 表达式
按顺序调用 -s-run 函数(这也在
指定订单)。与 -s-run 一样,评估不
终止会阻止系统初始化过程。

The erl man page][1] shows all the command line options, but [init(3)` seems to have the examples, and sometimes better descriptions. This post has some good examples as well.

Also, the options below are not mutually exclusive. The -run, -s, and -eval switches can be mixed.

Option 1: erl -run or erl -s

The erl man page describes the -s and -run switches (the
texts are the same), but the examples are in init(3) (see blockquote below).


Caveat:
The called module has to be compiled already, otherwise the Erlang runtime will just crash on init, producing a cryptic error message (that points to the fact that the function is undefined).


-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.

Option 2: erl -eval

As mentioned in the section above, the module has to be compiled to be used with -run or -s, so either call erlc before, or add -eval to the mix. Assuming amod.erl is in the same folder where erl is executed

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

This will drop to the Erlang shell prompt. See -noshell (erl man page) if only the Erlang runtime needs to be started up.

From 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文