MooseX...Config(3pm)
π MooseX::SimpleConfig π
okay bestie, welcome to MooseX::SimpleConfig β a Moose role for setting attributes from a simple configuration file. it lives in the User Contributed Perl Documentation section, and it's filed under MooseX...Config(3pm) if you're looking for it on the shelf. pull up a chair, we're doing config and we're doing it cutely. β¨
πΈ VERSION πΈ
version 0.11 β yeah she's been around a minute, she's got history, she's iconic. π
β¨ SYNOPSIS β¨
okay so first, imagine a YAML configfile chilling at /etc/my_app.yaml:
## A YAML configfile named /etc/my_app.yaml:
foo: bar
baz: 123
now in your class, you just with the role and you're basically done:
## 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
and in your script? baby it's one line. one. π«Ά
## 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 π¦
now THIS is the crossover episode. you can also stack it with MooseX::Getopt:
####################
###### 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
and your script just calls new_with_options() β no configfile arg needed, she figures it out:
## in your script
#!/usr/bin/perl
use My::App;
my $app = My::App->new_with_options();
# ... rest of the script here
and on the command line you can literally just pass the flags, main character energy:
## on the command line
% perl my_app_script.pl -configfile /etc/my_app.yaml -otherthing 123
π DESCRIPTION π
okay so the whole vibe: this role loads simple files and uses them to set object attributes. it's built on top of the abstract role MooseX::ConfigFromFile, and it uses Config::Any to actually load your config file. and Config::Any? she's a multi-talented queen β she supports a whole variety of different config formats, and she figures out which one you mean by looking at the file extension. for the full tea on what formats are supported, go read Config::Any itself. π
if you wanna pass extra arguments through to Config::Any, you have to give it a config_any_args() method, like so:
sub config_any_args {
return {
driver_args => { General => { '-InterPolateVars' => 1 } }
};
}
and like every single one of the MooseX::ConfigFromFile-derived file loaders, this module gets automatically supported by the MooseX::Getopt role too β which is the whole reason you can just say -configfile on the command line like it's nothing. β¨
π ATTRIBUTES π
configfile
this one is provided by the base role MooseX::ConfigFromFile. you can give it a default config file pathname like this:
has '+configfile' => ( default => '/etc/myapp.yaml' );
and yeah, you can pass an array of filenames if you want β but as usual with Moose, the array has to be wrapped in a sub ref:
has '+configfile' => ( default => sub { [ '/etc/myapp.yaml', '/etc/myapp_local.yml' ] } );
now here's the important bit: config files get merged trivially at the top level, and the files on the right-hand side take precedence. so the last one wins, bestie. π
π CLASS METHODS π
new_with_config
provided by the base role MooseX::ConfigFromFile. it acts just like regular new(), except it also accepts an argument configfile so you can tell it which file to load the other attributes from. and listen β any explicit arguments you pass to new_with_config will override anything that got loaded from the file. explicit beats config, every time. π₯
get_config_from_file
this one gets called internally by either new_with_config or MooseX::Getopt's new_with_options. its whole job is to invoke Config::Any to parse your configfile. she's the behind-the-scenes bestie making it all work. π§
πΈ AUTHOR πΈ
Brandon L. Black <blblack@gmail.com>
β COPYRIGHT AND LICENSE β
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.
π CONTRIBUTORS π
- Karen Etheridge <ether@cpan.org>
- Tomas Doran <bobtfish@bobtfish.net>
- Brandon L Black <blblack@gmail.com>
- Alexander Hartmaier <alex.hartmaier@gmail.com>
- lestrrat <lestrrat+github@gmail.com>
- XXXXXX XXXXXXX <sromanov@cpan.org>
- Yuval Kogman <nothingmuch@woobling.org>
- Zbigniew Lukasiak <zby@cpan.org>
- Alex Howarth <alex.howarth@gmail.com>
okay that's the whole page bestie!! π this doc came to you from perl v5.34.0, dated 2022-06-15, and that's the vibe check, signed, sealed, delivered. go set some attributes, you've got this. mwah. πβ¨π¦
π¦ where this page comes from
- debian bookworm: libmoosex-simpleconfig-perl 0.11-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libmoosex-simpleconfig-perl 0.11-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libmoosex-simpleconfig-perl 0.11-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libmoosex-simpleconfig-perl 0.11-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libmoosex-simpleconfig-perl 0.11-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms