1
<?xml version="1.0" encoding="latin1" ?>
2
<!DOCTYPE chapter SYSTEM "chapter.dtd">
7
<year>2003</year><year>2010</year>
8
<holder>Ericsson AB. All Rights Reserved.</holder>
11
The contents of this file are subject to the Erlang Public License,
12
Version 1.1, (the "License"); you may not use this file except in
13
compliance with the License. You should have received a copy of the
14
Erlang Public License along with this software. If not, it can be
15
retrieved online at http://www.erlang.org/.
17
Software distributed under the License is distributed on an "AS IS"
18
basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
19
the License for the specific language governing rights and limitations
24
<title>Modules</title>
29
<file>modules.xml</file>
33
<title>Module Syntax</title>
34
<p>Erlang code is divided into <em>modules</em>. A module consists
35
of a sequence of attributes and function declarations, each
36
terminated by period (.). Example:</p>
38
-module(m). % module attribute
39
-export([fact/1]). % module attribute
41
fact(N) when N>0 -> % beginning of function declaration
44
1. % end of function declaration</pre>
45
<p>See the <seealso marker="functions">Functions</seealso> chapter
46
for a description of function declarations.</p>
50
<title>Module Attributes</title>
51
<p>A <em>module attribute</em> defines a certain property of a
52
module. A module attribute consists of a tag and a value.</p>
55
<p><c>Tag</c> must be an atom, while <c>Value</c> must be a literal
56
term. As a convenience in user-defined attributes, the literal term
57
<c>Value</c> the syntax <c>Name/Arity</c>
58
(where <c>Name</c> is an atom and <c>Arity</c> a positive integer)
59
will be translated to <c>{Name,Arity}</c>.</p>
61
<p>Any module attribute can be specified. The attributes are stored
62
in the compiled code and can be retrieved by calling
63
<c>Module:module_info(attributes)</c> or by using
64
<seealso marker="stdlib:beam_lib#chunks/2">beam_lib(3)</seealso>.</p>
66
<p>There are several module attributes with predefined meanings,
67
some of which have arity two, but user-defined module
68
attributes must have arity one.</p>
71
<title>Pre-Defined Module Attributes</title>
72
<p>Pre-defined module attributes should be placed before any
73
function declaration.</p>
75
<tag><c>-module(Module).</c></tag>
77
<p>Module declaration, defining the name of the module.
78
The name <c>Module</c>, an atom, should be the same as
79
the file name minus the extension <c>erl</c>. Otherwise
80
<seealso marker="code_loading#loading">code loading</seealso> will
81
not work as intended.</p>
82
<p>This attribute should be specified first and is the only
83
attribute which is mandatory.</p>
85
<tag><c>-export(Functions).</c></tag>
87
<p>Exported functions. Specifies which of the functions
88
defined within the module that are visible outside
90
<p><c>Functions</c> is a list
91
<c>[Name1/Arity1, ..., NameN/ArityN]</c>, where each
92
<c>NameI</c> is an atom and <c>ArityI</c> an integer.</p>
94
<tag><c>-import(Module,Functions).</c></tag>
96
<p>Imported functions. Imported functions can be called
97
the same way as local functions, that is without any module
99
<p><c>Module</c>, an atom, specifies which module to import
100
functions from. <c>Functions</c> is a list similar as for
101
<c>export</c> above.</p>
103
<tag><c>-compile(Options).</c></tag>
105
<p>Compiler options. <c>Options</c>, which is a single option
106
or a list of options, will be added to the option list when
107
compiling the module. See <c>compile(3)</c>.</p>
109
<tag><c>-vsn(Vsn).</c></tag>
111
<p>Module version. <c>Vsn</c> is any literal term and can be
112
retrieved using <c>beam_lib:version/1</c>, see
113
<seealso marker="stdlib:beam_lib#version/1">beam_lib(3)</seealso>.</p>
114
<p>If this attribute is not specified, the version defaults
115
to the MD5 checksum of the module.</p>
121
<title>Behaviour Module Attribute</title>
122
<p>It is possible to specify that the module is the callback
123
module for a <em>behaviour</em>:</p>
125
-behaviour(Behaviour).</pre>
126
<p>The atom <c>Behaviour</c> gives the name of the behaviour,
127
which can be a user defined behaviour or one of the OTP
128
standard behaviours <c>gen_server</c>, <c>gen_fsm</c>,
129
<c>gen_event</c> or <c>supervisor</c>.</p>
130
<p>The spelling <c>behavior</c> is also accepted.</p>
131
<p>Read more about behaviours and callback modules in OTP Design
136
<title>Record Definitions</title>
137
<p>The same syntax as for module attributes is used by
138
for record definitions:</p>
140
-record(Record,Fields).</pre>
141
<p>Record definitions are allowed anywhere in a module,
142
also among the function declarations.
143
Read more in <seealso marker="records">Records</seealso>.</p>
147
<title>The Preprocessor</title>
148
<p>The same syntax as for module attributes is used by
149
the preprocessor, which supports file inclusion, macros,
150
and conditional compilation:</p>
152
-include("SomeFile.hrl").
153
-define(Macro,Replacement).</pre>
155
<p>Read more in <seealso marker="macros">The Preprocessor</seealso>.</p>
159
<title>Setting File and Line</title>
160
<p>The same syntax as for module attributes is used for
161
changing the pre-defined macros <c>?FILE</c> and <c>?LINE</c>:</p>
163
-file(File, Line).</pre>
164
<p>This attribute is used by tools such as Yecc to inform the
165
compiler that the source program was generated by another tool
166
and indicates the correspondence of source files to lines of
167
the original user-written file from which the source program
172
<title>Types and function specifications</title>
173
<p>A similar syntax as for module attributes is used for
174
specifying types and function specifications.
177
-type my_type() :: atom() | integer().
178
-spec my_function(integer()) -> integer().
180
<p>Read more in <seealso marker="typespec">Types and Function specifications</seealso>.
183
The desciption is based on
184
<url href="http://www.erlang.org/eeps/eep-0008.html">EEP8 -
185
Types and function specifications</url>
186
which will not be further updated.
192
<title>Comments</title>
193
<p>Comments may be placed anywhere in a module except within strings
194
and quoted atoms. The comment begins with the character "%",
195
continues up to, but does not include the next end-of-line, and
196
has no effect. Note that the terminating end-of-line has
197
the effect of white space.</p>
201
<title>The module_info/0 and module_info/1 functions</title>
203
<p>The compiler automatically inserts the two special, exported
204
functions into each module: <c>Module:module_info/0</c> and
205
<c>Module:module_info/1</c>. These functions can be called to
206
retrieve information about the module.</p>
209
<title>module_info/0</title>
210
<p>The <c>module_info/0</c> function in each module returns
211
a list of <c>{Key,Value}</c> tuples with information about
212
the module. Currently, the list contain tuples with the following
213
<c>Key</c>s: <c>attributes</c>, <c>compile</c>,
214
<c>exports</c>, and <c>imports</c>. The order and number of tuples
215
may change without prior notice.</p>
217
<warning><p>The <c>{imports,Value}</c> tuple may be removed in a future
218
release because <c>Value</c> is always an empty list.
219
Do not write code that depends on it being present.</p></warning>
223
<title>module_info/1</title>
224
<p>The call <c>module_info(Key)</c>, where key is an atom,
225
returns a single piece of information about the module.</p>
227
<p>The following values are allowed for <c>Key</c>:</p>
230
<tag><c>attributes</c></tag>
232
<p>Return a list of <c>{AttributeName,ValueList}</c> tuples,
233
where <c>AttributeName</c> is the name of an attribute,
234
and <c>ValueList</c> is a list of values. Note: a given
235
attribute may occur more than once in the list with different
236
values if the attribute occurs more than once in the module.</p>
238
<p>The list of attributes will be empty if
239
the module has been stripped with
240
<seealso marker="stdlib:beam_lib#strip/1">beam_lib(3)</seealso>.</p>
243
<tag><c>compile</c></tag>
245
<p>Return a list of tuples containing information about
246
how the module was compiled. This list will be empty if
247
the module has been stripped with
248
<seealso marker="stdlib:beam_lib#strip/1">beam_lib(3)</seealso>.</p>
251
<tag><c>imports</c></tag>
253
<p>Always return an empty list. The <c>imports</c> key may not
254
be supported in future release.</p>
257
<tag><c>exports</c></tag>
259
<p>Return a list of <c>{Name,Arity}</c> tuples with
260
all exported functions in the module.</p>
263
<tag><c>functions</c></tag>
265
<p>Return a list of <c>{Name,Arity}</c> tuples with
266
all functions in the module.</p>