spx2 has asked for the wisdom of the Perl Monks concerning the following question:

I just found this piece of code. This is how each_array is defined in List::MoreUtils. What it does is generate an iterator for iterating over multiple lists at the same time which is pretty useful.
sub each_array (\@;\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@) { return each_arrayref(@_); }
But what is striking is that you can call it like this and not worry that the lists will get merged before they will be used inside each_array
my $ea = each_array(@a, @b, @c); while ( my ($a, $b, $c) = $ea->() ) { .... }
I know that \@;\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@ is supposed to be some sort of prototype , I don't really understand what it does, what does this do ?

Replies are listed 'Best First'.
Re: interesting prototype
by biohisham (Priest) on Feb 17, 2010 at 07:23 UTC
    Passing arrays or hashes to a subroutine would flatten their content into one long list saved in @_, sometimes this might be unwieldy so you'd need to pass these by reference instead, as is done in the prototype you mention above, this way each list would be passed and preserved as distinct unflattened member of @_..
    my @array1=qw(something or something); my @array2=qw(Another Something); showElement(\@array1, \@array2); #by reference sub showElement{ foreach $element (@_){ print "Unreferenced: $element\n"; print "Referenced: @$element\n"; } }
    On the other hand, the passed lists would've been flattened into one large @_ if you pass them as follows:
    my @array4=qw(separate and yet); my @array5=qw(flattened it); showElements(@array4, @array5); #as-is sub showElements{ foreach $element(@_){ print "$element "; } }
    You might wanna also see the "prototype" section in perlsub..


    Excellence is an Endeavor of Persistence. Chance Favors a Prepared Mind.
Re: interesting prototype
by Anonymous Monk on Feb 17, 2010 at 05:03 UTC
    It allows you to say each_array( @a1,@a2,@a3,@a4,@a5,@a6,@a7, @a8,@a9,@a10,@a11,@a12,@a13,@a14,@a15, @a16,@a17,@a18,@a19,@a20,@a21, @a22,@a23,@a24,@a25,@a26 ) and instead of flattening the arguments, each_array will receive 26 array references in @_
      nice, is this documented somewhere ?

        See the Subroutine Attributes section in perldoc perlsub.

        Edit: Right, prototypes. Sorry!