模块中不存在Erlang函数?
问题描述:
在lager.elr(的https://github.com/basho/lager主模块)有名称为“调试”没有的功能,但我有一个调用调试功能从啤酒模块类似的应用程序: 啤酒:调试(力量,参数数量)模块中不存在Erlang函数?
我我是Erlang的初学者,但我知道当我们从一个模块lile调用函数“mymodule:myfunction”时,应该在文件mymodule.erl中有一个名为“myfunction”的函数,但是在这种情况下,当我在lager.erl中搜索函数“调试“我找不到它。
答
您不会在lager.erl文件中看到它,因为它位于lager.erl顶部包含的lager.hrl文件中。 Erlang允许你使用-include(“filename.hrl”)指令包含一个文件。作为惯例,包含文件以hrl扩展结尾,但它可能确实是任何东西。
https://github.com/basho/lager/blob/master/include/lager.hrl
答
的原因,你没有看到的lager:debug/2
一提的是,因为啤酒使用解析变换。所以在编译代码时,它是通过lagers解析转换进行的,并且调用lager:debug/2
将替换另一个调用另一个模块函数。
如果使用正确的lager转换选项编译代码,那么代码将起作用。
答
“我给了答案解答”对这种奇怪的行为给了很好的解释。我在这里发表的是应该告诉你什么是梁文件中生成的代码的代码:
在shell:
工具:编译([yourfile.beam])。
%% Author: PCHAPIER
%% Created: 25 mai 2010
-module(utility).
%%
%% Include files
%%
%%
%% Exported Functions
%%
-export([decompile/1, decompdir/1]).
-export([shuffle/1]).
%%
%% API Functions
%%
decompdir(Dir) ->
Cmd = "cd " ++ Dir,
os:cmd(Cmd),
L = os:cmd("dir /B *.beam"),
L1 = re:split(L,"[\t\r\n+]",[{return,list}]),
io:format("decompdir: ~p~n",[L1]),
decompile(L1).
decompile(Beam = [H|_]) when is_integer(H) ->
io:format("decompile: ~p~n",[Beam]),
{ok,{_,[{abstract_code,{_,AC}}]}} = beam_lib:chunks(Beam ++ ".beam",[abstract_code]),
{ok,File} = file:open(Beam ++ ".erl",[write]),
io:fwrite(File,"~s~n", [erl_prettypr:format(erl_syntax:form_list(AC))]),
file:close(File);
decompile([H|T]) ->
io:format("decompile: ~p~n",[[H|T]]),
decompile(removebeam(H)),
decompile(T);
decompile([]) ->
ok.
shuffle(P) ->
Max = length(P)*10000,
{_,R}= lists:unzip(lists:keysort(1,[{random:uniform(Max),X} || X <- P])),
R.
%%
%% Local Functions
%%
removebeam(L) ->
removebeam1(lists:reverse(L)).
removebeam1([$m,$a,$e,$b,$.|T]) ->
lists:reverse(T);
removebeam1(L) ->
lists:reverse(L).
但是在头文件中没有名称为“debug”的函数。在该头文件中有:-define(LEVELS,[调试,信息,通知,警告,错误,紧急,警报,紧急情况,无])。我找不到具有“调试”名称的功能。因为在另一个应用程序中它调用“调试”,如:lager:debug(parameter1,parameter2)。 –