是否可以保留未定义值的选项(在本例中为“maxdepth”)?
#!/usr/bin/env perl
use warnings;
use 5.012;
use File::Find::Rule::LibMagic qw(find);
use Getopt::Long qw(GetOptions);
my $max_depth;
GetOptions ( 'max-depth=i' => \$max_depth );
my $dir = shift;
my @dbs = find( file => magic => 'SQLite*', maxdepth => $max_depth, in => $dir );
say for @dbs;或者我应该这样写:
if ( defined $max_depth ) {
@dbs = find( file => magic => 'SQLite*', maxdepth => $max_depth, in => $dir );
} else {
@dbs = find( file => magic => 'SQLite*', in => $dir );
}发布于 2012-02-05 23:42:37
通过使用一个值为undef的变量将maxdepth设置为undef应该没有问题。Perl中的每个变量都以undef值开头。
更多细节
File::Find::Rule::LibMagic扩展了File::Find::Rule。File::Find::Rule中的find函数以:
sub find {
my $object = __PACKAGE__->new();new函数返回:
bless {
rules => [],
subs => {},
iterator => [],
extras => {},
maxdepth => undef,
mindepth => undef,
}, $class;请注意,默认情况下,maxdepth设置为undef。
发布于 2012-02-05 23:41:54
好的?它可能不会混淆File::Find::Rule
$ perl -MFile::Find::Rule -le " print for File::Find::Rule->maxdepth(undef)->in( q/tope/ ) "
tope
tope/a
tope/b
tope/c
tope/c/0
tope/c/1
tope/c/2
$ perl -MFile::Find::Rule -le " print for File::Find::Rule->maxdepth(1)->in( q/tope/ ) "
tope
tope/a
tope/b
tope/c
$ perl -MFile::Find::Rule -le " print for File::Find::Rule->maxdepth(-1)->in( q/tope/ ) "
tope
$ perl -MFile::Find::Rule -le " print for File::Find::Rule->maxdepth(2)->in( q/tope/ ) "
tope
tope/a
tope/b
tope/c
tope/c/0
tope/c/1
tope/c/2
$ pmvers File::Find::Rule
0.33https://stackoverflow.com/questions/9150395
复制相似问题