如何用 Perl 进行契约设计?
我正在研究在我们的 Perl 项目中使用 DbC,并且我正在尝试找到验证源中合同的最佳方法(例如检查前置/后置条件、不变量等)
Class::Contract 由 Damian Conway 编写,现在由 C 维护。加勒特·戈贝尔(Garret Goebel),但看起来它已经八年多没有被碰过了。
看起来我想使用的是 Moose,因为它似乎可能提供可以的功能用于 DbC,但我想知道是否有人有任何关于如何进行此操作的资源(文章等),或者是否有任何我无法找到的有用模块。
有人用 Perl 做 DbC 吗? 我应该“跳进”Moose 看看我能为我做些什么吗?
I'm investigating using DbC in our Perl projects, and I'm trying to find the best way to verify contracts in the source (e.g. checking pre/post conditions, invariants, etc.)
Class::Contract was written by Damian Conway and is now maintained by C. Garret Goebel, but it looks like it hasn't been touched in over 8 years.
It looks like what I want to use is Moose, as it seems as though it might offer functionality that could be used for DbC, but I was wondering if anyone had any resources (articles, etc.) on how to go about this, or if there are any helpful modules out there that I haven't been able to find.
Is anyone doing DbC with Perl? Should I just "jump in" to Moose and see what I can get it to do for me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Moose 为您提供了很多执行 DbC 的工具(如果不是全部糖分的话)。 具体来说,您可以使用 before、after 和 around 方法挂钩 (这里 一些示例)来执行您可能想要对参数和返回值进行的任何断言。
作为“推出自己的 DbC”的替代方案,您可以使用类似 MooseX 的模块: :Method::Signatures 或 MooseX::Method 来处理验证传递给子例程的参数。 然而,这些模块不处理 DbC 通常提供的“后”或“不变”验证。
编辑:受这个问题的启发,我一起破解了MooseX: :Contract 并将其上传到 CPAN。 我很想获得有关 API 的反馈,因为我从未真正亲自使用过 DbC。
Moose gives you a lot of the tools (if not all the sugar) to do DbC. Specifically, you can use the before, after and around method hooks (here's some examples) to perform whatever assertions you might want to make on arguments and return values.
As an alternative to "roll your own DbC" you could use a module like MooseX::Method::Signatures or MooseX::Method to take care of validating parameters passed to a subroutine. These modules don't handle the "post" or "invariant" validations that DbC typically provides, however.
EDIT: Motivated by this question, I've hacked together MooseX::Contract and uploaded it to the CPAN. I'd be curious to get feedback on the API as I've never really used DbC first-hand.
Moose 是 Perl 的一个优秀的 oo 系统,我衷心向任何使用 Perl 编码对象的人推荐它。 您可以为类成员指定“子类型”,这些成员在由访问器或构造函数设置时将强制执行(相同的系统可以与 Moose::Methods 包用于函数)。 如果您要编码多个衬垫,请使用 Moose;
至于 DbC,嗯,可能不是最适合 perl5。 对于一门几乎无法提供保证的语言来说,这会很困难。 就我个人而言,在很多动态语言中,尤其是 Perl,我倾向于采用 DRY 和测试驱动开发的指导思想。
Moose is an excellent oo system for perl, and I heartily recommend it for anyone coding objects in perl. You can specify "subtypes" for your class members that will be enforced when set by accessors or constructors (the same system can be used with the Moose::Methods package for functions). If you are coding more than one liners, use Moose;
As for doing DbC, well, might not be the best fit for perl5. It's going to be hard in a language that offers you very few guarantees. Personally, in a lot of dynamic languages, but especially perl, I tend to make my guiding philosophy DRY, and test-driven development.
我还推荐使用 Moose。
但是,作为“替代方案”,请查看 Sub::Contract 。
引用作者的话......
I would also recommend using Moose.
However as an "alternative" take a look at Sub::Contract.
To quote the author....
如果您不需要类不变量,我发现以下 Perl Hacks 书籍推荐对于某些程序来说是一个很好的解决方案。 请参阅
Smart::Comments
。If you don't need class invariants, I've found the following Perl Hacks book recommendation to be a good solution for some programs. See
Smart::Comments
.