Hi, Moo's a great choice. Also good to learn by doing, e.g. by writing your own simple isa type validator. But as ++choroba says you probably made a typo in your code, a "type typo" I guess. But that's a good illustration of one good reason to use libraries for common functionality. In the case of Moo type checking we have a very powerful and easy to use system in Type::Tiny.

Your code could be rewritten as

package Person; use Moo; use Types::Standard 'Int'; has name => (is => 'rw'); has age => (is => 'rw', isa => Int ); 1;
The output is more verbose:
perl -I. 1223262.pl Foo 22 Value "young" did not pass type constraint "Int" (in $self->{"age"}) a +t 1223262.pl line 9 "Int" is a subtype of "Num" "Num" is a subtype of "LaxNum" Value "young" did not pass type constraint "LaxNum" (in $self->{"a +ge"}) "LaxNum" is defined as: (defined($_) && !ref($_) && Scalar::Util:: +looks_like_number($_))
If you want to control the message, you can, by declaring the type yourself. (This would also allow you to disallow people who haven't been born yet!) See Type::Library for more info on that.

Hope this helps!


The way forward always starts with a minimal test.

In reply to Re: Moo-Type checking example from Perl Maven: What should be the expected result? by 1nickt
in thread Moo-Type checking example from Perl Maven: What should be the expected result? by Perl300

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.