[erlang-questions] Question concerning the use of -export

David Cabana dcabana@REDACTED
Thu Jul 12 05:24:30 CEST 2007

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

Here is the code:

-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()},
         pong ->
             io:format("Ping received pong~n", [])
     ping(N - 1, Pong_PID).

pong() ->
         finished ->
             io:format("Pong finished~n", []);
         {ping, Ping_PID} ->
             io:format("Pong received ping~n", []),
             Ping_PID ! pong,

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 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?

More information about the erlang-questions mailing list