Here's a different sort of attack you could take on this problem, assuming that you are only looking to count comments in perl code -- it involves two steps:
  1. Use the B::Deparse module to "canonicalize" a perl script -- this includes removing all comments
  2. Find the differences between the original and deparsed versions of the script, and isolate the diffs that involve comments in the original
This could be done with the following unix-style pipeline command (and I think you should be able to see how this could be formalized as a perl script):
perl -MO=Deparse some_script.pl | diff - some_script.pl | perl -ne 'print if /^> / and /[^\$]#/'
Note that the first step only works if the script does not contain syntax errors (it has to compile properly in order to be deparsed).

Of course, this is still not perfect -- the "deparsed" output may produce a lot of minor, irrelevant differences relative to the original script, and some of these might happen to contain "#" characters that are part of the code (not commentary); also, there may still be some challenges involved with trimming the final output down to just the commentary, if that is what you want to do. But this might help to eliminate some of the complexity, and may lead to some useful ideas.

Of course, another issue is that this does not find the pod-format documentation, if any happens to exist. But to extract that, you just need to run "perldoc some_script.pl".


In reply to Re: Bad Regex by graff
in thread Bad Regex by nofernandes

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.