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:
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.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 oddity is that an unblessed reference will wind up blessed if it is the argument of refaddr. Here's a demo:
The normally-loaded XS version of the function does not exhibit that behavior.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)
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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |