http://www.perlmonks.org?node_id=153321

NoamT has asked for the wisdom of the Perl Monks concerning the following question: (references)

I have a string which is the stringification of an object reference, e.g.

"MYOBJECT(0x3a89fc)"
How can I recover from this an actual reference, so that (e.g.) I'll be able to call the object's methods?

Originally posted as a Categorized Question.

Replies are listed 'Best First'.
Re: How to recover a reference from a stringified reference?
by knobunc (Pilgrim) on Mar 21, 2002 at 15:11 UTC

    The problem with doing this is that it bypasses perl's reference-counting memory management scheme. If you could ref-ify a stringified ref, you'd be getting a new reference to something without incrementing that thing's reference count. That means you could still have "live" references to an object which perl has already deallocated due to its reference count reaching zero.

    Now that you know all that, if you really still want to reconstitute a reference from a string (and have no fear of the potentially nasty consequences) try the Devel::Pointer module.

    See this post by Ovid for more information on references.

Re: How to recover a reference from a stringified reference?
by Fletch (Bishop) on Mar 22, 2002 at 03:04 UTC

    One additional point: If your refs are getting stringified because you're using them as keys to a hash, you might want to look into Tie::RefHash, which will avoid that problem.