foo() + bar() + baz()
1 2
3 4
5
####
#! /usr/bin/perl
use strict;
package Foo;
use overload
'+' => sub {
my ($self, $other) = @_;
print "Evaluating $self + $other\n";
return Foo->new($$self + $$other);
},
'0+' => sub {my $self = shift; $$self},
'""' => sub {my $self = shift; $$self};
sub new {
my $class = shift;
my $obj = \shift;
print "Creating $$obj\n";
bless $obj, $class;
}
package main;
Foo->new(1) + Foo->new(2) + Foo->new(4);
__END__
Creating 1
Creating 2
Evaluating 1 + 2
Creating 3
Creating 4
Evaluating 3 + 4
Creating 7
####
my $foo = 0;
print ++$foo + $foo++;