1MooseX::ConfigFromFile(U3s)er Contributed Perl DocumentatMiooonseX::ConfigFromFile(3)
2
3
4
6 MooseX::ConfigFromFile - An abstract Moose role for setting attributes
7 from a configfile
8
10 ########
11 ## A real role based on this abstract role:
12 ########
13
14 package MooseX::SomeSpecificConfigRole;
15 use Moose::Role;
16
17 with 'MooseX::ConfigFromFile';
18
19 use Some::ConfigFile::Loader ();
20
21 sub get_config_from_file {
22 my ($class, $file) = @_;
23
24 my $options_hashref = Some::ConfigFile::Loader->load($file);
25
26 return $options_hashref;
27 }
28
29
30 ########
31 ## A class that uses it:
32 ########
33 package Foo;
34 use Moose;
35 with 'MooseX::SomeSpecificConfigRole';
36
37 # optionally, default the configfile:
38 has +configfile ( default => '/tmp/foo.yaml' );
39
40 # ... insert your stuff here ...
41
42 ########
43 ## A script that uses the class with a configfile
44 ########
45
46 my $obj = Foo->new_with_config(configfile => '/etc/foo.yaml', other_opt => 'foo');
47
49 This is an abstract role which provides an alternate constructor for
50 creating objects using parameters passed in from a configuration file.
51 The actual implementation of reading the configuration file is left to
52 concrete subroles.
53
54 It declares an attribute "configfile" and a class method
55 "new_with_config", and requires that concrete roles derived from it
56 implement the class method "get_config_from_file".
57
58 Attributes specified directly as arguments to "new_with_config"
59 supercede those in the configfile.
60
61 MooseX::Getopt knows about this abstract role, and will use it if
62 available to load attributes from the file specified by the commandline
63 flag "--configfile" during its normal "new_with_options".
64
66 configfile
67 This is a Path::Class::File object which can be coerced from a regular
68 pathname string. This is the file your attributes are loaded from.
69 You can add a default configfile in the class using the role and it
70 will be honored at the appropriate time:
71
72 has +configfile ( default => '/etc/myapp.yaml' );
73
75 new_with_config
76 This is an alternate constructor, which knows to look for the
77 "configfile" option in its arguments and use that to set attributes.
78 It is much like MooseX::Getopts's "new_with_options". Example:
79
80 my $foo = SomeClass->new_with_config(configfile => '/etc/foo.yaml');
81
82 Explicit arguments will overide anything set by the configfile.
83
84 get_config_from_file
85 This class method is not implemented in this role, but it is required
86 of all subroles. Its two arguments are the classname and the
87 configfile, and it is expected to return a hashref of arguments to pass
88 to "new()" which are sourced from the configfile.
89
90 meta
91 The Moose meta stuff, included here because otherwise pod tests fail
92 sometimes
93
96 Brandon L. Black, <blblack@gmail.com>
97
99 This library is free software; you can redistribute it and/or modify it
100 under the same terms as Perl itself.
101
102
103
104perl v5.12.0 2008-01-23 MooseX::ConfigFromFile(3)