in reply to Techniques On Saving Memory

Voila! This was written in about 30 minutes (well, 5 minutes of re-reading the XS docs, 10 minutes of actual coding time, and 15 minutes of writing docs and tests and what-not). (Update: another 10 minutes produced version 0.02, which makes one function do the job of two.) Limbic~Region was asking about such a module on the CB, and when I described what I thought such a module should do, I decided to try writing it.

Devel::RecoverRef is a module which takes a stringified or numified version of a reference (objects included!) and returns the reference (or object!) that is represented therein. There's not a lot of protection, though. You'll probably dump core if you make stuff up. Scrutinize, and I'll upload it to CPAN by the end of the week.

_____________________________________________________
Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart

Replies are listed 'Best First'.
Re^2: Techniques On Saving Memory
by diotalevi (Canon) on Mar 09, 2005 at 23:41 UTC
    Is there some reason you didn't just write this as a wrapper over Devel::Pointer or Devel::Pointer::PP? They already handle the unsmashing part - you just add the part to get the hex address out of strings like "Some::Class=HASH(0x81471c8)". Or why didn't you just ask either Simon or me to update our modules? Its probably a feature that should already be in there.
      I hadn't known of those modules until after I wrote mine. They were mentioned in other nodes in this thread. Now that I know they exist, there's no need for me to release mine; you and Simon just need to simplify your modules.
      _____________________________________________________
      Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
      How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart

        Actually, I like yours best. A few other things that would improve it (for my envisage purposes) are:

      • If it also returned the package name into which the reference was blessed, if it was blessed, in a list context.
      • If it had a deref() function that (optionally?) incremented the ref count.
      • If it provided a safe method of checking the unsmashed reference was still valid.
      • Had a function that would detect and return the type of the unsmashed reference--SCALAR/ARRAY/HASH etc, but also (if it is possible?), the the type of magic to which it is bound. Ie. Classname, or REGEX (I forgot how these get created?) etc.

        Examine what is said, not who speaks.
        Silence betokens consent.
        Love the truth but pardon error.