[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [FWP] A Perl curiosity



>>>>> "Yitzchak" == Yitzchak Scott-Thoennes <sthoenna@efn.org> writes:

Yitzchak> That is the blurring to which I referred.  In C a "statement" has
Yitzchak> no return value and may not be used where an expression is expected.
Yitzchak> In Perl, a "statement" always has a return value (even do {}) and
Yitzchak> may sometimes (but not always) be used as an expression.

No, I think here's where people get into mischief.

A statement never has a return value.  A *block* has a return value
(when it can be used as an expression, such as a do block or a grep/map
block or a sort block or a subroutine block) of the *last expression*
evaluated in that block.

That's very consistent.  I could completely predict the results of the
grep that started this thread by using the "last expression evaluated"
rule, and I don't see entirely how people are mystified by it. :)

Think about what the 'last expression evaluated' is in:

        do { $a if $b }

it's $b if $b is false, otherwise $a.  So the last expression is either
"false $b" or $a.

And similarly, what's the last expression in:

        do { $a unless $b }

It's $b if $b is *true*, otherwise $a.  So the last expression is
"true $b" or $a.

They aren't completely symmetric, but they *are* very consistent with
the rule of "last expression evaluated".  Similarly, look at the return
value of while or until executed in a block:

        do { $a while $b }

that has to execute a "false $b" before it can get out, so the answer
is always "false" (whichever false is the result).  And similarly:

        do { $a until $b }

has to execute a "true $b" to get out, so that's the answer there.

Why are people mystified by this? :)

-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!

==== Want to unsubscribe from Fun With Perl?  Well, if you insist...
==== Send email to <fwp-request@technofile.org> with message _body_
====   unsubscribe