In a Moose class I have a rw attribute referring to a position within a declared list.
has 'option_index' => (
isa => 'Int',
is => 'rw',
);
my $options = [qw(one two three)];
has 'option' => (
is => 'rw',
# ???
# should take 'one' and set option_index to 0, ...
# should return $options->[$self->option_index]
);
I want to create a proxy attribute, that gets the value at option_index of the list and sets option_index to the position where the argument is found.
I can create a simple sub that does that, but I would want to get into the benefit of Moose type checking and therefore declare it as an attribute.
Can this be done?
- I don't care for the implementation, only for the Moose set up and
- option_index has to remain
Here is an example using a trigger function:
Output:
Edit:
To have the
optionattribute have a default value that is determined from theoption_indexattribute, you can try addlazyand adefaultsub like this: