On Wednesday 22 September 2010 13:43:02 C K Kashyap wrote:
> Hi,
> I have a requirement for which I think Traits will be a solution.
>
> I have a class - MyClass that the users subclass to use it. The users
> add attributes to the subclass, some of which need a default value to
> be picked up from a config file.
> So, I want to allow the users to indicate that the default value needs
> to be loaded from config file via a trait - SpecialAttributes
>
> package SubClass;
> use Moose;
> extends 'MyClass';
>
> for (qw/apple mango banana/) {
> has $_ => (
> is => 'rw',
> isa => 'Str',
> traits => [qw/SpecialAttributes/],
> );
> }
>
> package main;
> my $c=SubClass->new;
> print $c->apple,"\n";
>
> I get this error when I run the program (I've attached Main.pl and
> SpecialArguments.pm for reference)
> Can't use an undefined value as a HASH reference at
> /home/y/lib/perl5/site_perl/5.8/i686-linux-64int/Moose/Meta/Attribute/Nativ
> e/MethodProvider/Hash.pm line 22.
>
> I'd appreciate it very very much if you could give me a solution.
> I've pasted the code here - http://hpaste.org/40028/moose_code since I
> am not sure if the list supports attachments.
The list supports attachment alright. Your problem is that the has
'argument_values' declaration missed the 'lazy => 1,'. Fixing it to read:
{{{
has 'argument_values' => (
isa => 'HashRef',
traits => [qw/Hash/],
builder => '_build_argument_values',
handles => {
'get_argument_value' => 'get',
'has_argument_value' => 'exists'
},
lazy => 1,
);
}}}
Causes the output to be "A" and no exception thrown. The problem is that the
"argument_values" slot was used while still being uninitialised.
Regards,
Shlomi Fish
--
-----------------------------------------------------------------
Shlomi Fish http://www.shlomifish.org/
Understand what Open Source is - http://shlom.in/oss-fs
<rindolf> She's a hot chick. But she smokes.
<go|dfish> She can smoke as long as she's smokin'.
Please reply to list if it's a mailing list post - http://shlom.in/reply .