[erlang-questions] Introspecting the atom table

Kostis Sagonas kostis@REDACTED
Wed Dec 20 23:33:58 CET 2006


Michael McDaniel wrote:
> On Wed, Dec 20, 2006 at 11:07:48PM +0100, Kostis Sagonas wrote:
>> Eric Merritt wrote:
>>> Guys,
>>>
>>>  Is there anyway to introspect the atom table? I would like to be able
>>> to take a string and check if that string has been interned in the
>>> atom table. Even better would be a bif that converts the string to an
>>> atom if that atom already exists and doesn't otherwise. I suspect that
>>> this type of functionality doesn't exist, but I thought I would ask
>>> and make sure.
>> Handle with care:
>>
>>
>> Eshell V5.5.2.1  (abort with ^G)
>> 1> list_to_existing_atom("erlang").
>> erlang
>> 2> list_to_existing_atom("gazonk").
>>
>> =ERROR REPORT==== 20-Dec-2006::23:06:58 ===
>> Error in process <0.29.0> with exit value: 
>> {badarg,[{erlang,list_to_existing_atom,["gazonk"]},{erl_eval,do_apply,5},{shell,exprs,6},{shell,eval_loop,3}]}
>>
>> ** exited: {badarg,[{erlang,list_to_existing_atom,["gazonk"]},
>>                      {erl_eval,do_apply,5},
>>                      {shell,exprs,6},
>>                      {shell,eval_loop,3}]} **
>>
>> Kostis
> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> 
>   and here is how I might use it where RegName could be a
>   new server or an existing one already known.  RegName, 
>   for example, may be passed in to a fun which executes the
>   following ...
> 
> 
>   net_adm:ping(
> 
>     try list_to_existing_atom(RegName)
>     catch Class:Error ->
>         list_to_atom(RegName)
>     end
> 
>   )

Admittedly I am very tired, but why would you write the code above
rather than simply write:

   net_adm:ping( list_to_atom(RegName) )

?

Kostis



More information about the erlang-questions mailing list