in reply to Re: Moose coerce
in thread Moose coerce
package test; use Moose; use Moose::Util::TypeConstraints; subtype 'mytype' => as 'HashRef[Str]' ; coerce 'mytype' => from 'Str' => via { my %h = map { split('=', $_) } split(',', $_); return \%h; } ; has 'mytype' => ( traits => ['Hash'], is => 'rw', isa => 'mytype', coerce => 1, # presumably I need an explicit accessor # to prevent coercion when I pass # a key in to access the value? handles => { get_option => 'get', }, ); package main; use Data::Dumper; my $t = test->new; # coerce into a hash $t->mytype('foo=bar,foo2=bar2'); warn Dumper $t->mytype; # how do i now access the hash by key? # this tries to coerce all over again! warn $t->mytype('foo'); # and this doesn't work at all warn $t->mytype_get('foo');
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Moose coerce
by duelafn (Parson) on Oct 21, 2010 at 12:40 UTC | |
by Anonymous Monk on Oct 21, 2010 at 13:08 UTC |