模块

  • 模块文件以 .erl 结尾

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

模块 Attributes

模块 Attributes定义形式 -Tag(Value).

Tag 是 atom, Value 文字术语描述.

这种 syntax Name/Arity (where Name is an atom and Arity a positive integer) 是一个便捷的用户自定义attributes,会转化为 {Name,Arity}.

module attribute 可以任意指定. attributes 编译时会存储起来并且可calling Module:module_info(attributes) 获取, 也可使用 module beam_lib(3) in STDLIB.

预定义模块 Attributes

  • -module(ModuleName).

    文件名和模块名必须相同

  • -export(Functions).

    声明导出函数的列表,导出的函数是全局性的,可以在模块外部调用

  • -import(Module,Functions).

    导入函数到模块内部,这样可以像模块内部函数一样调用,即没有模块声明前缀

  • -compile(Options).

    设置编译属性,See the compile(3) manual page in Compiler.

  • -vsn(Vsn).

    模块版本。没有设置的话,默认使用module的 MD5 checksum

    Vsn is any literal term and can be retrieved using beam_lib:version/1, see the beam_lib(3) manual page in STDLIB.

  • -on_load(Function).

    这个属性是设置一些函数可以在module加载之后自动运行,see Running a Function When a Module is Loaded.

Record Definitions

形式: -record(Record,Fields).

Record definitions are allowed anywhere in a module, also among the function declarations. Read more in Records.

Behaviour Module Attribute

It is possible to specify that the module is the callback module for a behaviour:

-behaviour(Behaviour).

The atom Behaviour gives the name of the behaviour, which can be a user-defined behaviour or one of the following OTP standard behaviours:

  • gen_server
  • gen_fsm
  • gen_event
  • supervisor

The spelling behavior is also accepted.

The callback functions of the module can be specified either directly by the exported function behaviour_info/1:

behaviour_info(callbacks) -> Callbacks.

or by a -callback attribute for each callback function:

-callback Name(Arguments) -> Result.

Here, Arguments is a list of zero or more arguments. The -callback attribute is to be preferred since the extra type information can be used by tools to produce documentation or find discrepancies.

Read more about behaviours and callback modules in OTP Design Principles.

Preprocessor

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 Preprocessor.

Setting File and Line

The same syntax as for module attributes is used for changing the pre-defined macros ?FILE and ?LINE:

-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.

Types and function specifications

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 Types and Function specifications.

The description is based on EEP8 - Types and function specifications, which is not to be further updated.

Comments

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.

module_info/0 and module_info/1 functions

The compiler automatically inserts the two special, exported functions into each module:

  • Module:module_info/0
  • Module:module_info/1

These functions can be called to retrieve information about the module.

module_info/0

The module_info/0 function in each module, returns a list of {Key,Value} tuples with information about the module. Currently, the list contain tuples with the following Keys: module, attributes, compile, exports, md5 and native. The order and number of tuples may change without prior notice.

module_info/1

The call module_info(Key), where Key is an atom, returns a single piece of information about the module.

The following values are allowed for Key:

  • module

    Returns an atom representing the module name.

  • attributes

    Returns a list of {AttributeName,ValueList} tuples, where AttributeName is the name of an attribute, and ValueList is a list of values. Notice that a given attribute can occur more than once in the list with different values if the attribute occurs more than once in the module.

    The list of attributes becomes empty if the module is stripped with the beam_lib(3) module (in STDLIB).

  • compile

    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 beam_lib(3) module (in STDLIB).

  • md5

    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.

  • exports

    Returns a list of {Name,Arity} tuples with all exported functions in the module.

  • functions

    Returns a list of {Name,Arity} tuples with all functions in the module.

  • native

    Return true if the module has native compiled code. Return false otherwise. In a system compiled without HiPE support, the result is always false