Class-Accessor-Lazy
view release on metacpan or search on metacpan
lib/Class/Accessor/Lazy.pm view on Meta::CPAN
Foo->follow_best_practice
->mk_accessors(slow_accessor)
->fast_accessors
->mk_accessors(fast_accessor);
Main documentation may be found on L<Class::Accessor> and
L<Class::Accessor::Fast> pages.
The main extension of this module is possibility to make lazy properties, which will be inited on first get operation (if there was no write before).
Such methods are useful for database representation classes, where related data may not be read at all and there is no need to fetch it from database.
For example, there are C<Shop> class and C<Employee> class. Each C<Shop> has property C<employees>, which contains a reference to C<Employee> objects list. But, you could fetch Shop object from database just to check C<income> property and no don't ...
But, if you want to get access to them, they should be read from database. And here are lazy properties comes:
package Shop;
use base 'Class::Accessor::Lazy';
Shop->follow_best_practice # use set/get for accessors/mutators
->fast_accessors # use Class::Acessor::Fast algorithm
->mk_accessors('income') # regular property
->mk_lazy_accessors('employees'); # lazy property
...
sub _lazy_init_employees
{
# here we are reading employees from database and saving them in
# property directly or using mutator set_employees
}
On first C<get_employees> invocation, method C<Shop::_lazy_init_employees> will be invoked automatically, to allow your class to read related data from database, for example, and store it in object.
IMPORTANT: every lazy property of the class MUST have related init method. The name of such method is C<_lazy_init_{property name}>.
=head1 NEW METHODS
There are couple of new methods in addition to L<Class::Accessor>s ones. Also, class methods now returns C<$self> and you may use chain calls.
=head2 fast_acessors
Enables using L<Class::Accessor::Fast> accessors generators.
=head2 original_acessors
Enables using L<Class::Accessor> accessors generators.
=head2 mk_lazy_accessors
Same as C<mk_accessors>, but creating lazy ones.
=head2 mk_lazy_ro_accessors
Same as C<mk_ro_accessors>, but creating lazy ones.
=head1 BENCHMARKING
Accessors benchmark:
Benchmark: timing 20000000 iterations of Acessor, AcessorF, Direct, Lazy, LazyF...
Acessor: 12 wallclock secs (11.34 usr + 0.00 sys = 11.34 CPU) @ 1763512.92/s (n=20000000)
AcessorF: 6 wallclock secs ( 5.71 usr + 0.00 sys = 5.71 CPU) @ 3502626.97/s (n=20000000)
Direct: 1 wallclock secs ( 0.78 usr + 0.00 sys = 0.78 CPU) @ 25641025.64/s (n=20000000)
Lazy: 14 wallclock secs (13.85 usr + 0.00 sys = 13.85 CPU) @ 1443730.60/s (n=20000000)
LazyF: 9 wallclock secs ( 8.71 usr + 0.00 sys = 8.71 CPU) @ 2297530.16/s (n=20000000)
Mutators benchmark:
Benchmark: timing 20000000 iterations of Acessor, AcessorF, Direct, Lazy, LazyF...
Acessor: 16 wallclock secs (15.26 usr + 0.00 sys = 15.26 CPU) @ 1310959.62/s (n=20000000)
AcessorF: 8 wallclock secs ( 7.91 usr + 0.00 sys = 7.91 CPU) @ 2528764.70/s (n=20000000)
Direct: 2 wallclock secs ( 1.50 usr + 0.00 sys = 1.50 CPU) @ 13351134.85/s (n=20000000)
Lazy: 19 wallclock secs (18.83 usr + 0.00 sys = 18.83 CPU) @ 1062191.30/s (n=20000000)
LazyF: 9 wallclock secs (10.53 usr + 0.00 sys = 10.53 CPU) @ 1899335.23/s (n=20000000)
Direct means direct access to the object property, and F suffix means using C<fast_accessors>.
=head1 BUGS AND IMPROVEMENTS
If you found any bug and/or want to make some improvement, feel free to participate in the project on GitHub: L<https://github.com/hurricup/Class-Accessor-Lazy>
=head1 LICENSE
This module is published under the terms of the MIT license, which basically means "Do with it whatever you want". For more information, see the LICENSE file that should be enclosed with this distributions. A copy of the license is (at the time of wr...
=head1 SEE ALSO
=over
=item * Main project repository and bugtracker: L<https://github.com/hurricup/Class-Accessor-Lazy>
=item * Testing results: L<http://www.cpantesters.org/distro/C/Class-Accessor-Lazy.html>
=item * AnnoCPAN, Annotated CPAN documentation: L<http://annocpan.org/dist/Class-Accessor-Lazy>
=item * CPAN Ratings: L<http://cpanratings.perl.org/d/Class-Accessor-Lazy>
=item * See also: L<Class::Variable>, L<Class::Property>, L<Class::Accessor> and L<Class::Accessor::Lazy>.
=back
=head1 AUTHOR
Copyright (C) 2014 by Alexandr Evstigneev (L<hurricup@evstigneev.com|mailto:hurricup@evstigneev.com>)
=cut
( run in 1.039 second using v1.01-cache-2.11-cpan-71847e10f99 )