in reply to C style multiple line comment removal

Ok... You're unnecessarily (and possibly mistakenly) reading from the input filehandle twice; once in line mode, and once in slurp mode. The first read is removing a single line from the top of the C code, but if you're trying to be that specific in how you remove the comment you may as well just open up an editor and clip the first line.

I think you're looking for a general solution that will remove C style comments from source code.

I didn't even try to unravel the regular expression you posted. (Update: It looks like the one in perlfaq6.) The problem of removing C style comments from source code can be simpler than that (with a possible killer exception discussed below), because C style comments have the oft-hated feature of not being able to be nested. In other words, you can't do this with C style comments:

/* This is a comment /* and this is a nested one */ */

...because C sees it as:

/* This is a comment /* and this is a nested one */

...with a trailing unmatched */, which is a syntax error. That's unfortunate for people who like to comment out blocks of code that may also contain comments, without resorting to preprocessor directives. But for you, today, it's a great feature. It means you don't have to keep track of state. You don't need to push comment brackets onto a stack, parse parse parse, pop them off as closing brackets are seen, and so on. Today is a good day for you.

Consider the following code snippet:

local $/ = undef; $_ = <DATA>; s{ /\* .*? \*/ }{}gsmx; print; __DATA__ /*This is the file which is used for the developing the code for multiple IO operation Author : Grey*/int myfunc();/* remove */ void main(); /*This is another comment */ void main() { int a, b; int d; //This is the temp copy buffer. a = 10; b = 11;//This is the temp value assigned to the variables c = a+b; d = c+1; /*I am copying this with a increment*/ }

This will produce the following output:

int myfunc(); void main(); void main() { int a, b; int d; //This is the temp copy buffer. a = 10; b = 11;//This is the temp value assigned to the variables c = a+b; d = c+1; }

It's a lucky day when non-greedy matching actually works like you want. There's still a problem though, so the day may not be so lucky after all. The problem is explained in this article: A /* token that is commented out with a // style comment will still be picked up by the regular expression, resulting in whatever comes after being dropped until another */ is found. That may be a problem for you in your source code, or it may not, but it's kind of a risk.

Unfortunately, that same flaw exists in Regexp::Common:

use Regexp::Common qw( comment ); local $/ = undef; $_ = <DATA>; s/$RE{comment}{C}//gs; print; __DATA__ /*This is the file which is used for the developing the code for multiple IO operation Author : Grey*/int myfunc();/* remove */ void main(); /*This is another comment */ void main() { int a, b; int d; //This is the temp copy buffer. /* This breaks regex solutions a = 10; b = 11;//This is the temp value assigned to the variables c = a+b; d = c+1; /*I am copying this with a increment*/ }

If your source contains C comments in C++ style comments, you've got a problem that is better served with a proper lexer and parser.

Now please help yourself out by reading perlintro.

Update: The regexp you posted skirts the issue of having a /* embedded within a // (C++ style) comment by just removing both the C style, and the C++ style comments. If it was your intent to remove both C style and C++ style, I wish I had known so I could have saved myself some research. ...at least I found a bug in Regexp::Common that I will report. I wish I could provide a patch at the same time but I haven't figured out a pure regex solution yet.


Dave