Class::Contract

Class::Contract - Design-by-Contract OO in Perl.
Download

Class::Contract Ranking & Summary

Advertisement

  • Rating:
  • License:
  • Perl Artistic License
  • Price:
  • FREE
  • Publisher Name:
  • Damian Conway
  • Publisher web site:
  • http://search.cpan.org/~dconway/

Class::Contract Tags


Class::Contract Description

Class::Contract - Design-by-Contract OO in Perl. Class::Contract - Design-by-Contract OO in Perl.SYNOPSIS package ClassName use Class::Contract; contract { inherits 'BaseClass'; invar { ... }; attr 'data1'; attr 'data2' => HASH; class attr 'shared' => SCALAR; ctor 'new'; method 'methodname'; pre { ... }; failmsg 'Error message'; post { ... }; failmsg 'Error message'; impl { ... }; method 'nextmethod'; impl { ... }; class method 'sharedmeth'; impl { ... }; # etc. };BackgroundDesign-by-contract is a software engineering technique in which each module of a software system specifies explicitly what input (or data or arguments) it requires, and what output (or information or results) it guarantees to produce in response.These specifications form the "clauses" of a contract between a module and the client software that uses it. If the client software abides by the input requirements, the module guarantees to produce the correct output. Hence by verifying these clauses at each interaction with a module, the overall behaviour of the system can be confidently predicted.Design-by-contract reinforces the benefits of modular design techniques by inserting explicit compile-time or run-time checks on a contract. These checks are most often found in object-oriented languages and are typically implemented as pre-conditions and post-conditions on methods, and invariants on classes.Note that these features differ from simple verification statements such as the C assert statement. Conditions and invariants are properties of a class, and are inherited by derived classes.An additional capacity that is often provided in design-by-contract systems is the ability to selectively disable checking in production code. This allows the contractual testing to be carried out during implementation, without impinging on the performance of the final system.Adding design-by-contract to PerlThe Class::Contract module provides a framework for specifying methods and attributes for a class (much like the existing class definition modules Class::Struct, Class::MethodMaker, and Class::Generate). Class::Contract allows both per-object and per-class methods and attributes to be defined. Attributes may be scalar-, array-, hash-, or object-based.Class::Contract differs from other class-specification modules (except Class::Generate) in that it also provides the ability to specify invariant conditions on classes, and pre- and post-conditions on methods and attributes. All of these clauses are fully inheritable, and may be selectively disabled. It differs from all other modules in that it has a cleaner, simpler specification syntax, and -- more importantly -- it enforces encapsulation of object attributes, thereby ensuring that the class contract cannot be subverted.Defining classesClass::Contract provides an explicit syntax for defining the attributes, methods, and constructors of a class. The class itself is defined using the contract subroutine. contract takes a single argument -- a subroutine reference or a block. That block is executed once and the results used to construct and install the various components of the class in the current package: package Queue; contract { # specification of class Queue attributes and methods here }; Requirements: · Perl


Class::Contract Related Software