I am battling with Moose these days, and I ran into the following problem. I create an object that has many required attributes on its creation. However, I wish to add attributes to it when a method is called. More specifically, I'd like to add the arguments for that method as a hash attribute. I'd like to do this so that subsequent calls to other methods know that the earlier method has already been called, with said parameters.
Example, but fictional code:
package Banana;
use Moose;
has ['peel', 'edible'] => (
is => 'ro',
isa => 'Bool',
required => 1,
);
has 'color' => (
is => 'ro',
isa => 'Str',
required => 1,
);
has 'grow_params' => (
is => 'ro',
isa => 'HashRef',
);
sub grow {
my ($self, $params) = @_;
# params would be a hashref of method arguments
$self->grow_params = $params;
# Execute some code changing other, initial vars
}
This won't work, as the following error gets thrown:
Can't modify non-lvalue subroutine call of
&Banana::grow_params
I've looked here on SO and on PerlMonks but I can't seem to find a general explanation of what the error means. Most answers simply re-write the original code and that's that. So what does the error mean, and can I accomplish what I'm trying to do? Or is this not the way to do it?
Moose object attributes are hidden behind functions of the same name, so when you have $self->grow_params
that's a function. Trying to assign a value to it won't work, you need to call it like a function.
$self->grow_params($params);
But even then, as you've currently defined grow_params as read only, you can't change it's value once the object has been created, even within it's own methods.