1MooseX::ConfigFromFile(U3spemr)Contributed Perl DocumentMaotoisoenX::ConfigFromFile(3pm)
2
3
4
6 MooseX::ConfigFromFile - An abstract Moose role for setting attributes
7 from a configfile
8
10 version 0.14
11
13 ########
14 ## A real role based on this abstract role:
15 ########
16
17 package MooseX::SomeSpecificConfigRole;
18 use Moose::Role;
19
20 with 'MooseX::ConfigFromFile';
21
22 use Some::ConfigFile::Loader ();
23
24 sub get_config_from_file {
25 my ($class, $file) = @_;
26
27 my $options_hashref = Some::ConfigFile::Loader->load($file);
28
29 return $options_hashref;
30 }
31
32
33 ########
34 ## A class that uses it:
35 ########
36 package Foo;
37 use Moose;
38 with 'MooseX::SomeSpecificConfigRole';
39
40 # optionally, default the configfile:
41 sub _get_default_configfile { '/tmp/foo.yaml' }
42
43 # ... insert your stuff here ...
44
45 ########
46 ## A script that uses the class with a configfile
47 ########
48
49 my $obj = Foo->new_with_config(configfile => '/etc/foo.yaml', other_opt => 'foo');
50
52 This is an abstract role which provides an alternate constructor for
53 creating objects using parameters passed in from a configuration file.
54 The actual implementation of reading the configuration file is left to
55 concrete sub-roles.
56
57 It declares an attribute "configfile" and a class method
58 "new_with_config", and requires that concrete roles derived from it
59 implement the class method "get_config_from_file".
60
61 Attributes specified directly as arguments to "new_with_config"
62 supersede those in the configfile.
63
64 MooseX::Getopt knows about this abstract role, and will use it if
65 available to load attributes from the file specified by the command
66 line flag "--configfile" during its normal "new_with_options".
67
69 configfile
70 This is a Path::Tiny object which can be coerced from a regular path
71 string or any object that supports stringification. This is the file
72 your attributes are loaded from. You can add a default configfile in
73 the consuming class and it will be honored at the appropriate time; see
74 below at "_get_default_configfile".
75
76 If you have MooseX::Getopt installed, this attribute will also have the
77 "Getopt" trait supplied, so you can also set the configfile from the
78 command line.
79
81 new_with_config
82 This is an alternate constructor, which knows to look for the
83 "configfile" option in its arguments and use that to set attributes.
84 It is much like MooseX::Getopts's "new_with_options". Example:
85
86 my $foo = SomeClass->new_with_config(configfile => '/etc/foo.yaml');
87
88 Explicit arguments will override anything set by the configfile.
89
90 get_config_from_file
91 This class method is not implemented in this role, but it is required
92 of all classes or roles that consume this role. Its two arguments are
93 the class name and the configfile, and it is expected to return a
94 hashref of arguments to pass to new() which are sourced from the
95 configfile.
96
97 _get_default_configfile
98 This class method is not implemented in this role, but can and should
99 be defined in a consuming class or role to return the default value of
100 the configfile (if not passed into the constructor explicitly).
101
103 Brandon L. Black <blblack@gmail.com>
104
106 • Karen Etheridge <ether@cpan.org>
107
108 • Tomas Doran <bobtfish@bobtfish.net>
109
110 • Chris Prather <chris@prather.org>
111
112 • Yuval Kogman <nothingmuch@woobling.org>
113
114 • Zbigniew Lukasiak <zby@cpan.org>
115
117 This software is copyright (c) 2007 by Brandon L. Black.
118
119 This is free software; you can redistribute it and/or modify it under
120 the same terms as the Perl 5 programming language system itself.
121
122
123
124perl v5.38.0 2023-07-20 MooseX::ConfigFromFile(3pm)