in reply to Too Much Perl?
If you look at the following code and immediately understand it, then you have been spending way too much time with Perl. I suggest you take a few doses of Java. :-)
perl -p -e '$\+=$_}{'
The -p switch instructs perl to wrap your code with:
LINE: while (defined($_ = <ARGV>)) { # Your code here } continue { die "-p destination: $!\n" unless print $_; }
This transformation is so direct that you can use the token }{ to your advantage. My original code now becomes:
LINE: while (defined($_ = <ARGV>)) { $\ += $_; } { } continue { die "-p destination: $!\n" unless print $_; }
The while loop now focuses on summing the input (converting each line to a number in Perl's fashion, i.e. stopping after any non-number chars). The continue is now attached to an empty run-once block. So, the printing of $_ only occurs once, and after the input has been exhausted, and $_ is now undef. The print statement prints the empty $_, but also always prints $\ (the line terminator), which currently has our sum.
So, this is a sum command. Can be used with wc ... | perl ... or du ... |, etc.
I never code like this in production code, of course! ;-)
Ted Young
($$<<$$=>$$<=>$$<=$$>>$$) always returns 1. :-)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Too Much Perl?
by apotheon (Deacon) on Apr 27, 2006 at 20:42 UTC |