On Mon, 2 Aug 2010 19:50:12 +0200
Brian ROONEY <brian.xb.roo...@stericsson.com> wrote:

> Hi,
> 
> How does one get the helper traits for a parameterized type?  Here I
> am using the helper: Moose::Meta::Attribute::Native::Trait::Hash
> For HashRef
> 
> The hash actually contains parameterized CodeRefs so I would like to
> use: Moose::Meta::Attribute::Native::Trait::Code
> For CodeRef
> 
> Is this possible?
> Thanks
> 
> 
> has 'actions' => (
>       traits                  => ['Hash'],
>       is                      => 'rw', 
>       isa                     => 'HashRef[CodeRef]',  
>       required                => 0,
>       handles   => {
>           set_actions         => 'set',
>           get_actions         => 'get',
>           delete_action       => 'delete',
>           keys_actions        => 'keys',
>           exists_action       => 'exists',
>           defined_action      => 'defined',
>           values_actions      => 'values',
>           pairs_actions       => 'kv',          
>           elements_actions    => 'elements',
>           clear_actions       => 'clear',
>           num_actions         => 'count',          
>           has_no_actions      => 'is_empty',
>           accessor_actions    => 'accessor',        
>       }, 
> );
> 

Did you try it and see?



package Foo;
use Moose;

has 'bar' => (
    traits      => ['Hash'],
    is          => 'ro', 
    isa         => 'HashRef[CodeRef]',
    default     => sub { +{} },
    handles     =>
    {
        set_actions => 'set',
        get_actions => 'get',
    }
);

no Moose;

package main;

my $foo = Foo->new();
$foo->set_actions('Baz', sub { print "Yahoo!\n"; });
$foo->set_actions('Flarg', sub { print "Zoom!\n"; });
$foo->get_actions('Baz')->();
$foo->get_actions('Flarg')->();
$foo->set_actions('Yarg', 'Some random string'); # errors here;

__END__
Value Some random string did not pass container type constraint
'CodeRef'
at 
/home/nicholas/installed/perl5/perls/default/lib/site_perl/5.10.1/x86_64-linux/Moose/Meta/Attribute/Native/MethodProvider/Hash.pm
line 84
Moose::Meta::Attribute::Native::MethodProvider::Hash::__ANON__('Foo=HASH(0x1ee5400)',
'Yarg', 'Some random string') called
at 
/home/nicholas/installed/perl5/perls/default/lib/site_perl/5.10.1/x86_64-linux/Moose/Meta/Attribute/Native/Trait.pm
line 124 Foo::set_actions('Foo=HASH(0x1ee5400)', 'Yarg', 'Some random
string') called at /tmp/example.pl line 25 

Yahoo! 
Zoom!



-- 

Nicholas Perez
XMPP/Email: n...@nickandperla.net
http://search.cpan.org/~nperez/
http://github.com/nperez

Reply via email to