This is mildly clever. I'll disect it for the benefit of readers who may not be able to parse it.
Square brackets are an anonymous list constructor in Perl. What results is a scalar value that is a reference to a list. So:
$foo = [1,2]; is the same as: @foo = (1,2); $foo = \@foo;
=> is syntactic sugar, and is a synonym for ',' , so what you've done with the first part of the expression is an (anonymous) reference to a list containing $a and $b. It is a cool way to write it because it looks like numeric comparison, but actually does nothing of the sort.
Now, the '->' is the arrow operator used to dereference in Perl. When followed by square brackets, it is expects to dereference a list reference. For instance, with our foo example above, we'd get the first (zeroth, actually) value of @foo with:
$foo->[0];
So... $max = [$a=>$b]->[...];
intends to dereference the anonymous list and access the numerical value indicated by '...'. What is actually contained in that is, this time, an actual numeric comparison is being done with $a<=$b, which if false, will generate 0, the zeroth element, and if it is true, it will be equivalent to 1, giving the other element of the list.
And in more general terms, you are correct... there is more of this in Perl than anywhere else I've ever played, and it is among the multitude of reasons that I love Perl and Perl culture.
If you disliked this bit of fun, you are almost certain to not enjoy the Fun With Perl mailing list, accessible from http://lists.perl.org/ . This list has frequent mailings of things of this sort, along with other things like Perl Golf contests. These last are seeing who can perform a specified task in the fewest number of (key)strokes. You'd be amazed what Perl masters can do in 60 or strokes... I've never seen a problem posed that took more than that. Of course, the winning solution always look like this:
-ni $_=$#@*(%(#$@#___;eval
so it has little value until you get enough the hang of it to be able to parse those things... but once you have the basics, decoding those and asking questions about them is extremely educational.
I personally suck at these in that I've been programming Perl too long to justify entering those contests in the "beginner" category and yet I can't generally score well even for a beginner, let alone with the main group.
no subject
This is mildly clever. I'll disect it for the benefit of readers who may not be able to parse it.
Square brackets are an anonymous list constructor in Perl. What results is a scalar value that is a reference to a list. So:
$foo = [1,2];
is the same as:
@foo = (1,2);
$foo = \@foo;
=> is syntactic sugar, and is a synonym for ',' , so what you've done with the first part of the expression is an (anonymous) reference to a list containing $a and $b. It is a cool way to write it because it looks like numeric comparison, but actually does nothing of the sort.
Now, the '->' is the arrow operator used to dereference in Perl. When followed by square brackets, it is expects to dereference a list reference. For instance, with our foo example above, we'd get the first (zeroth, actually) value of @foo with:
$foo->[0];
So... $max = [$a=>$b]->[...];
intends to dereference the anonymous list and access the numerical value indicated by '...'. What is actually contained in that is, this time, an actual numeric comparison is being done with $a<=$b, which if false, will generate 0, the zeroth element, and if it is true, it will be equivalent to 1, giving the other element of the list.
And in more general terms, you are correct... there is more of this in Perl than anywhere else I've ever played, and it is among the multitude of reasons that I love Perl and Perl culture.
If you disliked this bit of fun, you are almost certain to not enjoy the Fun With Perl mailing list, accessible from http://lists.perl.org/ . This list has frequent mailings of things of this sort, along with other things like Perl Golf contests. These last are seeing who can perform a specified task in the fewest number of (key)strokes. You'd be amazed what Perl masters can do in 60 or strokes... I've never seen a problem posed that took more than that. Of course, the winning solution always look like this:
-ni
$_=$#@*(%(#$@#___;eval
so it has little value until you get enough the hang of it to be able to parse those things... but once you have the basics, decoding those and asking questions about them is extremely educational.
I personally suck at these in that I've been programming Perl too long to justify entering those contests in the "beginner" category and yet I can't generally score well even for a beginner, let alone with the main group.