1 package MooseX::SimpleConfig;
4 with 'MooseX::ConfigFromFile';
10 sub get_config_from_file {
11 my ($class, $file) = @_;
13 my $raw_cfany = Config::Any->load_files({
18 die q{Specified configfile '} . $file
19 . q{' does not exist, is empty, or is not readable}
20 unless $raw_cfany->[0]
21 && exists $raw_cfany->[0]->{$file};
23 my $raw_config = $raw_cfany->[0]->{$file};
25 die "configfile must represent a hash structure"
26 unless $raw_config && ref $raw_config && ref $raw_config eq 'HASH';
37 sub _build_extensions {
38 return Config::Any->extensions();
48 return [Config::Any->plugins()] ;
59 MooseX::SimpleConfig - A Moose role for setting attributes from a simple configfile
63 ## A YAML configfile named /etc/my_app.yaml:
71 with 'MooseX::SimpleConfig';
73 has 'foo' => (is => 'ro', isa => 'Str', required => 1);
74 has 'baz' => (is => 'rw', isa => 'Int', required => 1);
76 # ... rest of the class here
83 my $app = My::App->new_with_config(configfile => '/etc/my_app.yaml');
84 # ... rest of the script here
87 ###### combined with MooseX::Getopt:
93 with 'MooseX::SimpleConfig';
94 with 'MooseX::Getopt';
96 has 'foo' => (is => 'ro', isa => 'Str', required => 1);
97 has 'baz' => (is => 'rw', isa => 'Int', required => 1);
99 # ... rest of the class here
106 my $app = My::App->new_with_options();
107 # ... rest of the script here
109 ## on the command line
110 % perl my_app_script.pl -configfile /etc/my_app.yaml -otherthing 123
114 This role loads simple configfiles to set object attributes. It
115 is based on the abstract role L<MooseX::ConfigFromFile>, and uses
116 L<Config::Any> to load your configfile. L<Config::Any> will in
117 turn support any of a variety of different config formats, detected
118 by the file extension. See L<Config::Any> for more details about
121 Like all L<MooseX::ConfigFromFile> -derived configfile loaders, this
122 module is automatically supported by the L<MooseX::Getopt> role as
123 well, which allows specifying C<-configfile> on the commandline.
129 Provided by the base role L<MooseX::ConfigFromFile>. You can
130 provide a default configfile pathname like so:
132 has +configfile ( default => '/etc/myapp.yaml' );
136 =head2 new_with_config
138 Provided by the base role L<MooseX::ConfigFromFile>. Acts just like
139 regular C<new()>, but also accepts an argument C<configfile> to specify
140 the configfile from which to load other attributes. Explicit arguments
141 to C<new_with_config> will override anything loaded from the configfile.
143 =head2 get_config_from_file
145 Called internally by either C<new_with_config> or L<MooseX::Getopt>'s
146 C<new_with_options>. Invokes L<Config::Any> to parse C<configfile>.
150 Brandon L. Black, E<lt>blblack@gmail.comE<gt>
154 This library is free software; you can redistribute it and/or modify
155 it under the same terms as Perl itself.