Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Foo;
- use Moose;
- has 'expensive_attribute' => (
- is => 'rw',
- isa => 'Str',
- lazy_build => 1
- );
- sub _build_expensive_attribute { time }
- sub do_stuff {
- my ( $self ) = @_;
- # Modify the core stuff here
- $self->clear_expensive_attribute; # From lazy_build
- sleep(2);
- }
- no Moose;
- 1;
- package main;
- my $f = Foo->new;
- my $t1 = $f->expensive_attribute;
- $f->do_stuff; # Invalidates attribute
- my $t2 = $f->expensive_attribute;
- # As expected, t2 contains the current result!
- print "$t1 != $t2\n";
Add Comment
Please, Sign In to add comment