tanggang1740 发表于 2017-5-18 07:33:39

Perl 统计java代码行数

use warnings;
use strict;
use File::Find;
my $dir = "D:/workspace";
my @fileNames;
find(\&wanted,$dir);
sub wanted {
next unless $_ =~ /\.java$/;
push @fileNames , $File::Find::name;
}
my $count =0;
foreach my $filename (@fileNames){
open(FILE,$filename);
my $isCom = 0;
foreach my $line(<FILE>){
chomp $line;
$line =~ s{(.*)//.*}{$1\n}; #去掉注释//
$line =~ s{//.*}{\n}; #去掉以//开头的注释
$line =~ s/(.*)(\/\*.*\*\/)(.*)/$1 $3\n/; #用空格替换 /* */
next if $line =~ /^\s*$/; #去掉空白行
#去掉方法注释
$isCom = 1 if $line =~ /\S*\/\*/;
$isCom = 0 if $line =~ /\S*\*\//;
next if $isCom | $line =~ /\S*\*\//;
$count++;
}
}
print "Total lines:$count";
  同事要把java代码抽取出来,并且把注释和空行去掉于是写了一个perl的代码。心想那不再改成一个统计代码行数的程序。于是这段代码就产生了。
页: [1]
查看完整版本: Perl 统计java代码行数