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

标签 perl command-line-arguments optional-parameters file-find

是否可以保留具有未定义值的选项(在本例中为“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 . findFile::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/

相关文章:

带有附加配置文件的 perlcritic

regex - 如何让我的程序将第一行视为 'special'

perl - 如何使用 Pod::Weaver 在 Perl POD 中添加 HTML 部分

java - 如何通过命令行参数覆盖属性文件值?

sql-server-2008 - SQL Server 存储过程中的可选参数

perl - 取消引用哈希引用中的标量引用

c - 在 c 中使用 getopt 的选项

fortran - 这个 Fortran 程序有什么问题?

php - 选择的选项总是落入第一个值,莫名其妙

c++ - 在 C++ 中处理可选参数/默认值的最佳方法