http://qs321.pair.com?node_id=933396

lune has asked for the wisdom of the Perl Monks concerning the following question:

Hello everybody,

I want to use NetAddrIP as subtype and expected this to be quite straightforward:

package myip; use Moose; use MooseX::Types::NetAddr::IP qw( NetAddrIP NetAddrIPv4 NetAddrIPv6 ) +; has ip => ( is => 'rw', isa => 'NetAddrIP', ); my $ip = myip->new(ip => '127.0.0.1' ); print $ip->ip() . "\n";
However, this gives me a typechecking error, saying "Validation failed for 'NetAddrIP' with value 127.0.0.1". Any ideas what I'm missing?

Replies are listed 'Best First'.
Re: problem with using subtype from MooseX::Types::NetAddr::IP
by zwon (Abbot) on Oct 24, 2011 at 14:45 UTC

    As you declared ip attribute as 'NetAddrIP' Moose want it to be reference to NetAddrIP, you're passing the string. The points you're missing is that NetAddrIP should be without quotes (look into MooseX::Types documentation for the explanation, shortly -- NetAddrIP is a function call), and also you should allow coercion of attribute:

    has ip => ( is => 'rw', isa => NetAddrIP, coerce => 1, );