Guest User

Untitled

a guest
Apr 27th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.51 KB | None | 0 0
  1. package Foo;
  2.  
  3. use Moose;
  4.  
  5. has 'expensive_attribute' => (
  6. is => 'rw',
  7. isa => 'Str',
  8. lazy_build => 1
  9. );
  10.  
  11. sub _build_expensive_attribute { time }
  12.  
  13. sub do_stuff {
  14. my ( $self ) = @_;
  15. # Modify the core stuff here
  16. $self->clear_expensive_attribute; # From lazy_build
  17. sleep(2);
  18. }
  19.  
  20. no Moose;
  21. 1;
  22.  
  23. package main;
  24.  
  25. my $f = Foo->new;
  26.  
  27. my $t1 = $f->expensive_attribute;
  28.  
  29. $f->do_stuff; # Invalidates attribute
  30.  
  31. my $t2 = $f->expensive_attribute;
  32.  
  33. # As expected, t2 contains the current result!
  34. print "$t1 != $t2\n";
Add Comment
Please, Sign In to add comment