Either the Perl-QA mailing list is down, or it just doesn't like where I'm sending my mail from, because my emails just don't seem to the making the list. Well, not seeing an obvious Test:: module that would work for me to make sure
that two references referred to the same thing, I wrote my own module.
Before sending it off to CPAN, I'd like to see what you all think of it.
Also, I have a couple of questions. First, the module includes some XS,
which is unusual for Test:: modules. Do you think I should split my code
into two modules (say Devel::References::Same and Test::References) or keep
it the way it is. Second, I'm not particularly in love with the names
Test::Reference with the function "references_same". I was thinking of
Test::References::Same as a possible name for the module, but then I at a
loss of what to call the function. references_same_ok? I'd appreciate your
thoughts.
NAME
Test::Reference - Check to see if two references refer to the same
variable
SYNOPSIS
use Test::Reference;
my $val1 = 5;
my $val2 = 5;
my $a = \$val1;
my $b = \$val1;
my $c = \$val2;
references_same($a, $b, "Will return ok");
references_same($a, $c, "Will return not ok");
DESCRIPTION
This modules allows you to test to see if two references refer to the
same variable or not.
FUNCTIONS
"references_same( $ref1, $ref2, $mesg)"
Checks to see that the two references are references and that they both
refer to the same underlying variable.
SEE ALSO
Extending and Embedding Perl - my main reference for learning XS
Devel::Peek - my other main reference for learing XS
AUTHOR
Steve Peters,
use Test::More tests => 3;
my $a = 5;
my $b = \$a;
my $c = \$a;
my $d = \$b;
my $e = [];
is( $b, $c, "Same scalar data" );
isnt( $b, $d, "Not the same" );
isnt( $c, $e, "Not the same" );
As for names, I would go for Test::Ref, since the Perl operator is ref().
Re:Stringified references don't work?
speters on 2004-02-28T16:10:33
It does OK with scalars, but some of the other reference types, no.
For example,
use warnings;
use strict;
use Test::More tests => 2;
my $val1 = qr/foo/;
my $val2 = qr/foo/;
$a = $val1;
$b = $val1;
$c = $val2;
diag ref $a;
diag ref $b;
diag ref $c;
is($a, $b);
isnt($a, $c);
That seems a bit heavy.ref $a and ref $b and $a == $b
Re:A whole module for ... ?
speters on 2004-02-28T17:06:58
Well, if you want to do it the easy way...yeah, this works.:)
Thanks
Re:What problem were you solving?
speters on 2004-03-01T15:18:33
I am testing to ensure that an object returned by a function is actually the same object received by a previous call to this function. In OO terms, I testing to make sure a class is a Singleton.
I tried the XS route, until Randal's suggestion above, because I needed the address of what the
RV*
's were pointing to. The new route ofis((ref $a && ref $b && $a == $b), 1)
isn't pretty, but it works just fine. Also, I got a good look at howTest::Builder
andTest::Builder::Tester
work, so I wouldn't say my little exercise was a total waste of time. A cleaned up version of theis
above might be nice, but I doubt if there are people clammoring for a new module to come out of that.