簡體   English   中英

模塊中不存在Erlang功能?

[英]Erlang function does not exist in module?

在lager.elr( https://github.com/basho/lager的主要模塊)中沒有名稱為“debug”的函數,但我有一個應用程序從lager模塊調用調試函數,如:lager:debug(Str,參數)

我是Erlang的初學者但是我知道當我們從模塊lile“mymodule:myfunction”調用一個函數時,文件mymodule.erl中應該有一個名為“myfunction”的函數但是在這種情況下我在lager.erl中搜索函數“調試”我找不到它。

你沒有看到提到lager:debug/2是因為lager使用了解析變換 因此,在編譯代碼時,它通過lagers parse轉換和對lager:debug/2的調用lager:debug/2代替另一個模塊函數的調用。

如果使用正確的分析變換選項編譯代碼,則代碼可以正常工作。

“我給CRAP答案”給出了這個奇怪行為的一個很好的解釋。 我在這里發布一個代碼,它應該顯示在beam文件中生成的代碼:

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

你沒有在lager.erl文件中看到它,因為它位於lager.erl頂部的lager.hrl文件中。 Erlang允許您包含帶有-include(“filename.hrl”)指令的文件。 作為慣例,包含文件以hrl擴展名結尾,但它實際上可以是任何東西。

https://github.com/basho/lager/blob/master/include/lager.hrl

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM