如何验证枚举类型作为 Perl 子例程参数?
构建 Perl 有枚举类型吗?,如何执行动态类型检查(或静态类型检查,如果 use strict 能够这样做)我的子例程参数是否获得了正确的枚举类型?
package Phone::Type;
use constant {
HOME => 'Home',
WORK => 'Work',
};
package main;
sub fun
{
my ($my_phone_type_enum) = @_;
# How to check my_phone_type_enum, is either Phone::Type->HOME or Phone::Type->WORK or ... but not 'Dog' or 'Cat'?
}
fun(Phone::Type->HOME); # valid
fun(Phone::Type->WORK); # valid
fun('DOG'); # run-time or compile time error
Building off Does Perl have an enumeration type?, how can I perform dynamic type checking (or static type checking if use strict is able to do so) that my subroutine argument is getting the right type of enum?
package Phone::Type;
use constant {
HOME => 'Home',
WORK => 'Work',
};
package main;
sub fun
{
my ($my_phone_type_enum) = @_;
# How to check my_phone_type_enum, is either Phone::Type->HOME or Phone::Type->WORK or ... but not 'Dog' or 'Cat'?
}
fun(Phone::Type->HOME); # valid
fun(Phone::Type->WORK); # valid
fun('DOG'); # run-time or compile time error
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是一种方法:
Here is one way:
我建议您使用只读(如引用问题中所建议的那样)而不是常量。 我建议使用两种可能的方法(取决于您使用的是 Perl 5.10 还是 5.8)。
最初,相同的代码:
Perl 5.10:
Perl 5.8:
CPAN 上有一个模块,它允许您对参数类型和值进行大量控制,但我一生都无法记住它。 也许其他人可以。
I would suggest that you use Readonly (as suggested in the referenced question) rather than constant. I'd suggest on of two possible approaches (depending on if you are using Perl 5.10 or 5.8).
Initially, the same code:
Perl 5.10:
Perl 5.8:
There is a module on CPAN which will allow you to have a great deal of control over argument types and values but I can't for the life of me remember it. Perhaps someone else can.
(BEGIN 是在编译时设置 $types 所必需的,以便它可用于 use 语句。)
在 Perl 中,对这样的事情放松是相当常见的; 假设函数在您期望数字的地方传递了数字,等等。但是如果您想进行这种验证,您可能会对 参数::验证。
(The BEGIN is necessary to set $types at compile time so that it's available to the use statement.)
It's fairly common in Perl to be relaxed about things like this; to just assume that functions are passed numbers where you expect numbers, etc. But if you want to do this kind of validation, you may be interested in Params::Validate.