Module attributes and compilation (Learning Erlang 5).

Published on Apr 03, 2012

Other articles in the Learning Erlang series.

  1. Learning Erlang.
  2. Heads and tails, working with lists. (Learning Erlang 2)
  3. Variables, comparisons and dynamic typing. (Learning Erlang 3)
  4. Modules and functions, Erlang building blocks (Learning Erlang 4).
  5. Playing with recursion, (Learning Erlang 6).
  6. Playing with the file module (part 1). (Learning Erlang 7)
  7. The file module (part 2). (Learning Erlang 8)
  8. String manipulation in Erlang. (Learning Erlang 9)
  9. Records. (Learning Erlang 10)
  10. Functional arrays. (Learning Erlang 11)

Note of caution. These are my notes while learning Erlang. You are welcome to follow along and use them as a guide. Please make sure to check the Erlang language site

We saw some attributes in the previous post in this series.

An attribute take the form of: -Tag(Values). where Tag should be an atom and Value a literal term.
Let’s see what types of attributes we have and how we use them.

Predefined module attributes.

Predefined attributes need to be located on top of the module and before any function declaration.


The only mandatory attribute is the module attribute and should be used first. The module attribute take one argument that’s the name of the module. The name of the module is the same as the name of the file without the .erl extension.
Ex: if you write the code for your module in a file by the name dateformatter.erl the attribute will be:



The exports attribute will take a list of functions and arity to export for consumption by other modules. It will define the module interface.

-exports([isoformat/0, isoformat/1, utc/0]).


The import attribute is use to import functions from another module to use it as local.

According to my readings the use of the import attribute is frown upon in the Erlang community. See below.

For example, when doing this.

-import(list, [last/1, max/1, merge/1, merge/2]).

We can then use those functions directly in our code like this,

L = last([‘first’, ‘second’, ‘last’]).

Instead of
L = list:last([‘first’, ‘second’, ‘last’]).

The use of import tends to reduce code readability, since you won’t be able to know where a function have been defined without scrolling to the top of the file each time. For that reason, it’s use is discouraged. I think it’s a fair concern.


You can use the vsn attribute to indicate the version of a module. This attribute will default to the md5 checksum of the file.


This attribute is used to pass parameters for the compiler, you can pass a single parameter or a list.


Will export all functions in a module, do not use this for production unless you really mean it. Remember that the functions that you export represent the interface of your module.

Custom attributes.

You can easily create custom attributes. Custom attributes can only take one argument.

-company({name, “Dynamic Programmer”}).
-author(“Hernan Garcia”).

File and Line.

File and Line are predefined macros than can be overwritten using the file/2 attribute.
This attribute is used by code generation tools to map the generated code to the code input by the programmer. This will help with debugging.

-file(‘template.erl’, 35).

Module info functions.

The compiler inserts and exports two special functions that you can call to access the module information. module_info/0 and module_info/1

This is the output on calling those functions in our htmltags module.

Preprocesor, behaviours, macros and records.

For the sake of completeness you may want to check some of the other attributes used to define behaviours, insert files or define macros and records.

We will be dealing with them in future posts when we address those subjects.