MooseX-Contract
view release on metacpan or search on metacpan
lib/MooseX/Contract.pm view on Meta::CPAN
=head2 with_context
This helper method wraps a C<pre> and C<post> clause with closures that allow
a values to be compared between the two clauses. The C<SYNOPSIS> above shows
an example of how to use this functionality.
=cut
our @CARP_NOT = qw(Class::MOP::Method::Wrapped);
sub assert(&;$);
sub void() { return assert { shift; @_ == 0 } "too many values (expected 0)" }
sub invariant {
my $caller = shift;
my %packages = map { $_ => 1 } ($caller, grep { ! ref($_) } @_);
my @checks = map { (invar => $_) } grep { ref($_) eq 'CODE' } @_;
contract(
$caller,
[
map { $_->name }
lib/MooseX/Contract.pm view on Meta::CPAN
my $returns = shift;
if(ref($returns) eq 'ARRAY'){
return post => _make_type_validator( "returns", $returns);
} elsif(ref($returns) eq 'CODE') {
return post => $returns;
} else {
croak "invalid parameter to accepts: $returns";
}
}
sub check(&) { return @_ };
sub with_context {
my %args = @_;
if(!exists($args{pre}) || !exists($args{post})){
croak "both 'pre' and 'post' clauses must be specified when using context";
}
my $context;
return (
pre => sub { $context = $args{pre}->(@_) },
post => sub { $args{post}->( $context, @_ ) }
);
}
sub assert(&;$) {
my($code, $message) = @_;
$message ||= "assertion failed";
return sub {
$code->(@_) or croak $message;
}
}
=head1 PERFORMANCE
As the saying goes, you never get something for nothing. That is
( run in 0.326 second using v1.01-cache-2.11-cpan-49f99fa48dc )