如何创建内部(私有)Moose 对象变量(属性)?
我希望某些属性(也许在这种情况下这是错误的术语)是私有的,也就是说,仅在对象内部使用 - 不能从外部读取或写入。
例如,考虑一些内部变量,它计算一组方法中任何一个方法被调用的次数。
我应该在哪里以及如何定义这样的变量?
I would like some attributes (perhaps this is the wrong term in this context) to be private, that is, only internal for the object use - can't be read or written from the outside.
For example, think of some internal variable that counts the number of times any of a set of methods was called.
Where and how should I define such a variable?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
Moose::Manual::Attributes
显示了以下创建私有属性的方法:设置
init_arg
意味着该属性不能在构造函数中设置。如果您需要更新它,请将其设为rw
或添加writer
。/I3az/
The
Moose::Manual::Attributes
shows the following way to create private attributes:Setting
init_arg
means this attribute cannot be set at the constructor. Make it arw
or addwriter
if you need to update it./I3az/
你可以尝试这样的事情:
is =>; 'ro'
使属性只读。 Moose 生成吸气剂。您的方法将使用 getter 来递增值,如下所示:writer =>; '_set_call_counter'
生成一个名为_set_call_counter
的 setter。 Moose 不支持真正的私有属性。从技术上讲,外部代码可以调用_set_call_counter
。但按照惯例,应用程序不会调用以下划线开头的方法。You can try something like this:
is => 'ro'
makes the attribute read only. Moose generates a getter. Your methods will use the getter for incrementing the value, like so:writer => '_set_call_counter'
generates a setter named_set_call_counter
. Moose does not support true private attributes. Outside code can, technically, call_set_call_counter
. By convention, though, applications do not call methods beginning with an underscore.我认为您想要 MooseX::Privacy。
perldoc 告诉您您应该需要的一切 - 它为您的属性添加了一个新特征,允许您将它们声明为私有或受保护:
I think you want MooseX::Privacy.
The perldoc tells you all you should need - it adds a new trait to your attributes allowing you to declare them as private or protected:
我一直无法找到一种方法使 Moose 属性完全私有。每当我使用
has 'name' =>; (...);
要创建一个属性,它总是至少暴露于读取。对于我想要真正私有的项目,我在 Moose 包中使用标准的“我的”变量。作为一个快速示例,请使用以下模块“CountingObject.pm”。使用该模块的脚本无法直接访问 $cntr 变量。他们必须使用“add_one”和“get_count”方法作为与外界的接口。例如:
我是 Moose 的新手,但据我所知,这种方法提供了完全私有的变量。
I haven't been able to figure out a way to make Moose attributes completely private. Whenever I use
has 'name' => (...);
to create an attribute, it is always exposed to reading at a minimum. For items I want to be truly private, I'm using standard "my" variables inside the Moose package. For a quick example, take the following module "CountingObject.pm".Scripts that use that module have no direct access to the $cntr variable. They must use the "add_one" and "get_count" methods which act as an interface to the outside world. For example:
I'm new to Moose, but as far as I can tell, this approach provides completely private variables.
Alan W. Smith 提供了一个带有词法变量的私有类变量,但它由类中的所有对象共享。尝试在示例脚本的末尾添加一个新对象:
使用 MooseX:Privacy 是一个很好的答案,但如果你不能,你可以借用从内到外对象阵营的技巧:
这样,每个对象的计数器都被存储作为词法哈希中的条目。上面的内容可以更简洁地实现:
Alan W. Smith provided a private class variable with a lexical variable, but it is shared by all objects in the class. Try adding a new object to the end of the example script:
Using MooseX:Privacy is a good answer, though if you can't, you can borrow a trick from the inside-out object camp:
With that, each object's counter is stored as an entry in a lexical hash. The above can be implemented a little more tersely thus: