1 package MooseX::SimpleConfig;
4 with 'MooseX::ConfigFromFile';
10 sub get_config_from_file {
11 my ($class, $file) = @_;
13 my $files_ref = ref $file eq 'ARRAY' ? $file : [$file];
15 my $can_config_any_args = $class->can('config_any_args');
16 my $extra_args = $can_config_any_args ?
17 $can_config_any_args->($class, $file) : {};
19 my $raw_cfany = Config::Any->load_files({
27 foreach my $file_tested ( @{$files_ref} ) {
28 if ( ! exists $raw_cfany->{$file_tested} ) {
29 die qq{Specified configfile '$file_tested' does not exist, } .
30 q{is empty, or is not readable};
33 my $cfany_hash = $raw_cfany->{$file_tested};
34 die "configfile must represent a hash structure in file: $file_tested"
35 unless $cfany_hash && ref $cfany_hash && ref $cfany_hash eq 'HASH';
37 %raw_config = ( %raw_config, %{$cfany_hash} );
51 MooseX::SimpleConfig - A Moose role for setting attributes from a simple configfile
55 ## A YAML configfile named /etc/my_app.yaml:
63 with 'MooseX::SimpleConfig';
65 has 'foo' => (is => 'ro', isa => 'Str', required => 1);
66 has 'baz' => (is => 'rw', isa => 'Int', required => 1);
68 # ... rest of the class here
75 my $app = My::App->new_with_config(configfile => '/etc/my_app.yaml');
76 # ... rest of the script here
79 ###### combined with MooseX::Getopt:
85 with 'MooseX::SimpleConfig';
86 with 'MooseX::Getopt';
88 has 'foo' => (is => 'ro', isa => 'Str', required => 1);
89 has 'baz' => (is => 'rw', isa => 'Int', required => 1);
91 # ... rest of the class here
98 my $app = My::App->new_with_options();
99 # ... rest of the script here
101 ## on the command line
102 % perl my_app_script.pl -configfile /etc/my_app.yaml -otherthing 123
106 This role loads simple configfiles to set object attributes. It
107 is based on the abstract role L<MooseX::ConfigFromFile>, and uses
108 L<Config::Any> to load your configfile. L<Config::Any> will in
109 turn support any of a variety of different config formats, detected
110 by the file extension. See L<Config::Any> for more details about
113 Like all L<MooseX::ConfigFromFile> -derived configfile loaders, this
114 module is automatically supported by the L<MooseX::Getopt> role as
115 well, which allows specifying C<-configfile> on the commandline.
121 Provided by the base role L<MooseX::ConfigFromFile>. You can
122 provide a default configfile pathname like so:
124 has +configfile ( default => '/etc/myapp.yaml' );
126 You can pass an array of filenames if you want, but as usual the array
127 has to be wrapped in a sub ref.
129 has +configfile ( default => sub { [ '/etc/myapp.yaml', '/etc/myapp_local.yml' ] } );
131 Config files are trivially merged at the top level, with the right-hand files taking precedence.
135 =head2 new_with_config
137 Provided by the base role L<MooseX::ConfigFromFile>. Acts just like
138 regular C<new()>, but also accepts an argument C<configfile> to specify
139 the configfile from which to load other attributes. Explicit arguments
140 to C<new_with_config> will override anything loaded from the configfile.
142 =head2 get_config_from_file
144 Called internally by either C<new_with_config> or L<MooseX::Getopt>'s
145 C<new_with_options>. Invokes L<Config::Any> to parse C<configfile>.
149 Brandon L. Black, E<lt>blblack@gmail.comE<gt>
153 This library is free software; you can redistribute it and/or modify
154 it under the same terms as Perl itself.