NAME
Contract::Declare - Simple contract system for Perl interfaces
SYNOPSIS
package MyInterface;
use Contract::Declare;
use Standard::Types qw/Int Str/;
contract 'MyInterface' => interface {
method add_number => (Str), returns(Int);
method get_name => returns(Str);
};
package MyImpl;
sub new { bless {}, shift }
sub add_number { my ($self, $x) = @_; return $x + 1 }
sub get_name { return "example" }
# Using the interface
my $impl = MyImpl->new;
my $obj = MyInterface->new($impl);
say $obj->add_number(41); # 42
say $obj->get_name; # "example"
DESCRIPTION
Contract::Declare
allows you to define typed contracts (interfaces) in Perl, similar to lightweight design-by-contract.
This module is intended for lightweight validation, especially useful during development or for critical internal components.
FUNCTIONS
contract $package => interface { ... }
Defines a contract for the given package.
- $package
is the full package name as a string (e.g., 'MyInterface'). - interface { ... }
is a block where methods are declared.
interface { ... }
Marks the block containing method declarations.
method $name => @input_types, returns(@output_types)
Declares a method in the interface.
- $name
is the method name (string). - @input_types
is a list of type objects (must implement compiled_check
). - returns(@output_types)
defines expected return types.
Example:
method add => Int(), Int(), returns(Int());
returns(@types)
Specifies the expected return types for a method.
AUTHOR
Alexander Ponomarev <shootnix@gmail.com>
LICENSE
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.