On Apr 29, 2009, at 4:00 PM, Michael G Schwern wrote:
>>
>> For the if construct you can always wrap it into an eval:
>>
>> my $x = eval { if ($blah) { ... } else { 'foo' }; }
>
> Or a do block. Oh god, that works! I suspect its a side effect of
> Perl
> compiling if/else into some sort of || conditionals.
The value of a block has always been the value of the last statement
executed.
This is how subs get a default return value unless you explicitly do
return;
> while() and foreach()
> don't do that.
They do, the last expression in a while is the test expression, which
will be false on exit. Try until() and you will see the expression value
Consider the last expression in a foreach as a test of how many
elements are left to process, so it will always be false.
While you cannot use them as an expression, they have a value if they
are the last thing in a sub, eval or do block
>
>
> my $foo = given($blah) {
> when(cond1) { "first" }
> when(cond2) { "second" }
> when(cond3) { "third" }
> default { "default" }
> };
>
> That's pretty cool.
>
> So +1 to making given/when return the last evaluated expression.
I am surprised that the last statement executed rule does not apply to
given like it does to any other
Graham.
Thread Previous
|
Thread Next