Slash Boxes
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

speters (1787)

  (email not shown publicly)
AOL IM: fisharerojo (Add Buddy, Send Message)


Journal of speters (1787)

Saturday February 28, 2004
09:50 AM

RFC - Test::Reference

[ #17672 ]
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

       Test::Reference - Check to see if two references refer to the same

           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");

       This modules allows you to test to see if two references refer to the
       same variable or not.

       "references_same( $ref1, $ref2, $mesg)"

       Checks to see that the two references are references and that they both
       refer to the same underlying variable.

       Extending and Embedding Perl - my main reference for learning XS

       Devel::Peek - my other main reference for learing XS

       Steve Peters, <steve@XXXXXXXXXXX.XXX>

       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.
The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
More | Login | Reply
Loading... please wait.
  • 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().

    • 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);
  • ref $a and ref $b and $a == $b
    That seems a bit heavy.
    • Randal L. Schwartz
    • Stonehenge
  • What's the problem that you were solving? I'm thinking that maybe this reference matching is really a different problem.


    • 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 wou