Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Et Moose objekt begynner alltid med use Moose.
- I dette eksemplet har vi tatt med MooseX::AttributeHelpers som lar oss spesifisere navnet pƄ accessorene til objektet.
- package FishPound;
- use Moose;
- use MooseX::AttributeHelpers;
- has 'fish' => (
- is => 'rw',
- isa => 'HashRef[HashRef]',
- default => sub { {} },
- auto_deref => 1,
- metaclass => 'Collection::Hash',
- provides => {
- keys => 'get_fish_keys',
- get => 'get_fish',
- set => 'add_fish',
- exists => 'has_fish',
- values => 'fish_values',
- count => 'count_fish',
- },
- );
- package Main;
- use Moose;
- use MooseX::AttributeHelpers;
- my $fish_pound = FishPound->new;
- my %fish = (
- name => 'trouth',
- weight => '30g',
- id => '1',
- );
- unless ( $fish_pound->has_fish( $fish{id} ) ) {
- $fish_pound->add_fish( $fish{id} => \%fish );
- }
- print "The pound has ", $fish_pound->count_fish, " fish in it.\n";
Add Comment
Please, Sign In to add comment