Home: Perl Programming Help: Regular Expressions:
Extracting comments from C code.



paralacsa
New User

Dec 3, 2008, 3:24 PM


Views: 6938
Extracting comments from C code.

Hi,
first of all I know that there's such a topic in FAQ, I've read it but I didn't get answers my questions. Here is my short code:

Code
use warnings; 

open(F1,"C.txt")|| die "Couldn't open file";
$/ = undef;
$_=<F1>;



@matches = m/(\/\*([^\*]|[\r\n]|(\*+([^\*\/]|[\r\n])))*\*+\/)/gm;
print ("@matches\n\n\n");


I have multi-lined string with C code stored in $_, I'd like to get all comments from this string but when I compile file compiler is throwing such a warnings even though the output is right:

Use of uninitialized value $matches[6] in join or string at kom.pl line 17, <F1>
chunk 1.
Use of uninitialized value $matches[7] in join or string at kom.pl line 17, <F1>
chunk 1.
Use of uninitialized value $matches[10] in join or string at kom.pl line 17, <F1
> chunk 1.
Use of uninitialized value $matches[11] in join or string at kom.pl line 17, <F1
> chunk 1.

This making me nervous. Where's the problem. This approach of getting all matches from regexp was presented in some Perl book.

Approach like this is fine and works properly on the same input data:

Code
while (m/(\/\*([^*]|[\r\n]|(\*+([^*\/]|[\r\n])))*\*+\/)/gm) { 
print("$1\n");



winfinit
User

Dec 10, 2008, 5:51 AM


Views: 6874
Re: [paralacsa] Extracting comments from C code.

are you trying to push all of the comments into an array?