in reply to Re^2: Extracting elements from array
in thread Extracting elements from array

The point to map is that you can run the elements of the list through any code you like in the map block.

map assigns the result of the last evaluated statement in the block, you don't always have to can't say return $foo;, but can just say $foo, as shown in all these examples. I tend to eschew many of Perl's shortcuts (like omitting a return statement from a block) because I prefer readability, but when it's a single statement, as is often the case with map, it's nice to be able to skip it.

But if it's clearer to you to write:

my @hash_names = map{ return {name => $_}; } @all_names;
then by all means do that.

edit: no, don't do that
The way forward always starts with a minimal test.

Replies are listed 'Best First'.
Re^4: Extracting elements from array
by choroba (Cardinal) on Jan 15, 2016 at 14:00 UTC
    Using return in a map returns from the enclosing subroutine. Cf:
    say for sub { map { "\U$_" } @_ } ->(qw( abc def ghi )); # ABC # DEF # GHI

    versus

    say for sub { map { return "\U$_" } @_ } ->(qw( abc def ghi )); # ABC

    and even

    say for map { return "\U$_" } qw( abc def ghi ); # Can't return outside a subroutine at -e line 1. # [255]
    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,