Class-Accessor-Constructor
view release on metacpan or search on metacpan
lib/Class/Accessor/Constructor.pm view on Meta::CPAN
Suppose you have a class that has one preferred accessor, and you want to
simplify its usage so that if the constructor is called with a single value,
it is passed to that preferred accessor.
Given that the C<Simple> class defines
sub MUNGE_CONSTRUCTOR_ARGS {
my $self = shift;
return %{ $_[0] } if @_ == 1 && ref($_[0]) eq 'HASH';
return (b => @_) if @_ % 2; # odd number of args
return @_;
}
then an object could be constructed like this
my $test = Simple->new('blah');
which would be munged to be equivalent to
my $test = Simple->new(b => 'blah');
If you define an C<init()> method, the constructor calls it with the munged
args as the very last thing.
=head2 mk_constructor_with_dirty
Like C<mk_constructor()>, but also keeps track of whether the object has been
modified. This is useful, for example, when you have read the object from a
storage and at the end you want to write it back if it has changed. This
method generated saves you from having to update a dirty-flag in each
accessor. It achieves its purpose by doing a tie() on the blessed hash that is
the object, so there is some performance penalty. But it also works when
someone tries to break encapsulation by accessing hash elements directly
instead of going via the accessors. See L<Class::Accessor::Constructor::Base>
for details.
If you want that behaviour only in a part of your inheritance tree, redefine
the constructor at the appropriate point. For example:
package Foo;
use base 'Class::Accessor::Constructor';
__PACKAGE__->mk_constructor;
package Bar;
use base 'Foo';
__PACKAGE__->mk_constructor_with_dirty;
Now objects of type C<Foo> will not keep a dirty-flag, but objects of type
C<Bar> and its descendants will.
=head2 mk_singleton_constructor
Like C<constructor> but constructs a singleton object.
=head1 INSTALLATION
See perlmodinstall for information and options on installing Perl modules.
=head1 BUGS AND LIMITATIONS
No bugs have been reported.
Please report any bugs or feature requests through the web interface at
L<http://rt.cpan.org/Public/Dist/Display.html?Name=Class-Accessor-Constructor>.
=head1 AVAILABILITY
The latest version of this module is available from the Comprehensive Perl
Archive Network (CPAN). Visit L<http://www.perl.com/CPAN/> to find a CPAN
site near you, or see L<http://search.cpan.org/dist/Class-Accessor-Constructor/>.
The development version lives at L<http://github.com/hanekomu/Class-Accessor-Constructor>
and may be cloned from L<git://github.com/hanekomu/Class-Accessor-Constructor.git>.
Instead of sending patches, please fork this project using the standard
git and github infrastructure.
=head1 AUTHOR
Marcel Gruenauer <marcel@cpan.org>
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2007 by Marcel Gruenauer.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
=cut
( run in 1.689 second using v1.01-cache-2.11-cpan-39bf76dae61 )