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...
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...
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).
-eval
in case you need to start that function with an argument other than a list of strings or atoms. Oh, and you might want to add -noshell
to prevent the interactive shell from opening, and possibly add -s init stop
at the end to actually quit Erlang when the function is done. –
Steamship 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.
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 tostart
. 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.
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.
© 2022 - 2024 — McMap. All rights reserved.