哪些Perl模块可用于验证子例程参数? [英] What Perl modules are useful for validating subroutine arguments?

查看:170
本文介绍了哪些Perl模块可用于验证子例程参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个通用模块,以消除繁琐的验证子程序和方法参数的麻烦.我已经仔细研究了CPAN上的各种可能性: Params::Validate Params::Smart Getargs::Long ,还有其他一些.

I'm looking for a general-purpose module to take the drudgery out of validating subroutine and method arguments. I've scanned through various possibilities on CPAN: Params::Validate, Params::Smart, Getargs::Mixed, Getargs::Long, and a few others.

有关这些或其他模块的优缺点的任何信息将不胜感激.谢谢.

Any information regarding pros and cons of these or other modules would be appreciated. Thanks.

推荐答案

如果您开始使用 ,您会发现自己喜欢的 MooseX::Types .类型自动具有is_ $ type()和to_ $ type().这些是为了确保您输入的内容通过类型约束,或者使您的输入具有对类型的有效强制.即使对于这些类型的东西,我也更喜欢它们,因为您可以确保对象的状态具有上述类型,而无需支付额外费用.

If you start to use Moose, you'll find MooseX::Types to your liking. Types automatically have an is_$type(), and to_$type(). These are for making sure you input passes type constraints, or making your input has a valid coercion to the type. I like them better even for these types of things because you can ensure the state of your object has the said types at no additional cost.

use Moose;
has 'foo' => ( isa => MyType, is => ro );

sub _check_my_type {
  my ( $self, $type ) = @_;
  is_MyType( $type );
};

它可能缺少对深度/递归类型的支持,但是如果您在现代perl中使用此东西,则可能是做错了".而是使用具有自己的一致性检查的对象(例如上面的MyType),然后通过该对象.

It might be lacking some support for deep/recursive types, but if your using this stuff in modern perl you're probably "doing it wrong." Instead use an object that has its own consistency checks (like mine above with MyType), and just pass the object.

这篇关于哪些Perl模块可用于验证子例程参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆