Introduction to Erlang : Modules & Compilation

This entry is part 6 of 16 in the series Introduction to Erlang

Modules

A module is a container for functions; it provided the contained functions with a common namespace. Modules are used to organize functions in Erlang. Usually, a program in Erlang spans over more than one modules. You can imagine a module as a package in Java, or a header file in C.

Calling a Function

The calling convention in Erlang is module:function(argument1, argument2, ...). For example:

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

Defining Modules

Lets say 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 Function within the Module

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

Module Directives

Then we need to define the module’s attributes. An attribute is 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(…).

Defines the name of the module. For our example, in the top of mlists.erl place:

-module(mlists).

I repeat: the module name should be the same as the filename containing the module.

-export([…])

More attributes are available to be passed to the compiler. A “necessary” one is the:

-export([function1/arity1, function2/arity2, ...]).

that is used to define which functions the module exports, where “exports” means that they will be available to be called outside the module. Think of it as an Interface in Java.

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

-compile([…])

This one can be used to pass compilation instructions to the compiler. A usefull one is

-compile([export_all]).

which automatically exports all the module’s functions. It is very convenient while developing and debugging, but you should not use it on production code, since the exported functions is the interface of your module, so you do not want to provide functions that are intended only for use within the module.

-import(module_name, [function1/arity1, function2/arity2, …)

You can use this directive in order to import the selected exported functions of a module in the namespace of another one. That 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.

Other attributes

You can even define your own attributes. For example:

-author("V. Trigonakis").
-date({2011, 03, 11}).

Example

-module(md).
-export([same/1, double/1]).
-author("Vasileios Trigonakis").
-date({2011, 03, 13}).
 
same(I) ->
    I.
 
double(N) ->
    2 * N.
 
not_exported() ->
    same(smthing),
    double(123).

As you can see, this module has 3 functions and exports the 2 of them (same/1, double/1).

Compiling Modules

Emulator

Start an Erlang emulator on the folder that contains your source files. In order to compile a .erl file you use the c(module_name) Bult-in Function (BIF):

1> c(md).
./md.erl:12: Warning: function not_exported/0 is unused
{ok,md}

If no error occurs, the compiler generates the compiled .beam file. In most of the cases, an Erlang warning indicates that something is not proper. For example, the warning for the md module can help us find that the not_exported/0 function is neither exported, nor used within the file.

2> ls().
md.beam     md.erl      md.erl~     
ok

You can also provide a path to the file to be compiled, or even move around folders using the cd("path") function.

3> cd("..").
~/Documents/playing/erlang
ok
4> c("post_modules/md").
post_modules/md.erl:12: Warning: function not_exported/0 is unused
{ok,md}

Three other usefull utilities (functions) are the ls(), pwd(), and os:cmd("command") functions.

1> pwd().
/var
ok
2> ls().
backups     cache       crash       games       lib         local       
lock        log         mail        opt         run         spool       
tmp         
ok
3> os:cmd("echo echoing...").
"echoing...\n"

The last one executes the given command on a shell and returns the result.

Erlang Compiler (erlc)

You can also use the Erlang Compiler to compile a module to a beam:

$ ls
md.erl  md.erl~
$ erlc md.erl
./md.erl:12: Warning: function not_exported/0 is unused
$ ls
md.beam  md.erl  md.erl~

Loading a Module

You can load an already compiled module

1> l(md).     
{module,md}

Getting the Module’s Attributes

Every compiled module exports the module_info/0 and module_info/1 functions that can be used to fetch the attributes of a module.

1> erlang:module_loaded(md).
false
2> l(md).
{module,md}
3> erlang:module_loaded(md).
true
4> md:module_info().
[{exports,[{same,1},
           {double,1},
           {module_info,0},
           {module_info,1}]},
 {imports,[]},
 {attributes,[{vsn,[205824271517095806442935620583334286333]},
              {author,"Vasileios Trigonakis"},
              {date,[{2011,3,13}]}]},
 {compile,[{options,[{cwd,"~/Documents/playing/erlang/post_modules"},
                     {outdir,"~/Documents/playing/erlang/post_modules"}]},
           {version,"4.6.4"},
           {time,{2011,3,14,7,54,12}},
           {source,"~/Documents/playing/erlang/post_modules/md.erl"}]}]
5> md:module_info(attributes).  
[{vsn,[205824271517095806442935620583334286333]},
 {author,"Vasileios Trigonakis"},
 {date,[{2011,3,13}]}]

Next

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.

Series NavigationIntroduction to Erlang : Basic Types (2/2)Introduction to Erlang : Declaring Functions

Leave a Reply

*

Posts’ Calendar
March 2011
M T W T F S S
« Feb   Apr »
 123456
78910111213
14151617181920
21222324252627
28293031