Tim McDaniel
Usenet Archive
- Joined
- Sep 12, 2008
- Messages
- 362
- Reaction score
- 0
I inherited code that had, in effect,
my $kind = 'Val';
...
if ($kind eq 'Val')
For various reasons, I want to change it to
my $kind = \&some_sub;
Is there a reliable, guaranteed way to do that and still have the conditional? (This is in 5.8.8 and I have no way to change that.)
man perlref says
Using a string or number as a reference produces a symbolic reference, as explained above. Using a reference as a number produces an integer representing its storage location in memory. The only useful thing to be done with this is to compare two references numerically to see whether they refer to the same location.
if ($ref1 == $ref2) { # cheap numeric compare of references print "refs 1 and 2 refer to the same thing\n"; }
I also ran across
http://stackoverflow.com/questions/4064001/how-should-i-compare-perl-references , where there was one reply that said
The function you are looking for is refaddr from Scalar::Util (after ensuring that the values being compared really are references):
use Scalar::Util 'refaddr';
if ($obj1 and ref($obj1) and $obj2 and ref($obj2) and
refaddr($obj1) == refaddr($obj2))
{
# objects are the same...
}
with tchrist replying "The extraordinary measures taken by cpan/List-Util/lib/Scalar/Util/PP.pm's refaddr function to divine the referent's real address are exceeded only by the blessed function's measures to find the package name.", and a reply to that that it's in the Perl core and compiled, so it's cheap.
Is there a reason to use SCalar::Util::refaddr instead of ==?
my $kind = 'Val';
...
if ($kind eq 'Val')
For various reasons, I want to change it to
my $kind = \&some_sub;
Is there a reliable, guaranteed way to do that and still have the conditional? (This is in 5.8.8 and I have no way to change that.)
man perlref says
Using a string or number as a reference produces a symbolic reference, as explained above. Using a reference as a number produces an integer representing its storage location in memory. The only useful thing to be done with this is to compare two references numerically to see whether they refer to the same location.
if ($ref1 == $ref2) { # cheap numeric compare of references print "refs 1 and 2 refer to the same thing\n"; }
I also ran across
http://stackoverflow.com/questions/4064001/how-should-i-compare-perl-references , where there was one reply that said
The function you are looking for is refaddr from Scalar::Util (after ensuring that the values being compared really are references):
use Scalar::Util 'refaddr';
if ($obj1 and ref($obj1) and $obj2 and ref($obj2) and
refaddr($obj1) == refaddr($obj2))
{
# objects are the same...
}
with tchrist replying "The extraordinary measures taken by cpan/List-Util/lib/Scalar/Util/PP.pm's refaddr function to divine the referent's real address are exceeded only by the blessed function's measures to find the package name.", and a reply to that that it's in the Perl core and compiled, so it's cheap.
Is there a reason to use SCalar::Util::refaddr instead of ==?