=pod =head1 NAME Attribute::Contract - Design by contract via Perl attributes =head1 SYNOPSIS package Interface; use AttributeContract; sub do_smth :ContractRequires(VALUE, @ANY?) :ContractEnsures(VALUE) { ...; } package Implementation; use base 'Interface'; use AttributeContract; sub do_smth { my $self = shift; my ($foo, @rest) = @_; return 1; } Implementaion->do_smth('hi', 'there'); # works Implementaion->do_smth(); # croaks! Implementaion->do_smth(sub {}); # croaks! =head1 DESCRIPTION L by using Perl attributes allows you to specify contract (L) for every method in your class. You can check incoming and outgoing values by specifying C and C attributes. It's the most useful for interfaces or abstract classes when you want to control whether your implementation follows the same interface and respects the Liskov substitution principle. This module does not check the actual types like C, C etc, but the Perl data types like scalars, arrays, hashes, references and so on. When the type does not match a L's C function will be called with detailed information like: 0 param(s) passed, at least 1 param(s) is required Why attributes? They feel and look natural and are applied during compile time. =head2 TYPES =head3 Scalar types =over =item * ANY Any scalar value is accepted. =item * VALUE Anything but not a reference. =item * REF A non blessed reference to anything. =item * REF(SCALAR) A reference to scalar. =item * REF(ARRAY) A reference to array. =item * REF(HASH) A reference to hash. =item * REF(Regexp) A reference to regular expression. =item * OBJECT A blessed reference. =item * OBJECT(ISA) A blessed reference with specified isa. =back =head3 Greedy types Types that eat all the elements. Can be specified at the end of the elements list for manual unpacking. C<@> stands for arrays and C<%> stands for hashes. All the scalar types can be used to specify the types of the elements. =over =item * @ARRAY @VALUE Which could mean something like: $object->method(1, 2, 3, 4); =item * %HASH %ANY Which could mean something like: $object->method(foo => 'bar', 'baz' => \123); It also checks that the number of elements is even. =back =head2 MULTIPLE VALUES Use C<,> when specifying several arguments. VALUE,ANY,REF(CODE),@VALUE Which could mean something like: $object->method($foo, \@array, sub { ... }, 1, 2, 3); =head2 ALTERNATIVES Use C<|> when specifying an alternative type. VALUE|REF(VALUE) Which could mean something like: $object->method($foo); or $object->method(\$foo); Alternatives can be really deep, like this one: @(REF(HASH|CODE)|VALUE) Which is an array of references to hash or code or simple value. =head2 OPTIONAL VALUES Use C when specifying an optional value. VALUE,VALUE? Which could mean something like: $object->method('foo'); or $object->method('foo', 'bar'); =head2 IMPLEMENTATION =head3 Inheritance By default all the contracts are inherited. Just don't forget to C L in the derived class. But if no methods are override then even C this module is not needed. =head3 Caching During the compile time for every contract a Perl subroutine is built and evaled. If the methods share the same contract they use the same checking code reference. This speeds up the checking and saves some memory. =head3 Error reporting Errors are as specific as possible. On error you will get a meaningful message and a stack trace. =head2 SWITCHING OFF You can switch off contract checking by specifying an environment variable C. =head1 DEVELOPMENT =head2 Repository http://github.com/vti/attribute-contract =head1 AUTHOR Viacheslav Tykhanovskyi, C. =head1 COPYRIGHT AND LICENSE Copyright (C) 2012, Viacheslav Tykhanovskyi This program is free software, you can redistribute it and/or modify it under the terms of the Artistic License version 2.0. =cut