<div dir="ltr"><div>When you call rpc(abc, blah), Pid = abc. In your rpc(...) code you are waiting for <br>{abc, Resp} in receive loop.<br></div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Mon, May 20, 2013 at 10:12 AM, Edmund Sumbar <span dir="ltr"><<a href="mailto:esumbar@gmail.com" target="_blank">esumbar@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Hello,<br><br>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.<br>
<br>For example (taken from Programming Erlang 2/ed by Joe Armstrong),<br>
<br><font face="courier new, monospace">-module(kvs).<br>-export([start/0, rpc/1]).<br><br>start() -> register(kvs, spawn(fun() -> loop() end)).<br><br>rpc(Q) -><br> kvs ! {self(), Q},<br> receive<br> {kvs, Reply} -><br>
Reply<br> end.<br><br>loop() -><br> receive<br> {From, {store, Key, Value}} -><br> put(Key, {ok, Value}),<br> From ! {kvs, true},<br> loop();<br> {From, {lookup, Key}} -><br> From ! {kvs, get(Key)},<br>
loop()<br> end.<br></font><br>This module works as expected.<br><br><font face="courier new, monospace">$ erl<br>Erlang R16B (erts-5.10.1) [source] [64-bit] [smp:8:8] [async-threads:10] [hipe] [kernel-poll:false] [dtrace]<br>
<br>Eshell V5.10.1 (abort with ^G)<br>1> c(kvs).<br>{ok,kvs}<br>2> kvs:start().<br>true<br>3> kvs:rpc({store, name, "Joe"}).<br>true<br>4> kvs:rpc({lookup, name}).<br>{ok,"Joe"}<br></font><br>
Then, I modify the code to remove the registered name.<br><br><font face="courier new, monospace">-module(kvsx).<br>-export([start/0, rpc/2]).<br><br>start() -> spawn(fun() -> loop() end).<br><br>rpc(Pid, Q) -><br>
Pid ! {self(), Q},<br> receive<br> {Pid, Reply} -><br> Reply<br> end.<br><br>loop() -><br> receive<br> {From, {store, Key, Value}} -><br> put(Key, {ok, Value}),<br> From ! {self(), true},<br>
loop();<br> {From, {lookup, Key}} -><br> From ! {self(), get(Key)},<br> loop()<br> end.<br></font><br>The modified module also works as expected.<div><br><font face="courier new, monospace">$ erl<br>
Erlang R16B (erts-5.10.1) [source] [64-bit] [smp:8:8] [async-threads:10] [hipe] [kernel-poll:false] [dtrace]<br><br>Eshell V5.10.1 (abort with ^G)<br>1> c(kvsx).<br>{ok,kvsx}<br>2> Pid = kvsx:start().<br><0.40.0><br>
3> kvsx:rpc(Pid, {store, name, "Joe"}).<br>true<br>4> kvsx:rpc(Pid, {lookup, name}).<br>{ok,"Joe"}<br></font><br>However, when I register a name for Pid and pass it to the function, the process hangs.<div>
<br><font face="courier new, monospace">5> register(abc, Pid).<br>true<br>6> whereis(abc).<br><0.40.0><br>7> kvsx:rpc(abc, {lookup, name}).<br>^G<br>User switch command<br> --> q<br></font></div></div><div>
<br></div><div>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.</div><div><br></div><div>
Ed</div></div>
<br>_______________________________________________<br>
erlang-questions mailing list<br>
<a href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a><br>
<a href="http://erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://erlang.org/mailman/listinfo/erlang-questions</a><br>
<br></blockquote></div><br></div>