develooper Front page | perl.perl5.porters | Postings from August 2011

Re: [perl #94350] definition of truthiness in the perldocs

Thread Previous | Thread Next
From:
Aristotle Pagaltzis
Date:
August 6, 2011 09:28
Subject:
Re: [perl #94350] definition of truthiness in the perldocs
Message ID:
20110806162845.GB29531@klangraum.plasmasturm.org
* l.mai@web.de <perlbug-followup@perl.org> [2011-07-08 07:15]:
> So I was looking for the official definition of "truth" in the
> perldocs.
>
> perldata says:
>
>     A scalar value is interpreted as TRUE in the Boolean sense
>     if it is not the null string or the number 0 (or its string
>     equivalent, "0").
>
> perlsyn says:
>
>     The number 0, the strings C<'0'> and C<''>, the empty list
>     C<()>, and C<undef> are all false in a boolean context. All
>     other values are true.
>
> I think both are wrong.
>
> perldata is missing undef in its list of false values

Agreed.

> and perlsyn claims the empty list is false, which doesn't even
> make sense. There are no lists in scalar context. Booleans are
> scalars. Therefore you can't have an "empty list" in boolean
> context (C<()> simply evaluates to C<undef> but I don't know if
> that's documented anywhere).

I never really understood the point of the “no lists in scalar
context” orthodoxy. I don’t see how it helps in reasoning about

    $foo = ();

And likewise you can write `if (()) { this_never_runs }`.

I don’t see what mistakes are caused by the inaccurate mental
model in “a list in scalar context evaluates to its last element
or to undef if it’s empty” or what you stand to gain by going
through the gymnastics required to re-interpret list-ish syntax
as being special set of operators in scalar context, even if it’s
true on a grammar level.

In any case, the fact that you can write `if (())` remains. And I
argue that it needs to be called out as another way to express
falsity. If you want to be fastidious, be my guest, but you are
going to end up with contortions like “Note that expressions that
look like empty lists yield undef in scalar context, so in the
boolean sense they’ll also be false.”

But to omit it entirely because of the fact that it evaluates
to undef would in my view be a disservice to anyone who does
not read the document already primed with knowledge of that
consequence. It’s a matter of how you approach documentation:
are you trying to be exact and concise, or trying help the user
achieve an aim?

> I propose the following changes:
>
> * perldata:
> A scalar value is interpreted as TRUE in the Boolean sense if
> it is not undef, the empty string, or the number 0 (or its
> string equivalent, "0").

I don’t like the double negation in “if it is not undef”. I would
reverse the sense just how perlsyn does: a scalar value is FALSE
if it is undefined, 0, "0" or the empty string, and it is TRUE if
it is anything else.

> * perlsyn:
> The number 0, the strings C<'0'> and C<''>, and C<undef> are
> all false in a boolean context. All other values are true.

As per above, I’d want some note about empty lists here.

Regards,
-- 
Aristotle Pagaltzis // <http://plasmasturm.org/>

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About