Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

How to recover a reference from a stringified reference?

( #153321=categorized question: print w/ replies, xml ) Need Help??
Contributed by Zombie NoamT on Mar 21, 2002 at 14:16 UTC
Q&A  > references


Description:

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?

Answer: How to recover a reference from a stringified reference?
contributed by knobunc

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.

Answer: How to recover a reference from a stringified reference?
contributed by Fletch

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.

Please (register and) log in if you wish to add an answer



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others scrutinizing the Monastery: (8)
    As of 2014-11-24 08:26 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My preferred Perl binaries come from:














      Results (137 votes), past polls