Note that there are some explanatory texts on larger screens.

plurals
  1. POPerl: fixing Moose attribute and type coercion problems
    text
    copied!<p>I recently upgraded Moose to v1.15 and found that a set of modules I use no longer worked. The error I get is:</p> <pre><code>You cannot coerce an attribute (source) unless its type (GOBO::Node) has a coercion at /opt/local/lib/perl5/site_perl/5.12.0/darwin-multi-2level/Moose/Meta/Role/Application/ToClass.pm line 142 </code></pre> <p>I can see several possible sources of error and would be grateful for advice on how to fix the problem.</p> <p>The first bit of code for GOBO::Node looks like this:</p> <pre><code>package GOBO::Node; [...] extends 'GOBO::Base'; with 'GOBO::Labeled'; with 'GOBO::Attributed'; coerce 'GOBO::Node' =&gt; from 'Str' =&gt; via { new GOBO::Node(id=&gt;$_) }; has 'source' =&gt; (is =&gt; 'rw', isa =&gt; 'GOBO::Node'); </code></pre> <p>The roles used by this package also have attributes that are GOBO::Nodes, and the attribute 'source' mentioned in the error message is one of them.</p> <ul> <li><p>part of the reason for having the coercion in GOBO::Node seems to be as a shortcut when creating a new node. Would it be better to use BUILDARGS rather than coerce?</p></li> <li><p>where should I put the coercion if I want several packages to be able to use it? If I add the coercion to (e.g.) GOBO::Attributed, I get a warning that it already exists. Without the coercion, though, I get the warning above about not being able to coerce.</p></li> <li><p>there is a separate package of subtypes; would it be better to create a subtype of GOBO::Node--e.g. GOBO::Node::ProtoNode--and a coercion, and use that for the attributes should be GOBO::Nodes?</p></li> </ul> <p>Thank you for any help or advice on this problem!</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