How can I ignore C comments when I process a C source file with Perl?

Posted by YoDar on Stack Overflow See other posts from Stack Overflow or by YoDar
Published on 2010-04-14T08:01:05Z Indexed on 2010/04/16 4:33 UTC
Read the original article Hit count: 242

Filed under:

I'm running a code that read files, do some parsing, but need to ignore all comments. There are good explanations how to conduct it, like the answer to How can I strip multiline C comments from a file using Perl?

$/ = undef;
$_ = <>;
s#/\*[^*]*\*+([^/*][^*]*\*+)*/|("(\\.|[^"\\])*"|'(\\.|[^'\\])*'|.[^/"'\\]*)#defined $2 ? $2 : ""#gse;
print;

My first problem is that after run this line $/ = undef; my code doesn't work properly. Actually, I don't know what it does. But if I could turn it back after ignoring all comments it will be helpful.

In general, What is the useful way to ignore all comments without changing the rest of the code?

© Stack Overflow or respective owner

Related posts about perl