tomgracey has asked for the wisdom of the Perl Monks concerning the following question:
Dear Monks
What is the easiest way to constrain the keys of a HashRef in Moose?
I have been looking through the various type constraint modules in Moose (for the first time!) and am feeling a little overwhelmed.
I initially thought of something on the lines of
but then if I run the following script:#!/usr/bin/perl package Test::HashK; use Moose; use Moose::Util::TypeConstraints; enum 'colors', [qw(red green blue)]; has scores => ( is => 'rw', isa => 'HashRef[colors]', default => sub{{ +}} ); # I want the allowed *keys* to be colors 1;
#!/usr/bin/perl use strict; use Test::HashK; my $hk=Test::HashK->new; $hk->scores->{'red'} = 53; $hk->scores->{'black'} = 100; $hk->scores->{1} = 'blue'; $hk->scores->{2} = 'dinosaur';
it runs through to completion without any problem, thus not appearing to contrain either the keys or the values
Obviously I have no idea what I am doing! Any thoughts?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: constraining the keys of a HashRef in Moose
by tobyink (Canon) on Aug 29, 2013 at 17:49 UTC | |
by Your Mother (Archbishop) on Aug 29, 2013 at 23:04 UTC | |
by tomgracey (Scribe) on Aug 30, 2013 at 08:03 UTC | |
by kennethk (Abbot) on Aug 30, 2013 at 15:33 UTC | |
by tomgracey (Scribe) on Sep 02, 2013 at 08:53 UTC | |
by tobyink (Canon) on Sep 03, 2013 at 08:09 UTC | |
by tomgracey (Scribe) on Sep 04, 2013 at 11:13 UTC | |
|
Re: constraining the keys of a HashRef in Moose
by kennethk (Abbot) on Aug 29, 2013 at 15:30 UTC |