[erlang-questions] pattern matching registered name
James Aimonetti
james@REDACTED
Mon May 20 19:47:30 CEST 2013
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
On 05/20/2013 10:12 AM, Edmund Sumbar wrote:
> Hello,
>
> I'm new to erlang and am perplexed by the use of registered names
> when they are passed as function parameters and subsequently used
> in pattern matching.
>
> For example (taken from Programming Erlang 2/ed by Joe Armstrong),
>
> -module(kvs). -export([start/0, rpc/1]).
>
> start() -> register(kvs, spawn(fun() -> loop() end)).
>
> rpc(Q) -> kvs ! {self(), Q}, receive {kvs, Reply} -> Reply end.
>
> loop() -> receive {From, {store, Key, Value}} -> put(Key, {ok,
> Value}), From ! {kvs, true}, loop(); {From, {lookup, Key}} -> From
> ! {kvs, get(Key)}, loop() end.
>
> This module works as expected.
>
> $ erl Erlang R16B (erts-5.10.1) [source] [64-bit] [smp:8:8]
> [async-threads:10] [hipe] [kernel-poll:false] [dtrace]
>
> Eshell V5.10.1 (abort with ^G) 1> c(kvs). {ok,kvs} 2>
> kvs:start(). true 3> kvs:rpc({store, name, "Joe"}). true 4>
> kvs:rpc({lookup, name}). {ok,"Joe"}
>
> Then, I modify the code to remove the registered name.
>
> -module(kvsx). -export([start/0, rpc/2]).
>
> start() -> spawn(fun() -> loop() end).
>
> rpc(Pid, Q) -> Pid ! {self(), Q}, receive {Pid, Reply} -> Reply
> end.
>
> loop() -> receive {From, {store, Key, Value}} -> put(Key, {ok,
> Value}), From ! {self(), true}, loop(); {From, {lookup, Key}} ->
> From ! {self(), get(Key)}, loop() end.
>
> The modified module also works as expected.
>
> $ erl Erlang R16B (erts-5.10.1) [source] [64-bit] [smp:8:8]
> [async-threads:10] [hipe] [kernel-poll:false] [dtrace]
>
> Eshell V5.10.1 (abort with ^G) 1> c(kvsx). {ok,kvsx} 2> Pid =
> kvsx:start(). <0.40.0> 3> kvsx:rpc(Pid, {store, name, "Joe"}).
> true 4> kvsx:rpc(Pid, {lookup, name}). {ok,"Joe"}
>
> However, when I register a name for Pid and pass it to the
> function, the process hangs.
>
> 5> register(abc, Pid). true 6> whereis(abc). <0.40.0> 7>
> kvsx:rpc(abc, {lookup, name}). ^G User switch command --> q
>
> As a newcomer to the language (but with decades of programming
> experience), I'm confused. Can someone explain registered names and
> when/where they can be used.
>
> Ed
>
>
>
> _______________________________________________ erlang-questions
> mailing list erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
>
rpc/2 expects the first arg into the function to be the first element
in the response tuple.
The loop/0 uses self(), which always returns a pid(), not the
registered name.
So you call rpc with an atom, and receive a tuple with a pid(). Those
don't match.
Your call to rpc/2 hangs because the selective receive in rpc/2 never
matches (and you have no timeout clause).
- --
James Aimonetti
Distributed Systems Engineer / DJ MC_
2600hz | http://2600hz.com
sip:james@REDACTED
tel: 415.886.7905
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/
iQEcBAEBAgAGBQJRmmGyAAoJENc77s1OYoGgjYkH/jT0bUtnghdmbNo8w0wQnqhL
sRSVuh9wKRLTuPf4x/fyCBHBEC7g6Ii+i9Xdnyc7EnaCfI7vEAx8IRmNJiMnh/Rx
9P1P+48hQmkTjRqj1aBfPR5O0EqezGB/72oPxFK5lN8hLVg0+FOjvRjF2FVezQEt
9gv5kbu7ORla9NJK+9CzGj8pUlx0CQ5ba7pXIU0ib00hf6DA3g8rJmpbS1Fe7+6i
5eYJLvh0/m6F8YjGtrdYIEfCfey7assHMgOIZ+iC2QjcbnqWPZHCbuhsPUz0LbMu
BQOOn3/8NAbstOOMzW7LHjCJ80yLJX9HMHn+bvORTaFVIDy1kUxhDJSUDDP7mSs=
=iAES
-----END PGP SIGNATURE-----
More information about the erlang-questions
mailing list