Class::Declare

Class::Declare is a Perl module created to declare classes with public, private and protected attributes and methods.
Download

Class::Declare Ranking & Summary

Advertisement

  • Rating:
  • License:
  • Perl Artistic License
  • Price:
  • FREE
  • Publisher Name:
  • Ian Brayshaw
  • Publisher web site:
  • http://search.cpan.org/~ibb/Class-Declare-0.08/Declare.pm

Class::Declare Tags


Class::Declare Description

Class::Declare is a Perl module created to declare classes with public, private and protected attributes and methods. Class::Declare is a Perl module created to declare classes with public, private and protected attributes and methods.SYNOPSIS package My::Class; use strict; use warnings; use base qw( Class::Declare ); __PACKAGE__->declare( public => { public_attr => 42 } , private => { private_attr => 'Foo' } , protected => { protected_attr => 'Bar' } , class => { class_attr => } static => { static_attr => { a => 1 } } , restricted => { restricted_attr => 'string' } , abstract => 'abstract_attr' , friends => 'main::trustedsub' , new => , init => sub { # object initialisation ... 1; } , strict => 0 ); sub publicmethod { my $self = __PACKAGE__->public( shift ); ... } sub privatemethod { my $self = __PACKAGE__->private( shift ); ... } sub protectedmethod { my $self = __PACKAGE__->protected( shift ); ... } sub classmethod { my $self = __PACKAGE__->class( shift ); ... } sub staticmethod { my $self = __PACKAGE__->static( shift ); ... } sub restrictedmethod { my $self = __PACKAGE__->restricted( shift ); ... } sub abstractmethod { __PACKAGE__->abstract } 1; ... my $obj = My::Class->new( public_attr => 'fish' );MOTIVATIONOne of Perl's greatest strengths is it's flexible object model. You can turn anything (so long as it's a reference, or you can get a reference to it) into an object. This allows coders to choose the most appropriate implementation for each specific need, and still maintain a consistent object oriented approach.A common paradigm for implementing objects in Perl is to use a blessed hash reference, where the keys of the hash represent attributes of the class. This approach is simple, relatively quick, and trivial to extend, but it's not very secure. Since we return a reference to the hash directly to the user they can alter hash values without using the class's accessor methods. This allows for coding "short-cuts" which at best reduce the maintainability of the code, and at worst may introduce bugs and inconsistencies not anticipated by the original module author.On some systems, this may not be too much of a problem. If the developer base is small, then we can trust the users of our modules to Do The Right Thing. However, as a module's user base increases, or the complexity of the systems our module's are embedded in grows, it may become desirable to control what users can and can't access in our module to guarantee our code's behaviour. A traditional method of indicating that an object's data and methods are for internal use only is to prefix attribute and method names with underscores. However, this still relies on the end user Doing The Right Thing.Class::Declare provides mechanisms for module developers to explicitly state where and how their class attributes and methods may be accessed, as well as hiding the underlying data store of the objects to prevent unwanted tampering with the data of the objects and classes. This provides a robust framework for developing Perl modules consistent with more strongly-typed object oriented languages, such as Java and C++, where classes provide public, private, and protected interfaces to object and class data and methods.Requirements:· Perl Requirements: · Perl


Class::Declare Related Software