首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >File::Find::Rule::LibMagic:保留未定义值的选项可以吗?

File::Find::Rule::LibMagic:保留未定义值的选项可以吗?
EN

Stack Overflow用户
提问于 2012-02-05 23:22:14
回答 2查看 160关注 0票数 2

是否可以保留未定义值的选项(在本例中为“maxdepth”)?

代码语言:javascript
复制
#!/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;

或者我应该这样写:

代码语言:javascript
复制
if ( defined $max_depth ) {
    @dbs = find( file => magic => 'SQLite*', maxdepth => $max_depth, in => $dir );
} else {
    @dbs = find( file => magic => 'SQLite*', in => $dir );
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-02-05 23:42:37

通过使用一个值为undef的变量将maxdepth设置为undef应该没有问题。Perl中的每个变量都以undef值开头。

更多细节

File::Find::Rule::LibMagic扩展了File::Find::RuleFile::Find::Rule中的find函数以:

代码语言:javascript
复制
sub find {
    my $object = __PACKAGE__->new();

new函数返回:

代码语言:javascript
复制
bless {
    rules    => [],
    subs     => {},
    iterator => [],
    extras   => {},
    maxdepth => undef,
    mindepth => undef,
}, $class;

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

票数 6
EN

Stack Overflow用户

发布于 2012-02-05 23:41:54

好的?它可能不会混淆File::Find::Rule

代码语言:javascript
复制
$ 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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9150395

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档