我正在通过Moose
食谱,我仍然看不到我是否可以使用它创建私有(private)变量和函数?是否可以?如果是,如何用 Moose 创建它们?
最佳答案
就像 daxim 指出的那样,私有(private)方法具有“_”前缀。因为属性(实例变量)生成开箱即用的 getter 方法(如果 rw 还生成 setters 方法),您应该这样做:
has 'myvariable' => (
is => 'ro',
writer => '_myvariable',
init_arg => undef,
# other options here
);
这样你就可以在你的类/实例中设置这个属性,并且它不能从外部设置。如果只读权限过多,也可以将其标记为“私有(private)”:
has '_myvariable' => (
is => 'ro',
writer => '_set_myvariable'
init_arg => undef,
# other options here
);
关于perl - 如何使用 Moose 创建私有(private)/公共(public)变量和函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5645356/