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


In reply to Re: C style multiple line comment removal by davido
in thread C style multiple line comment removal by prassi

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.