如何在访问未初始化的 Moose 对象属性时死亡?
我有一个带有非必需属性的 Moose 对象:
has 'optional_attr' => (
is => 'ro',
isa => 'MyCoolType',
required => 0,
);
如果我尝试在未设置的情况下读取此属性,我该如何承认
?
I have a Moose object with a non-required attribute:
has 'optional_attr' => (
is => 'ro',
isa => 'MyCoolType',
required => 0,
);
How can I confess
if I ever try to read this attribute while it's not set?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
怎么样:
您可能还想添加一个谓词:
这样您就可以在不死的情况下查明它是否已设置。
还有 MooseX::LazyRequire,它可以让你说:
在幕后,它使用了我建议的相同技巧,但它在你的课堂上看起来更优雅。
How about:
You might want to throw in a predicate, too:
so you can find out if it's set without dying.
There's also MooseX::LazyRequire, which lets you say just:
Under the hood, it uses the same trick I suggested, but it looks more elegant in your class.
MooseX::LazyRequire 符合您的要求吗?
Would MooseX::LazyRequire fit your requirement?