RFC - Test::Reference

speters on 2004-02-28T14:50:33

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, 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?

brian_d_foy on 2004-02-28T15:19:06

I am curious how you ended up using XS---I figure there must be a good story about that. Did comparing them as strings not work for some reason?
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);

A whole module for ... ?

merlyn on 2004-02-28T15:39:55

ref $a and ref $b and $a == $b
That seems a bit heavy.

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

What problem were you solving?

petdance on 2004-02-29T01:09:40

What's the problem that you were solving? I'm thinking that maybe this reference matching is really a different problem.

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 of is((ref $a && ref $b && $a == $b), 1) isn't pretty, but it works just fine. Also, I got a good look at how Test::Builder and Test::Builder::Tester work, so I wouldn't say my little exercise was a total waste of time. A cleaned up version of the is above might be nice, but I doubt if there are people clammoring for a new module to come out of that.