http://qs321.pair.com?node_id=933872

lune has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I want to create a class that reads in simple yaml config files, based on an argument.

With MooseX::SimpleConfig I want the path to be constructed from the argument and some base path already known. However, it seems the role Attribute '+configfile' cannot be made lazy and is always being created before anything else:

#!/usr/bin/perl package My::Config; use Moose; with 'MooseX::SimpleConfig'; has filename => ( is => 'ro', isa => 'Str', required => 1, ); has '+configfile' => ( default => sub { my $self = shift; '/some/path/' . $self->filename + . ".yaml"; }, lazy => 1, ); my $config = My::Config->new_with_config( filename => "config");
This code gives me:

Can't call method "filename" on an undefined value at ./myconf3.pl line 11.

Or is there an error on my side?

Replies are listed 'Best First'.
Re: MooseX::SimpleConfig: role attribute cannot be made lazy
by stvn (Monsignor) on Oct 26, 2011 at 17:04 UTC

    The problem is that MooseX::ConfigFromFile (the "parent" role of MooseX::SimpleConfig) is doing some pretty odd stuff with the configfile attribute, such as trying to call the default before an instance has been created (see here for details). What you are trying to do simply won't work because of this. Probably the simplest alternative is to do something like:

    #!/usr/bin/perl package My::Config; use Moose; with 'MooseX::SimpleConfig'; sub new_from_config_shortname { my ($class, $filename) = @_; $class->new_from_config( configfile => '/some/path/' . $filename . + ".yaml" ); } my $config = My::Config->new_from_config_shortname("config");
    Obviously shorten 'new_from_config_shortname' to something more appropriate, but that should give you what you want.

    -stvn

      Thank you, this works - except the call to "new_from_config" should read "new_with_config"

      Should this be reported as an error in MooseX::SimpleConfig?

        Should this be reported as an error in MooseX::SimpleConfig?

        You can, but the original author is mostly AWOL and the maintainer might not be interested in untangling this mess. If nothing more it can become a documentation caveat.

        -stvn
Re: MooseX::SimpleConfig: role attribute cannot be made lazy
by choroba (Cardinal) on Oct 26, 2011 at 15:13 UTC
    Perhaps a problem/bug(?) in MooseX::SimpleConfig, because for plain Moose, it works fine:
    package My::Base; use Moose; has filename => (is => "ro", isa => "Str", required => 1); has config => (is => "ro"); no Moose; package My::Derived; use Moose; extends "My::Base"; has "+config" => (lazy => 1, default => sub {my $self = shift; $self->filename . +".cfg"}); no Moose; package main; my $x=My::Derived->new(filename=>"/etc/myconfig"); print $x->config,"\n";'
    Prints
    /etc/myconfig.cfg
Re: MooseX::SimpleConfig: role attribute cannot be made lazy
by zby (Vicar) on Oct 27, 2011 at 10:20 UTC
    There is an alternative to MooseX::SimpleConfig at CPAN now: Config::Role.