So this question is purely for learning purposes and curiosity, but can anyone explain how the function below works?
sub mesh (\@\@;\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@) {
my $max = -1;
$max < $#$_ && ( $max = $#$_ ) foreach @_;
map {
my $ix = $_;
map $_->[$ix], @_;
} 0 .. $max;
}
It’s from the List::MoreUtils module. I’m using it in one of my applications and I happened to see the source code, and it made me feel like I don’t know perl at all! Can anyone explain this craziness? 🙂 Thanks!
I won’t cover the prototypes part (mob said he will).
Here’s a more readable version – ideally, it should be self explanatory
here’s a commented original version
One of the unusual things in this method is the
function signature (prototype).As @mob and @ikegami wisely noted in the comments,
In other words, the following 2 calls behave the same
This is done so that you can pass individual arrays (and not arrayrefs) as arguments to functions that will behave like built-ins (e.g.
map/sort)To answer Zaid’s query as to what happens if 1 or 33 arrays are listed as parameters to call to
mesh(), it will generate a compile time error: