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

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;

Replies are listed 'Best First'.
Re^3: Moo-Type checking example from Perl Maven: What should be the expected result?
by hippo (Archbishop) on Sep 29, 2018 at 21:39 UTC
    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.