Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Comparing a reference?

Reply
Thread Tools

Comparing a reference?

 
 
Tim McDaniel
Guest
Posts: n/a
 
      01-30-2013
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/4...erl-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 ==?

--
Tim McDaniel, http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
 
Tim McDaniel
Guest
Posts: n/a
 
      01-30-2013
In article <(E-Mail Removed)>,
Ben Morrow <(E-Mail Removed)> wrote:
>
>Quoth (E-Mail Removed):
>> Is there a reason to use SCalar::Util::refaddr instead of ==?

>
>The only reason is if there is a chance either ref might point to an
>object which overloads either == or numify, or if there is a chance
>$kind might not hold a ref at all. If you know you are dealing with
>unblessed refs there is no reason not to use ==.
>
>If there is a chance you might be running under ithreads, it's important
>to check against the current value of \&some_sub, rather than trying to
>cache the numeric value, since the value of the ref will change when a
>new thread is forked.


Thank you for the quick response.

$kind is one reference out of four possible refs, along the lines of

my $lookup = {
CASE1 => \&Pkg::Sub1,
CASE2 => \&Pkg::Sub2,
CASE3 => \&Pkg::Sub3,
CASE4 => \&Pkg::Sub4,
};
$kind = $lookup->{$arg};
return unless $kind;

There is no threading, no blessing, and no object munging in the 40
lines between setting and this comparison.

Reading in further Google hits, though, I think I may do refaddr just
to be paranoid.

--
Tim McDaniel, (E-Mail Removed)

 
Reply With Quote
 
 
 
 
Tim McDaniel
Guest
Posts: n/a
 
      01-31-2013
In article <keca6v$d31$(E-Mail Removed)>,
Tim McDaniel <(E-Mail Removed)> wrote:
>$kind is one reference out of four possible refs, along the lines of
>
> my $lookup = {
> CASE1 => \&Pkg::Sub1,
> CASE2 => \&Pkg::Sub2,
> CASE3 => \&Pkg::Sub3,
> CASE4 => \&Pkg::Sub4,
> };
> $kind = $lookup->{$arg};
> return unless $kind;
>
>There is no threading, no blessing, and no object munging in the 40
>lines between setting and this comparison.
>
>Reading in further Google hits, though, I think I may do refaddr just
>to be paranoid.


I just realized that $arg is also in scope at the point of the
comparison. So instead of doing

if (Scalar::Util::refaddr($kind) == Scalar::Util::refaddr(\&Pkg::Sub1)) {

I can simply do

if ($arg eq 'CASE1') {

and finesse away the whole issue.

Still, thank you for the answer.

--
Tim McDaniel, (E-Mail Removed)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
comparing the array in parallel srinukasam VHDL 3 07-30-2005 12:27 PM
comparing the array for generic parameters srinukasam VHDL 3 06-30-2005 07:40 PM
comparing the contents of memory srinukasam VHDL 5 06-23-2005 07:49 PM
[VHDL] Comparing entity and component declarations M.D. van de Burgwal VHDL 3 10-07-2004 08:58 AM
Comparing values in 2 textfiles and returning the missing values Jorgen Gustafsson Perl 4 12-12-2003 09:09 AM



Advertisments