[erlang-questions] Introspecting the atom table

Michael McDaniel erlangx@REDACTED
Wed Dec 20 23:42:33 CET 2006


On Wed, Dec 20, 2006 at 11:33:58PM +0100, Kostis Sagonas wrote:
> 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
______________________________________________________________________

 My understanding is that "an atom is forever".  In long running
 code I don't want to keep creating new atoms if I already have
 one.  The above I use where I have sporadic communication between
 remote servers that could come online or offline.

 This was one example where I might not want to keep creating new
 atoms so use the try/catch idiom.

~M



More information about the erlang-questions mailing list