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

Question: return inside Map

Thread Next
From:
=?UTF-8?Q?Alberto_Sim=c3=b5es?=
Date:
May 15, 2016 14:46
Subject:
Question: return inside Map
Message ID:
83edb768-3a1c-824a-78ff-53049df90f3e@alfarrabio.di.uminho.pt
Hi

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 (??).


Cheers
ambs

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