$result = [$a=>$b]->[$b<=$a];
This returns the lowest of the two values.
Let's see what they said about it!
"This wonderfully symmetrical one-liner contributed by Phil Abercrombie returns the lesser of $a and $b.
It can be written with less wasted technology, but then it isn't nearly as pretty:
$result = ($a, $b)[$b <= $a]"
* Perl Best Practices:
"[...] The syntactic symmetry is very elegant, of course, and devising it obviously provided the original developer with a welcome diversion from the tedium of everyday coding. But a clever line of code like that is a (recurring) nightmare to understand and to maintain, and imposes an unnecessary burden on everyone in the development and maintenance teams.
[...] However, it's also possible to write that same expression in a way that's so obvious, straightforward, and plain-spoken that it requires no effort at all to verify that it implements the desired behaviour:
use List::Util qw( min ); $result = min($a, $a);
It's not "clever" and it's even marginally slower, but it is clean, clear, efficient, scalable, and easy to maintain. And that's always a much better choice."
Note: I changed the variable names so that both books are consistent with each other.
In reply to Clever vs. Readable by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |