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.
1 2 3 4 5 6 7
Defining ModulesAssume 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 FunctionThe calling function of a module, convention in Erlang is
module:function(argument1, argument2, ...).
Calling a Module Function within the ModuleA 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 AttributesThen 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 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:
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.
-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 is used to pass compilation instructions to the compiler.
Names a function that should be run automatically when a module a loaded.
Compiling ModulesErlang 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
If there is no error occurs, the compiler generates the compiled .beam file.
Loading a ModuleThe 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
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! :)