#!/usr/bin/perl
package Dummy;
use strict;
use warnings;
use overload '==' => \&iseq;
sub new { return bless [ $_[1] ], $_[0];}
sub iseq { return $_[0]->[0] == $_[1]->[0] ;}
package MyTie;
use strict;
use warnings;
use Carp;
our $AUTOLOAD;
sub TIEARRAY {
my $class = shift;
return bless [ map { Dummy->new($_) } @_ ], $class;
}
sub FETCH { $_[0]->[ $_[1] ]; }
sub FETCHSIZE { scalar @{$_[0]}; }
sub AUTOLOAD { croak "Undefined method $AUTOLOAD called in " . __PACKAGE__;}
package main;
use strict;
use warnings;
use Data::Dumper;
use Devel::Peek;
tie my @arr, 'MyTie', 1,3,4;
for my $i (@arr){
for my $j (@arr){
# uncomment this row and comment out the next one, and it works
# if($i->iseq($j)){
if($i == $j){
print $i->[0] . " == " . $j->[0] . "\n";
} else {
print $i->[0] . " != " . $j->[0] . "\n";
}
}
}
exit 0;
####
Operation `0+': no method found, argument in overloaded package Dummy at 5perltie.pl line 42.
####
use overload '==' => \&iseq, '0+' => \#
...
sub num { print "Someone wants me (" . $_[0]->[0] . ") in numeric context\n"; }
####
Someone wants me (1) in numeric context
Someone wants me (1) in numeric context
1 == 1
1 != 3
1 != 4
Someone wants me (1) in numeric context
Someone wants me (3) in numeric context
3 == 1
3 == 3
3 != 4
Someone wants me (1) in numeric context
Someone wants me (4) in numeric context
4 == 1
4 != 3
4 == 4