On Thu, Nov 20, 2003 at 07:27:56PM -0600, Jonathan Scott Duff wrote:
: On Thu, Nov 20, 2003 at 03:26:36PM -0700, Luke Palmer wrote:
: > One wonders what the return value of a loop will be:
: > 
: >     my $what = do {
: >         while $cond {...}
: >     }
: 
: I would expect it to be the value of the last statement executed.

Note that Perl 5 interprets it as the value of the last *expression*
evaluated, which for a while loop is going to be some variant of false:

    my $what = do {
            $cond = 1; 
            while ($cond) { $cond = 0; 123 }
    };
    print $what,"\n";
    __END__
    0

But compare:

    my $what = do {
            $cond = 0; 
            until ($cond) { $cond = 1; 123 }
    };
    print $what,"\n";
    __END__
    1

Implementation-wise, all that's going on is that the final value on the
expression evaluation stack is magically turned into the return value.
It would take extra effort to remember the result of the last statement
rather than the last expression.  And it would be wasted effort most of
the time.

Larry

Reply via email to