论坛首页 综合技术论坛

Erlang动态编译加载模块

浏览 2763 次
精华帖 (3) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2009-10-06  
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>
   发表时间:2009-11-11  
哈哈,这个是个好东西,动态编译出const pool,读取速度上来不少呢,
0 请登录后投票
论坛首页 综合技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics