4 ms·
Many of these don't pass muster as Modern Perl development, or any professional software development. As a code reviewer, I'd reject everything but <=> and @{[
by DougWebb 17y ago
Many of these don't pass muster as Modern Perl development, or any professional software development. As a code reviewer, I'd reject everything but <=> and @{[ ... ]} as being too cryptic. <=> passes because it's the standard way to compare numbers in sort routines, and @{[ ... ]} passes because it's the standard way to interpolate lists and function call return values into strings.
}{
Useful for one-liners, but has no use in production code.
=()=
In production code, I'd prefer to see the match results assigned to a named array, and the array used in a scalar context to get the length. Alternatively, I'd accept a comment explaining why there is an assignment to an anonymous array. It had better not call =()= an operator, though.
~~
You can force scalar context using the scalar keyword, which is much clearer.
~-
You can type $x-1, which is much clearer and works for numbers <= 0 too.
-+-
I didn't know "3.99" was an integer... in any case, would probably use printf or sprintf for formatting numbers for display, using explicit formats rather than relying on whatever -+- spits out.
0+
Again, I would use printf or sprintf in most cases where 0+ might be used.
- SwellJoe 17y ago}{ Useful for one-liners, but has no use in production code. What about production one-liners? Let us not underestimate the power of a well-applied one-liner.
- pkrumins 17y agoWell said!
- joe_the_user 17y agoI wouldn't underestimate the power of a one-liner. That's why I'd avoid using one! With great power comes great responsibility - and it's hard to cram the responsibility part into one line as contrasted with a clear, well-behaved subroutine.
- ramchip 17y agoI think this is tongue in cheek. Just re-read: Compare these two examples: print "these people @{[get_names()]} get promoted" and print "these people ", join " ",get_names(), " get promoted" Clearly, the first example wins for code clarity.
- pkrumins 17y agoThanks for the comments. I fixed the wording mistake of 3.99 being an integer. You are right about everything you say. @{[]}, <=>, and =()= are acceptable in production code, but the others are not. These other "operators" actually were invented to chop off characters in Perl Golf competitions. This article wasn't meant as "good Perl coding tips and tricks", it was more written for fun.
- mst 17y agoI tend to prefer using ${\...} for interpolation - so print "${\join ',', @array}"; or so. See http://shadowcat.co.uk/blog/matt-s-trout/madness-with-methods http://shadowcat.co.uk/blog/matt-s-trout/madness-with-method... for more fun you can have like this. Oh, and remember - sometimes makefiles that do useful things to and with production code should contain one liners. Don't assume that the intersection of "one liner" and "production" is zero - when I ran the Web Server Survey at Netcraft about 3/4 of it was one liners driven by make ...