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';
39 MooseX::SimpleConfig - A Moose role for setting attributes from a simple configfile
43 ## A YAML configfile named /etc/my_app.yaml:
51 with 'MooseX::SimpleConfig';
53 has 'foo' => (is => 'ro', isa => 'Str', required => 1);
54 has 'baz' => (is => 'rw', isa => 'Int', required => 1);
56 # ... rest of the class here
63 my $app = My::App->new_with_config(configfile => '/etc/my_app.yaml');
64 # ... rest of the script here
67 ###### combined with MooseX::Getopt:
73 with 'MooseX::SimpleConfig';
74 with 'MooseX::Getopt';
76 has 'foo' => (is => 'ro', isa => 'Str', required => 1);
77 has 'baz' => (is => 'rw', isa => 'Int', required => 1);
79 # ... rest of the class here
86 my $app = My::App->new_with_options();
87 # ... rest of the script here
89 ## on the command line
90 % perl my_app_script.pl -configfile /etc/my_app.yaml -otherthing 123
94 This role loads simple configfiles to set object attributes. It
95 is based on the abstract role L<MooseX::ConfigFromFile>, and uses
96 L<Config::Any> to load your configfile. L<Config::Any> will in
97 turn support any of a variety of different config formats, detected
98 by the file extension. See L<Config::Any> for more details about
101 Like all L<MooseX::ConfigFromFile> -derived configfile loaders, this
102 module is automatically supported by the L<MooseX::Getopt> role as
103 well, which allows specifying C<-configfile> on the commandline.
109 Provided by the base role L<MooseX::ConfigFromFile>.
113 =head2 new_with_config
115 Provided by the base role L<MooseX::ConfigFromFile>. Acts just like
116 regular C<new()>, but also accepts an argument C<configfile> to specify
117 the configfile from which to load other attributes. Explicit arguments
118 to C<new_with_config> will override anything loaded from the configfile.
120 =head2 get_config_from_file
122 Called internally by either C<new_with_config> or L<MooseX::Getopt>'s
123 C<new_with_options>. Invokes L<Config::Any> to parse C<configfile>.
127 Brandon L. Black, E<lt>blblack@gmail.comE<gt>
131 This library is free software; you can redistribute it and/or modify
132 it under the same terms as Perl itself.