MooseX::SimpleConfig - A Moose role for setting attributes from a simple configuration file |
MooseX::SimpleConfig - A Moose role for setting attributes from a simple configuration file
version 0.11
## A YAML configfile named /etc/my_app.yaml: foo: bar baz: 123
## In your class package My::App; use Moose;
with 'MooseX::SimpleConfig';
has 'foo' => (is => 'ro', isa => 'Str', required => 1); has 'baz' => (is => 'rw', isa => 'Int', required => 1);
# ... rest of the class here
## in your script #!/usr/bin/perl
use My::App;
my $app = My::App->new_with_config(configfile => '/etc/my_app.yaml'); # ... rest of the script here
#################### ###### combined with MooseX::Getopt:
## In your class package My::App; use Moose;
with 'MooseX::SimpleConfig'; with 'MooseX::Getopt';
has 'foo' => (is => 'ro', isa => 'Str', required => 1); has 'baz' => (is => 'rw', isa => 'Int', required => 1);
# ... rest of the class here
## in your script #!/usr/bin/perl
use My::App;
my $app = My::App->new_with_options(); # ... rest of the script here
## on the command line % perl my_app_script.pl -configfile /etc/my_app.yaml -otherthing 123
This role loads simple files to set object attributes. It is based on the abstract role the MooseX::ConfigFromFile manpage, and uses the Config::Any manpage to load your configuration file. the Config::Any manpage will in turn support any of a variety of different config formats, detected by the file extension. See the Config::Any manpage for more details about supported formats.
To pass additional arguments to the Config::Any manpage you must provide a
config_any_args()
method, for example:
sub config_any_args { return { driver_args => { General => { '-InterPolateVars' => 1 } } }; }
Like all the MooseX::ConfigFromFile manpage -derived file loaders, this
module is automatically supported by the the MooseX::Getopt manpage role as
well, which allows specifying -configfile
on the command line.
Provided by the base role the MooseX::ConfigFromFile manpage. You can provide a default configuration file pathname like so:
has '+configfile' => ( default => '/etc/myapp.yaml' );
You can pass an array of filenames if you want, but as usual the array has to be wrapped in a sub ref.
has '+configfile' => ( default => sub { [ '/etc/myapp.yaml', '/etc/myapp_local.yml' ] } );
Config files are trivially merged at the top level, with the right-hand files taking precedence.
Provided by the base role the MooseX::ConfigFromFile manpage. Acts just like
regular new()
, but also accepts an argument configfile
to specify
the file from which to load other attributes. Explicit arguments
to new_with_config
will override anything loaded from the file.
Called internally by either new_with_config
or the MooseX::Getopt manpage's
new_with_options
. Invokes the Config::Any manpage to parse configfile
.
Brandon L. Black <blblack@gmail.com>
This software is copyright (c) 2007 by Brandon L. Black <blblack@gmail.com>.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
MooseX::SimpleConfig - A Moose role for setting attributes from a simple configuration file |