File::Find::Rule::LibMagic:是否可以保留具有未定义值的选项



是否可以保留具有未定义值的选项(在本例中为"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 .File::Find::Rule中的find函数以以下内容开头:

sub find {
    my $object = __PACKAGE__->new();

new函数返回:

bless {
    rules    => [],
    subs     => {},
    iterator => [],
    extras   => {},
    maxdepth => undef,
    mindepth => undef,
}, $class;

请注意,默认情况下maxdepth设置为 undef

好吗?它可能不会混淆文件::查找::规则

$ 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.33

最新更新