[erlang-questions] Question concerning the use of -export
Gaspar Chilingarov
nm@REDACTED
Thu Jul 12 08:01:37 CEST 2007
David Cabana wrote:
> I have a question regarding the -export module attribute; it arose as
> a result of playing with this code,
> taken from the section 3.2 of the Erlang Getting Started document
> available at
> http://www.erlang.org/doc/getting_started/part_frame.html
>
> Here is the code:
> %---------------------------------------------------
> -module(tut15).
>
> -export([start/0, ping/2, pong/0]).
>
> ping(0, Pong_PID) ->
> Pong_PID ! finished,
> io:format("ping finished~n", []);
>
> ping(N, Pong_PID) ->
> Pong_PID ! {ping, self()},
> receive
> pong ->
> io:format("Ping received pong~n", [])
> end,
> ping(N - 1, Pong_PID).
>
> pong() ->
> receive
> finished ->
> io:format("Pong finished~n", []);
> {ping, Ping_PID} ->
> io:format("Pong received ping~n", []),
> Ping_PID ! pong,
> pong()
> end.
>
> start() ->
> Pong_PID = spawn(tut15, pong, []),
> spawn(tut15, ping, [3, Pong_PID]).
> %---------------------------------------------------
>
> My question is this: why do ping/2 and pong/0 have to be exported?
> The code is invoked from a command line by tut15:start(),
> so I can see that start/0 has to be exported. I tried not exporting
> pong/0, and got a compiler warning:
> ./tut15.erl:17: Warning: function pong/0 is unused
>
> I disregarded the warning and executed the code, producing a runtime
> error:
> Error in process <0.141.0> with exit value: {undef,[{tut15,pong,[]}]}
>
> It seems to me that pong/0 is not being called from outside the
> tut15 module, so I should not have to export it. What am I missing here?
I had the same questions, when I first started programming erlang -- the
annswer is that spawn'ed functions should be exported.
or you should do
spawn(fun pong() end ) or spawn(fun pong/0)
which will not require exporting of function ping
--
Gaspar Chilingarov
System Administrator,
Network security consulting
t +37493 419763 (mob)
i 63174784
e nm@REDACTED
w http://zanazan.am/
More information about the erlang-questions
mailing list