I am trying to learn Moops and I can't quite grasp how to use populate and iterate over lexical_has arrayRefs. Can you demonstrate their usage here with code please?
I wrote the following:
lexical_has people => (is => 'rw',
isa => ArrayRef,
default => sub { [] },
accessor => \(my @people),
required => 0);
I tried to populate it thusly:
$self->$people[$counter](Employee->new()->dispatch());
But it keeps error-ing on me "Syntax error near >$people[]"
You are setting
accessor => \@peoplewhich shows a fundamental misunderstanding of whatlexical_hasdoes.lexical_hasinstalls a coderef into that variable, so it ought to be a scalar.So, once you have
$peopleas a scalar, whichlexical_hashas installed a coderef into, then$self->$people()or$self->$peopleis a method call which returns an arrayref. Thus@{ $self->$people }is the (non-ref) array itself, which you can use for push/pop/shift/unshift/grep/map/sort/foreach/etc.Quick example:
Output is:
Here is the equivalent code using a public attribute for
people...