develooper Front page | perl.perl5.porters | Postings from June 2008

Re: [perl #56150] return return

Thread Previous | Thread Next
From:
Nicholas Clark
Date:
June 20, 2008 09:34
Subject:
Re: [perl #56150] return return
Message ID:
20080620163417.GU99534@plum.flirble.org
On Fri, Jun 20, 2008 at 12:06:31PM -0400, Ronald J Kimball wrote:

> (Note that return can be usefully used as an expression:
> my $foo = $bar ? return : 'baz';
> Odd, but it works.)

But then people start writing code that looks like this:

$basket->calculate() or return $self->emit("Could not recalculate", 'error') if $basket->complete();

and I go http://www.flickr.com/photos/twoshortplanks/28698637/

(sadly I no longer have the other example of code from the same author, that
mixed declaration, conditional initialisation and control flow in one
statement. But actually, I don't think that he was using ternaries in either)

On Fri, Jun 20, 2008 at 06:29:45PM +0200, Rafael Garcia-Suarez wrote:

> return is technically parsed as a list operator (LSTOP).
> That enables using it in compound expressions.
> 
> Note that this syntax works also for other control flow statements :
> 
> $ perl -wle '$i=3;while(1){print($i--||last)}'
> 3
> 2
> 1
> 
> I don't really see a reason to change it.

Ah. Makes sense.
I guess I can reject my own bug.

Nicholas Clark

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