develooper Front page | perl.perl5.porters | Postings from May 2016

Question: return inside Map

Thread Next
Alberto Simões
May 15, 2016 14:46
Question: return inside Map
Message ID:

If you use 'return' inside a map, it returns from the current sub, and 
not just from the anonymous sub.

So, for example, this happens:

perl -E '@a = qw.a b c.; @b = map { $_="-$_"; return $_; } @a; say @b'
Can't return outside a subroutine at -e line 1.

Is this the desired behavior?
I would expect return there to return just from the anonymous sub.

In the other hand, this works as expected:

perl -E 'sub x { $_="-$_"; return $_ }; @a = qw.a b c.; @b = map &x, @a; 
say @b;'

For me, this detail is kind of weird. I can live with it, but I think to 
have similar behavior on both approaches would be great (??).


Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About