"raw" or "verbatim" PIDs?

Richard Carlsson richardc@REDACTED
Fri Jul 7 10:57:55 CEST 2006


Jon Slenk wrote:
> I'm very new to Erlang, I'm just working my way through a tutorial. I 
> spawned a process and was told that it has PID <0.47.0>, but I failed to 
> assign that to a variable. Now, is there any way I can send a message to 
> that PID by using the "raw" ID? I tried writing stuff like "<0.47.0> ! 
> foobar." or "0.47.0 ! foobar." in an attempt to mimic the code I've seen 
> where people write something like "ThePid = spawn(...). ThePid ! 
> foobar." However, the Erlang shell did not like my attempts :-) Is there 
> a way to use "raw" PIDs, or must it be stored in a variable?

You can, but it's probably best not to make it a habit. Try to learn
how to work with variables in the shell instead - it pays off.

First, note that if you do something like this:

	1> ThePid = spawn(...).
	<0.47.0>
         2> ThePid ! hello.
         ...
         7> ThePid = spawn(...).

then the last "assignment" will fail, since ThePid is already bound
to another pid, so the "=" match will fail. You can use the shell
function f() to forget all previous variable bindings, or f(ThePid)
to forget a particular binding.

A handy thing is the v(N) shell function. If you e.g. spawn a process
but forget to bind the pid to something, you can still access the value
by referring to the number of the command. For example:

	17> spawn(...).
	<0.99.0>
	...
	23> MyPid = v(17).
	<0.99.0>
	24> MyPid ! hello.

(only the last N results are saved, though - about 20 or so).

	/Richard




More information about the erlang-questions mailing list