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
See the
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
There are several module attributes with predefined meanings, some of which have arity two, but user-defined module attributes must have arity one.
Pre-defined module attributes should be placed before any function declaration.
Module declaration, defining the name of the module.
The name
This attribute should be specified first and is the only attribute which is mandatory.
Exported functions. Specifies which of the functions defined within the module that are visible outside the module.
Imported functions. 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.
Names a function that should be run automatically when a
module a loaded. See
It is possible to specify that the module is the callback module for a behaviour:
-behaviour(Behaviour).
The atom
The spelling
Read more about behaviours and callback modules in OTP Design Principles.
The same syntax as for module attributes is used by 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 was generated by another tool and indicates the correspondence of source files to lines of the original user-written file from which the source program was 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 may be placed anywhere in a module except within strings and quoted atoms. The comment begins with the character "%", continues up to, but does not include the next end-of-line, and has no effect. Note that the terminating end-of-line has the effect of white space.
The compiler automatically inserts the two special, exported
functions into each module:
The
The
The call
The following values are allowed for
Return a list of
The list of attributes will be empty if
the module has been stripped with
Return a list of tuples containing information about
how the module was compiled. This list will be empty if
the module has been stripped with
Always return an empty list. The
Return a list of
Return a list of