With Perl 6, it will (probably) be possible to have values with boolean
value independent of integer or string values, so that it will be possible
to have a value that when viewed as string or number will be "" or 0, but
will evaluate as true in a condition.

I think this should be applied to the `defined' function, so that, instead
of returning a true/false value, it returns an overloaded value that has the
same string/number value as its argument, but boolean value being true iff
it's not undef.

This will make possible things like


perl5:  $x = undef;
        $y = $x || "N/A"   ===> $y = "N/A" (not available)

        $x = 25.4;
        $y = $x || "N/A"   ===> $y = 25.4

        $x = 0;
        $y = $x || "N/A"   ===> $y = "N/A"  (wrong, should be 0)

        $x = 0;
        $y = defined($x) ? $x : "N/A"  ==> $y = 0  (right)


perl6:  $x = undef;
        $y = defined($x) || "N/A"   ===> $y = "N/A"

        $x = 25.4;
        $y = defined($x) || "N/A"   ===> $y = 25.4

        $x = 0;
        $y = defined($x) || "N/A"   ===> $y = 0


In Perl 5, it would be written `defined($x) ? $x : "N/A"', but this has the
problem that $x is evaluated twice, so it doesn't work if instead of $x we
have a function call (or even if $x is tied...).

- Branden

Reply via email to