in reply to Moo-Type checking example from Perl Maven: What should be the expected result?

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.

Replies are listed 'Best First'.
Re^2: Moo-Type checking example from Perl Maven: What should be the expected result?
by tobyink (Canon) on Sep 29, 2018 at 21:27 UTC

    Thanks for the advertisement. :)

    I'll also point out that /^\d+$/ isn't a great integer check. \d allows more characters than the digits 0 to 9. It also allows a whole bunch of other Unicode digit characters, which 99% of the time, you probably didn't really intend to allow. So use /^[0-9]+$/ which is barely any more typing.

    I'll also just give a quick demo of:

    package Person; use Moo; use Types::Standard 'Int'; has name => (is => 'rw'); has age => (is => 'rw', isa => Int->where(q{ $_ > 0 })); 1;

    ... as a simple way to prohibit negative integers. Or even:

    package Person; use Moo; use Types::Common::Numeric 'PositiveInt'; has name => (is => 'rw'); has age => (is => 'rw', isa => PositiveInt); 1;

    Or:

    package Person; use Moo; use Types::Common::Numeric 'IntRange'; has name => (is => 'rw'); has age => (is => 'rw', isa => IntRange[0,200]); # 200 is a realisti +c age limit 1;
      So use /^[0-9]+$/ which is barely any more typing.

      Quite right. There's also an even shorter alternative: /^\d+$/a which uses the handy /a modifier. This has the added benefit of enforcing ASCII restriction on some other classes too, should they be used in the regex.

        Indeed, but it was only added in 5.14, and I usually like to support older versions of Perl when possible.