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.