Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

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'

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://996001]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2018-05-22 22:42 GMT
Find Nodes?
    Voting Booth?