我目前使用一个块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
通过用!
. 双重否定也提供了这一点。因此,您可以$is_read_only
在is()
函数中使用,而无需打印出 coderef。
有关最完整的答案,请参阅下面的 Robert P 的答案。每个人都非常乐于助人,并建立在彼此的答案和评论的基础上。总的来说,我认为他对我的帮助最大,因此他现在被标记为已接受的答案。再次感谢 Ether、Robert P、friedo 和 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.