在我们的代码库中,我们经常使用此构造来处理程序的选项:
Readonly my $ARGS => Getopt::Declare->new(
join( "\n",
'[strict]',
"--option1 <some-option-description> [required]",
)
) or exit(1);
问题是,当违反严格性时Getopt::Declare->new(...)
,它将返回undef
,通常可以这样undef
做,只是将其存储在Readonly变量中,该变量在布尔上下文中评估为true。
一个明显的解决方案是删除Readonly
和代码将按预期运行;但是我真的不喜欢这个解决方案,因为改变它可以$ARGS
被修改。
my $ARGS => Getopt::Declare->new(
join( "\n",
'[strict]',
"--option1 <some-option-description> [required]",
)
) or exit(1);
另一个解决方案是将Getopt::Declare->new(...) or exit(1)
in包裹起来,以便在分配给Readonly变量之前对其进行求值。
Readonly my $ARGS => (
Getopt::Declare->new(
join( "\n",
'[strict]',
"--option1 <some-option-description> [required]",
)
) or exit(1);
)
我猜一个替代方案是使用或的更高优先级版本||
,该版本比绑定更强,=>
但我不确定它是否可读。
Readonly my $ARGS => Getopt::Declare->new(
join( "\n",
'[strict]',
"--option1 <some-option-description> [required]",
)
) || exit(1);
Readonly my $ARGS => Getopt::Declare->new(
# ...
);
$ARGS or exit(1);
也应该工作。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句