On Thu, 2004-06-24 at 08:04, Jonadab the Unsightly One wrote:
> > In Perl5, the following values are FALSE: undef, '0', 0, and ''.
> ... The really special case is '0', which
> is false for arcane (but very sensible) reasons.

I don't agree that '0' being false is sensible.  This, plus less than
vigilant programmers, has led to a great '0' hole, where many Perl
programs correctly handle all possible input except for one: a string
consisiting of a single 0.

To ensure that your program doesn't suffer from the '0' hole, you need
to use

        frob($k) if defined($k) && length($k);

anywhere you would normally just say "frob($k) if $k".  This somewhat
strange incantation has become idiomatic in Perl5.

As a related question, why is "0" false, but "0.0" is true?  Floats are
second-class citizens?


> As I understand it, the addition of properties in Perl6 (See the
> Apocalypse 12 article) allows for even undefined values to be true
> if you choose to make them so.

That's the way I understand it too.  You can also do the same in
reverse, making it so your function can return, say, "Failure!" but
evaluate to false.

However, it seems that because Perl is finally getting a typing system,
this hack can be fixed in Perl itself!  No programmer intervention
needed.  Undef and '' can be false for strings, undef and 0 can be false
for integers, undef, 0, and 0.0 can be false for floats, etc.

Unfortunately, so that machine-converted Perl5 code still works, I think
that untyped variables will probably still need to follow this oddball
rule.

Am I understanding correctly?  Thanks,

    - Scott


Reply via email to