Modules, exports and imports

Every Erlang program is a set of modules, and a module is one file with a name, a list of exported functions and their code. The export list is the whole public surface: a function that is not on it exists, but no other module may call it. This lesson reads a module header, makes a call from one module to another, and looks at the import attribute that lets a caller drop the module prefix.

The module header

The first attribute is the module name, and the compiler insists that it equals the file name: math_util.erl must declare -module(math_util).. The export list names the functions other modules may call, each as a name and an arity. Everything else in the file is private to the module:

math_util.erl

-module(math_util). -export([double/1, sum/1]). double(X) -> X * 2. sum([]) -> 0; sum([Head | Tail]) -> Head + sum(Tail).

erlc math_util.erl

erl

1> math_util:double(21). 42 2> math_util:sum([1, 2, 3]). 6

A function that the export list leaves out is still callable inside its own module, but a call from outside fails with undef. The next module keeps square/1 private and exports only area/1:

geometry.erl

-module(geometry). -export([area/1]). area({square, Side}) -> square(Side); area({rectangle, Width, Height}) -> Width * Height. square(Side) -> Side * Side.

Call across modules

A remote call writes the module, a colon and the function: geometry:area({square, 3}). The module name is an atom like any other, so it may sit in a variable, which is how a program chooses an implementation at run time. After both modules are compiled, the shell reaches the exported function and the private one:

erlc geometry.erl

erl

1> geometry:area({square, 3}). 9 2> geometry:square(3). ** exception error: undefined function geometry:square/1 3> lists:member(2, [1, 2, 3]). true 4> string:uppercase("hello"). "HELLO"

The undef is the mistake to recognise: the function exists in the file, but the export list does not name it, so for every other module it does not exist. The same error appears when the module itself is not on the code path, which is the usual reason a program works in the shell of its folder and fails elsewhere. The standard library is a set of modules like these, so lists:member/2 and string:uppercase/1 are ordinary remote calls. A whole program uses them the same way:

summary.erl

-module(summary). -export([main/0]). main() -> Prices = [2, 4, 6], io:format("~p~n", [lists:sum(Prices)]), io:format("~s~n", [string:uppercase("done")]).

erlc summary.erl

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

12 DONE

Import selectively

The -import attribute brings a foreign function into the local namespace, so the call drops the module prefix. It names the module and then one or more functions by name and arity. This is a fragment of a module, not a whole one: the line sits after the export list, and from there on member(2, [1, 2, 3]) means lists:member(2, [1, 2, 3]).

imports.erl

-import(lists, [member/2]).

The attribute is legal, but the course and its style checker do not use it. Elvis reports it as no_import: Usage of the import attribute, on line 4, is discouraged, and the reader loses the one word that says where member/2 comes from. A prefixed call tells the reader at a glance which module owns the function, and it cannot be confused with a local function of the same name. Write lists:member(2, [1, 2, 3]) instead. The macros and attributes lesson covers the attributes a module really needs.

Where modules live

erlc math_util.erl writes math_util.beam next to the source, and the runtime loads that file the first time a call names the module. The file base name, the module attribute and the beam name are the same string, which is why a renamed file that still declares the old module does not compile. The code server keeps the table of loaded modules and answers questions about them.

A program started with erl -noshell sees the modules of its own folder only when that folder is on the code path: -pa . adds the current folder, and the page from source to a running process follows a file from erlc to the loaded module. The code loading lesson loads, reloads and removes a module at run time.

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(math_util). match?

    Answer

    The file name math_util.erl

    erlc checks that the module attribute and the file name agree.

  2. What does -export([double/1, sum/1]). do?

    Answer

    Lists the functions other modules may call

    The export list is the public surface of the module, written as name and arity pairs.

  3. How do you call double/1 of math_util from another module?

    Answer

    math_util:double(21)

    A remote call names the module, a colon, and the function.

  4. What happens when another module calls a function that the export list leaves out?

    Answer

    It fails with undef

    A function that is not exported does not exist for any other module, so the call is an undefined function.

  5. What does this module print? -module(q1). -export([main/0]). main() -> Prices = [2, 4, 6], io:format("~p~n", [lists:sum(Prices)]), io:format("~s~n", [string:uppercase("done")]).

    Answer

    12 / DONE

    lists:sum/1 adds the three prices and string:uppercase/1 returns the upper-case string.

  6. What does this module print? -module(q2). -export([main/0]). main() -> Module = lists, io:format("~p~n", [Module:member(2, [1, 2, 3])]).

    Answer

    true

    A module name is an atom, so it may be held in a variable and used in a remote call.

  7. What does this module print? -module(q3). -export([main/0, area/1]). area({square, Side}) -> Side * Side; area({rectangle, Width, Height}) -> Width * Height. main() -> io:format("~p~n", [area({square, 3})]), io:format("~p~n", [area({rectangle, 2, 5})]).

    Answer

    9 / 10

    The square clause multiplies 3 by 3 and the rectangle clause multiplies 2 by 5.

  8. What does this module print? -module(q4). -export([main/0, total/1]). total(Items) -> lists:sum(Items). main() -> io:format("~p~n", [?MODULE:total([1, 2, 3])]).

    Answer

    6

    total/1 is exported, so the remote call through ?MODULE reaches it and returns the sum.

  9. Why does the course avoid -import?

    Answer

    The module prefix shows where the function lives

    A prefixed call tells the reader at a glance which module owns the function, and the style checker flags the attribute.

  10. Which file does erlc math_util.erl write?

    Answer

    math_util.beam

    erlc writes BEAM bytecode named after the module, with the .beam extension.

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