Hi Dermot!
On Tuesday 22 Dec 2009 14:28:30 Dermot wrote:
> aHi,
>
> I just starting out with Moose so forgive me if I'm slow on the up-take.
>
> I have a parent class and I want it to contain some configuration
> information. I'm using AppConfig and that returns configuration as
> instance. But when I try and access data from the configuration, such
> as `print $self->config->media_root`, I am getting "unblessed
> reference" errors.
>
> I am not sure if my attribute option choices are best suited for what
> I want. Can anyone give me some pointers? I could use Config::General
> but, as an exercise, I'd like to know how to install an instance
> within my classes.
> Thanks in advance,
> Dp.
>
>
> package Media;
>
> use metaclass (
> metaclass => 'Moose::Meta::Class',
> error_class => 'Moose::Error::Croak',
> );
> use Moose;
> use AppConfig;
> use FindBin qw($Bin);
> use lib "$Bin/../";
>
>
> has 'config' => (
> is => 'ro',
> lazy => 1,
> default => sub{ {} },
> trigger => \&_get_config,
> );
Reading the description of trigger on "perldoc Moose", I see this:
<<<<<<<<<<<<
trigger => $code
The trigger option is a CODE reference which will be called
after the value of the attribute is set. The CODE ref will be
passed the instance itself and the updated value. If the
attribute already had a value, this will be passed as the third
value to the trigger.
You can have a trigger on a read-only attribute.
NOTE: Triggers will only fire when you assign to the attribute,
either in the constructor, or using the writer. Default and
built values will not cause the trigger to be fired.
>>>>>>>>>>>>
This doesn't seem to do what you want. I think you want a meaningful "default"
or "builder" instead:
<<<<<<<<<
builder => Str
The value of this key is the name of the method that will be
called to obtain the value used to initialize the attribute.
See the builder option docs in Class::MOP::Attribute
and/or Moose::Cookbook::Basics::Recipe8 for more information.
>>>>>>>>>
>
> sub _get_config {
> my $self = shift;
> my $config = AppConfig->new( {
> CREATE => 0,
> });
> $config->define('media_root',{ ARGCOUNT=> 'ARGCOUNT_ONE' });
> my $config_file = "$Bin/../media.conf";
> $self->croak("Can't open config file $config_file\n") if (! -e
> $config_file);
> $config->file($config_file);
> return $config;
> }
>
Again, this should most probably be a "builder" method.
Wow! This is the first time I was able to reply to a question here. Party at
my place at 16:00. Bring a lot of Moose-shaped cakes. (If it turned out, I was
misleading, I won't get to eat any of them - ;-)).
Regards,
Shlomi Fish
--
-----------------------------------------------------------------
Shlomi Fish http://www.shlomifish.org/
What does "Zionism" mean? - http://shlom.in/def-zionism
Bzr is slower than Subversion in combination with Sourceforge.
( By: http://dazjorz.com/ )