"Can't modify non-lvalue subroutine call" when adding Moose attribute from method

Viewed 1474

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?

2 Answers
Related