Your first Erlang script
The module file
Every Erlang program starts with a module header. The name must match the file name, and the export list names the functions the outside world may call.
Here is the whole program. Save it as hello.erl.
-module(hello).
-export([main/0]).
main() ->
io:format("Hello from Erlang~n").
Compile and run
Compile with erlc, which produces a hello.beam file, the compiled bytecode. Then start the shell, call the function, and quit.
erlc hello.erl
erl -noshell -eval 'hello:main(), halt().'
Hello from Erlang
The escript shortcut
The escript runner skips the compile step. Add a main function, mark the file executable, and run it directly.
The shebang line tells the system which interpreter to use, just like a shell script.
#!/usr/bin/env escript
%%! -noshell -noinput
main(_) ->
io:format("Hello from escript~n").
chmod +x hello.escript
./hello.escript
Hello from escript
Why modules
The module header is not ceremony. It gives the compiler a name, the export list a boundary, and every later lesson a place to hang its code.
The arity in the export list is the argument count, so main/0 means main with zero arguments. Two functions may share a name when their arity differs.
Next: Variables and single assignment
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What must the name in
-module(hello).match?Answer
The file name,
hello.erlerlcchecks that the module attribute and the file name agree, sohellolives inhello.erl. -
What does
-export([main/0]).allow?Answer
mainwith zero arguments may be called from another moduleThe export list names the functions and their arity;
main/0ismainwith no arguments. -
Which command compiles
hello.erl?Answer
erlc hello.erlerlcis the compiler driver; it writeshello.beamnext to the source. -
Which file does
erlc hello.erlproduce?Answer
hello.beamThe compiler writes BEAM bytecode, which the runtime loads and runs.
-
Which command runs
hello:main()and then stops the node?Answer
erl -noshell -eval 'hello:main(), halt().'-noshellskips the interactive shell,-evalruns the expression, andhalt()ends the node. -
What is an escript?
Answer
A script Erlang compiles and runs directly
The escript runner reads the script, compiles it in memory and calls
main/1, so there is noerlcstep. -
Which line names the interpreter at the top of an escript?
Answer
#!/usr/bin/env escriptThe shebang lets the shell start the file with the escript runner, exactly like a shell script.
-
In
main(_) -> ..., what does the underscore mean?Answer
The argument list is ignored
An escript calls
main/1with the command-line arguments;_matches that list and binds nothing. -
Two functions in one module share a name. When is that allowed?
Answer
When their arity differs
A function is identified by name and arity, so
main/0andmain/1are different functions. -
What does
~ndo inio:format("Hello from Erlang~n")?Answer
Prints a newline
In a format string
~nstands for a newline, so the next output starts on a new line.
The examples on this page were compiled and run with Erlang/OTP 27 (erts-15.2.7.14) on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev