[erlang-questions] Request for help on creating BIF (or finding existing one)
Ulf Wiger
ulf.wiger@REDACTED
Wed Mar 11 00:15:57 CET 2009
=Bill.Barnhill wrote:
>
> I was recently asked how to have a process get its registered name.
> AFAIK there is no BIF for this. It can be accomplished via:
> [ N || N <- global:registered_names(), global:whereis_name(N) == self()]
> which will either return [] if it's not registered or [Name] if it is.
Here's a way to do it without BIFs, and with some peeking in
global's innards:
names(Pid) when is_pid(Pid) ->
Global = [{global,G} ||
{_, G} <- ets:lookup(global_pid_names, Pid)],
case process_info(Pid, registered_name) of
{registered_name, N} ->
[{local, N}|Global];
[] ->
Global
end.
BR,
Ulf W
--
Ulf Wiger
CTO, Erlang Training & Consulting Ltd
http://www.erlang-consulting.com
More information about the erlang-questions
mailing list