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

2

10

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

Rozella answered 27/11, 2010 at 14:30 Comment(0)
K
20

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

Kus answered 27/11, 2010 at 14:54 Comment(4)
Thanks Jon! As always, you pull through :)Rozella
Also. If you are making a command line tool check out escript. It's a pretty nice tool to have in the war chest. erlang.org/doc/man/escript.htmlKus
There is also -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
jon and ndim... thanks for the additional tips. definitely useful and in line with what i'm trying to do.Rozella
C
1

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.

Option 1: 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 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. 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.

Option 2: 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.

Caddis answered 27/11, 2010 at 14:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.