Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The example code you pasted doesn't actually trigger the error. The <code>source</code> attribute as written there won't try to coerce anything. I assume however that one of the roles you mention has an attribute with <code>coerce =&gt; 1</code> defined.</p> <p>In Moose types, and thus coercions, are global. When combined with the the fact that Moose builds a class dynamically you end up with the strange behavior you're seeing here. You'll need to move the definition of the coercion to someplace <em>before</em> the first use of the <code>GOBO::Node</code> type. Typically this is done by creating a package of subtypes (which you note you already have) and including that as early as possible (via <code>use</code>).</p> <p>Simply moving the <code>GOBO::Node</code> coercion definition into this package of subtypes, and making sure it's used everywhere the coercion is needed should fix your problem.</p> <p>To answer your other questions: </p> <ul> <li><p>In general I would recommend using coercion over <code>BUILDARGS</code> because it is a much finer grained tool. The usage you're showing here is a text book example of proper coercion usage, so no real reason to change that. </p></li> <li><p>As stated above, the typical answer is to build a Library package in a seperate namespace (<code>MyApp::TypeLibrary</code>) and then include that package at the top of the classes you want your types available. Perl won't re-compile a package that it has already compiled, meaning the error you got about the coercion already existing won't be triggered in this case.</p></li> <li><p>Based on the examples you've provided there is no need to create a new subtype, GOBO::Node should already work, and without a new subtype this is effectively the same answer as the last one. Yes use the subtype library. </p></li> </ul> <p>I hope that helps.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload