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?
In reply to constraining the keys of a HashRef in Moose by tomgracey
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |