map
is list operator and a core function. It is simple Perl syntax that expects no comma after a block parameter to a subroutine. The special thing about map
is that it can also take the form map EXPR, LIST
. If this was used with a standard subroutine the EXPR
would just be evaluated and passed as the first parameter.
Block parameters are valid for all subroutines, and can be used if you apply prototypes to your subroutine definition. For instance, you could define a mymap
that behaved just the same way by writing
use strict;
use warnings;
use 5.010;
sub mymap(&@) {
use Data::Dump;
my $sub = shift;
my @newlist;
push @newlist, $sub->($_) for @_;
@newlist;
}
say for mymap { $_ * $_ } 1, 2, 3;
output
1
4
9
But in general you should avoid prototypes unless you know exactly what you are doing. There is generally a better way to write your code.