in reply to Regular experssion for starts with doesn't work

You aren't exactly matching comments. It looks like you are matching lines that start with "//", "/*", or "*". Assuming that is good enough for your purposes, this might work as a quick and dirty fix:

if ($line =~ m!^\s*(?://|/?\*)!)
That allows leading space for some robustness, changes the delimiter for readability, combines the regular expressions, and matches against $line, which is presumably what you want.

The line

if (length($line) != 0)
would probably be better expressed as simply if (length($line)) too.

As diotalevi already mentioned, however, you should really be parsing (at least minimally) to find comments successfully in every case and without false positives. If you want to go that route and build a truly useful tool, it might help you to look into JPL1 (Java/Perl Lingo) source code. Maybe. I don't really know as I've pretty successfully avoided Java since 1.0 and therefore have never needed JPL.

1It seems jens posted a question about JPL earlier. I must have seen his post in the newest nodes and been reminded of JPL's existence. :-)

-sauoq
"My two cents aren't worth a dime.";