2009/12/22 Shlomi Fish <shlo...@iglu.org.il>: > On Tuesday 22 Dec 2009 15:10:06 Dermot wrote: >> 2009/12/22 Shlomi Fish <shlo...@iglu.org.il>: >> > Hi Dermot! >> >> Hi Shlomi, >> > > Hi. > >> >> 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, >> >> ); >> >> ... >> >> > 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. >> >> Okay, I'll take that advice. So now I have >> >> has 'config' => ( >> is => 'ro', >> lazy_build => 1, >> ); >> >> sub _build_config { >> my $self = shift; >> my $config = AppConfig->new( { >> CREATE => 1, >> GLOBAL => { >> ARGCOUNT => 1, >> }, >> }); >> my $config_file = "$Bin/../spl.conf"; >> $self->croak("Can't open config file $config_file\n") if (! -e >> $config_file); >> $config->file($config_file); >> return $config; >> } > > I don't see where you've associated << _build_config >> with the 'config' > attribute. Or is this some kind of undocumented default or a property of your > meta-class.
>From Moose pod: lazy_build => (0|1) Automatically define lazy => 1 as well as builder => "_build_$attr", clearer => "clear_$attr', predicate => 'has_$attr' unless they are already defined. so lazy_build is shorthand for has config => ( is => 'ro', builder=> _build_foo); sub _build_foo {} Is will take all $attr and prepend _build_ to it and look for the relevant method, in this case "_build_config". Least that what I took it to mean. Dp. >> Elkles cake....poor I know. >> > > What is Elkles? http://www.google.com/search?q=elkles does not seem to know. I > know what an "Elk" is. Eccles. I did say it was poor. Perhaps I should have said shockingly poor and tenuous. Dp.