Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run a custom function when starting an Erlang shell / node? (That is, a function within an `.erl` file)

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

like image 642
Ted Karmel Avatar asked Nov 27 '10 14:11

Ted Karmel


1 Answers

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

like image 119
Jon Gretar Avatar answered Oct 24 '22 08:10

Jon Gretar