Erlang模块基础 #

一、模块概述 #

模块是Erlang代码的基本组织单元,每个Erlang文件定义一个模块。

1.1 模块特点 #

  • 每个文件一个模块
  • 模块名与文件名相同
  • 模块包含函数和数据
  • 支持信息隐藏

二、模块定义 #

2.1 基本结构 #

erlang
-module(my_module).
-export([public_func/1]).

public_func(X) -> X * 2.
private_func(X) -> X + 1.

2.2 模块命名 #

erlang
-module(my_app_server).
-module(http_client).
-module(user_manager).

命名规范:

  • 使用小写字母和下划线
  • 使用有意义的名称
  • 可以使用层级命名

三、导出 #

3.1 导出函数 #

erlang
-module(export_example).
-export([func1/0, func2/1]).
-export([func3/2]).

func1() -> ok.
func2(X) -> X.
func3(A, B) -> {A, B}.
func4() -> private.

3.2 export_all #

erlang
-module(export_all_example).
-compile(export_all).

func1() -> ok.
func2() -> ok.

注意:仅用于开发调试。

3.3 导出类型 #

erlang
-module(export_types).
-export_type([my_type/0, result/0]).

-type my_type() :: integer() | atom().
-type result() :: {ok, term()} | {error, term()}.

四、导入 #

4.1 导入函数 #

erlang
-module(import_example).
-import(lists, [map/2, filter/2]).
-import(io, [format/2]).

demo() ->
    Numbers = [1, 2, 3, 4, 5],
    Doubled = map(fun(X) -> X * 2 end, Numbers),
    format("Doubled: ~p~n", [Doubled]).

4.2 导入 vs 完全限定调用 #

erlang
-module(import_vs_qualified).
-import(lists, [map/2]).

with_import(List) ->
    map(fun(X) -> X * 2 end, List).

with_qualified(List) ->
    lists:map(fun(X) -> X * 2 end, List).

建议:使用完全限定调用更清晰。

五、模块属性 #

5.1 预定义属性 #

erlang
-module(module_attrs).
-author('developer@example.com').
-vsn("1.0.0").
-export([info/0]).

info() ->
    Attrs = ?MODULE:module_info(attributes),
    io:format("Attributes: ~p~n", [Attrs]).

5.2 自定义属性 #

erlang
-module(custom_attrs).
-export([get_version/0, get_author/0]).

-define(VERSION, "1.0.0").
-author('developer@example.com').
-created('2026-03-27').

get_version() -> ?VERSION.
get_author() -> ?MODULE:module_info(attributes).

5.3 编译属性 #

erlang
-module(compile_attrs).
-compile([debug_info, export_all]).
-compile({nowarn_unused_function}).

unused_func() -> ok.

5.4 行为属性 #

erlang
-module(gen_server_example).
-behaviour(gen_server).

-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).

init(_Args) -> {ok, #{}}.
handle_call(_Request, _From, State) -> {reply, ok, State}.
handle_cast(_Request, State) -> {noreply, State}.
handle_info(_Info, State) -> {noreply, State}.
terminate(_Reason, _State) -> ok.
code_change(_OldVsn, State, _Extra) -> {ok, State}.

六、模块信息 #

6.1 获取模块信息 #

erlang
-module(module_info).
-export([demo/0]).

demo() ->
    Info = ?MODULE:module_info(),
    io:format("Module info: ~p~n", [Info]),
    
    Functions = ?MODULE:module_info(functions),
    io:format("Functions: ~p~n", [Functions]),
    
    Attributes = ?MODULE:module_info(attributes),
    io:format("Attributes: ~p~n", [Attributes]),
    ok.

6.2 模块信息字段 #

字段 说明
module 模块名
exports 导出函数列表
functions 所有函数列表
attributes 模块属性
compile 编译信息

七、包含文件 #

7.1 include #

erlang
-module(include_example).
-include("records.hrl").
-include("macros.hrl").

demo() ->
    User = #user{name = "Alice", age = 30},
    io:format("User: ~p~n", [User]),
    io:format("Version: ~p~n", [?VERSION]).

records.hrl:

erlang
-record(user, {name, age}).

macros.hrl:

erlang
-define(VERSION, "1.0.0").

7.2 include_lib #

erlang
-module(include_lib_example).
-include_lib("kernel/include/file.hrl").

file_info(Path) ->
    {ok, Info} = file:read_file_info(Path),
    #file_info{size = Size, type = Type} = Info,
    io:format("Size: ~p, Type: ~p~n", [Size, Type]).

八、模块组织 #

8.1 函数顺序 #

erlang
-module(function_order).
-export([public_api/1]).

public_api(Data) ->
    validate(Data),
    process(Data),
    format_result(Data).

validate(_) -> ok.
process(_) -> ok.
format_result(Data) -> {ok, Data}.

8.2 分组注释 #

erlang
-module(grouped_functions).
-export([create/1, read/1, update/2, delete/1]).

create(Data) -> {ok, Data}.

read(Id) -> {ok, #{id => Id}}.

update(Id, Data) -> {ok, #{id => Id, data => Data}}.

delete(Id) -> {ok, Id}.

九、最佳实践 #

9.1 模块大小 #

  • 每个模块专注于单一职责
  • 函数数量适中(建议50-200行)
  • 复杂功能拆分为多个模块

9.2 接口设计 #

erlang
-module(api_design).
-export([create_user/1, get_user/1, update_user/2, delete_user/1]).

create_user(Params) -> 
    case validate(Params) of
        ok -> do_create(Params);
        {error, _} = Error -> Error
    end.

get_user(Id) -> 
    do_get(Id).

update_user(Id, Params) -> 
    do_update(Id, Params).

delete_user(Id) -> 
    do_delete(Id).

validate(_) -> ok.
do_create(P) -> {ok, P}.
do_get(I) -> {ok, #{id => I}}.
do_update(I, P) -> {ok, #{id => I, data => P}}.
do_delete(I) -> {ok, I}.

9.3 文档 #

erlang
-module(documented_module).
-export([process/1]).

process(Data) -> 
    do_process(Data).

do_process(D) -> D.

十、总结 #

本章学习了:

  • 模块定义
  • 导出和导入
  • 模块属性
  • 模块信息
  • 包含文件
  • 模块组织
  • 最佳实践

准备好学习模块属性了吗?让我们进入下一章。

最后更新:2026-03-27