Zaxo has asked for the wisdom of the Perl Monks concerning the following question:
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: A Scalar::Util::refaddr Oddity
by ambrus (Abbot) on Sep 25, 2005 at 20:24 UTC | |
by Zaxo (Archbishop) on Sep 25, 2005 at 20:31 UTC | |
|
Re: A Scalar::Util::refaddr Oddity
by nothingmuch (Priest) on Sep 25, 2005 at 16:52 UTC | |
by xdg (Monsignor) on Sep 26, 2005 at 11:16 UTC | |
by adrianh (Chancellor) on Sep 26, 2005 at 11:49 UTC | |
by xdg (Monsignor) on Sep 26, 2005 at 13:54 UTC | |
by adrianh (Chancellor) on Sep 26, 2005 at 16:04 UTC | |
| |
by hv (Prior) on Sep 27, 2005 at 16:23 UTC | |
by BrowserUk (Patriarch) on Sep 26, 2005 at 12:39 UTC | |
by adrianh (Chancellor) on Sep 26, 2005 at 12:49 UTC | |
by ambrus (Abbot) on Sep 26, 2005 at 17:00 UTC | |
by adrianh (Chancellor) on Sep 27, 2005 at 13:08 UTC | |
| |
by xdg (Monsignor) on Sep 27, 2005 at 15:06 UTC |