On Sat, 12 Aug 2000 05:44:54 +1000 (EST), Damian Conway wrote:
>And people, seeing every other operator overloaded for a particular
>module, will assume that || and && do the right thing too.
>
>But they won't :-(
OK, how about this idea then. && and || still work as they did, but you
provide a callback method (say, TRUTH or BOOLEAN) that says whether this
particular object is true or false. So:
my $x = new Blah::Blah(1,2,3);
my $y = new Blah::Blah(4,5,6);
my $z = new Blah::Blah(7,8,9);
my $w = $x || $y || $z;
What happens? || tests $x for TRUTH. Your callback method gets called
for object $x. Your function decides wether it's a yey or a ney. || sees
that value, and returns $x in case of a yey, or continues evaluating $y
for a ney. For $y, again, $y->TRUTH is invoked. If it's a yey, $y is
returned, if it's a ney, it's $z. In total: $w becomes one of $x, $y or
$z.
There is NO NEED to overload any of qw(|| && or and).
No way to screw it up, either. ('What, a "||" that evaluates from right
to left? ARGH!') You simply can't touch the internals.
Hmmm... you can use this to do:
if($x) { .... }
with $x such an object.
--
Bart.