Name : perl-Class-Dot
| |
Version : 1.5.0
| Vendor : obs://build_opensuse_org/devel:languages:perl
|
Release : 10.51
| Date : 2024-08-05 19:09:28
|
Group : Development/Libraries/Perl
| Source RPM : perl-Class-Dot-1.5.0-10.51.src.rpm
|
Size : 0.09 MB
| |
Packager : (none)
| |
Summary : Simple and fast properties for Perl 5
|
Description :
Simple and fast properties for Perl 5.
\'Class::Dot\' also lets you define types for your properties, like Hash, String, Int, File, Code, Array and so on.
All the types are populated with sane defaults, so you no longer have to write code like this:
sub make_healthy { my ($self) = AATT_; my $state = $self->state; my $fur = $self->fur;
$state ||= { }; # < -- you don\'t have to do this with class dot. $fur ||= [ ]; # < -- same with this. }
Class::Dot can also create a default constructor for you if you pass it the \'-new\' option on the use line:
use Class::Dot qw(-new :std);
If you pass a hashref to the constructor, it will use them as values for the properties:
my $cat = Animal::Mammal::Carnivorous::Cat->new({ gender => \'male\', fur => [\'black\', \'white\', \'short\'], }
If you want to intialize something at object construction time you can! Just define a method named \'BUILD\'. \'Class::Dot\' will pass on the instance and all the arguments that was sent to \'new\'.
sub BUILD { my ($self, $options_ref) = AATT_;
warn \'Someone created a \', ref $self;
return; }
The return value of the \'BUILD\' method doesn\'t mean anything, that is unleass you have to \'-rebuild\' option on. When the \'-rebuild\' option is on, \'Class::Dot\' uses the return value of BUILD as the new object, so you can create a abstract factory or similar:
use Class::Dot qw(-new -rebuild :std);
sub BUILD { my ($self, $option_ref) = AATT_;
if (exists $options_ref->{delegate_to}) { my $new_class = $options_ref->{delegate_to}; my $new_instasnce = $new_class->new($options_ref);
return $new_instance; }
return; }
A big value of using properties is that you can override them at a later point to make them support additional functionality, like setting a hardware flag, logging, etc. In Class::Dot you override a property simply by defining their accessors:
property name => isa_String();
sub name { # < -- overrides the get accessor my ($self) = AATT_;
warn \'Acessing the name property\';
return $self->__getattr__(\'name\'); }
sub set_name { # < -- overrides the set accessor my ($self, $new_name) = AATT_;
warn $self->__getattr__(\'name\'), \" changed name to $new_name!\";
$self->__setattr__(\'name\', $new_name);
return; }
There is one exception where this won\'t work, though. That is if you define a property in a \'BEGIN\' block. If you do that you have to use the \'after_property_get()\' and \'after_property_set()\' functions:
BEGIN { use Class::Dot qw(-new :std); property name => isa_String(); }
after_property_get name => sub { my ($self) = AATT_;
warn \'Acessing the name property\';
return $self->__getattr__(\'name\'); }; # < -- the semicolon is required here!
after_property_set name => sub { my ($self, $new_name) = AATT_;
warn $self->__getattr__(\'name\'), \" changed name to $new_name!\";
$self->__setattr__(\'name\', $new_name);
return; }; # < -- the semicolon is required here!
You can read more about \'Class::Dot\' in the Class::Dot::Manual::Cookbook and Class::Dot::Manual::FAQ. (Not yet written for the 2.0 beta release).
Have a good time working with \'Class::Dot\', and please report any bug you might find, or send feature requests. (although \'Class::Dot\' is not meant to be Moose, it\'s meant to be simple and fast).
|
RPM found in directory: /packages/linux-pbone/ftp5.gwdg.de/pub/opensuse/repositories/devel:/languages:/perl:/CPAN-C/openSUSE_Tumbleweed/noarch |