in reply to Re: Perl project lines of code "analyzer"
in thread Perl project lines of code "analyzer"

I have already noted that as the only gotcha, in the description of the code. I also noted there that the mechanics are in place so one can add any multi-line filtering desired, since each file is first slurped. Thank you for commenting though!
  • Comment on Re^2: Perl project lines of code "analyzer"

Replies are listed 'Best First'.
Re^3: Perl project lines of code "analyzer"
by jdhedden (Deacon) on Dec 01, 2005 at 14:48 UTC
    Opps, missed that. I got too anxious trying out the code. Anyways, I created my own version that includes POD counts. Thanks for your contribution, psychotic.
    #!/usr/bin/perl use strict; use warnings; use File::Find; MAIN: { # Top directory to look through my $dir = (@ARGV) ? shift : '.'; # Find Perl files in directory my @files; find sub { my $fname = $_; if (-f $fname) { foreach my $type (qw(pl pm)) { my (undef, $ext) = split (/\./, $fname); if (defined($ext) && $ext eq $type) { push(@files, $File::Find::name); } } } }, $dir; if (! @files) { print("No Perl files found in '$dir'\n"); exit(1); } # Header print(" Code Cmts POD Total File\n"); print("=" x 60, "\n"); # Process files and generate totals my ($tloc, $tcom, $ttot, $tpod) = (0,0,0,0); foreach my $file (@files) { my ($loc, $com, $pod) = (0,0,0); # Slurp file my $contents = do { local (*ARGV, $/) = [$file] and <> }; my @lines = split(/$\//, $contents); # Process lines while (@lines) { my $line = shift(@lines); # Ignore blank lines if ($line =~ /^\s*$/) { next; } # Count POD if ($line =~ /^=[a-z]/) { $pod++; while (@lines) { my $pline = shift(@lines); if ($pline =~ /^\s*$/) { next; # Ignore blank lines } $pod++; if ($pline =~ /^=cut/) { last; # Done with POD block } } } # Comments and code elsif ($line =~ /^\s*\#/) { $com++; } else { $loc++; } } # File results my $tot = $loc + $com + $pod; printf("% 5d % 5d % 5d % 5d %s\n", $loc, $com, $pod, $tot, $fi +le); # Totals $tloc += $loc; $tcom += $com; $tpod += $pod; $ttot += $tot; } # Print grand totals print("=" x 60, "\n"); printf("% 5d % 5d % 5d % 5d %s\n", $tloc, $tcom, $tpod, $ttot, '-- + Summary of all files'); } exit(0); # EOF
    UPDATE: Added correction suggested by psychotic.

    Remember: There's always one more bug.
      Excellent! Nice doing business with you, sir. I initially figured that "parsing" POD would clutter things up significantly, but apparently that stemmed from my partial misunderstanding of the it's nature. Thanks for not only clearing that up, but also coming up with a solution.

      I noticed you prefered the obvious '.' instead of cwd(). Is there a specific reason for doing so? I tend to do that myself when writing up something in a hurry, but i always thought cwd() as being the "proper" way. Anyhow, that isn't related to the current thread.

        cwd() caused the lines to get too long and wrap on my terminal. Besides, I already knows the path to '.' from my shell prompt. If I need the full path, I can always invoke it with: sloc `pwd`
      Using preprocess option can get rid off of unwanted Subversion or CVS directory
      find {wanted=>sub { my $fname = $_; if (-f $fname) { foreach my $type (qw(pl pm)) { my (undef, $ext) = split (/\./, $fname); if (defined($ext) && $ext eq $type) { push(@files, $File::Find::name); } } } }, preprocess=>sub {return grep{!/^.svn|CVS$/}@_}}, $dir;
      jdhedden,
      Interesting. Unfortunately it includes anything after
      __END__ or __DATA__
      as potential code. While it could potentially be POD, it likely shouldn't be counted as code. If I can't find something that behaves correctly here I might get motivated and update this with a patch.

      Cheers - L~R