2014-01-14 50 views
2

我需要將兩個參數傳遞給我的Erlang代碼。它在Erlang shell中工作正常。Erlang命令行

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 

但它給人意外的標記錯誤。

回答

5

作爲documentation states,所述-s通過作爲-run原子的只是一個列表和提供的所有參數不相同,但是作爲一個字符串列表。如果要使用任意參數計數和類型調用任意函數,則應使用-eval

$ erl -noshell -eval 'io:format("test\n",[]),init:stop()' 
test 
$ 
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"