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.