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

#####  CGI::Application community mailing list  ################
##                                                            ##
##  To unsubscribe, or change your message delivery options,  ##
##  visit:  http://www.erlbaum.net/mailman/listinfo/cgiapp    ##
##                                                            ##
##  Web archive:   http://www.erlbaum.net/pipermail/cgiapp/   ##
##  Wiki:          http://cgiapp.erlbaum.net/                 ##
##                                                            ##
################################################################

Reply via email to