Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Erlang function does not exist in module?

Tags:

erlang

In lager.elr (the main module of https://github.com/basho/lager) there is no function with name "debug" but I have an application that call debug function from lager module like: lager:debug(Str, Args)

I am beginner in Erlang but I know when we call a function from a module lile "mymodule:myfunction" there should be a function with name "myfunction" in file mymodule.erl but in this case when I search in lager.erl for function "debug" I can't find it.

like image 442
Sepehr Samini Avatar asked Oct 12 '12 22:10

Sepehr Samini


People also ask

What does Module do in Erlang?

Modules are a bunch of functions regrouped in a single file, under a single name. Additionally, all functions in Erlang must be defined in modules. Most of the basic functionality like arithmetic, logic and Boolean operators are already available because the default modules are loaded when a program is run.

What is function clause?

A function declaration is a sequence of function clauses separated by semicolons, and terminated by period (.). A function clause consists of a clause head and a clause body, separated by ->.


2 Answers

The reason you don't see a mention of lager:debug/2 is because lager uses a parse transform. So when compiling the code, it is fed through lagers parse transform and the call to lager:debug/2 is substituted for another call to another module function.

If you compile your code with the correct parse transform option for lager, then the code would work.

like image 127
I GIVE CRAP ANSWERS Avatar answered Oct 18 '22 04:10

I GIVE CRAP ANSWERS


"I GIVE CRAP ANSWERS" gave a good explanation of this strange behaviour. I post here a code that should show you what was the code generated in the beam file:

In the shell:

utility:decompile([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).
like image 45
Pascal Avatar answered Oct 18 '22 03:10

Pascal