http://qs321.pair.com?node_id=947580

suaveant has asked for the wisdom of the Perl Monks concerning the following question:

I am looking for a module to assign pre and post assertions to subroutines in development and pre-production but be able to be skipped on production, and also to double as documentation (or at least facilitate documentation) of inputs and outputs.

I've found Sub::Assert which looks promising except for the documenting part (which maybe I could work in somehow) and Sub::Spec which looks interesting but not at all what I want.

Anyone know of anything useful?

                - Ant
                - Some of my best work - (1 2 3)

  • Comment on Pre/post subroutine assertions and documentation module?

Replies are listed 'Best First'.
Re: Pre/post subroutine assertions and documentation module?
by educated_foo (Vicar) on Jan 12, 2012 at 17:29 UTC
    Smart::Comments may help -- what do you mean by "documentation"? POD? Comments? Code examples?
      I want something that verifies incoming parameters (usually a hash ref, so sub-keys) are correct, and also is designed in such a way that a short blurb can be added for viewing/extracting that describes the inputs.

      It seems an obvious conjoining, if you are verifying inputs why not also document them, or vice versa. I may end up using something like Smart::Comments/Getopt::Euclid as a starting point for something of my own.

                      - Ant
                      - Some of my best work - (1 2 3)

        These subs... are they intended to be called as functions, or as methods? If the latter, then something like Method::Signatures, or MooseX::Declare might be an idea.

Re: Pre/post subroutine assertions and documentation module?
by TJPride (Pilgrim) on Jan 12, 2012 at 17:59 UTC
    Couldn't you just put in extra lines of code with a test flag?
    sub myFunc { my ($x, $y) = @_; ### Assertion if ($::test) { ### Do something } ### Rest of sub }

    Might help if you could give an example of what a simple sub might do with and without an assertion.

      If you do this, you want to make $::test a constant, so it can be optimized away when you're done testing, e.g.
      use constant TEST => $ENV{TEST}; if (TEST) { ... }
      I can think of a number of ways I could do it in a custom manner, I'm just making sure I'm not reinventing anything.

                      - Ant
                      - Some of my best work - (1 2 3)

Re: Pre/post subroutine assertions and documentation module?
by Khen1950fx (Canon) on Jan 12, 2012 at 19:15 UTC