MooseX...Config(3pm)

πŸ“¦
version 0.11 Β· 4 versions
, in debian bookworm, debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking

πŸ’– 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>


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