subtypes + type strictness in Perl

chorny on 2010-08-08T20:08:38

file IPType.pm

package IPType; use Regexp::Common 2.105; use MooseX::Types::Moose qw( Str ); use MooseX::Types -declare => [ qw(IP) ];

subtype IP, as Str, where { $_ =~ /\A $RE{net}{IPv4} \z/msx }, message { "'$_' is not a valid IP"; };

file typed.pl

use IPType qw/IP/; use MooseX::Lexical::Types qw/IP/;

my IP $foo; # declare typed variable $foo = '192.168.1.1'; # works $foo = 'bar'; # fails