Overloading works on objects, for example:
use v5.10;
package Number {
use overload
'|' => sub { 1 / ( 1 / ${$_[0]} + 1 / ${$_[1]} ) },
fallback => 1
;
sub new {
my( $class, $arg ) = @_;
bless \ $arg, $class;
}
}
my $n = Number->new( 5 );
my $m = Number->new( 5 );
say( $n | $m );
There are many things to watch out for since, since Perl 5 does not send multiple methods. In your routine, you need to figure out the second argument, and so right. This can get complicated. I would prefer to use the usual methods for this.
source
share