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 => \@people
which shows a fundamental misunderstanding of whatlexical_has
does.lexical_has
installs a coderef into that variable, so it ought to be a scalar.So, once you have
$people
as a scalar, whichlexical_has
has installed a coderef into, then$self->$people()
or$self->$people
is 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
...