有没有一种简单的方法来测试 Moose 属性是否是只读的?
我目前使用块 eval
来测试我是否已将属性设置为只读。有没有更简单的方法来做到这一点?
工作代码示例:
#Test that sample_for is ready only
eval { $snp_obj->sample_for('t/sample_manifest2.txt');};
like($@, qr/read-only/xms, "'sample_for' is read-only");
更新
感谢 Friedo、Ether 和 Robert P 的回答,感谢 Ether、Robert P 和 jrockway 的评论。
我喜欢 Ether 的回答如何确保 $is_read_only
只是一个true 或 false 值(即,但绝不是 coderef),通过使用 !
对其进行否定。 双重否定也提供了这一点。因此,您可以在 is()
函数中使用 $is_read_only
,而无需打印出 coderef。
请参阅下面 Robert P 的回答以获得最完整的答案。每个人都非常有帮助,并建立在彼此的答案和评论之上。总的来说,我认为他对我的帮助最大,因此他的答案现在被标记为已接受的答案。再次感谢 Ether、Robert P、frido 和 jrockway。
如果您可能想知道,正如我一开始所做的那样,这里是有关 get_attribute
和 find_attribute_by_name
之间差异的文档(来自 Class::MOP::Class):
$metaclass->get_attribute($attribute_name)
This will return a Class::MOP::Attribute for the specified $attribute_name. If the
class does not have the specified attribute, it returns undef.
NOTE that get_attribute does not search superclasses, for that you need to use
find_attribute_by_name.
I currently use a block eval
to test that I've set an attribute as read-only. Is there a simpler way to do this?
Example from working code:
#Test that sample_for is ready only
eval { $snp_obj->sample_for('t/sample_manifest2.txt');};
like($@, qr/read-only/xms, "'sample_for' is read-only");
UPDATE
Thanks to friedo, Ether, and Robert P for their answers and to Ether, Robert P, and jrockway for their comments.
I like how Ether's answer ensures that $is_read_only
is only a true or false value (i.e. but never a coderef) by negating it with a !
. Double negation also provides that. Thus, you can use $is_read_only
in an is()
function, without it printing out the coderef.
See Robert P's answer below for the most complete answer. Everyone has been very helpful and built on each other's answers and comments. Overall, I think he's helped me the most, hence his is now marked the accepted answer. Again, thanks to Ether, Robert P, friedo, and jrockway.
In case you might be wondering, as I did at first, here is documentation about the difference between get_attribute
and find_attribute_by_name
(from Class::MOP::Class):
$metaclass->get_attribute($attribute_name)
This will return a Class::MOP::Attribute for the specified $attribute_name. If the
class does not have the specified attribute, it returns undef.
NOTE that get_attribute does not search superclasses, for that you need to use
find_attribute_by_name.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
从技术上讲,属性不需要具有读取或写入方法。 大多数会,但并非总是如此。一个例子(从 jrockway 的评论)如下:
该属性将存在,但没有标准的读取器和写入器。
因此,要在每种情况下测试属性已定义为
is =>; 'RO'
,需要检查写入和读取方法。您可以使用此子例程来完成此操作:或者,您可以在最后一个
return
之前添加双重否定来布尔化返回值:Technically, an attribute does not need to have a read or a write method. Most of the time it will, but not always. An example (graciously stolen from jrockway's comment) is below:
This attribute will exist, but not have standard readers and writers.
So to test in every situation that an attribute has been defined as
is => 'RO'
, you need to check both the write and the read method. You could do it with this subroutine:Alternatively, you could add a double negation before the last
return
to boolify the return value:如 Class::MOP::Attribute 中所述:
$attr->get_write_method()
将获取 writer 方法(您创建的方法或生成的方法),或者 undef(如果没有)。As documented in Class::MOP::Attribute:
$attr->get_write_method()
will get the writer method (either one you created or one that was generated), or undef if there isn't one.您应该能够从对象的元类中获取此信息:
请参阅 Class::MOP::Attribute 了解更多信息。
You should be able to get this from the object's metaclass:
See Class::MOP::Attribute for more.