I guessed you were using undef $/.
I found a working solution, though incomplete. I'm not sure what the last few lines of your regex do. I'm guessing it also finds all quoted strings in the text. You can modify this to work for that case too.
my $match;
my $line;
while(<F>) {
if(m{/\*} .. m{\*/}) {
## single line
if(m{(/\*.*?\*/)}) {
$match = $1;
$line = $.;
$hash{$line} = $match;
#print "Line $line: Got match '$match'\n";
} else {
## multi-line
if( m{(/\*.*)} ) {
## Initial line '/*'
$match = $1 . "\n";
$line = $.; # record this line number
} elsif( m{(.*\*/)} ) {
## Final line '*/'
$match .= $1;
$line = $.;
$hash{$line} = $match;
#print "Line $line: Got match '$match'\n";
$match = undef;
$line = undef;
} else {
# We are between lines, and have no /* or */
$match .= $_;
}
}
} elsif ( m{(//.*)\Z} ) {
$match = $1;
$line = $.;
$hash{$line} = $match;
#print "Line $line: Got match '$match'\n";
}
}
So, this stores the starting line number and comment string in a hash.
Hopefully this gives you an idea of how to process several lines. It's certainly not as nice as the quick and simple grep solution, but grep aggregates all the results together, so you can't get line numbers out of the results.
I did see this in the output of perldoc -f grep
'grep returns aliases into the original list'
so, perhaps you can somehow map back the results into the original array, but I have no idea how.
My ideal solution would be some variant of the grep statement you have, perhaps with a map statement instead of grep, and some nice (linenumber, string) pair returned.
Can't seem to find anything that works though.
Hope this helps,
~J