Class::LazyObject

Class::LazyObject contains deferred object construction.
Download

Class::LazyObject Ranking & Summary

Advertisement

  • Rating:
  • License:
  • Perl Artistic License
  • Price:
  • FREE
  • Publisher Name:
  • Daniel C. Axelrod
  • Publisher web site:
  • http://search.cpan.org/~daxelrod/Class-LazyObject-0.10/lib/Class/LazyObject.pm

Class::LazyObject Tags


Class::LazyObject Description

Class::LazyObject contains deferred object construction. Class::LazyObject contains deferred object construction.SYNOPSIS use Class::LazyObject; package Bob::Class::LazyObject; our @ISA = 'Class::LazyObject'; Class::LazyObject->inherit( deflated_class => __PACKAGE__, inflated_class => 'Bob' inflate => sub { my ($class, $id) = @_; return $class->new($id); } ); package main; my @bobs; foreach (0..10_000)#make 10 thousand lazy Bobs { push @bobs, Bob::Class::LazyObject->new($_); } # @bobs now contains lazy objects, not real Bobs. # No Bob objects have been constructed yet. my $single = $bobs; #rand returned 10 $single->string;#returns 10. #Single is now an actual Bob object. Only one #Bob object has been constructed. package Bob; #It's really expensive to create Bob objects. sub string { #return the scalar passed to ->new() } #other Bob methods hereClass::LazyObject allows you to create lazy objects. A lazy object holds the place of another object, (Called the "inflated object"). The lazy object turns into the inflated object ("inflates") only after a method is called on the lazy object. After that, any variables holding the lazy object will hold the inflated object.In other words, you can treat a lazy object just like the object it's holding the place of, and it won't turn into a real object until necessary. This also means that the real object won't be constructed until necessary.A lazy object takes up less memory than most other objects (it's even smaller than a blessed empty hash). Constructing a lazy object is also likely to be computationally cheaper than constructing an inflated object (especially if a database is involved).A lazy object can hold a scalar (called the "ID") that is passed to the constructor for the inflated object. Requirements: · Perl


Class::LazyObject Related Software