验证子进程的 Perl 参数 [英] Verifying Perl Arguments to a Subprocesses
问题描述
所以我是一个初级的 Perl 程序员.我已经使用它大约一个月了,但是仅在上周左右我才将它用于除系统管理员类型的任务之外的任何其他任务.在这样做时,我遇到了以下问题,
So I am a beginning Perl programmer. I have been using it for about a month, however only in the last week or so have I been using it for anything other than sysadmin type tasks. In doing so I ran into the following question,
Perl 子进程非常灵活,它们不会对您传入的参数施加很多/任何约束.如何强制参数数量和/或检查它们是否是引用、标量等?
Perl subprocesses are really flexible, and they don't impose many/any constraints on arguments you pass in. How is it possible to either enforce the number of arguments and/or check whether they're references, scalars etc etc?
澄清一下,这是我目前为 Perl 子进程所做的:
To clarify, here's what I currently do for Perl subprocesses:
sub mySub{
($a, $b) = @_;
continue and use these methods
}
但这不能保证 $a
和 $b
持有什么.无论如何要确保它们包含值,例如 $a
的引用和 $b
的标量?
But this provides no guarantees about what $a
and $b
hold. Is there anyway to make sure they contain values, say a reference for $a
and a scalar for $b
?
提前致谢.
当我说 $b
的标量时,我的意思是包含一个整数,而不是对其他数据结构的引用.
When I said scalar for $b
I mean containing an integer, and not being a reference to some other datastructure.
推荐答案
您可以使用 Params::Validate 模块,它提供了广泛的检查参数列表的可能性.
You can use the Params::Validate module, it provides wide possibilities of checking the argument list.
就你而言,就像
validate_pos(@_, { type => HASHREF | ARRAYREF }, { type => SCALAR })
会这样做(请注意,它没有单一类型的ref").当参数不匹配时它会死.
would do it (note that it doesn't have a single type for "ref"). It dies when the parameters don't match.
这篇关于验证子进程的 Perl 参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!