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

Ted Karmel picture Ted Karmel · Nov 27, 2010 · Viewed 7.7k times · Source

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

Answer

Jon Gretar picture Jon Gretar · Nov 27, 2010

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