Category: | Code |
Author/Contact Info | psychotic |
Description: | Hello fellow monks. I have only recently started to walk the path of Perl enlightement, what a great resource PerlMonks is! Having said that, i'd like to contribute some code i wrote last night.
As i am working on a personal little project, i wanted to be able to check its progress in terms of lines of code written. I also wanted to be able to tell how much of the total lines of code comprising the project were comments. So i hacked together a small script. I've tried to reuse as much code as possible, so you'll notice my using of at least one Perl idiom i picked up from here.
Looking at the code itself, i think it is self-documenting. The only thing i'd like to comment on, is the initially wasteful look of slurping the entire file in prior to processing. I did that so it would be easier Usage is simple. Edit the list of extesions you want to be scanned for --inside the script-- to suit your liking. These are assumed to contain Perl code. If it is fed a directory as a first parameter, it will use that as the directory to start the recursive search, else it will use the current working directory. Of course searches inside every subdirectory and its subdirectories, etc. I would appreciate any suggestions or comments that might be applicable. Feel free to bash me for any errors in either logic, style, or anything you will. Excuse the ugly presentation of results, as illustrated in the sample run below: Update I updated the code to add POD parsing (correct at least :)), and impoved a bit the readability of the results. |
use strict;
use warnings;
use File::Find;
use Cwd;
our $dir = shift || cwd();
our @types = ('pl', 'plex', 'pm');
our @files;
find sub {
return unless -f;
my $fName = $_;
foreach (@types) {
my (undef, $ext) = split (/\./, $fName);
$ext and push @files, $File::Find::name if $ext eq $_;
}
}, $dir;
unless (@files) {
die "No files found to operate on!";
}
print(" Code Cmts %Code Total File \n");
print "=" x 80 . "\n";
my ($TLOC, $TCOM, $TTOT) = (0,0,0);
foreach my $file (@files) {
my ($LOC, $COM) = (0,0);
my $contents = do { local (*ARGV, $/) = [$file] and <> };
my @lines = grep { ! /^\s*$/ } split(/$\//, $contents);
while (@lines) {
$_ = shift @lines;
if ( /^=[a-z0-9]+\s/ ) {
do {
$_ = shift @lines and $COM++;
} until /^=cut/;
}
/^\s*\#/ ? undef $_ || $COM++ : $LOC++;
}
my $TOT = $LOC + $COM;
$TLOC += $LOC;
$TCOM += $COM;
$TTOT += $TOT;
my $PER = sprintf("%3s", sprintf("%.0f", ($COM/$TOT) * 100));
printf("% 6d % 6d % 6d % 6d %s\n",
$LOC, $COM, $PER, $TOT,
' '. substr $file, length($dir));
}
my $TPER = sprintf "%.2f", ($TCOM/$TTOT) * 100;
print "=" x 80 . "\n";
print "SEARCHD: '$dir'\n";
print "RESULTS: $TLOC LoC ($TTOT w/comments: $TCOM lines -> $TPER% of
+total)";
|
|
---|