Guest User

Untitled

a guest
May 23rd, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1.  
  2. Et Moose objekt begynner alltid med use Moose.
  3. I dette eksemplet har vi tatt med MooseX::AttributeHelpers som lar oss spesifisere navnet pƄ accessorene til objektet.
  4.  
  5. package FishPound;
  6. use Moose;
  7. use MooseX::AttributeHelpers;
  8.  
  9. has 'fish' => (
  10. is => 'rw',
  11. isa => 'HashRef[HashRef]',
  12. default => sub { {} },
  13. auto_deref => 1,
  14. metaclass => 'Collection::Hash',
  15. provides => {
  16. keys => 'get_fish_keys',
  17. get => 'get_fish',
  18. set => 'add_fish',
  19. exists => 'has_fish',
  20. values => 'fish_values',
  21. count => 'count_fish',
  22. },
  23. );
  24.  
  25.  
  26. package Main;
  27. use Moose;
  28. use MooseX::AttributeHelpers;
  29.  
  30. my $fish_pound = FishPound->new;
  31. my %fish = (
  32. name => 'trouth',
  33. weight => '30g',
  34. id => '1',
  35. );
  36.  
  37. unless ( $fish_pound->has_fish( $fish{id} ) ) {
  38. $fish_pound->add_fish( $fish{id} => \%fish );
  39. }
  40.  
  41. print "The pound has ", $fish_pound->count_fish, " fish in it.\n";
Add Comment
Please, Sign In to add comment