What does the question mark in ?MODULE (which can be seen in all generated code by Mochiweb make command) mean?
-export([start/1, stop/0, loop/2]).
start(Options) ->
{DocRoot, Options1} = get_option(docroot, Options),
Loop = fun (Req) ->
?MODULE:loop(Req, DocRoot)
end,
mochiweb_http:start([{name, ?MODULE}, {loop, Loop} | Options1]).
stop() ->
mochiweb_http:stop(?MODULE).
loop(Req, DocRoot) ->
...
It denotes a preprocessor macro. ?MODULE
is one of the predefined macro constants that expand to current module's name.
Well this is the way we represent MACROS in Erlang. At compile time, these macros are replaced with the actual meanings. They save on re-writing pieces of code or on abstracting out a parameter you may change in future without changing your code (would only require a re-compilation of the source that depends on the MACRO).
Forexample:
-module(square_plus). -compile(export_all). -define(SQUARE(X),X * X). add_to_square(This,Number)-> ?SQUARE(This) + Number.
Is the same as:
-module(square_plus). -compile(export_all). add_to_square(This,Number)-> (This * This) + Number.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With