use v5.12; use warnings; use Data::Dump qw/pp dd/; our $c; # count sub cmap(&@) { my $block = shift @_ ; local $c=-1; map { $c++; &$block } @_; } # -------- DEMO my @myArray = qw(alfa beta gamma); say "--- used as loop"; cmap { say "at position $c there is $_" } @myArray; say "--- used as map"; dd cmap { [ $c => $_] } @myArray; #### --- used as loop at position 0 there is alfa at position 1 there is beta at position 2 there is gamma --- used as map ([0, "alfa"], [1, "beta"], [2, "gamma"])