[cgiapp] size of scripts and dependencies

Richard Jones ra.jones at dpw.clara.co.uk
Sat Sep 19 05:49:42 EDT 2009


Ron Savage wrote:
[..]
> #!/usr/bin/perl
> #
> # Name:
> #	count.code.pl.
> 
> use strict;
> use warnings;
> 
> use File::Find;
> 
> my($count);
> 
> # --------------------------------------------------------------
> 
> sub found
> {
> 	return if (-d $_ || ! -T $_);
> 
> 	open(INX, $_) || die("Can't open($_): $!");
> 	my(@line) = grep{! /^$/ && ! /^[#{}]/} map{s/^\s+//; s/\s+$//; $_;}
> <INX>;
> 	close(INX);
> 
> 	$count += $#line + 1;
> }
> 
> # --------------------------------------------------------------
> 
> $count   = 0;
> my($dir) = shift || "Usage: $0 <dir.name>";
> 
> find(\&found, $dir);
> 
> print "Line count: $count. \n";

Hi Ron, hey that's nice - I just used it to profile my current app. I 
modified it slightly to make it a bit less painful on the eyes (though I 
haven't managed to get rid of the line noise in the 'for' block yet) and 
introduced a files counter and an average lines/file function:

#-----------------------------------------
use strict;
use warnings;

use IO::All;
use File::Find;

my $lines = my $files = 0;

my $dir = shift || "Usage: $0 <dir.name>";
find(\&found, $dir);

sub found {
   return if (-d $_ || ! -T $_);

   $files++;
   for ( io($_)->slurp ) {
     s/^\s+|\s+$//;
     next if /^$|^[#{}]/;
     $lines++;
}

print "File count: $files\n";
print "Line count: $lines\n";
print "Average lines / file = " . int ($lines / $files) . "\n";

#-----------------------------------------

I'm also playing with the 'next if' line to exempt single-line blocks eg 
{ do_something() if $foo } which are currently skipped using ^[#{}]
-- 
Richard Jones


More information about the cgiapp mailing list