Because of the dynamic nature of perl, it isn't possible to eliminate isa checks in a general way. It is certainly possible to do in your own code though. For example:

package MyApp::SomeObject; use constant DEBUG => $ENV{MYAPP_DEBUG}; use Moo; has foo => ( is => 'ro', ( DEBUG ? ( isa => sub { my $value = shift; if ($value !~ /^[0-9]+$/) { die "Not an integer: $value"; } } ) : () ), );

Note that isa and coerce work slightly differently in Moose vs Moo. In Moose, first it performs the isa check. If that fails, it tries to do a coercion. The result of the coercion is then validated with the isa check. These checks are tied to Moose's type system.

Moo doesn't have a type system, so its isa and coerce are both just coderefs. The coerce sub is always run (if it exists), then its result is checked with the isa sub (if it exists). The isa sub should throw an exception on invalid values. Its return value is ignored. The coerce sub should return the coerced value. It would be possible to do all of this logic in a coerce sub, but it is convenient to be able to specify them separately.


In reply to Re: Moo's coerce and isa difference by Haarg
in thread Moo's coerce and isa difference by krautcat

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.