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

In reply to Overload and nomethd by ewaters

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.