我正在阅读Moose食谱,我仍然无法看到我是否可以使用它创建私有变量和函数?可能吗?如果是,如何使用Moose创建它们?
dax*_*xim 10
在标识_函数/变量等的前缀标识为私有.这在perlstyle中有关范围的部分中记录,在文档的中间.
这是由理智的程序员和一些工具(源解析器/文档)所尊重,但不是由编译器强制执行.见perlmodlib#NOTE.
nxa*_*adm 10
像daxim指出的那样,私有方法具有"_"前缀.因为属性(实例变量)生成了getters方法(如果rw也是setter方法),所以你应该这样做:
has 'myvariable' => (
is => 'ro',
writer => '_myvariable',
init_arg => undef,
# other options here
);
Run Code Online (Sandbox Code Playgroud)
这样,您可以在类/实例中设置此属性,并且不能从外部设置.如果只读访问权限太多,您也可以将其标记为"私有":
has '_myvariable' => (
is => 'ro',
writer => '_set_myvariable'
init_arg => undef,
# other options here
);
Run Code Online (Sandbox Code Playgroud)