I wrote a Moose role that fetches and analyzes ifconfig output to nice hash of interfaces and IPs for each interface. I wanted to use Net::Ifconfig::Wrapper which seems highly portable but something didn't work properly with the virtual interfaces of VPS servers. Also, I ended up not being able to run remotely-installed modules. I tried IPC::PerlSSH which is nice but ended up opting for just fetching and parsing it. Sometimes it comes out faster, ya know?
I wanted to make sure I have the correct input and that means some additional testing in the code itself. It's loops and checks and more regexp and so on and so on. Instead, I decided to let Moose do the work:
use Moose::Role; use Moose::Util::TypeConstraints; use Data::Validate::IP qw( is_ipv4 ); requires qw( connect_server ); subtype 'Ifaces' => as 'HashRef', => where { foreach my $suspect_ip ( values %{ $_[0] } ) { is_ipv4($suspect_ip) || return 0; } return 1; } => message { 'Incorrect Ifaces' }; has 'ifaces' => ( is => 'rw', isa => 'Ifaces', );
Then, in my testing suite I wrote this additional test:
use Test::More qw( no_plan ); use Test::Block qw( $Plan ); use Test::Exception; # ... some more stuff up here ... { # testing ifaces type local $Plan = 2; my %ifaces = ( 'venet0' => '1.1.2.3', 'venet2' => '1.2.3.4', 'venet3' => '2.2.2.2', ); ok( $map->ifaces(\%ifaces), 'good ifaces' ); $ifaces{'venet0'} = '1.2.3.'; throws_ok { $map->ifaces(\%ifaces) } qr/Incorrect Ifaces/, 'bad ifaces'; }
Sidenote: isn't it weird that the cookbook for Moose says "How to cook a Moose?" Love the Moose, don't cook it! (have it help you in the kitchen, maybe?)
... and people should stop telling me there's more than one way to skin a cat. Quit skinning cats, people, it's very unnerving :)