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
The output is more verbose:package Person; use Moo; use Types::Standard 'Int'; has name => (is => 'rw'); has age => (is => 'rw', isa => Int ); 1;
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.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($_))
Hope this helps!
|
|---|
| 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 | |
by hippo (Archbishop) on Sep 29, 2018 at 21:39 UTC | |
by tobyink (Canon) on Sep 29, 2018 at 21:46 UTC |