1 package MooseX::SimpleConfig;
4 with 'MooseX::ConfigFromFile';
10 sub get_config_from_file {
11 my ($class, $file) = @_;
13 my $can_config_any_args = $class->can('config_any_args');
14 my $extra_args = $can_config_any_args ?
15 $can_config_any_args->($class, $file) : {};
17 my $raw_cfany = Config::Any->load_files({
23 die q{Specified configfile '} . $file
24 . q{' does not exist, is empty, or is not readable}
25 unless $raw_cfany->[0]
26 && exists $raw_cfany->[0]->{$file};
28 my $raw_config = $raw_cfany->[0]->{$file};
30 die "configfile must represent a hash structure"
31 unless $raw_config && ref $raw_config && ref $raw_config eq 'HASH';
44 MooseX::SimpleConfig - A Moose role for setting attributes from a simple configfile
48 ## A YAML configfile named /etc/my_app.yaml:
56 with 'MooseX::SimpleConfig';
58 has 'foo' => (is => 'ro', isa => 'Str', required => 1);
59 has 'baz' => (is => 'rw', isa => 'Int', required => 1);
61 # ... rest of the class here
68 my $app = My::App->new_with_config(configfile => '/etc/my_app.yaml');
69 # ... rest of the script here
72 ###### combined with MooseX::Getopt:
78 with 'MooseX::SimpleConfig';
79 with 'MooseX::Getopt';
81 has 'foo' => (is => 'ro', isa => 'Str', required => 1);
82 has 'baz' => (is => 'rw', isa => 'Int', required => 1);
84 # ... rest of the class here
91 my $app = My::App->new_with_options();
92 # ... rest of the script here
94 ## on the command line
95 % perl my_app_script.pl -configfile /etc/my_app.yaml -otherthing 123
99 This role loads simple configfiles to set object attributes. It
100 is based on the abstract role L<MooseX::ConfigFromFile>, and uses
101 L<Config::Any> to load your configfile. L<Config::Any> will in
102 turn support any of a variety of different config formats, detected
103 by the file extension. See L<Config::Any> for more details about
106 Like all L<MooseX::ConfigFromFile> -derived configfile loaders, this
107 module is automatically supported by the L<MooseX::Getopt> role as
108 well, which allows specifying C<-configfile> on the commandline.
114 Provided by the base role L<MooseX::ConfigFromFile>. You can
115 provide a default configfile pathname like so:
117 has +configfile ( default => '/etc/myapp.yaml' );
121 =head2 new_with_config
123 Provided by the base role L<MooseX::ConfigFromFile>. Acts just like
124 regular C<new()>, but also accepts an argument C<configfile> to specify
125 the configfile from which to load other attributes. Explicit arguments
126 to C<new_with_config> will override anything loaded from the configfile.
128 =head2 get_config_from_file
130 Called internally by either C<new_with_config> or L<MooseX::Getopt>'s
131 C<new_with_options>. Invokes L<Config::Any> to parse C<configfile>.
135 Brandon L. Black, E<lt>blblack@gmail.comE<gt>
139 This library is free software; you can redistribute it and/or modify
140 it under the same terms as Perl itself.