代码: 全选
#!/bin/perl -w
#Remove comments in c/c++ source file
#Usage: removecomment.pl Infile [Outfile]
use strict;
if(@ARGV < 1)
{
print STDERR "removecomment.pl Infile [Outfile]\n";
exit;
}
my ($infile,$outfile) = @ARGV;
open (FI,"$infile") || die "Can not open file:$!";
my @lines = <FI>;
close (FI);
my $contents = join '',@lines;
$contents =~ s/((?<=\n)|^)[ \t]*\/\*.*?\*\/\n?|\/\*.*?\*\/|((?<=\n)|^)[ \t]*\/\/[^\n]*\n|\/\/[^\n]*//gs;
if(!(defined($outfile)))
{
print $contents;
}
else
{
open (FO,">$outfile") || die "Can not open file:$!";
print FO $contents;
close (FO);
}