# dualvar.pl package DualVar; use strict; use warnings; use overload 'bool' => \&overload_true, '.' => \&overload_concat; sub overload_true { # Return 1 if either $obj->[0] or $obj->[1] # are true. Else return 0. my $obj = shift; if($obj->[0] || $obj->[1]) { return 1 } return 0; } sub overload_concat { my ($obj, $other, $reversed) = (shift, shift, shift); if($reversed) { return $other . $obj->[1] } return $obj->[1] . $other; } # The above procedures would normally be put into # a module, which would be loaded as needed. # The code below relies on the above procedures. # Here we see that $x is True, and also that $y # remains unchanged when $x is concatenated onto it. my $x = [1, ""]; my $y = "hello"; my ($r1, $r2); bless $x, 'DualVar'; if($x) { $r1 = $x . $y; $r2 = $y . $x; } print "# ok 1\n" if $r1 eq $y; print "# ok 2\n" if $r2 eq $y; # Outputs: # ok 1 # ok 2