perchance has asked for the wisdom of the Perl Monks concerning the following question:

I have come across a need to compare an object value inside a function.
Unfortunately, the object gets interpolated. I can do the comparison as a string, e.g.:

if ($record eq "oRecord=HASH(0x1475d44)")

but I was wondering if there is a way to "resurrect" the
object so that I can ask:

if  ($record == ??)

Would this kind of behaviour be frowned upon? Any good reason I shouldn't do it?

thanks,
me

--- Strange Currencies

Replies are listed 'Best First'.
Re: resurrecting objects?
by davorg (Chancellor) on May 03, 2001 at 12:12 UTC

    Well, only you can know what it means for two of your objects to be "equal", so the best way would be for you to implement a compare method within the object that would compare the attributes of the object with a different object. You could then do something like this:

    if ($obj1->compare($obj2)) { # ... }

    Another nice touch would be to use overload to redefine the == or eq operators like this:

    use overload '==' => \&compare;

    You can then write code like this:

    if ($obj1 == $obj2) { # ... }

    But it's still your compare method that is actually being called.

    --
    <http://www.dave.org.uk>

    "Perl makes the fun jobs fun
    and the boring jobs bearable" - me

      I understand and we may implement it as proposed (have never overloaded
      in Perl before :-), but what I wanted was to get the reference back from the
      string that describes it, I know I may be missing something.
      Is it possible to backtrack in this way, or is the conversion conducted
      by the interpolation irreversible?

      Thanks,
      me

      ---Strange Currencies

        OK. I think I understand now. Your problem is that somewhere along the line you have "stringified" your object and turned the reference into a string. There's no way (that I know of) to undo this, so you'll need to find out where the stringification takes place and fix that piece of code. You're looking for somewhere where the reference is quoted unnecessarily.

        --
        <http://www.dave.org.uk>

        "Perl makes the fun jobs fun
        and the boring jobs bearable" - me

        Would I be correct in guessing that your stringified reference comes out of a hash key? If so, you may want to have a look at Tie::RefHash. Otherwise, just create a hash table mapping pointer strings back to their original objects, as tye suggests.

        If, however, by resurrecting, you mean reclaiming an object that was deleted by garbage collection (ie. refcount went to 0), this would be a very dangerous and unwise thing do, for reasons that are fairly obvious.

           MeowChow                                   
                       s aamecha.s a..a\u$&owag.print
(tye)Re: resurrecting objects?
by tye (Sage) on May 03, 2001 at 18:13 UTC

    This works:

    my %grave; sub bury { my( $object )= @_; $grave{$obj}= $object; } sub resurrect { my( $string )= @_; return delete $grave{$string}; }
    Note that resurrect deletes the entry so if you want to resurrect an object again, you have to bury it again first.

    Now, if you want to resurrect an object that you haven't cached beforehand, then, no, that isn't supported. For one thing, when you turn your object into a string, the string doesn't hold a reference to the object so the object will be destroyed if there isn't some reference to it elsewhere.

            - tye (but my friends call me "Tye")