June 2014 Archives

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
    1034
» perlsloc t
    1633

Conflict Resolution: local::lib and git's Perl

I ran into a frustrating problem the other day:

$ git add -i
/usr/bin/perl: symbol lookup error: ~/perl5/lib/perl5/x86_64-linux-thread-multi/auto/List/Util/Util.so:
undefined symbol: Perl_xs_apiversion_bootcheck
fatal: 'add--interactive' appears to be a git command, but we were not
able to execute it. Maybe git-add--interactive is broken?

I've seen this error message from Perl a lot. It basically means that I'm trying to load an XS module compiled for a different version of Perl. Since git is directly trying to run /usr/bin/perl (5.10.1) as opposed to the perlbrew Perl I have installed (5.16.3), the error comes as no surprise: PERL5LIB is checked before Perl's built-in libraries. So if you have a local::lib (which adds its directories to PERL5LIB) and try to use those modules in a different Perl, things may not work as you expected.

What is more surprising is that Git explicitly uses /usr/bin/perl in the first place. Some Google-fu brought up a thread on the Git mailing list about changing to #!/usr/bin/env perl, but it appears this was rejected. According to another post in that thread, it is possible to set PERL_PATH when running make on Git, but that did not seem to work for me.

About preaction

user-pic