我正在遍历所有文件以递归方式在某个目录树中获取所需的文件,只要我获取该文件我对它们执行某些操作但在执行操作之前我需要检查我是否对此文件执行了操作或者如果是,那么不要再做,否则继续:
但问题是,我无法找到检查条件的方法:(
这是我的代码:
use strict;
use warnings;
use autodie;
use File::Find 'find';
use File::Spec;
use Data::Printer;
my ( $root_path, $id ) = @ARGV;
our $anr_name;
opendir my ($dh), $root_path;
my @dir_list = grep -d, map File::Spec->catfile( $root_path, $_ ), grep { not /\A\.\.?\z/ } readdir $dh;
closedir $dh;
my $count;
for my $dir (@dir_list) {
find(
sub {
return unless /traces[_d]*/;
my $file = $_;
my @all_anr;
#print "$file\n\n";
my $file_name = $File::Find::name;
open( my $fh, "<", $file ) or die "cannot open file:$!\n";
my @all_lines = <$fh>;
my $i = 0;
foreach my $check (@all_lines) {
if ( $i < 10 ) {
if ( $check =~ /Cmd line\:\s+com\.android\..*/ ) {
$anr_name = $check;
my @temp = split( ':', $anr_name );
$anr_name = $temp[1];
push( @all_anr, $anr_name );
#print "ANR :$anr_name\n";
my $chk = check_for_dublicate_anr(@all_anr);
if ( $chk eq "1" ) {
# performed some action
}
}
$i++;
} else {
close($fh);
last;
}
}
},
$dir
);
}
sub check_for_dublicate_anr {
my @anrname = @_;
my %uniqueAnr = ();
foreach my $item (@anrname) {
unless ( $uniqueAnr{$item} ) {
# if we get here, we have not seen it before
$uniqueAnr{$item} = 1;
return 1;
}
}
}
答案 0 :(得分:1)
您可以使用Path::Class
和Path::Class::Rule
简化:
use 5.010;
use warnings;
use Path::Class;
use Path::Class::Rule;
my $root = ".";
my @dirs = grep { -d $_ } dir($root)->children();
my $iter = Path::Class::Rule->new->file->name(qr{traces[_d]*})->iter(@dirs);
my $seen;
while ( my $file = $iter->() ) {
for ( $file->slurp( chomp => 1 ) ) {
next unless /Cmd line:\s+(com\.android\.\S*)/;
do_things( $file, $1 ) unless $seen->{$1}++;
}
}
sub do_things {
my ( $file, $str ) = @_;
say "new $str in the $file";
}