Saturday February 28, 2004
09:50 AM
RFC - Test::Reference
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, <steve@XXXXXXXXXXX.XXX>
COPYRIGHT AND LICENSE
Copyright (C) 2004 by Steve Peters
This library is free software; you can redistribute it and/or modify it
under the same terms as Perl itself, either Perl version 5.8.3 or, at
your option, any later version of Perl 5 you may have available.
Stringified references don't work? (Score:2)
As for names, I would go for Test::Ref, since the Perl operator is ref().
Re:Stringified references don't work? (Score:1)
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);
A whole module for ... ? (Score:2)
Re:A whole module for ... ? (Score:1)
What problem were you solving? (Score:2)
--
xoa
Re:What problem were you solving? (Score:1)
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::BuilderandTest::Builder::Testerwork, so I wou