ewaters has asked for the wisdom of the Perl Monks concerning the following question:
Comparing 4 and 4 x equals y Comparing 4 and 4 nomethod(==) returning value '1' x doesn't equal yAs 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 |