MooseX...omFile(3pm)
π MooseX::ConfigFromFile π
okay bestie, so this is a page from the User Contributed Perl Documentation set β MooseX::ConfigFromFile(3pm), to be exact. it's a whole abstract Moose role for setting attributes from a configfile and it is, genuinely, a little bit iconic. let's get into it π«Ά
β¨ NAME β¨
MooseX::ConfigFromFile β an abstract Moose role for setting attributes from a configfile. yes that is literally the whole name. slay.
πΈ VERSION πΈ
version 0.14. noted. locked in. π
π¦ SYNOPSIS π¦
bestie, here's the whole vibe. three parts. first: a real role built on top of this abstract one:
########
## A real role based on this abstract role:
########
package MooseX::SomeSpecificConfigRole;
use Moose::Role;
with 'MooseX::ConfigFromFile';
use Some::ConfigFile::Loader ();
sub get_config_from_file {
my ($class, $file) = @_;
my $options_hashref = Some::ConfigFile::Loader->load($file);
return $options_hashref;
}
then a class that actually consumes it:
########
## A class that uses it:
########
package Foo;
use Moose;
with 'MooseX::SomeSpecificConfigRole';
# optionally, default the configfile:
sub _get_default_configfile { '/tmp/foo.yaml' }
# ... insert your stuff here ...
and finally a lil script that uses that class with a configfile:
########
## A script that uses the class with a configfile
########
my $obj = Foo->new_with_config(configfile => '/etc/foo.yaml', other_opt => 'foo');
that's the whole flow. no cap. π
π DESCRIPTION π
so this is an abstract role. what it does is hand you an alternate constructor for making objects using parameters that got passed in from a configuration file. the actual reading of the config file? not its problem. that part is left to concrete sub-roles. delegation, queen behavior. π
it declares a configfile attribute and a class method new_with_config, and it requires that any concrete roles derived from it implement the class method get_config_from_file.
attributes you pass in directly as arguments to new_with_config supersede the ones sitting in the configfile. direct args win. periodt.
also: MooseX::Getopt knows about this abstract role, and if it's available it'll use it to load attributes from the file specified by the command line flag --configfile during its normal new_with_options. iykyk. β¨
π Attributes π
π configfile π
this one is a Path::Tiny object, and it can be coerced from a regular path string or from any object that supports stringification. it's the file your attributes get loaded from, bestie. you can add a default configfile in the consuming class and it will be honored at the appropriate time β scroll down to _get_default_configfile to see how. π
and if you have MooseX::Getopt installed, this attribute will also get the Getopt trait supplied, which means you can set the configfile straight from the command line too. versatile! π₯
π§ Class Methods π§
β new_with_config β
this is an alternate constructor, and it knows to look for the configfile option in its arguments and use that to set attributes. it's much like MooseX::Getopt's new_with_options. example time:
my $foo = SomeClass->new_with_config(configfile => '/etc/foo.yaml');
and remember: explicit arguments will override anything set by the configfile. you're the main character, your args come first. π
πΈ get_config_from_file πΈ
this class method is not implemented in this role β nope, not here. but it is required of all classes or roles that consume this role. its two arguments are the class name and the configfile, and it's expected to return a hashref of arguments to pass to new() which are sourced from the configfile. so like, that's your homework. do it. β¨
π¦ _get_default_configfile π¦
this class method is also not implemented in this role, but it can and should be defined in a consuming class or role to return the default value of the configfile β that's the case when nothing got passed into the constructor explicitly. π
π AUTHOR π
Brandon L. Black <blblack@gmail.com>
π CONTRIBUTORS π
- Karen Etheridge <ether@cpan.org>
- Tomas Doran <bobtfish@bobtfish.net>
- Chris Prather <chris@prather.org>
- Yuval Kogman <nothingmuch@woobling.org>
- Zbigniew Lukasiak <zby@cpan.org>
π COPYRIGHT AND LICENSE π
this software is copyright (c) 2007 by Brandon L. Black.
it's free software, bestie β you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. π«Ά
okay so that's the whole page, and honestly? it ate. π this was MooseX::ConfigFromFile(3pm) from perl v5.34.0, dated 2022-06-15, and that's the tea. go forth and configfile, queen. πβ¨π
π¦ where this page comes from
- debian bookworm: libmoosex-configfromfile-perl 0.14-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libmoosex-configfromfile-perl 0.14-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libmoosex-configfromfile-perl 0.14-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libmoosex-configfromfile-perl 0.14-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libmoosex-configfromfile-perl 0.14-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