Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Non-local return via last()

by tobyink (Abbot)
on Sep 27, 2012 at 12:37 UTC ( #996001=note: print w/ replies, xml ) Need Help??


in reply to Non-local return via last()

Nice. I've been looking for a solution for a while for resolving this disparity:

use 5.010; use List::Util qw(first); sub sub1 { first { return 1 } @_; return 2; } sub sub2 { grep { return 1 } @_; return 2; } say sub1(0); say sub2(0);

Given that first and grep are conceptually similar, it is counter-intuitive that return inside them means different things. While prototypes help first look like a built-in list operator, they don't help it act like one.

I've played around with Scope::Upper a little to try to come up with a resolution for this, but haven't had any luck so far. (Though as a by-product I managed to come up with returning which is pretty fun.) I wonder if last might provide some kind of solution.

perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'


Comment on Re: Non-local return via last()
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://996001]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (15)
As of 2015-07-06 17:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (77 votes), past polls