Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

MooseX::SimpleConfig: role attribute cannot be made lazy

by lune (Pilgrim)
on Oct 26, 2011 at 12:28 UTC ( #933872=perlquestion: print w/replies, xml ) Need Help??

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 zby (Vicar) on Oct 27, 2011 at 10:20 UTC
    There is an alternative to MooseX::SimpleConfig at CPAN now: Config::Role.
Re: MooseX::SimpleConfig: role attribute cannot be made lazy
by choroba (Archbishop) 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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://933872]
Approved by planetscape
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (3)
As of 2022-06-28 00:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My most frequent journeys are powered by:









    Results (89 votes). Check out past polls.

    Notices?