perlsloc - Count Perl Source Lines with Perl::Tidy

While spending some time putting together my own perltidyrc file, I became intimately familiar with the Perl::Tidy documentation.

One day, I decided to find out exactly how much code I was maintaining. Since perltidy can strip comments and POD, and also normalize the source code to make a fair measurement, it's a perfect tool for counting Source Lines of Code (SLOC).

Here's a small shell script using ack, perltidy, xargs, and wc to count the source lines of code in any number of directories.

ack -f --perl $@ | xargs -L 1 perltidy --noprofile --delete-pod --mbl=0 --standard-output | wc -l

ack -f lists the files that would be searched, and --perl searches Perl files, so we get ack's heuristics for finding Perl files. xargs -L 1 invokes the following command for every 1 line of input. The perltidy command strips the pod and tightens up the whitespace and writes the result to stdout, which wc -l will then count, line by line.

So, as an example, the current Statocles release has 50% more test lines than source lines:

» perlsloc lib bin
» perlsloc t


One comment, one question:

First, this seems to leave a few blank lines in the stream, mostly between files. A "egrep -v '^$'" will get rid of those.

Second, does SLOC include source comments? I don't know, and I am asking because you make a point to remove POD. If it does include comments, then that's OK. But if it should not count comments, adding another egrep would remove them: egrep -v '^\s*#'

Leave a comment

About preaction

user-pic I blog about Perl. I work for Bank of America. I own Double Cluepon Software.