[erlang-questions] Issue with "Programming Erlang" sample

Bengt Kleberg bengt.kleberg@REDACTED
Tue Apr 16 15:08:20 CEST 2013


Greetings,

When using the old erlang:spawn/3 you must export the function (the
second argument).

You may consider erlang:spawn/1 that takes a fun. In your case:

start(Dir) -> erlang:spawn( fun() -> loop(Dir) end ).

Here no export of loop/1 is needed.


bengt

On Tue, 2013-04-16 at 05:52 -0700, John Williston wrote:
> Good morning,
> 
>  
> 
> I’m learning Erlang using the Programming Erlang (2d ed) text from
> Pragmatic Bookshelf. Chapter two, “A Whirlwind Tour of Erlang”
> includes a file server sample that’s failing to work as the book
> suggests, and I’m afraid I’m not getting anywhere figuring it out.
> First, here’s the server code:
> 
>  
> 
> -module(afile_server).
> 
> -export([start/1]).
> 
>  
> 
> start(Dir) -> spawn(afile_server, loop, [Dir]).
> 
>  
> 
> loop(Dir) ->
> 
>     receive
> 
>         {Client, list_dir} ->
> 
>             Client ! {self(), file:list_dir(Dir)};
> 
>         {Client, {get_file, File}} ->
> 
>             Full = filename:join(Dir, File),
> 
>             Client ! {self(), file:read_file(Full)}
> 
>     end,
> 
>     loop(Dir).
> 
>  
> 
> That compiles (albeit with a warning saying “Warning: function loop/1
> is unused”) but when I try to execute it from within the shell I get
> an error. For sake of reference the shell commands I’m issuing are:
> 
>  
> 
> Eshell V5.10.1  (abort with ^G)
> 
> 1> c(afile_server).
> 
> afile_server.erl:7: Warning: function loop/1 is unused
> 
> {ok,afile_server}
> 
> 2> FileServer = afile_server:start(".").
> 
> <0.39.0>
> 
> 3>
> 
> =ERROR REPORT==== 10-Apr-2013::10:31:17 ===
> 
> Error in process <0.39.0> with exit value:
> {undef,[{afile_server,loop,["."],[]}]}
> 
>  
> 
> Can anyone suggest what might be wrong? I’m running under Windows 7
> Professional x64 for sake of reference. Thanks in advance!
> 
>  
> 
> John
> 
> 
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions




More information about the erlang-questions mailing list