"Cannot change non-lvalue subroutine call" when adding Moose attribute from method

Today I’m fighting Mus , and I’m faced with the following problem. I create an object with many necessary attributes when creating it. However, I want to add attributes to it when the method is called. In particular, I would like to add arguments for this method as a hash attribute. I would like to do this so that subsequent calls to other methods know that an earlier method has already been called with the specified 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 will not work as the following error occurs:

Unable to change non-lvalue subroutine call &Banana::grow_params

SO PerlMonks, , . . , , , ? ?

0
2

Moose , , $self->grow_params, . , .

$self->grow_params($params);

, grow_params , , , .

+1

, ...

:

$variable = 'value';

($variable) "lvalue". ('value') "rvalue".

r , , , :

$variable = $some_other_variable;

:

$variable = 2 * $pi * $radius ** 2;

(, ), lvalue . , :

'value' = $variable;

Moose ( "getters" "seters" ) - . . , () :

$obj->attribute = 'value';

:

$obj->attribute('value');

Moose, MooseX::LvalueAttribute, lvalue-, . , , , .

+1

Source: https://habr.com/ru/post/1682141/


All Articles