是否可以保留具有未定义值的选项(在本例中为“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 );
}
最佳答案
通过使用以undef
为值的变量将maxdepth
设置为undef
应该没有问题。 Perl 中的每个变量都以 undef
值开始。
更多详情
File::Find::Rule::LibMagic
延伸File::Find::Rule
. find
在 File::Find::Rule
中发挥作用开始于:
sub find {
my $object = __PACKAGE__->new();
new
函数返回:
bless {
rules => [],
subs => {},
iterator => [],
extras => {},
maxdepth => undef,
mindepth => undef,
}, $class;
请注意,maxdepth
默认设置为 undef
。
关于perl - File::Find::Rule::LibMagic:保留具有未定义值的选项是否可以?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9150395/