[英]Using Find::File::Rule to find Perl scripts and exclude .bak files
I am using Find::File::Rule
to find Perl scripts. 我正在使用Find::File::Rule
查找Perl脚本。 I want to exclude certain files, like test files and backup files, but I cannot make it exclude *.bak
files if they start with a dot, for example ( p.pl
): 我想排除某些文件,例如测试文件和备份文件,但是如果它们以点开头,则不能排除*.bak
文件,例如( p.pl
):
use warnings;
use strict;
use Data::Dump;
use File::Find::Rule;
open(my $fh,">",".c.bak");
print $fh "#! /usr/bin/env perl\n";
close($fh);
my $rule = File::Find::Rule->new;
$rule->or(
$rule->new->name('*.bak')->prune->discard,
$rule->new->grep( qr/^#!.*perl\s*$/, [ sub { 1 } ] )
);
my @files=$rule->in(".");
dd @files;
This gives output: 这给出了输出:
("p.pl", ".c.bak")
whereas expected output should be: 而预期输出应为:
"p.pl"
The problem here is that files prefixed with a dot aren't matched by the '*.bak' quantifier, because they're 'hidden' files. 这里的问题是,前缀为点的文件与'* .bak'量词不匹配,因为它们是“隐藏”文件。
If you chdir
to your directory and do echo *
or echo *.bak
you won't see the file there either. 如果您将chdir
转到目录并执行echo *
或echo *.bak
,则也不会在该文件中看到该文件。 So effectively - that rule isn't matching, because it's a hidden file. 如此有效-该规则不匹配,因为它是一个隐藏文件。
Solutions would be: 解决方案是:
Something like: 就像是:
$rule->new->name(qr/\.bak$/)->prune->discard,
You just have to add another filter rule for the hidden backup files: 您只需要为隐藏的备份文件添加另一个过滤规则:
#!/usr/bin/perl
use warnings;
use strict;
use Data::Dump;
use File::Find::Rule;
open(my $fh,">",".c.bak");
print $fh "#! /usr/bin/env perl\n";
close($fh);
my $rule = File::Find::Rule->new;
$rule->or(
$rule->new->name('*.bak')->prune->discard,
$rule->new->name('.*.bak')->prune->discard, # <== hidden backups
$rule->new->grep( qr/^#!.*perl\s*$/, [ sub { 1 } ] )
);
my @files=$rule->in(".");
dd @files;
Notice the starting .
注意开始.
in the pattern. 在模式中。 This script will produce: 该脚本将产生:
"p.pl"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.