#!/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 $return ? "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";