The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

ContractClosure - An alternative implementation av Sub::Contract, using closures instead of dynamic compilation

SYNOPSIS

to control arguments passed in array style, and cache the results:

   use ContractClosure;

   contract('foo',
            in => { # define constraints on input arguments
                    count => 3,           # there must be exactly 3 input arguments
                    defined => 1,         # they must all be defined
                    check => [ undef,                              # no constraint on first argument
                               \&is_integer,                       # argument ok if is_integer(<arg>) returns true
                               sub { return (ref $_[0] eq ""); },  # ok if argument is a scalar
                             ],
                   },
            out => { # define constraints on output arguments
                     count => 2,
                   },
            cache => { size => 10000 },
           );

   sub foo {
       my($a,$b,$c) = @_;
       return (1,undef);
   }

and to control arguments passed in hash style:

   contract('foo',
            in => { count => 4,     # must be 4 input arguments
                                    # do not need to be all defined ('defined => 0' is the default)
                    check => { bib => \&is_year,      # if key 'bib' exists, its value must pass is_year()
                               bob => \&is_shortdate, # if key 'bob' exists, its value must pass is_shortdate()
                               bub => undef,          # no constraint on bub except that this key must exist (but can be 'undef')
                             },
                    optional => ['bib']               # allow key 'bib' to be non existing (but if it exists, it must pass 'is_year')
                  },
            out => { count => 1,
                     defined => 1,
                   },
           );

   sub foo {
       my(%hash) = @_;
       print "arg1: ".$hash{bib};
       print "arg2: ".$hash{bob};
       return $b;
   }