Erlang Modules and Compiling

Erlang Variables

Erlang code is divided into modules. A module consists of a sequence of attributes and function declarations, each terminated by period (.). It provided the contained functions with a common namespace as well, You can imagine a module as a package in Java, or a header file in C. Program in Erlang spans over more than one modules.

Example:

m.erl
1
2
3
4
5
6
7
-module(m).          % module attribute
-export([fact/1]).   % module attribute

fact(N) when N>0 ->  % beginning of function declaration
  N * fact(N-1);     %  |
fact(0) ->           %  |
  1.                 % end of function declaration

Defining Modules

Assume that we want to create a module that will contain our own implementation of list functions and name it mlists.
First, we create a file named mlists.erl that will contain the module. The container file and the module names have to be the same.

Calling a Module’s Function

The calling function of a module, convention in Erlang is module:function(argument1, argument2, ...).

Example:

1
2
1> lists:max([1,3,2]).
3

Calling a Module Function within the Module

A function defined within the module file can be called either as module_name:function(arguments1, argument2, ...), or function(arguments1, argument2, ...), so the module name can be skipped.

Modules Attributes

Then we need to define the module’s attributes. An attribute is the information that we provide to the Erlang compiler. It is placed as a directive (usually) in the top of the file and has the -attribute_name(attribue_value(s)). format. The one attribute that we have to define is the one providing the module name.

-module(Module).
Module declaration, defining the name of the module. The name Module, an atom, should be the same as the file name minus the extension erl. Otherwise code loading will not work as intended.

Example in the top of mlists.erl place:

mlists.erl
1
-module(mlists).

-export(Functions).
Exported functions is used to define which functions the module exports, where “exports” means that they will be available to be called outside the module.

All the functions that are not exported by the module are only visible within the file, similar with the private functions in Ruby, Java and the static ones in C.

Example:

mlists.erl
1
2
-module(mlists).
-export([function1/arity1, function2/arity2, ...]).

-import(Module,Functions).
You can use this directive in order to import the selected exported functions of a module in the namespace of another one. It means that if you do so, you will be able to call the functions without the module prefix. Although in some cases it could be convenient, it is not recommended to use this directive, because it decreases the code’s readability.

-compile(Options).
Compile is used to pass compilation instructions to the compiler.

Example:

1
-compile([export_all]).

-on_load(Function).
Names a function that should be run automatically when a module a loaded.

Compiling Modules

Erlang programs must be compiled to object code. The compiler can generate a new file which contains the object code. The current abstract machine which runs the object code is called BEAM. To compile the modules start an Erlang emulator on the folder that contains your source files. In order to compile a .erl, using c(Module) Bult-in Function.

Example:

1
2
1> c(mlist).
{ok, mlist}

If there is no error occurs, the compiler generates the compiled .beam file.

Loading a Module

The object code must be loaded into the Erlang runtime system. To load the modules start an Erlang emulator on the folder that contains your source files. In order to load a compiled module .beam, using l(Module) Bult-in Function.

1
2
1> l(mlist).
{module, mlist}

I promise, from now on the posts will be far more interesting. Next one, or two posts will be about defining functions in Erlang. You can imagine how important functions are for a functional programming language. see ya! :)