【发布时间】:2017-11-26 07:55:45
【问题描述】:
我正在尝试使用 Moose reader 和 writer 来设置和获取值。
以下是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;
以下是我的脚本1.pl(使用了上面的模块):
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" ;
在脚本中,我尝试将salary 属性更新为100000。
我收到以下错误:
Can't locate object method "set_salary" via package "employee" at 1.pl line 7
如果我在1.pl 中注释$emp->set_salary(100000); 行,那么我会得到正确的输出(显然没有salary 属性的更新值)。
Tom works in R&D and his salary is 50000 and age is 27
在employee.pm 中,我已授予salary 属性的读写权限。谁能建议我哪里出错了?提前致谢。
【问题讨论】:
-
你假设存在一些“自动”方法生成器。这不是 Moose 的工作方式!请阅读文档! google.com.br/…