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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.