2

我需要将两个参数传递给我的 Erlang 代码。它在 Erlang shell 中运行良好。

2> crop:fall_velocity(x,23).
  21.23205124334434

但是我应该如何在没有 Erlang shell 的情况下运行 Erlang 代码。像普通的python,c程序一样。./program_name (不传递 $1 $2 参数)。

我正在尝试这个

erl -noshell -s crop fall_velocity(x,20) -s init stop

但它给出了意外的令牌错误。

4

2 回答 2

6

文档所述-s传递所有参数仅作为一个原子列表提供,并且-run执行相同但作为字符串列表。如果您想使用任意参数计数和类型调用任意函数,您应该使用-eval

$ erl -noshell -eval 'io:format("test\n",[]),init:stop()'
test
$
于 2014-01-14T01:41:27.487 回答
4

您可以使用escript 从命令行运行 Erlang 脚本。在该脚本中,您应该创建一个main函数,该函数将参数数组作为字符串。

#!/usr/bin/env escript

main(Args) ->
  io:format("Printing arguments:~n"),
  lists:foreach(fun(Arg) -> io:format("Got argument: ~p~n", [Arg]) end,Args).

输出:

./escripter.erl hi what is your name 5 6 7 9
Printing arguments:
Got argument: "hi"
Got argument: "what"
Got argument: "is"
Got argument: "your"
Got argument: "name"
Got argument: "5"
Got argument: "6"
Got argument: "7"
Got argument: "9"
于 2014-01-14T01:33:08.270 回答