This program takes one argument which is the directory to search (like c:\Devel). It then counts and adds up all the lines in every .pm or .pl file in that directory and then recursively in every subdirectory. Simple but useful. And I was pleased with my implementation. I run this on windows but it should work fine in Linux as well.
use strict; #counter for the number of lines we'll get my $count = 0; #first call our subroutine with program argument count_dir($ARGV[0]); #our recursive subroutine that counts .pl|.pm file-lines sub count_dir($) { my ($dir) = @_; #try to open directory passed or return if fail unless (opendir(DIR, $dir)) { print "\tCouldn't open dir: $dir\n"; return; } #get all files in directory my @files = readdir(DIR); #go through each file in dir foreach my $file (@files) { #move on if this file is a symbol next if (length($file) < 3); #if file is a .pl|.pm then count it's lines if ($file =~ /\.pl|\.pm/) { open(FILE, "<", "$dir\\$file") or print "\tCouldn't open $file to analyze\n"; while (<FILE>) { $count++; } } #otherwise if we can make this a directory lets dive into it elsif (opendir(TDIR, "$dir\\$file")) { count_dir("$dir\\$file"); } } } #print out our results print "\nLines of Perl code in directory:\t$count\n";

In reply to Perl LineCount by Fideist11

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.