如何用 Perl 进行契约设计?

发布于 2024-07-11 08:30:00 字数 499 浏览 6 评论 0原文

我正在研究在我们的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

谈场末日恋爱 2024-07-18 08:30:00

Moose 为您提供了很多执行 DbC 的工具(如果不是全部糖分的话)。 具体来说,您可以使用 before、after 和 around 方法挂钩 (这里 一些示例)来执行您可能想要对参数和返回值进行的任何断言。

作为“推出自己的 DbC”的替代方案,您可以使用类似 MooseX 的模块: :Method::SignaturesMooseX::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.

天邊彩虹 2024-07-18 08:30:00

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.

寻梦旅人 2024-07-18 08:30:00

我还推荐使用 Moose。

但是,作为“替代方案”,请查看 Sub::Contract

引用作者的话......

Sub::Contract 提供了一种实用的方法来通过 Perl 中的契约范式实现部分编程。

Sub::Contract 不是一个按合约设计的框架。

Sub::Contract 旨在使约束子例程输入参数和返回值变得非常容易,以便在运行时模拟强类型。

I would also recommend using Moose.

However as an "alternative" take a look at Sub::Contract.

To quote the author....

Sub::Contract offers a pragmatic way to implement parts of the programming by contract paradigm in Perl.

Sub::Contract is not a design-by-contract framework.

Sub::Contract aims at making it very easy to constrain subroutines input arguments and return values in order to emulate strong typing at runtime.

忘年祭陌 2024-07-18 08:30:00

如果您不需要类不变量,我发现以下 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文