in reply to Functions in Perl
sub showargs{ print map {"$_ $_[$_]\n"} 0..$#_}
Doing things fast often leads to code that other people can't read. So don't assume this is good practice. But it's fun, and for a throwaway script it's acceptable.
An explanation:
map {code} @array runs code on every element of an array, just like a foreach loop. Inside the code block, each element of the array is aliased to $_.
0..$#array creates a list going from 0, to the index of the last element in @array. So 0..$#_ goes from 0 to the last element in @_, which is your array of arguments.
Finally, "$_ $_[$_]" prints first the aliased element, then the corresponding member of @_.
I'm sure some other monks can get this faster (and more obscure....)
|
|
---|