0

I'm trying to use Moose reader and writer for setting and getting a value.

The following is employee.pm:

package employee;
use Moose;

has 'firstName' => (is => 'ro' , isa => 'Str' , required => 1);

has 'salary' => (is => 'rw',
         isa => 'Str',
         writer => 'set_slalary',
         reader => 'get_salary',
         );

has 'department' => (is => 'rw' , default => 'support' );
has 'age' => (is => 'ro' , isa=> 'Str');

no Moose;
__PACKAGE__->meta->make_immutable;
1;

The following is my script 1.pl (which uses the above module):

use employee;
use strict;
use warnings;

my $emp = employee->new(firstName => 'Tom' , salary => 50000 , department => 'R&D' , age => '27');

$emp->set_salary(100000);

print $emp->firstName, " works in ", $emp->department, " and his salary is ",  $emp->get_salary() , " and age is ", $emp->age ,"\n" ;

In the script, I'm trying to update the salary attribute to 100000. I'm getting the following error:

Can't locate object method "set_salary" via package "employee" at 1.pl line 7

If I comment the line $emp->set_salary(100000); in 1.pl, then I get proper output (without the updated value for the salary attribute, obviously).

Tom works in R&D and his salary is 50000 and age is 27

In employee.pm, I have given read and write permission for salary attribute. Can anyone suggest where I am going wrong? Thanks in advance.

ikegami
  • 367,544
  • 15
  • 269
  • 518
kart1657
  • 78
  • 8
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/159833/discussion-between-hobbs-and-ikegami). – hobbs Nov 26 '17 at 09:27
  • You´re presuming that exists some "automatic" method generator. This is not how Moose works! Please, read the docs! https://www.google.com.br/search?q=metacpan+%2B+Moose&oq=metacpan+%2B+Moose&aqs=chrome..69i57j69i64.3064j0j7&sourceid=chrome&ie=UTF-8 – Andre Carneiro Nov 28 '17 at 18:19

1 Answers1

0

You misspelled set_salary.

writer => 'set_slalary'

should be

writer => 'set_salary'

Note that

is => 'rw'

is just another way of writing

accessor => 'salary'

Usually. is is unreliable when reader, writer and/or accessor are also provided for the same attribute. For example, is is sometimes ignored when reader, writer and/or accessor are also provided for the same attribute. (Demonstration below.) This is the case for your program.

As such, it's a bad idea to mix is with reader/writer/accessor. Use one style or the other, but not both for any given attribute. In your case, you should get rid of useless is => 'rw'.


Demonstration of is=>'rw'+reader+writer bug:

Class.pm:

package Class;

use Moose;

has attr1 => (
   is => 'rw',
   default => 'val1',
);

has attr2 => (
   is => 'rw',
   reader => 'get_attr2',
   writer => 'set_attr2',
   default => 'val2',
);

1;

a.pl:

use feature qw( say );

use FindBin qw( $RealBin );
use lib $RealBin;

use Class;

my $o = Class->new();
say $o->attr1();
say $o->attr2();

Output:

val1
Can't locate object method "attr2" via package "Class" at a.pl line 10.
ikegami
  • 367,544
  • 15
  • 269
  • 518