Class::props - Pragma to implement lvalue accessors with options
Included in OOTools 1.76 distribution.
The latest versions changes are reported in the Changes file in this distribution.
The distribution includes:
Class::constr
Pragma to implement constructor methods
Class::props
Pragma to implement lvalue accessors with options
Class::groups
Pragma to implement groups of properties accessors with options
Object::props
Object::groups
Perl version >= 5.6.1
perl -MCPAN -e 'install OOTools'
From the directory where this file is located, type:
perl Makefile.PL make make test make install
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{$_[0]->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{$_[0]->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 time
my $object = MyClass->new(digits => '123'); $object->digits = '123'; MyClass->digits = '123'; # same thing $object->digits('123'); # old way supported my $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"; $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).
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::property) and sets/gets it using the options you set.
IMPORTANT NOTE: Since the version 1.7 the options don't work if you access the scalar without using the accessor, so you can access the value directly when you need to bypass the options.
The main difference between Object::props and Class::props is that the first pragma creates instance properties related with the object and stored in $object->{property}, while the second pragma creates class properties related with the class and stored in $Class::property.
A Class property is accessible either through the class or through all the objects of that class, while an object property is accessible only through the object that set it.
package MyClass; use Class::constr ; use Object::props 'obj_prop' ; use Class::props qw( class_prop1 class_prop2 ) ; package main ; my $object1 = MyClass->new( obj_prop => 1 , class_prop1 => 11 ) ; my $object2 = MyClass->new( obj_prop => 2 , class_prop2 => 22 ) ; print $object1->obj_prop ; # would print 1 print $object1->{obj_prop} ; # would print 1 print $object2->obj_prop ; # would print 2 print $object2->{obj_prop} ; # would print 2 print $object1->class_prop1 ; # would print 11 print $object2->class_prop1 ; # would print 11 print $MyClass::class_prop1 ; # would print 11 print $object1->class_prop2 ; # would print 22 print $object2->class_prop2 ; # would print 22 print $MyClass::class_prop2 ; # would print 22 $object2->class_prop1 = 100 ; # object method MyClass->class_prop2 = 200 ; # static method works as well print $object1->class_prop1 ; # would print 100 print $object2->class_prop1 ; # would print 100 print $object1->class_prop2 ; # would print 200 print $object2->class_prop2 ; # would print 200
If you want to see some working example of this module, take a look at the source of my other distributions.
The name of the property is used as the identifier to create the accessor method, and the scalar that contains it.
Given 'my_prop' as the class property name:
MyClass->my_prop = 10 ; # assign 10 to $MyClass::my_prop MyClass->my_prop( 10 ); # assign 10 to $MyClass::my_prop # assuming $object is an object of class MyClass $object->my_prop = 10 ; # assign 10 to $MyClass::my_prop $object->my_prop( 10 ); # assign 10 to $MyClass::my_prop # same thing if MyClass::constr is implemented # by the Class::constr pragma $object = MyClass->new( my_prop => 10 );
You can group properties that have the same set of option by passing a reference to an array containing the names. If you don't use any option you can pass a list of plain names as well. See "SYNOPSYS".
Use this option to set a default value. If any validation option is set, then the default value is validated as well (the no_strict option override this).
validation
no_strict
If you pass a CODE reference as default it will be evaluated at runtime and the property will be set to the result of the referenced CODE.
You can reset a property to its default value by assigning it the undef value.
With no_strict option set to a true value, the default value will not be validate even if a validation option is set. Without this option the method will croak if the default are not valid.
default
You can set a code reference to validate a new value. If you don't set any validation option, no validation will be done on the assignment.
In the validation code, the object or class is passed in $_[0] and the value to be validated is passed in $_[1] and for regexing convenience it is aliased in $_.
$_[0]
$_[1]
$_
# web color validation use Class::props { name => 'web_color' , validation => sub { /^#[0-9A-F]{6}$/ } } ; # this would croak $object->web_color = 'dark gray' ;
You can alse use the validation code as a sort of pre_process or filter for the input values: just assign to $_ in the validation code in order to change the actual imput value.
# this will uppercase all input values use Class::props { name => 'uppercase_it' , validation => sub { $_ = uc } } ; # when used $object->uppercase_it = 'abc' ; # stored value will be 'ABC'
The validation code should return true on success and false on failure. Croak explicitly if you don't like the default error message.
You can set a code reference to transform the stored value, just before it is returned. If you don't set any post_process option, no transformation will be done on the returned value, so in that case the returned value will be the same stored value.
post_process
In the post_process code, the object or class is passed in $_[0] and the value to be transformed is passed in $_[1]; the accessor will return the value returned from the post_process code
# this will uppercase all output values use Class::props { name => 'uppercase_it' , post_process => sub { uc $_[1] } } # when used Class->uppercase_it = 'aBc'; # stored value will be 'aBc' print Class->uppercase_it ; # would print 'ABC'
Warning: The post_process code is ALWAYS executed in SCALAR context regardless the execution context of the accessor itself.
The property is settable only by the caller sub that matches with the content of this option. The content can be a compiled RE or a simple string that will be used to check the caller. (Pass an array ref for multiple items)
use Class::props { name => 'restricted' allowed => [ qr/::allowed_sub1$/ , qr/::allowed_sub2$/ ] }
You can however force the assignation from not matching subs by setting $Class::props::force to a true value.
Set this option to a true value and the property will be turned read-only when used from outside its class or sub-classes. This allows you to normally read and set the property from your class but it will croak if your user tries to set it.
You can however force the protection and set the property from outside the class that implements it by setting $Class::props::force to a true value.
This will add to the package package the accessors for the properties. It is useful to add properties in other packages.
package Any::Package; Class:props->('My::Package', { name => 'any_name', ... }); # which has the same effect of package My::Package; use Class::props { name => 'any_name', ... }
If you need support or if you want just to send me some feedback or request, please use this link: http://perl.4pro.net/?Class::props.
© 2004 by Domizio Demichelis.
All Rights Reserved. This module is free software. It may be used, redistributed and/or modified under the same terms as perl itself.
Thanks to Juerd Waalboer (http://search.cpan.org/author/JUERD) that with its Attribute::Property inspired the creation of this distribution.
1 POD Error
The following errors were encountered while parsing the POD:
Non-ASCII character seen before =encoding in '©'. Assuming CP1252
To install Class::props, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Class::props
CPAN shell
perl -MCPAN -e shell install Class::props
For more information on module installation, please visit the detailed CPAN module installation guide.