Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
#!/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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (2)
As of 2024-04-18 23:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found