in reply to Re^5: Why does changing a Moose attributes' properties remove the around modifier
in thread Why does changing a Moose attributes' properties remove the around modifier
If we suppose you have a good reason to store the IP address an object, then I still find your approach very poor. Don't try to change the behaviour of an existing getter; create a getter that has the behaviour your want.
coerce 'NetAddr::IP' => from 'Str' => via { NetAddr::IP->new($_, '255.255.255.255') }; for my $field (qw( server peer monitor netlog )) { my $object = "${field}_object"; has $object => ( init_arg => $field, is => 'ro', isa => 'NetAddr::IP', coerce => 1, required => 1, handles => { $field => sub { $_[0]->$object->addr }, }, ); }
This makes
a shortcut for$o->server
$o->server_object->addr
The constructor takes
rather thanC->new(server => ...)
thanks to init_arg.C->new(server_object => ...)
Thanks to coerce => 1 and the coercion rule, both a NetAddr::IP object and a IP address in string form can be used as the value passed to the constructor.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^7: Why does changing a Moose attributes' properties remove the around modifier
by mcrose (Beadle) on Jul 20, 2011 at 14:18 UTC | |
by ikegami (Patriarch) on Jul 20, 2011 at 18:46 UTC |