型変換 (coerce)User.pmpackage User; use Mouse; use Mouse::Util::TypeConstraints; use URI; subtype 'My::Types::URI' => as class_type('URI'); coerce 'My::Types::URI' => from 'Str' => via { URI->new($_); }; has 'uri' => ( is => 'rw', isa => 'My::Types::URI', default => sub { URI->new }, coerce => 1 ); __PACKAGE__->meta->make_immutable(); 1; main.plmy $user = User->new( uri => 'http://www.yahoo.co.jp/' ); say ref $user->uri; 説明
参考http://perldoc.perlassociation.org/pod/Moose-Doc-JA/Moose/Cookbook/Basics/Recipe5.html |
|