Erlang code is divided into modules. A module consists of a sequence of attributes and function declarations, each terminated by period (.).
Example:
-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
For a description of function declarations, see
A module attribute defines a certain property of a module.
A module attribute consists of a tag and a value:
-Tag(Value).
Any module attribute can be specified. The attributes are stored
in the compiled code and can be retrieved by calling
Several module attributes have predefined meanings. Some of them have arity two, but user-defined module attributes must have arity one.
Pre-defined module attributes is to be placed before any function declaration.
Module declaration, defining the name of the module.
The name
This attribute is to be specified first and is the only mandatory attribute.
Exported functions. Specifies which of the functions, defined within the module, that are visible from outside the module.
Imported functions. Can be called the same way as local functions, that is, without any module prefix.
Compiler options.
Module version.
If this attribute is not specified, the version defaults to the MD5 checksum of the module.
This attribute names a function that is to be run
automatically when a
module is loaded. For more information, see
It is possible to specify that the module is the callback module for a behaviour:
-behaviour(Behaviour).
The atom
The spelling
The callback functions of the module can be specified either
directly by the exported function
behaviour_info(callbacks) -> Callbacks.
or by a
-callback Name(Arguments) -> Result.
Here,
Read more about behaviours and callback modules in
The same syntax as for module attributes is used for record definitions:
-record(Record,Fields).
Record definitions are allowed anywhere in a module,
also among the function declarations.
Read more in
The same syntax as for module attributes is used by the preprocessor, which supports file inclusion, macros, and conditional compilation:
-include("SomeFile.hrl"). -define(Macro,Replacement).
Read more in
The same syntax as for module attributes is used for
changing the pre-defined macros
-file(File, Line).
This attribute is used by tools, such as Yecc, to inform the compiler that the source program is generated by another tool. It also indicates the correspondence of source files to lines of the original user-written file, from which the source program is produced.
A similar syntax as for module attributes is used for specifying types and function specifications:
-type my_type() :: atom() | integer(). -spec my_function(integer()) -> integer().
Read more in
The description is based on
Comments can be placed anywhere in a module except within strings and quoted atoms. A comment begins with the character "%", continues up to, but does not include the next end-of-line, and has no effect. Notice that the terminating end-of-line has the effect of white space.
The compiler automatically inserts the two special, exported functions into each module:
These functions can be called to retrieve information about the module.
The
The call
The following values are allowed for
Returns an atom representing the module name.
Returns a list of
The list of attributes becomes empty if
the module is stripped with the
Returns a list of tuples with information about
how the module was compiled. This list is empty if
the module has been stripped with the
Returns a binary representing the MD5 checksum of the module. If the module has native code loaded, this will be the MD5 of the native code, not the BEAM bytecode.
Returns a list of
Returns a list of
Return