1MouseX::ConfigFromFile(U3s)er Contributed Perl DocumentatMioounseX::ConfigFromFile(3)
2
3
4
6 MouseX::ConfigFromFile - An abstract Mouse role for setting attributes
7 from a configfile
8
10 A real role based on this abstract role:
11
12 package MyApp::ConfigRole;
13 use Mouse::Role;
14 with 'MouseX::ConfigFromFile';
15
16 use MyApp::ConfigLoader;
17
18 sub get_config_from_file {
19 my ($class, $file) = @_;
20
21 my $config_hashref = MyApp::ConfigLoader->load($file);
22
23 return $config_hashref;
24 }
25
26 A class that uses it:
27
28 package MyApp;
29 use Mouse;
30 with 'MyApp::ConfigRole';
31
32 # optionally, default the configfile:
33 has '+configfile' => ( default => '/tmp/myapp.yml' );
34
35 A script that uses the class with a configfile:
36
37 my $app = MyApp->new_with_config(
38 configfile => '/etc/myapp.yml',
39 other_opt => 'foo',
40 );
41
43 This is an abstract role which provides an alternate constructor for
44 creating objects using parameters passed in from a configuration file.
45 The actual implementation of reading the configuration file is left to
46 concrete subroles.
47
48 It declares an attribute "configfile" and a class method
49 "new_with_config", and requires that concrete roles derived from it
50 implement the class method "get_config_from_file".
51
52 Attributes specified directly as arguments to "new_with_config"
53 supercede those in the configfile.
54
56 new_with_config(%params?)
57 This is an alternate constructor, which knows to look for the
58 "configfile" option in its arguments and use that to set attributes.
59 It is much like MouseX::Getopts' "new_with_options".
60
61 Example:
62
63 my $app = MyApp->new_with_config( configfile => '/etc/foo.yaml' );
64
65 Explicit arguments will override anything set by the configfile.
66
67 get_config_from_file($file)
68 This method is not implemented in this role, but it is required of all
69 subroles. Its two arguments are the class name and the configfile, and
70 it is expected to return a hashref of arguments to pass to "new()"
71 which are sourced from the configfile.
72
73 Example:
74
75 sub get_config_from_file {
76 my ($class, $file) = @_;
77
78 my $config = {};
79
80 # ... load config from $file ...
81
82 return $config;
83 }
84
86 configfile
87 This is a Path::Class::File object which can be coerced from a regular
88 path name string. This is the file your attributes are loaded from.
89 You can add a default configfile in the class using the role and it
90 will be honored at the appropriate time:
91
92 has '+configfile' => ( default => '/etc/myapp.yaml' );
93
95 NAKAGAWA Masaki <masaki@cpan.org>
96
98 Brandon L. Black, "AUTHOR" in MooseX::ConfigFromFile
99
101 This library is free software; you can redistribute it and/or modify it
102 under the same terms as Perl itself.
103
105 Mouse, Mouse::Role, MouseX::Types::Path::Class, MooseX::ConfigFromFile
106
107
108
109perl v5.32.0 2020-07-28 MouseX::ConfigFromFile(3)