locked_user sundialsvc4 has asked for the wisdom of the Perl Monks concerning the following question:
Trying to follow the example in Moose::Meta::Attribute::Native::Trait::Hash, specifically the example at the middle of the page under kv, with this code: (Perl 5.10.0 on OS/X)
... gives ...package foo; use Moose; has "k"=>( traits=>["Hash"], is=>"rw", isa=>"HashRef[Str]", default=>sub{{}} ); my $f=foo->new(); print $f->k->kv;
Can't call method "kv" on unblessed reference
Incidentally, I find that ref($f) eq 'foo' and that ref($f->k) eq 'HASH'. So it is, indeed, “an unblessed reference.”
So, I guess I just really don't understand how these Trait thingies are supposed to work ... (A quick cover-your-asterisk check confirms that including or omitting handles as per the Synopsis makes no difference, as expected.)
I honestly think that I am writing the test exactly a shown in the page and it’s not working. I pray for Enlightenment.
Update: I now took the exact text of the example on the page and got the same result:
package Stuff; use Moose; has 'options' => ( traits => ['Hash'], is => 'ro', isa => 'HashRef[Str]', default => sub { {} }, handles => { set_option => 'set', get_option => 'get', has_no_options => 'is_empty', num_options => 'count', delete_option => 'delete', option_pairs => 'kv', }, ); my $object=Stuff->new(); for my $pair ( $object->options->kv ) { print "$pair->[0] = $pair->[1]\n"; }
| Replies are listed 'Best First'. | |
|---|---|
|
Re: (Moose/Hash): Can't call method "kv" on unblessed reference
by chromatic (Archbishop) on Nov 16, 2011 at 23:45 UTC | |
|
Re: (Moose/Hash): Can't call method "kv" on unblessed reference
by ikegami (Patriarch) on Nov 17, 2011 at 00:18 UTC | |
by locked_user sundialsvc4 (Abbot) on Nov 17, 2011 at 01:08 UTC | |
|
Re: (Moose/Hash): Can't call method "kv" on unblessed reference
by locked_user sundialsvc4 (Abbot) on Nov 17, 2011 at 00:12 UTC | |
|
Re: (Moose/Hash): Can't call method "kv" on unblessed reference
by locked_user sundialsvc4 (Abbot) on Nov 16, 2011 at 23:50 UTC |