Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: A Scalar::Util::refaddr Oddity

by nothingmuch (Priest)
on Sep 25, 2005 at 16:52 UTC ( [id://494925]=note: print w/replies, xml ) Need Help??


in reply to A Scalar::Util::refaddr Oddity

I think it's worth fixing even if just to make sure accidental users don't get surprised.

Most people don't expect to use the pure perl Scalar::Util, so if by accident they do, they should get completely equivalent behavior (even if it's 100% compatible it might not be equivalent), so appearance also matters, IMHO.

-nuffin
zz zZ Z Z #!perl

Replies are listed 'Best First'.
Re^2: A Scalar::Util::refaddr Oddity
by xdg (Monsignor) on Sep 26, 2005 at 11:16 UTC

    I agree that the behaviors should be identical, though I don't understand why the pure-perl version uses that code versus the 0 + $object idiom.

    use strict; use warnings; use Scalar::Util qw( refaddr ); sub refaddr_pp { return if not ref $_[0]; return 0 + $_[0]; } my $obj = bless {}, 'Foo'; my $ref = {}; print 'refaddr $obj: ', refaddr($obj), "\n"; print 'refaddr_pp $obj: ', refaddr_pp($obj), "\n\n"; print 'refaddr $ref: ', refaddr($ref), "\n"; print 'refaddr_pp $ref: ', refaddr_pp($ref), "\n";

    Prints

    refaddr $obj: 149445664 refaddr_pp $obj: 149445664 refaddr $ref: 149445904 refaddr $ref: 149445904

    -xdg

    Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

      I don't understand why the pure-perl version uses that code versus the 0 + $object idiom.

      Because that idiom fails if $object overloads addition to do something else.

        Because that idiom fails if $object overloads addition to do something else.

        Doh! (Can you tell I don't mess with overloading much? Wasn't even on my radar screen.) OK, then, quick perusal of overload reveals the StrVal function, which can get us back to the original "grab the right substring and convert" approach without the reblessing.

        use strict; use warnings; use Scalar::Util qw( refaddr ); package Foo; use overload '0+' => sub { 0 }, q{""} => sub { "Some Object" }, fallback => 1; package main; use overload; sub refaddr_pp { return if not ref $_[0]; overload::StrVal($_[0]) =~ /0x(\w+)/; return hex $1; } my $obj = bless {}, 'Foo'; my $ref = {}; print 'stringify $obj: ', "$obj", "\n"; print 'refaddr $obj: ', refaddr($obj), "\n"; print 'refaddr_pp $obj: ', refaddr_pp($obj), "\n\n"; print 'stringify $ref: ', "$ref", "\n"; print 'refaddr $ref: ', refaddr($ref), "\n"; print 'refaddr_pp $ref: ', refaddr_pp($ref), "\n\n";

        Prints

        stringify $obj: Some Object refaddr $obj: 155892768 refaddr_pp $obj: 155892768 stringify $ref: HASH(0x94abd10) refaddr $ref: 155893008 refaddr_pp $ref: 155893008

        -xdg

        Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

        Wouldn't blessing a copy of the reference in order to extract the address work, and allow the blessed copy to be discarded leaving the original unmodified?


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
        "Science is about questioning the status quo. Questioning authority".
        The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.

        Anyway, after reblessing the object, wouldn't it be better to take the reference in numeric context than to extract the number from the string form?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://494925]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (10)
As of 2024-04-18 09:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found