[erlang-questions] Problem running code from Armstrong's Programming Erlang

Joe Armstrong <>
Sun Jun 17 03:13:04 CEST 2007


The idea of the template is to provide a starting point for building a
server. It's not
runnable by itself - but has to be edited with your application
specific code first.

The idea is that you issue an rpc and you will get some printout
telling you that
you have not catered for the message that you sent to the server. You add a bit
more code so that the printout does not occur and the program is done :-)

In your example you evaluated

> 3> ctemplate:rpc(Pid, {foo}).

The response was

> Received:{<0.31.0>,{foo}}

This shows that the server received a particular message, but that no
code handled this message.

Then you have to add some code that does something (like sending a reply)

loop(X) ->
    receive
        {From, {foo}} ->
              From ! {self(), receivedFoo},
              loop(X);
        Any ->
              ...
              loop(X)
      end.

And you will get a response.

The final case in the receive statement

     Any ->
         io:format(...)

is added so that you get a warning printout to handle all message that
are not matched
in the receive loop.

Sending a tuple with one element {foo} looks strange - tuples are
usually used to
group together more than one values into a compound term. So probably just foo
would have done here :-)

Cheers

/Joe Armstrong







On 6/16/07, David Cabana <> wrote:
> I am reading a beta copy of Joe Armstrong's "Programming Erlang."
> The book's support site provides code samples for download; I have a
> question regarding a pair of similar bits of code from the site. One
> behaves as I would expect, the other locks up my erl session. I am
> trying to understand why the latter happens.
>
> Here is the first example, the one that works as expected. It is
> available at "http://media.pragprog.com/titles/jaerlang/code/
> area_server_final.erl".  I have omitted some boilerplate comments at
> the beginning of the example files, for the sake of brevity.
>
> %% ---------------  begin first example
> -module(area_server_final).
> -export([start/0, area/2]).
>
> start() -> spawn(fun loop/0).
>
> area(Pid, What) ->
>      rpc(Pid, What).
>
> rpc(Pid, Request) ->
>      Pid ! {self(), Request},
>      receive
>         {Pid, Response} ->
>             Response
>      end.
>
>
> loop() ->
>      receive
>         {From, {rectangle, Width, Ht}} ->
>             From ! {self(), Width * Ht},
>             loop();
>         {From, {circle, R}} ->
>             From !  {self(), 3.14159 * R * R},
>             loop();
>         {From, Other} ->
>             From ! {self(), {error,Other}},
>             loop()
>      end.
> %% ---------------- end first example
>
> Here is the expected behavior, taken from an erl session:
> %% -------------- begin behavior, first example
>
> Eshell V5.5.4  (abort with ^G)
>
> 1>c(area_server_final).
> {ok,area_server_final}
>
> 2> Pid = area_server_final:start().
> <0.42.0>
>
> 3> area_server_final:area(Pid, {rectangle,2,3}).
> 6
>
> 4>
>
> %% -------------- end behavior, first example
> In particular, the area is computed and printed, and control returns
> to erl's read-eval-print loop.
>
> The second example is one that Mr Armstrong provides as a template
> for exploratory programming. I tried to use it as such, with no luck,
> and am trying to understand what is going wrong. The code is
> available at http://media.pragprog.com/titles/jaerlang/code/
> ctemplate.erl
> %% --------------- begin second example
>
> -module(ctemplate).
> -compile(export_all).
>
> start() ->
>      spawn(fun() -> loop([]) end).
>
> rpc(Pid, Request) ->
>      Pid ! {self(), Request},
>      receive
>         {Pid, Response} ->
>             Response
>      end.
>
> loop(X) ->
>      receive
>         Any ->
>             io:format("Received:~p~n",[Any]),
>             loop(X)
>      end.
>
> %% --------------- end second example
>
> Notice that the second example is very similar to the first. It's
> purpose is to provide some scaffolding into which the user can
> interactively insert new messages and message handling code.  When I
> try to use it, my session hangs. Here's the terminal session,
> starting with a new erl session.
>
> %% --------------- begin second example behavior
>
> Eshell V5.5.4  (abort with ^G)
> 1> c(ctemplate).
> {ok,ctemplate}
>
> 2> Pid = ctemplate:start().
> <0.38.0>
>
> 3> ctemplate:rpc(Pid, {foo}).
> Received:{<0.31.0>,{foo}}
>
> %% --------------- end second example behavior
>
> The erl session hangs at that point. I suspect user error, but have
> no clue what the error might be. I'm brand new to Erlang, and would
> much appreciate any insight on what's going wrong here. Thank you.
>
> _______________________________________________
> erlang-questions mailing list
> 
> http://www.erlang.org/mailman/listinfo/erlang-questions
>



More information about the erlang-questions mailing list