blib/lib/Config/FreeForm.pm

filter: Statementstmt 80.9% 51 / 63 Branchbran 31.2% 5 / 16 Conditioncond 15.3% 2 / 13 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 83.3% 10 / 12 Podpod 0.0% 0 / 4 totaltotal 58.6% 68 / 116 CC 17 SCAR 28.2
CC
17
Cov
63%
reload_changed
50.9
handler
6.0
import
4.1
CRAP 16.7
↑ Summary conf/Foo.conf »
1 package Config::FreeForm;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use vars qw/$VERSION $CONF_DIR %_Sets %_Stat $DEBUG/;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 $VERSION = '0.01';
6 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶sub import {
Subroutine import: called CC=4 SCAR=14.2
9 1 my $class = shift;
10 1 ▶ croak "import: Not an even number of arguments" if @_%2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 0 1 1
11 1 my %p = @_;
12 1 ▶ if ($p{dir}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p{'dir'}) { } 1 0 1
13 1 $CONF_DIR = $p{dir};
14 }
15 else {
16 0 ($CONF_DIR = __FILE__) =~ s/\.pm$//;
17 }
18 1 $DEBUG = $p{debug};
19 1 for my $set ( @{ $p{sets} } ) {
20 1 $_Sets{$set} = "$CONF_DIR/$set.conf";
21 1 reload($set);
22 }
23 }
24  
25 0 ▶sub reload_changed {
Subroutine reload_changed: not called CC=8 SCAR=39.3
Pod undocumented
26 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
27 0 for my $set (keys %_Sets) {
28 0 my $mtime = (stat $_Sets{$set})[9];
29 0 ▶ warn(__PACKAGE__ . ": Can't locate $_Sets{$set}\n"), next
Branch: 1 branch
BranchTrueFalseTotal
unless defined $mtime and $mtime 0 0 0
Condition0%0/3
defined $mtime and $mtime
!ll&&!rl&&r
000
MC/DC0%0/2
defined $mtime and $mtime
defined $mtime $mtime
Truth table0%0/3
A and B
A= defined $mtimeB= $mtime
ABresult
0X0
100
111
30 unless defined $mtime && $mtime;
31  
32 0 ▶ if (!$_Stat{$set} || $mtime > $_Stat{$set}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $Config::FreeForm::_Stat{$set} or $mtime > $Config::FreeForm::_Stat{$set}) 0 0 0
Condition0%0/3
not $Config::FreeForm::_Stat{$set} or $mtime > $Config::FreeForm::_Stat{$set}
l!l&&r!l&&!r
000
MC/DC0%0/2
not $Config::FreeForm::_Stat{$set} or $mtime > $Config::FreeForm::_Stat{$set}
not $Config::FreeForm::_Stat{$set} $mtime > $Config::FreeForm::_Stat{$set}
Truth table0%0/3
not(A) or B
A= not $Config::FreeForm::_Stat{$set}B= $mtime > $Config::FreeForm::_Stat{$set}
ABresult
1X1
011
000
33 warn sprintf "%s: process %d reloading %s (%d < %d)\n",
34 0 ▶ __PACKAGE__, $$, $set, $_Stat{$set} || 0, $mtime
Branch: 1 branch
BranchTrueFalseTotal
if $Config::FreeForm::DEBUG 0 0 0
Condition0%0/2
$Config::FreeForm::_Stat{$set} || 0
l!l
00
MC/DC0%0/1
$Config::FreeForm::_Stat{$set} || 0
$Config::FreeForm::_Stat{$set}
Truth table0%0/2
A
A= $Config::FreeForm::_Stat{$set}
Aresult
11
00
35 if $DEBUG;
36 0 reload($set);
37 }
38 0 $_Stat{$set} = $mtime;
39 }
40 }
41  
42 2 ▶sub reload {
Subroutine reload: called CC=3 SCAR=11.0
Pod undocumented
43 2 my $set = shift;
44 2 ▶ croak "reload: $set not found" unless exists $_Sets{$set};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $Config::FreeForm::_Sets{$set} 0 2 2
45 2 my $conf = do $_Sets{$set};
46 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
47 2 for my $key (keys %$conf) {
48 2 *{ __PACKAGE__ . '::' . $key } = \$conf->{$key};
49 }
50 }
51  
52 0 ▶sub handler {
Subroutine handler: not called CC=2 SCAR=17.9
Pod undocumented
53 0 my $r = shift;
54 0 ▶ $DEBUG = ($r->dir_config("ConfigDebug") || '') eq 'on';
Condition0%0/2
$r->dir_config('ConfigDebug') || ''
l!l
00
MC/DC0%0/1
$r->dir_config('ConfigDebug') || ''
$r->dir_config('ConfigDebug')
Truth table0%0/2
A
A= $r->dir_config('ConfigDebug')
Aresult
11
00
55 0 reload_changed();
56 0 return 1;
57 }
58  
59 3 ▶sub rewrite {
Subroutine rewrite: called CC=4 SCAR=14.0
Pod undocumented
60 3 my $set = shift;
61 3 ▶ my $file = shift || $_Sets{$set};
Condition66%2/3
shift() || $Config::FreeForm::_Sets{$set}
l!l&&r!l&&!r
120
MC/DC0%0/2
shift() || $Config::FreeForm::_Sets{$set}
shift() $Config::FreeForm::_Sets{$set}
Truth table66%2/3
A || B
A= shift()B= $Config::FreeForm::_Sets{$set}
ABresult
1X1
011
000
62 3 my $hash;
63 {
64 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
65 3 $hash = { $set => ${ __PACKAGE__ . "::${set}" } };
66 }
67 3 local *FH;
68 3 ▶ open FH, ">" . $file
Branch: 1 branch
BranchTrueFalseTotal
open FH, '>' . $file or croak("Can't open ${file}: $!") 3 0 3
69 or croak "Can't open $file: $!";
70 3 local $Data::Dumper::Indent = 1;
71 3 local $Data::Dumper::Purity = 1;
72 3 print FH Dumper($hash);
73 3 ▶ close FH or croak "Can't close $file: $!";
Branch: 1 branch
BranchTrueFalseTotal
close FH or croak("Can't close ${file}: $!") 3 0 3
74 }
75  
76 =head1 NAME
77  
78 Config::FreeForm - Provide in-memory configuration data
79  
80 =head1 SYNOPSIS
81  
82 use Config::FreeForm %options;
83  
84 =head1 DESCRIPTION
85  
86 I<Config::FreeForm> provides in-memory configuration data
87 in a free-form manner. Many existing configuration modules
88 attempt to provide some structure to your configuration
89 data; in doing so, they force you to use their own
90 configuration paradigm (association of keywords with values,
91 etc.). Often this isn't what you need in a complex
92 application--you need complete control over your configuration
93 data, and you need the ability to structure it however you
94 like. This is what I<Config::FreeForm> gives you.
95  
96 In I<Config::FreeForm> configuration data is stored as a
97 Perl data structure. The logic behind this is that you know
98 Perl--you shouldn't need to learn another little language
99 to set up your configuration data, however simple that
100 language may be. Of course, this works best if programmers
101 or tools do the updating of your configuration files; it
102 does make it more difficult for other possible users to edit
103 the files. If this is a problem for you, try some of the
104 other configuration modules listed in I<MISCELLANEOUS>.
105  
106 Still here? Good. You might then ask what I<Config::FreeForm>
107 gives you that rolling your own light module using I<Data::Dumper>
108 and I<do> would not. It's a good question, considering in
109 particular that I<Config::FreeForm> uses I<Data::Dumper>
110 and I<do> to write and read your data, respectively.
111 I<Config::FreeForm> adds some very nice features, though:
112  
113 =over 4
114  
115 =item * Configuration File Management
116  
117 So as not to clutter one file with configuration for all
118 purposes, you can separate your configuration data into
119 multiple files, and specify which files to load when you
120 load in the module:
121  
122 use Config::FreeForm sets => [ ... ];
123  
124 I<Config::FreeForm> manages the various configuration files
125 that you've told it to load, and lets you update your
126 data in memory, then write it back to its original location
127 on disk, using the I<rewrite> function (below, in
128 I<UPDATING CONFIGURATION>).
129  
130 =item * Automated Reloading
131  
132 In a I<mod_perl> context, your configuration data will be
133 loaded once, at webserver startup; subsequent access to the
134 configuration data will come from memory. If you update
135 your configuration on disk, then, you'll want those
136 changes to be reflected in the in-memory versions of the
137 configuration. I<Config::FreeForm> will handle this
138 automatically for you if you install it as a I<PerlInitHandler>
139 on your I<mod_perl>-enabled server. For more details, see
140 I<AUTOMATED RELOADING>, below.
141  
142 =back
143  
144 =head1 HOW TO USE IT
145  
146 To create a configuration file, add its configuration to a
147 file like I<Foo.conf>:
148  
149 $conf = {
150 Foo => { Bar => 1 }
151 }
152  
153 Once you've written your I<Foo.conf> configuration file,
154 load that file explicitly (without the I<.conf>):
155  
156 use Config::FreeForm sets => [ 'Foo' ];
157  
158 When the module is loaded, it will attempt to find a
159 configuration file for the set I<Foo>; it will load the
160 data in this file using I<do>; then it will loop over
161 the top-level variables in the tree structure and alias
162 variables into the I<Config::FreeForm> namespace to the
163 values in the structure.
164  
165 For example, if you have the above I<Foo.conf>, the
166 variable I<$Config::FreeForm::Foo> will be equal to the
167 following structure:
168  
169 { Bar => 1 }
170  
171 So you could access the value of the I<Bar> attribute
172 by treating the aliased variable as a hash
173 reference:
174  
175 my $value = $Config::FreeForm::Foo->{Bar};
176  
177 In addition to specifying which configuration files to
178 load, you can use the I<%options> in the import list to
179 set the directory holding the configuration files. By
180 default I<Config::FreeForm> looks in the directory
181 I<FreeForm> within the directory from which it was loaded
182 for the files. For example, if the module were loaded
183 from F</foo/bar/Config/FreeForm.pm>, the I<Foo.conf>
184 configuration file would be default be looked up in
185 F</foo/bar/Config/FreeForm/Foo.conf>.
186  
187 By using the I<dir> import list parameter, though, you
188 can override this default behavior:
189  
190 use Config::FreeForm dir => '/foo', sets => [ 'Foo' ];
191  
192 This would look up F<Foo.conf> in the directory I</foo>.
193  
194 =head1 UPDATING CONFIGURATION
195  
196 If you wish to update the configuration files
197 programatically (as opposed to editing them by
198 hand), you can use the I<rewrite> function.
199  
200 This is a two-step process. First, you'll need to update
201 the in-memory configuration--just make a change to one
202 of the variables. For example:
203  
204 $Config::FreeForm::Foo->{Bar} = 2;
205  
206 This updates the configuration in memory; now you need
207 to write the configuration to the disk. You can do
208 that using I<rewrite>, which takes the name of a
209 configuration "set" (which corresponds to the name of
210 the configuration file). In this case, that set would
211 be I<Foo>:
212  
213 Config::FreeForm::rewrite('Foo');
214  
215 And you're done. The configuration is now updated on
216 disk.
217  
218 If you'd like to write the configuration data to a
219 file different than that from which it was read, you
220 can pass a filepath as a second argument. For example:
221  
222 Config::FreeForm::rewrite('Foo', './myfoo.conf');
223  
224 This will write out the I<Foo> configuration data to
225 F<./myfoo.conf>.
226  
227 Keep in mind that, if you're rewriting your configuration
228 in a webserver context, you'll want your on-disk changes
229 to propagate to the other webserver children (the children
230 in which you didn't already change the in-memory
231 configuration). Read on--this can be made to happen
232 automatically.
233  
234 =head1 AUTOMATED RELOADING
235  
236 When used in a webserver context, the configuration files
237 are parsed once at webserver startup, then stored in
238 memory. If changes occur in the configuration files, under
239 normal circumstances the configuration stored in memory
240 would not be reloaded. However, I<Config::FreeForm> has a
241 built-in capability to automatically reload configuration
242 files that have changed on disk. This allows you to make
243 a change to a config file, then let the webserver
244 automatically pick up the new changes.
245  
246 This is particularly important when using the I<rewrite>
247 function; if you alter the in-memory configuration, then
248 write the file to disk, you want the other webserver
249 children to pick up the changes, in addition to the child
250 where you actually made the in-memory changes. Using the
251 automated reloading, these changes will be automatically
252 picked up by all of the webserver children.
253  
254 To use this capability, just install I<Config::FreeForm> as
255 a I<PerlInitHandler> in the webserver. Add this to the
256 configuration:
257  
258 PerlInitHandler Config::FreeForm
259  
260 You can either stick this into a I<Location> block or make
261 it global for your entire server. The latter may be easier
262 in terms of maintenance, but the former may give you more
263 flexibility.
264  
265 By default, I<Config::FreeForm> will go about its business
266 quietly. If you'd like it to write a message to the error
267 log each time it reloads a configuration file, you can
268 add a configuration directive to do so:
269  
270 PerlSetVar ConfigDebug on
271  
272 Now, each time I<Config::FreeForm> reloads a configuration
273 file, it will write a message to the log file telling
274 you the process ID, the configuration set, and the
275 modified-time comparison that caused the reload.
276  
277 =head1 MISCELLANEOUS
278  
279 If the so-called freeform nature of I<Config::FreeForm>
280 doesn't appeal to you, and you'd like a more structured
281 approach to your configuration files, check out
282 I<App-Config>, I<Boulder>, or I<XML::Simple>.
283  
284 =head1 AUTHOR
285  
286 Benjamin Trott, ben@rhumba.pair.com
287  
288 =cut
289  
290 1 1;
↑ Summary conf/Foo.conf »