11 Apr 2013, 14:08

Kalle Lindström (3 posts)

Hello, I am having some trouble running the afile_server example code. This happens when I try to compile the file:

PS C:\Users\Kalle\programming\erlang> erl
Eshell V5.10.1  (abort with ^G)
1> c(afile_server).
afile_server.erl:6: Warning: function loop/1 is unused
2> FileServer = afile_server:start(".").
=ERROR REPORT==== 11-Apr-2013::15:52:36 ===
Error in process <0.39.0> with exit value: {undef,[{afile_server,loop,["."],[]}]}

I’m running the R16B Windows 64-bit release from erlang.org. Any idea of what the problem is?

11 Apr 2013, 18:13

Alessandro Bemporad (1 post)

Hello, I’m having exactly the same problem. I’m using this shell:

Erlang R16A (erts-5.10) [source] [smp:4:4] [async-threads:10] [hipe] [kernel-poll:false]

on a Mac OS 10.8.3

11 Apr 2013, 21:02

Edward McDowell (5 posts)

There is an error in the code. The export statement must be

-export([loop/1, start/1]).

loop/1 must be exported because it is the target of spawn/3.

14 Apr 2013, 15:22

jacques Villemur (1 post)

To summarize this is the working code:

%% — %% Excerpted from “Programming Erlang, Second Edition”, %% published by The Pragmatic Bookshelf. %% Copyrights apply to this code. It may not be used to create training material, %% courses, books, articles, and the like. Contact us if you are in doubt. %% We make no guarantees that this code is fit for any purpose. %% Visit http://www.pragmaticprogrammer.com/titles/jaerlang2 for more book information. %%— -module(afile_server). -export([start/1,loop/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).

Just compiled in the shell and used. Works pretty well.

  You must be logged in to comment