I was just wondering if this is posssible:
use Modern::Perl;
my @list = ('a' .. 'j');
map { func($_) } each(@list);
sub func {
my ($index, $value) = @_;
say "$index => $value";
}
No you can't, because each
returns a key/value pair each time it is called. You are calling it just once, so it will pass (0, $list[0])
to the map
, and the subroutine will be called once for each value.
If you want to call func
with each key/value pair you can write
map { func($_, $list[$_]) } keys @list;
but that is misusing map
because it is meant for mapping one list to another. You should use for
instead like this
func($_, $list[$_]) for keys @list;
You could also use each
like this
my ($i, $v);
func($i, $v) while ($i, $v) = each @list;