#!/usr/local/bin/perl -w use strict; my %open_comment = ( '.c' => '/*', '.h' => '/*', '.pas' => '{', ); my %close_comment = ( '.c' => '*/', '.h' => '*/', '.pas' => '}', ); my %line_comment = ( '.com' => '!', '.opt' => '!', '.mms' => '#', '.mar' => ';', '.pl' => '#', '.pm' => '#', '.sh' => '#', ); my $lincnt = 0; my $meatcnt = 0; my $tokencnt = 0; my $filcnt = 0; my $g_lincnt = 0; my $g_meatcnt = 0; my $g_tokencnt = 0; foreach my $filnam (@ARGV) { next if !open INFILE,$filnam; my $incomment = 0; my $inpod = 0; # Parse out filename extension. Needs to be portable # Note: VMS can have ;version on the end of the file name my $ext = $1 if $filnam =~ /(\.[^.;]+)(;|$)/; next if !$ext; # Get comment delimiters for extension my $lc = $line_comment{$ext}; my $oc = $open_comment{$ext}; my $cc = $close_comment{$ext}; while (defined ($_ = <INFILE>)) { # Line comment if ($lc) { s/\Q$lc\E.*//; } # Begin and end comment e.g. /* ... */ if ($oc) { $incomment = 0 if $incomment && s/.*?\Q$cc//; s/.*// if $incomment; s/\Q$oc\E.*?\Q$cc//g; $incomment++ if s/\Q$oc\E.*//; } # POD section $inpod++ if /^=/; $inpod = 0 if /^=cut/; s/.*// if $inpod; s/^=cut//; # Warning, does not handle token count for multi line # strings, \', regexs or <<FOO # but token count is only a rough guide s/(["'`]).*?\1/ string /g; s/^\s+//; my @tokens = /\w+\s*|.\s*/g; $lincnt++; $g_lincnt++; $meatcnt++,$g_meatcnt++ if $_; $g_tokencnt += @tokens; $tokencnt += @tokens; } close INFILE; printf("%s %d lines, %d non-commentary, %d tokens = %2.2f per line +\n", $filnam,$lincnt,$meatcnt,$tokencnt, $meatcnt ? ($tokencnt/$meatcnt) : 0); $lincnt = 0; $meatcnt = 0; $tokencnt = 0; $filcnt++; } printf("\nGrand total %d files, %d lines, %d non-commentary, %d tokens + = %2.2f per line\n", $filcnt,$g_lincnt,$g_meatcnt,$g_tokencnt, $g_meatcnt ? ($g_tokencnt/$g_meatcnt) : 0);

In reply to Yet another code counter by rinceWind

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.