是否可以使用条件项创建数组?
my @a = (1, ($condition) ? 2 : "no-op", 3);
这样"no-op"
是有效的功能,如果$condition
为假,那么我得到列表(1, 3)
,但如果$condition
为真,我得到{{1} }
背景
(1, 2, 3)
在某些情况下,我想加入
行use strict;
use warnings;
use File::Find::Rule;
my $rule = File::Find::Rule->new();
$rule->or(
$rule->new->name('*.cfg')->prune->discard,
$rule->directory->name("_private.d")->prune->discard,
$rule->new->name('*.t')->prune->discard,
$rule->new->name('*.bak')->prune->discard,
$rule->new->name('.*.bak')->prune->discard,
$rule->new->name('.#*')->prune->discard,
);
my @files = $rule->in(".");
在其他情况下,我不想排除目录$rule->directory->name("_private.d")->prune->discard
..
答案 0 :(得分:6)
您可以使用空列表()
跳过第二个元素
my @a = (1, ($condition ? 2 : ()), 3);
答案 1 :(得分:2)
一般来说,您可以使用
获得一些可读性增益my @a;
push @a, 1;
push @a, 2 if $condition;
push @a, 3;
在上下文中,那将是
my @rules;
push @rules, $rule->new->name('*.cfg')->prune->discard;
push @rules, $rule->directory->name("_private.d")->prune->discard;
push @rules, $rule->new->name('*.t')->prune->discard;
push @rules, $rule->new->name('*.bak')->prune->discard;
push @rules, $rule->new->name('.*.bak')->prune->discard;
push @rules, $rule->new->name('.#*')->prune->discard;
my $rule = File::Find::Rule->new()->or(@rules);
my @files = $rule->in(".");