Hi,

I was going to post this on perl-qa mailing list, but got sick of waiting
for the "subscription" procedure to do its stuff - so I'll post here for a
start - mainly because the T::M author (Schwern) is known to occasionally
lurk here. There are also others here who might be interested in this, and
be able to provide some useful input - and if there's somewhere other than
perl-qa that this should be posted, please let me know.

I'm not sure that my simple demo is up to scratch, either. If it needs some
refining I'll try to oblige.

Anyway ... it's perl 5.8.7, Test::More-0.54, and the same behaviour on both
Win32 (Windows 2000) and linux (Mandrake-9.1).

--- Foo.pm ---
package Foo;

#use overload
#'eq'    => \&overload_equiv,
# '=='   => \&overload_equiv;

sub new {
    my ($class, $foo) = @_;
    my $self = {foo => $foo};
    bless $self, $class;
    return $self;
}

sub overload_equiv {
    if (ref($_[0]) ne 'Foo' || ref($_[1]) ne 'Foo') {
       print ref($_[0]),  " ", ref($_[1]), "\n";
       die "Invalid object passed to overload_equiv\n";
       }
    print "equivalence overloaded\n";
    return 1; # change to 0 ... makes little difference
}

1;
__END__
--- end Foo.pm ---

#################

--- footest.pl ---
use warnings;
use Foo;

use Test::More;
plan tests => 1;

$obj1 = Foo->new();
$obj2 = Foo->new();
$obj3 = Foo->new();
$obj4 = Foo->new();

@x = ($obj1, $obj2);
@y = ($obj3, $obj4);

ok(eq_array([EMAIL PROTECTED], [EMAIL PROTECTED]));
__END__
--- end footest.pl ---

Run 'perl footest.pl' and I get:

D:\pscrpt>perl footest.pl
1..1
ok 1

But if, In Foo.pm, I comment *in* the overloading, here is what I
get:

D:\pscrpt>perl footest.pl
1..1
Foo Does::Not::Exist
Invalid object passed to overload_equiv
# Looks like your test died before it could output anything.

That's caused by the following line in Test::More's sub _deep_check():

elsif ( $e1 == $DNE xor $e2 == $DNE ) {

If I leave the overloading in, and run under perl 5.8.6,  the problem goes
away (irrespective of whether I code overload_equiv() to return 0 or to
return 1).

If overload_equiv() returns 1, I get this under perl 5.8.6:

D:\pscrpt>\perl58_M\5.8.6\bin\perl footest.pl
1..1
equivalence overloaded
equivalence overloaded
ok 1

And if overload_equiv() returns 0, I get this under perl 5.8.6:

D:\pscrpt>\perl58_M\5.8.6\bin\perl footest.pl
1..1
equivalence overloaded
equivalence overloaded
equivalence overloaded
equivalence overloaded
ok 1

(I think that each of those occurrences of "equivalence overloaded" arises
from the overloading of the 'eq' operator, not the '==' operator.)

The "real life" situation is in relation to one of the PDL test files
(namely limits_normalize_dsets.t). As with Foo.pm, PDL checks that the
arguments its overloaded operators receive are of an appropriate type - and,
as with Foo.pm, it finds that $DNE is *not* of an appropriate type, and the
test script croaks with essentially the same error message.

This only happens with perl 5.8.7. Earlier versions of perl 5.8
(Test::More-0.47 or earlier) get it right. Or is my idea of "right" a little
questionable ? In any case, surely the results should not vary upon the
basis of whether operators are overloaded or not.

Cheers,
Rob

Reply via email to