views:

416

answers:

4

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?

+2  A: 

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.

Todd Gardner
Thanks, Todd. I had a sneaking feeling that might be the prevailing point of view, but since Damian had, at one point, decided it was worth writing a module, I thought that maybe it could work. I guess only they know if they abandoned it because it wasn't worthwhile.
Adam Bellaire
+6  A: 

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 an example) 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.

Brian Phillips
That's great, it looks like it handles the cases I was asking for. Unfortunately, I too am new to the DbC paradigm so I don't know that I'd be the best person to judge your API. :)
Adam Bellaire
+2  A: 

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.

/I3az/

draegtun
A: 

If you don't need class invariants, I've found the following Perl Hacks book recommendation to be a good solution for some programs -- Smart::Comments, http://search.cpan.org/perldoc?Smart::Comments#Checks_and_Assertions_via_Comments

Mark