perlfaq6 - stripping C++ style comments from a file
[email protected] ("William F. Mann") Fri, 22 Apr 2005 12:25:25 -0400
| Newsgroups | perl.perlfaq.workers |
|---|---|
| Organization | MIT Lincoln Laboratory |
| Message-ID | <[email protected]> |
This FAQ is needlessly obscure, even with Fred Curtis's modifications. Nobody needs a version which strips just C style comments. How about substituting the attached version which does both C and C++ style? (The C spec requires /*...*/ to act as white space. DOS format text needs the \r.) I am placing this code in the public domain. I think the perl4 version of the code should be omitted, or moved to the end if you want to make the "perl5 with /x is more legible" point. Bill Mann <[email protected]>
nocomment
(text/plain, 367 B)
#!/usr/bin/perl -w $/ = undef; # no line delimiter $_ = <>; # read entire file s! ((['"]) (?: \\. | .)*? \2) | # skip quoted strings /\* .*? \*/ | # delete C comments // [^\n\r]* # delete C++ comments ! $1 || ' ' # change comments to a single space !xseg; # ignore white space, treat as single line # evaluate result, repeat globally print;