1 package MooseX::ConfigFromFile;
4 use MooseX::Types::Path::Tiny 'Path';
5 use Try::Tiny qw/ try /;
7 use namespace::autoclean;
9 requires 'get_config_from_file';
15 predicate => 'has_configfile',
16 do { try { require MooseX::Getopt; (traits => ['Getopt']) } },
20 my ($class, %opts) = @_;
24 if(defined $opts{configfile}) {
25 $configfile = $opts{configfile}
28 # This would only succeed if the consumer had defined a new configfile
29 # sub to override the generated reader
30 $configfile = try { $class->configfile };
32 # this is gross, but since a lot of users have swapped in their own
33 # default subs, we have to keep calling it rather than calling a
34 # builder sub directly - and it might not even be a coderef either
35 my $cfmeta = $class->meta->find_attribute_by_name('configfile');
36 $configfile = $cfmeta->default if not defined $configfile and $cfmeta->has_default;
38 if (ref $configfile eq 'CODE') {
39 $configfile = $configfile->($class);
43 if (defined $configfile) {
44 my $hash = $class->get_config_from_file($configfile);
46 no warnings 'uninitialized';
47 croak "get_config_from_file($configfile) did not return a hash (got $hash)"
48 unless ref $hash eq 'HASH';
50 %opts = (%$hash, %opts);
64 MooseX::ConfigFromFile - An abstract Moose role for setting attributes from a configfile
69 ## A real role based on this abstract role:
72 package MooseX::SomeSpecificConfigRole;
75 with 'MooseX::ConfigFromFile';
77 use Some::ConfigFile::Loader ();
79 sub get_config_from_file {
80 my ($class, $file) = @_;
82 my $options_hashref = Some::ConfigFile::Loader->load($file);
84 return $options_hashref;
89 ## A class that uses it:
93 with 'MooseX::SomeSpecificConfigRole';
95 # optionally, default the configfile:
96 around configfile => sub { '/tmp/foo.yaml' };
98 # ... insert your stuff here ...
101 ## A script that uses the class with a configfile
104 my $obj = Foo->new_with_config(configfile => '/etc/foo.yaml', other_opt => 'foo');
108 This is an abstract role which provides an alternate constructor for creating
109 objects using parameters passed in from a configuration file. The
110 actual implementation of reading the configuration file is left to
113 It declares an attribute C<configfile> and a class method C<new_with_config>,
114 and requires that concrete roles derived from it implement the class method
115 C<get_config_from_file>.
117 Attributes specified directly as arguments to C<new_with_config> supercede those
120 L<MooseX::Getopt> knows about this abstract role, and will use it if available
121 to load attributes from the file specified by the commandline flag C<--configfile>
122 during its normal C<new_with_options>.
128 This is a L<Path::Tiny> object which can be coerced from a regular pathname
129 string or any object that supports stringification.
130 This is the file your attributes are loaded from. You can add a default
131 configfile in the consuming class and it will be honored at the appropriate time
132 (note that a simple sub declaration is not sufficient, as there is already a
133 sub by that name being added by Moose as the attribute reader)
135 around configfile => sub { '/etc/myapp.yaml' };
137 Note that you can alternately just provide a C<configfile> method which returns
138 the config file when called - this will be used in preference to the default of
141 If you have L<MooseX::Getopt> installed, this attribute will also have the
142 C<Getopt> trait supplied, so you can also set the configfile from the
147 =head2 new_with_config
149 This is an alternate constructor, which knows to look for the C<configfile> option
150 in its arguments and use that to set attributes. It is much like L<MooseX::Getopts>'s
151 C<new_with_options>. Example:
153 my $foo = SomeClass->new_with_config(configfile => '/etc/foo.yaml');
155 Explicit arguments will overide anything set by the configfile.
157 =head2 get_config_from_file
159 This class method is not implemented in this role, but it is required of all subroles.
160 Its two arguments are the classname and the configfile, and it is expected to return
161 a hashref of arguments to pass to C<new()> which are sourced from the configfile.
165 Copyright (c) - the MooseX::ConfigFromFile "AUTHOR" and "CONTRIBUTORS" as listed below.
169 Brandon L. Black, E<lt>blblack@gmail.comE<gt>
177 =item Karen Etheridge
181 =item Zbigniew Lukasiak
187 This library is free software; you can redistribute it and/or modify
188 it under the same terms as Perl itself.