· Offer Kaye > Perl > Line Length

Explanation of 'Line Length' Perl oneliner

Here is the code again: perl -lpe'$_=length'
"length" - calculates and returns the length of the current line ($_ by default).
"$_=length" - after length calculates the length of the current line, set $_ to the result.
"-lp" - the command line switch "-p" takes care of iterating over the input lines and also doing a print of $_ after the length is calculated, while "-l" makes sure to strip the "\n" before the calulation (so it isn't counted) and to add it before the print.

Usage Example

Given an input file called "example" with the contents:
a2
b33
c444
d5555
Then running the following from the command line:
perl -lpe'$_=length' example
will print to STDOUT the following four lines:
2
3
4
5

Offer Kaye / To contact me, email me at: offer.kaye no spam at gmail dot com / Last Modified: Thu Jul 15 23:04:09 2004

Valid XHTML 1.0! Valid CSS!