Class::props

Class::props is a pragma to implement lvalue accessors with options.
Download

Class::props Ranking & Summary

Advertisement

  • Rating:
  • License:
  • Perl Artistic License
  • Price:
  • FREE
  • Publisher Name:
  • Domizio Demichelis
  • Publisher web site:
  • http://search.cpan.org/~domizio/CGI-Application-Plus-1.21/lib/CGI/Application/CheckRM.pm

Class::props Tags


Class::props Description

Class::props is a pragma to implement lvalue accessors with options. Class::props is a pragma to implement lvalue accessors with options.SYNOPSISClass package MyClass ; # implement constructor without options use Class::constr ; # just accessors without options (list of strings) use Class::props @prop_names ; # @prop_names (1) # a property with validation and default (list of hash refs) use Class::props { name => 'digits', validation => sub{ /^d+z/ } , # just digits default => 10 } ; # a group of properties with common full options use Class::props { name => @prop_names2, # @prop_names2 (1) default => sub{$_->other_default} , validation => sub{ /w+/ } , protected => 1 , no_strict => 1 , allowed => qr/::allowed_sub$/ } ; # all the above in just one step (list of strings and hash refs) use Class::props @prop_names , # @prop_names (1) { name => 'digits', validation => sub{ /^d+z/ } , default => 10 } , { name => @prop_names2, # @prop_names2 (1) default => sub{$_->other_default} , validation => sub{ /w+/ } , protected => 1 , no_strict => 1 , allowed => qr/::allowed_sub$/ } ; # (1) must be set in a BEGIN block to have effect at compile timeUsage: $object = MyClass->new(digits => '123'); $object->digits = '123'; MyClass->digits = '123'; # same thing $object->digits('123'); # old way supported $d = $object->digits; # $d == 123 $d = $MyClass::digits # $d == 123 undef $object->digits # $object->digits == 10 (default) # these would croak $object->digits = "xyz"; MyClass->digits = "xyz"; # this will bypass the accessor whithout croaking $MyClass::digits = "xyz";This pragma easily implements lvalue accessor methods for the properties of your Class (lvalue means that you can create a reference to it, assign to it and apply a regex to it; see also "KNOWN ISSUE"), which are very efficient function templates that your modules may import at compile time. "This technique saves on both compile time and memory use, and is less error-prone as well, since syntax checks happen at compile time." (quoted from "Function Templates" in the perlref manpage).You can completely avoid to write the accessor by just declaring the names and eventually the default value, validation code and other option of your properties.The accessor method creates a scalar in the class that implements it (e.g. $Class::any_property) and sets/gets it using the options you set.This module allows also "lazy" data computing (see the default option).Requirements:· Perl version >= 5.6.1INSTALLATION:CPAN perl -MCPAN -e 'install OOTools'Standard installationFrom the directory where this file is located, type: perl Makefile.PL make make test make install


Class::props Related Software