1 package MooseX::SimpleConfig;
4 with 'MooseX::ConfigFromFile';
10 sub get_config_from_file {
11 my ($class, $file) = @_;
13 $file = $file->() if ref $file eq 'CODE';
14 my $files_ref = ref $file eq 'ARRAY' ? $file : [$file];
16 my $can_config_any_args = $class->can('config_any_args');
17 my $extra_args = $can_config_any_args ?
18 $can_config_any_args->($class, $file) : {};
20 my $raw_cfany = Config::Any->load_files({
28 foreach my $file_tested ( reverse @{$files_ref} ) {
29 if ( ! exists $raw_cfany->{$file_tested} ) {
30 warn qq{Specified configfile '$file_tested' does not exist, } .
31 qq{is empty, or is not readable\n};
35 my $cfany_hash = $raw_cfany->{$file_tested};
36 die "configfile must represent a hash structure in file: $file_tested"
37 unless $cfany_hash && ref $cfany_hash && ref $cfany_hash eq 'HASH';
39 %raw_config = ( %raw_config, %{$cfany_hash} );
53 MooseX::SimpleConfig - A Moose role for setting attributes from a simple configfile
57 ## A YAML configfile named /etc/my_app.yaml:
65 with 'MooseX::SimpleConfig';
67 has 'foo' => (is => 'ro', isa => 'Str', required => 1);
68 has 'baz' => (is => 'rw', isa => 'Int', required => 1);
70 # ... rest of the class here
77 my $app = My::App->new_with_config(configfile => '/etc/my_app.yaml');
78 # ... rest of the script here
81 ###### combined with MooseX::Getopt:
87 with 'MooseX::SimpleConfig';
88 with 'MooseX::Getopt';
90 has 'foo' => (is => 'ro', isa => 'Str', required => 1);
91 has 'baz' => (is => 'rw', isa => 'Int', required => 1);
93 # ... rest of the class here
100 my $app = My::App->new_with_options();
101 # ... rest of the script here
103 ## on the command line
104 % perl my_app_script.pl -configfile /etc/my_app.yaml -otherthing 123
108 This role loads simple configfiles to set object attributes. It
109 is based on the abstract role L<MooseX::ConfigFromFile>, and uses
110 L<Config::Any> to load your configfile. L<Config::Any> will in
111 turn support any of a variety of different config formats, detected
112 by the file extension. See L<Config::Any> for more details about
115 To pass additional arguments to L<Config::Any> you must provide a
116 C<config_any_args()> method, for example:
118 sub config_any_args {
120 driver_args => { General => { '-InterPolateVars' => 1 } }
124 Like all L<MooseX::ConfigFromFile> -derived configfile loaders, this
125 module is automatically supported by the L<MooseX::Getopt> role as
126 well, which allows specifying C<-configfile> on the commandline.
132 Provided by the base role L<MooseX::ConfigFromFile>. You can
133 provide a default configfile pathname like so:
135 has '+configfile' => ( default => '/etc/myapp.yaml' );
137 You can pass an array of filenames if you want, but as usual the array
138 has to be wrapped in a sub ref.
140 has '+configfile' => ( default => sub { [ '/etc/myapp.yaml', '/etc/myapp_local.yml' ] } );
142 Config files are trivially merged at the top level, with the right-hand files taking precedence.
146 =head2 new_with_config
148 Provided by the base role L<MooseX::ConfigFromFile>. Acts just like
149 regular C<new()>, but also accepts an argument C<configfile> to specify
150 the configfile from which to load other attributes. Explicit arguments
151 to C<new_with_config> will override anything loaded from the configfile.
153 =head2 get_config_from_file
155 Called internally by either C<new_with_config> or L<MooseX::Getopt>'s
156 C<new_with_options>. Invokes L<Config::Any> to parse C<configfile>.
160 Brandon L. Black, E<lt>blblack@gmail.comE<gt>
162 =head1 CURRENT MAINTAINER
164 Tomas Doran (t0m), C<< <bobtfish@bobtfish.net> >>.
168 This library is free software; you can redistribute it and/or modify
169 it under the same terms as Perl itself.