2 MooseX::ConfigFromFile - An abstract Moose role for setting attributes
7 ## A real role based on this abstract role:
10 package MooseX::SomeSpecificConfigRole;
13 with 'MooseX::ConfigFromFile';
15 use Some::ConfigFile::Loader ();
17 sub get_config_from_file {
18 my ($class, $file) = @_;
20 my $options_hashref = Some::ConfigFile::Loader->load($file);
22 return $options_hashref;
27 ## A class that uses it:
31 with 'MooseX::SomeSpecificConfigRole';
33 # ... insert your stuff here ...
36 ## A script that uses the class with a configfile
39 my $obj = Foo->new_with_config(configfile => '/etc/foo.yaml', other_opt => 'foo');
42 This is an abstract role which provides an alternate constructor for
43 creating objects using parameters passed in from a configuration file.
44 The actual implementation of reading the configuration file is left to
47 It declares an attribute "configfile" and a class method
48 "new_with_config", and requires that concrete roles derived from it
49 implement the class method "get_config_from_file".
51 MooseX::Getopt knows about this abstract role, and will use it if
52 available to load attributes from the file specified by the commandline
53 flag "--configfile" during its normal "new_with_options".
57 This is a Path::Class::File object which can be coerced from a regular
58 pathname string. This is the file your attributes are loaded from.
62 This is an alternate constructor, which knows to look for the
63 "configfile" option in its arguments and use that to set attributes. It
64 is much like MooseX::Getopts's "new_with_options". Example:
66 my $foo = SomeClass->new_with_config(configfile => '/etc/foo.yaml');
68 Explicit arguments will overide anything set by the configfile.
71 This class method is not implemented in this role, but it is required of
72 all subroles. Its two arguments are the classname and the configfile,
73 and it is expected to return a hashref of arguments to pass to "new()".
76 The Moose meta stuff, included here because otherwise pod tests fail
81 Brandon L. Black, <blblack@gmail.com>
84 This library is free software; you can redistribute it and/or modify it
85 under the same terms as Perl itself.