Erlang动态编译加载模块
October 6th, 2009
原创文章,转载请注明: 转载自系统技术非业余研究
本文链接地址: Erlang动态编译加载模块
ejabberd最新的版本有个模块叫做 dynamic_compile, 支持从string动态加载一个模块。有了这个功能我们就可以很方便的动态生成一个模块,加入到我们的运行期。我想的有以下几个功能:
1. const 模块
2. 如日志系统的级别:
log(S) when 0 > 1 -> do_log(S); log(_)-> skip.
这样的模块 编译的时候 会把前面的就省去了判断, 直接由compiler去掉了,因为when永远不满足。
试验如下:
yu-fengdemacbook-2:~ yufeng$ erl
Erlang (BEAM) emulator version 5.6.5 [source] [async-threads:0] [hipe] [kernel-poll:false]
Eshell V5.6.5 (abort with ^G)
1> {Mod,Code} = dynamic_compile:from_string("-module(test).\n-export([start/0]).\n start()->ok.\n"), code:load_binary(Mod, "test.erl", Code).
{module,test}
2> m(test).
Module test compiled: Date: October 6 2009, Time: 08.43
Compiler options: []
Object file: test.erl
Exports:
module_info/0
module_info/1
start/0
ok
3> test:start().
ok
4>
Post Footer automatically generated by wp-posturl plugin for wordpress.
Categories: Erlang探索
Recent Comments