3 MooseX::ConfigFromFile - An abstract Moose role for setting attributes from a configfile
8 ## A real role based on this abstract role:
11 package MooseX::SomeSpecificConfigRole;
14 with 'MooseX::ConfigFromFile';
16 use Some::ConfigFile::Loader ();
18 sub get_config_from_file {
19 my ($class, $file) = @_;
21 my $options_hashref = Some::ConfigFile::Loader->load($file);
23 return $options_hashref;
29 ## A class that uses it:
33 with 'MooseX::SomeSpecificConfigRole';
35 # optionally, default the configfile:
36 sub _get_default_configfile { '/tmp/foo.yaml' }
38 # ... insert your stuff here ...
41 ## A script that uses the class with a configfile
44 my $obj = Foo->new_with_config(configfile => '/etc/foo.yaml', other_opt => 'foo');
48 This is an abstract role which provides an alternate constructor for creating
49 objects using parameters passed in from a configuration file. The
50 actual implementation of reading the configuration file is left to
53 It declares an attribute `configfile` and a class method `new_with_config`,
54 and requires that concrete roles derived from it implement the class method
55 `get_config_from_file`.
57 Attributes specified directly as arguments to `new_with_config` supersede those
60 [MooseX::Getopt](http://search.cpan.org/perldoc?MooseX::Getopt) knows about this abstract role, and will use it if available
61 to load attributes from the file specified by the command line flag `--configfile`
62 during its normal `new_with_options`.
68 This is a [Path::Tiny](http://search.cpan.org/perldoc?Path::Tiny) object which can be coerced from a regular path
69 string or any object that supports stringification.
70 This is the file your attributes are loaded from. You can add a default
71 configfile in the consuming class and it will be honored at the appropriate
72 time; see below at ["\_get\_default\_configfile"](#\_get\_default\_configfile).
74 If you have [MooseX::Getopt](http://search.cpan.org/perldoc?MooseX::Getopt) installed, this attribute will also have the
75 `Getopt` trait supplied, so you can also set the configfile from the
82 This is an alternate constructor, which knows to look for the `configfile` option
83 in its arguments and use that to set attributes. It is much like [MooseX::Getopts](http://search.cpan.org/perldoc?MooseX::Getopts)'s
84 `new_with_options`. Example:
86 my $foo = SomeClass->new_with_config(configfile => '/etc/foo.yaml');
88 Explicit arguments will override anything set by the configfile.
90 ## get\_config\_from\_file
92 This class method is not implemented in this role, but it is required of all
93 classes or roles that consume this role.
94 Its two arguments are the class name and the configfile, and it is expected to return
95 a hashref of arguments to pass to `new()` which are sourced from the configfile.
97 ## \_get\_default\_configfile
99 This class method returns nothing by default, but can and should be redefined
100 in a consuming class to return the default value of the configfile (if not
101 passed into the constructor explicitly).
105 Copyright (c) - the MooseX::ConfigFromFile "AUTHOR" and "CONTRIBUTORS" as listed below.
109 Brandon L. Black, <blblack@gmail.com>
120 This library is free software; you can redistribute it and/or modify
121 it under the same terms as Perl itself.