I'm having a brain fart here. I have a Moose property:
has '_posts' => (is => 'ro', lazy => 1, traits => ['Hash'], isa => 'Ha +shRef[HashRef]', builder => '_load_posts', + handles => { set_post => 'set', get_post => 'get', num +_posts => 'count', + post_pairs => 'kv', post_keys => 'keys' }, writer => ' +_set_posts');
As you can see, it has a trait of a hash reference which will store other hash reference values inside of it. I am initializing the property with data from an empty file created like so:
use Storable; store { {} }, $file; # This line needs to be changed. ... $s->_set_posts(retrieve $file);
But I get an error:
Attribute (_posts) does not pass the type constraint because: Validati +on failed for 'HashRef[HashRef]' with value 1 at native delegation me +thod Site::Posts::num_posts (count) of attribute _posts
How do I create an empty hash reference of hash references properly to satisfy the type restraint for my property? Here is the entire package:
package Site::Posts ; use Moose; use Storable; use Data::Dumper qw(Dumper); use namespace::autoclean; has '_posts' => (is => 'ro', lazy => 1, traits => ['Hash'], isa => 'Ha +shRef[HashRef]', builder => '_load_posts', handles => { set_post => 'set', get_post => 'get', num +_posts => 'count', post_pairs => 'kv', post_keys => 'keys' }, writer => ' +_set_posts'); has 'file' => (is => 'ro', isa => 'Str', required => 1 ); ### Public methods ### sub list { my $s = shift; if ($s->num_posts) { my @keys = $s->post_keys; print Dumper \@keys; } } sub add { my $s = shift; my $id = shift; $s->set_post($id => {exists => 1}); } sub delete { my $s = shift; store { {} }, $s->file; } sub save { my $s = shift; store $s->_posts, $s->file; } ### Private methods ### sub _load_posts { + + my $s = shift; + + + + if ( !-f $s->file ) { + + + store { {} }, $s->file; + + } + + my $posts = retrieve $s->file; + + $s->_set_posts($posts); + + } + + 1;
$PM = "Perl Monk's";
$MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate Priest";
$nysus = $PM . ' ' . $MCF;
Click here if you love Perl Monks
In reply to Creating an empty hash reference of an empty hash reference by nysus
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |