use strict; use warnings; use Data::Dumper; sub groupsOf (&$@); my $doubleStack = [ groupsOf { [ @_ ] } 3, qw{ a b c e 1 2 3 4 5 3 f } ]; print Data::Dumper ->Dumpxs( [ $doubleStack ], [ qw{ doubleStack } ] ); sub groupsOf (&$@) { my $rcToRun = shift; my $groupsOf = shift; my $rcDoIt; $rcDoIt = sub { $rcToRun->( map shift, 1 .. ( @_ < $groupsOf ? @_ : $groupsOf ) ), @_ ? &$rcDoIt : (); }; &$rcDoIt; }