Your first Erlang script

The module file

Every program of Erlang is a module. The first line names it, and the name must match the file name, so the module greet lives in greet.erl. The export list on the next line names the functions the outside world may call; main/0 is the function this page starts, and name/0 stays private.

greet.erl

-module(greet). -export([main/0]). main() -> io:format("Hello, ~s!~n", [name()]). name() -> "Erlang".

The file holds two functions and no top-level statement: loading the module defines the functions, and calling one runs the code. That is why the file can be compiled once and called many times.

Compile and run

erlc compiles the file and writes greet.beam next to it. The beam is the bytecode of the module, the file the runtime loads. Then start a node without a shell, call greet:main() and stop it:

erlc greet.erl

erl -noshell -eval 'greet:main(), halt().'

Hello, Erlang!

-noshell keeps the interactive shell out of the way, -eval runs the expression, and halt() ends the node with status 0. Without halt() the node would start, print the line and wait forever. The command is exactly what a build script runs after a successful compile.

What the export list protects

A function that is not exported can still be called by the module itself, as main/0 calls name/0, but not from outside. The shell is outside, and it answers with an exception instead of running the function:

erl

1> greet:name(). ** exception error: undefined function greet:name/0 2> greet:main(). Hello, Erlang! ok

The message is the same as for a module that does not exist, because the runtime only knows the exported functions. The export list is the boundary of the module: adding name/0 to it would make the call work, and keeping it out is how a module hides the helpers a caller has no business with.

The escript shortcut

An escript is one file with no compile step: the runner reads it, compiles it in memory and calls main/1 with the command-line arguments. The shebang line lets the shell start the file directly, exactly as it starts a shell script. Save this as hello.escript:

hello.escript

#!/usr/bin/env escript %%! -noshell -noinput main(_) -> io:format("Hello from escript~n").

The %%! line passes emulator flags: no shell, no input. Then make the file executable and run it:

chmod +x hello.escript

./hello.escript

Hello from escript

main/1 takes one argument, the list of arguments after the script name; _ is a pattern that matches that list and binds nothing. An escript that prints its arguments is a two-line change, saved as arguments.escript:

arguments.escript

#!/usr/bin/env escript %%! -noshell -noinput main(Args) -> io:format("arguments: ~p~n", [Args]).

escript arguments.escript one two

arguments: ["one","two"]

Why modules

The header is not ceremony. It gives the compiler a name for the beam, the export list a boundary, and every later lesson a place to hang its code. A module is identified by its name, and a function by its name and arity: main/0 is main with no arguments, and two functions may share a name when their arity differs. greet:main() says both at once.

The runtime finds a module through the code path: the directory of the node, the ebin folders of the libraries, and any -pa folder a command adds. The next lesson, From source to a running process, follows the file from the compiler to the process that runs it.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What must the name in -module(hello). match?

    Answer

    The file name, hello.erl

    erlc checks that the module attribute and the file name agree, so hello lives in hello.erl.

  2. What does -export([main/0]). allow?

    Answer

    main with zero arguments may be called from another module

    The export list names the functions and their arity; main/0 is main with no arguments.

  3. Which file does erlc hello.erl produce?

    Answer

    hello.beam

    The compiler writes BEAM bytecode, which the runtime loads and runs.

  4. 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 no erlc step.

  5. A module exports only main/0 and defines a private name/0. What does greet:name(). in the shell do?

    Answer

    Raises undefined function greet:name/0

    The runtime only knows exported functions, so a call to a private one is an undefined function, exactly as for a missing module.

  6. 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/0 and main/1 are different functions.

  7. What does this module print? -module(q1). -export([main/0]). main() -> io:format("~p~n", [add(2, 3)]). add(A, B) -> A + B.

    Answer

    5

    A private function may be called inside its own module, and 2 + 3 is 5.

  8. What does this module print? -module(q2). -export([main/0]). main() -> io:format("~p~n", [double(7)]). double(N) -> N * 2.

    Answer

    14

    double(7) returns 14, and the module prints it with ~p.

  9. The file count.escript is #!/usr/bin/env escript main(Args) -> io:format("~p~n", [length(Args)]). What does escript count.escript print?

    Answer

    0

    No arguments follow the script name, so Args is [] and its length is 0.

  10. What does this module print? -module(q4). -export([main/0]). main() -> io:format("~s~n", [message()]). message() -> "hi".

    Answer

    hi

    ~s prints the string without quotes, and a private function may be called from its own module.

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