Modules, exports and imports

The module header

A module starts with its name and its export list. The name must match the file name, and the export list is the module's public surface.

The behaviour attribute, covered in later lessons, declares the callbacks a module promises to implement.

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

erlc math_util.erl
erl

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

Call across modules

Call another module with the colon: module name, colon, function name. The arguments follow in parentheses, and the result returns like any call.

The standard library is a set of modules like this, so lists:map and file:read_file are just calls into other modules.

erl

1> lists:member(2, [1, 2, 3]).
true
2> string:uppercase("hello").
"HELLO"

Import selectively

The import attribute brings a foreign function into your namespace so you can drop the module prefix. Import one function at a time, by name and arity.

Most style guides skip imports: the module prefix shows where a function lives, and the reader thanks you.

-module(imports). -export([has_two/0]). -import(lists, [member/2]). has_two() -> member(2, [1, 2, 3]).

erlc imports.erl
erl

1> imports:has_two().
true

Where modules live

The compiler looks for module_name.erl, compiles it to module_name.beam, and loads the beam file at runtime. The source file name and the module name are the same string.

The code server keeps all loaded modules in one table, which lesson 44 explores with code:which and friends.

Next: Funs: anonymous functions

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 from another module?

    Answer

    math_util:double(21)

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

  4. What is lists:member/2?

    Answer

    A function in the standard library module lists

    lists is a standard library module, and member/2 is one of its exported functions.

  5. What does math_util:double(21). return?

    Answer

    42

    The exported double/1 multiplies its argument by two.

  6. What does math_util:sum([1, 2, 3]). return?

    Answer

    6

    sum/1 adds the list with a base case for the empty list and a recursive step for the head.

  7. What does -import(lists, [member/2]). allow?

    Answer

    Calling member(2, L) without the module prefix

    The import brings the named function into the module namespace.

  8. Why do many style guides skip imports?

    Answer

    The module prefix shows where the function lives

    A prefixed call tells the reader at a glance which module owns the function.

  9. Which file does the compiler write for math_util.erl?

    Answer

    math_util.beam

    erlc writes BEAM bytecode with the module name and the .beam extension.

  10. Which process keeps track of loaded modules?

    Answer

    The code server

    The code server holds the table of loaded modules and answers code:which/1 and friends.

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