During a hot cb discussion of Scalar::Util::refaddr, I noticed that the pure perl version of the function will alter an unblessed reference passed to it. The sub goes as follows:

sub refaddr($) { my $pkg = ref($_[0]) or return undef; bless $_[0], 'Scalar::Util::Fake'; "$_[0]" =~ /0x(\w+)/; my $i = do { local $^W; hex $1 }; bless $_[0], $pkg; $i; }
The reblessing to a fake namespace is to eliminate problems from overloading in the target's class, the issue that first made me look at the source.

The oddity is that an unblessed reference will wind up blessed if it is the argument of refaddr. Here's a demo:

use DynaLoader; # disable XS loading sub DynaLoader::bootstrap {1} # thanks, demerphq use Scalar::Util qw(refaddr); my $foo = {}; print $foo, $/; refaddr $foo; print $foo, $/; __END__ HASH(0x804b3f8) HASH=HASH(0x804b3f8)
The normally-loaded XS version of the function does not exhibit that behavior.

Pure perl refaddr can be fixed to act like the XS version by making the bless lines conditional on a stored value of Scalar::Util::blessed($_[0]).

My question: Is it worth fixing? Are there any odd side effects of that blessing? Are any significant properties changed?

After Compline,
Zaxo


In reply to A Scalar::Util::refaddr Oddity by Zaxo

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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.