現在、ブロック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");
UPDATE
答えてくれたfrido、Ether、Robert P、コメントしてくれたEther、Robert P、jrockwayに感謝します。
私は、Ether の答え$is_read_only
が!
. 二重否定もそれを提供します。したがって、 coderef を出力せずに関数$is_read_only
内で使用できます。is()
最も完全な回答については、以下の 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.