ewaters has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks! In my script below I am attempting to use overload to perform a "$obj_a == $obj_b" comparison. This works fine if I overload a simple object. But in my app I want a cascading overload, where an object may not have the overload operator for '==' defined, but it's child object may have one. This is replicated below, with the child object being 'test' and the parent object being 'parent'. The output of this script is:
Comparing 4 and 4
x equals y
Comparing 4 and 4
nomethod(==) returning value '1'
x doesn't equal y
As you can see, the last call "$x == $y" where x and y are both in class 'parent' returns an empty string, which evaluates to false in bool context. Clearly, though, the call to nomethod() in the parent class returned the value '1', so why does it wind up being an empty string? Am I misunderstanding the usage of the overload.pm feature nomethod?
#!/usr/bin/perl package test; use strict; use warnings; use overload '==' => sub { my ($self, $other) = @_; print STDERR "Comparing $$self[0] and $$other[0]\n"; return $self->[0] == $other->[0]; }; sub new { my ($class, $num) = @_; return bless [ $num ], $class; } package parent; use strict; use warnings; use overload nomethod => sub { my ($obj, $other, $inv, $meth) = @_; if (my $sub = overload::Method($obj->{test}, $meth)) { my $return = $sub->($obj->{test}, $other->{test}, $inv); print STDERR "nomethod($meth) returning " . (defined $retu +rn ? "value '$return'" : 'undef') . "\n"; return $return; } }; sub new { my $class = shift; my $self = { test => test->new(@_), }; return bless $self, $class; } package main; use strict; use warnings; my $x = test->new(4); my $y = test->new(4); print "x " .($x == $y ? "equals" : "doesn't equal"). " y\n"; $x = parent->new(4); $y = parent->new(4); print "x " .($x == $y ? "equals" : "doesn't equal"). " y\n";

Replies are listed 'Best First'.
Re: Overload and nomethd
by Somni (Friar) on Oct 15, 2007 at 01:41 UTC
    You have some strange code reversal here. I would think it'd be a better design to make what you call 'parent' a subclass of 'test', and specify methods to overload to let 'parent' inherit the overloading, or specialize it if need be. For example:

    { package test; use overload '+' => 'op_add'; sub op_add { ... } } { package parent; use test; use base qw(test); }
    Instances of 'parent' inherit the overload of '+' here.

    Overloading is a pretty delicate and specific thing to just toss into your class based on what your attribute classes are doing. If you insist on going this direction you describe you might be able to accomplish by setting up an accessor for this 'test' attribute of 'parent'. When it's set check to see if the object specified has the overloading you're looking for, then set it up at run-time. You'll probably need a closure to do it properly, and even then it may not work.

    That's the most advice I'm willing to give. Falling back on overloading, or setting it up at run-time, are both going to make for some very confusing classes to use. I seriously suggest you rethink your design.