Framework/Feature/Config.pm

filter: Statementstmt 96.2% 332 / 345 Branchbran 78.4% 80 / 102 Conditioncond 77.6% 52 / 67 MC/DCmcdc 59.4% 22 / 37 Subroutinesub 92.5% 25 / 27 Podpod 100% 16 / 16 totaltotal 88.7% 527 / 594 CC 97 SCAR 21.9
CC
97
Cov
90%
_new_cfg
16.3
_process
14.1
application_entry
11.3
CRAP 8.9
« Framework/Base/Object/ErrorHandle.pm ↑ Summary Framework/Feature/Data.pm »
1 package App::Framework::Feature::Config ;
2  
3 =head1 NAME
4  
5 App::Framework::Feature::Config - Configuration file read/write
6  
7 =head1 SYNOPSIS
8  
9 use App::Framework '+Config' ;
10  
11  
12 =head1 DESCRIPTION
13  
14 Provides a standard interface for reading/writing application configuration files. When this feature is included into an application, it attempts to read
15 a configuration file for the application (which may be stored in one of severeal places). If found, the configuartion file is processed and may update
16 the application options (see L<App::Framework::Feature::Options>).
17  
18 Also, an application may create one or more extra instances of the feature to read addtional configuration files.
19  
20  
21 =head2 Configuration File Definition
22  
23 Configuration files are text files containing variable / value pairs. Optionally these variable/value pairs may be gouped into 'sections' (see L</sections>).
24  
25  
26 =head3 Simple format
27  
28 The simplest format consists of an optional description line followed immediately by a variable/value setting:
29  
30 # description
31 var=value
32  
33 (NOTE: There can be no empty lines between the description "comment" and the variable).
34  
35 =head3 Extended format
36  
37 An alternative to the simple format is as shown below. This contains additional information useful for checking the value setting.
38  
39 ## Summary: Configuration for Apache 2
40 ## Type: s
41 #
42 # Here you can name files, separated by spaces, that should be Include'd from
43 # httpd.conf.
44 #
45 # This allows you to add e.g. VirtualHost statements without touching
46 # /etc/apache2/httpd.conf itself, which makes upgrading easier.
47 #
48 apache_include_files="mod_dav"
49  
50 The lines prefixed by ## are extra information about the variable and are used to specify a summary, and a variable
51 type. The extra information prefixed by # is used as the description. The above example will be
52 shown in the application man page as:
53  
54 -apache_include_files <string> [Default: "mod_dav"]
55 Config option:Here you can name files, separated by spaces, that
56 should be Include'd from httpd.conf. This allows you to add e.g.
57 VirtualHost statements without touching /etc/apache2/httpd.conf
58 itself, which makes upgrading easier.
59  
60 Any configuration variables specified in this manner will automatically be put into the application's options, but will also be available
61 via the application's 'Config' feature.
62  
63  
64 =head3 Sections
65  
66 Each section is defined by a string contained within '[]'. Where there are multiple sections with the same name, they are added to an array. All variables
67 defined before the sections are treated as "global".
68  
69 global=string
70  
71 [top]
72 a=1
73
74 [instance]
75 a=11
76  
77 [instance]
78 a=22
79  
80 The above example will be stored as the HASH:
81  
82 {
83 global => 'string'
84 top => [
85 {
86 a => 1
87 }
88 ]
89 instance => [
90 {
91 a => 11
92 },
93 {
94 a => 22
95 }
96 ],
97 }
98  
99 Even if a section has only one instance, it is always stored as an array.
100  
101  
102 =head2 Configuration as Options
103  
104 As stated above, any variables defined in the configuration file before the sections are treated as "global" (see L</Sections>). These global variables
105 have the additional property that they are automatically treated like options definitions (see L<App::Framework::Feature::Options>).
106  
107 This means that the global variables are indistinguishable from options (in fact all of the options variables appear in the global area of the configurations and
108 vice versa). Also, you do not need to specify options in the application script - you can just define them once in the configuration file (although see L</Writing>).
109  
110 =head2 File Paths
111  
112 The configuration file is searched for using the path specification. This path is actually one or more paths, specified
113 in the order in which to search for the configuration file. The search is stopped as soon as the first valid file is found.
114  
115 The application configuration search path is set to the following default, unless it is over-ridden by either the application
116 script or by the user (via command line options):
117  
118 =over 4
119  
120 =item * $HOME/$app_dir
121  
122 User-specific configuration. $HOME is replaced with the user's home directory, and $app_dir is replaced by ".I<name>" (or "I<name>" on Windows)
123 where I<name> is the name of the script.
124  
125 This allows users to set up their own settings.
126  
127 =item * $SYSTEM/$name
128  
129 System configuration. $SYSTEM is replaced with "/etc" (or "C:" on Windows), and $name is replaced by the name of the script.
130  
131 This allows sysadmins to set up a common set of settings.
132  
133 =item * $app_path/config
134  
135 Application-specific configuration. $app_path is replaced by the path to the installed script.
136  
137 This allows script developers to bundle their settings with the installed script.
138  
139 =back
140  
141 As an example, the script 'test_script' installed on a Linux under '/usr/local/bin' will, by default, have the following search path:
142  
143 $HOME/.test_script
144 /etc/test_script
145 /usr/local/bin/config
146  
147 In addition to the search path described above, there is also a write search path. This path is searched until a file
148 (and it's path) can be written to by the script user. It is set, by default, to:
149  
150 =over 4
151  
152 =item * $HOME/$app_dir
153  
154 User-specific configuration. $HOME is replaced with the user's home directory, and $app_dir is replaced by ".I<name>" (or "I<name>" on Windows)
155 where I<name> is the name of the script.
156  
157 This allows users to set up their own settings.
158  
159 =item * $SYSTEM/$name
160  
161 System configuration. $SYSTEM is replaced with "/etc" (or "C:" on Windows), and $name is replaced by the name of the script.
162  
163 This allows sysadmins to set up a common set of settings.
164  
165 =back
166  
167 (i.e. the same as the read path, but without the application-bundle directory).
168  
169 Uses L<App::Framework::Base::SearchPath> to provide the path search.
170  
171  
172 =head2 Creating Config Files
173  
174 You can, of course, just write your config files from scratch. Alternatively, if you predominantly use "global" settings, then you specify
175 them as application options (L<App::Framework::Feature::Options>). Run your script with '-config_write' and it will automatically create
176 a formatted configuration file (see L</ADDITIONAL COMMAND LINE OPTIONS> for other command line settings).
177  
178 =head2 Addtional Config Instances
179  
180 In addition to having the application tied in with it's own configuration file, you can create multiple extra configuration files and read/write
181 then using this feature. To do this, create a new App::Framework::Feature::Config object instance per configuration file. You can then access
182 the contents of the file using the object's methods.
183  
184 For example:
185  
186 sub app
187 {
188 my ($app, $opts_href, $args_href) = @_ ;
189
190 ## use application config object to create a new one
191 my $new_cfg = $app->feature('Config')->new(
192 'filename' => 'some_file.conf',
193 'path' => '$HOME,/etc/new_config',
194 'write_path' => '$HOME',
195 ) ;
196 $new_cfg->read() ;
197
198 # do stuff with configuration
199 ...
200
201 # (debug) show configuration
202 $app->prt_data("Readback config=", $new_cfg->config) ;
203
204 ## write out file
205 $new_cfg->write() ;
206 }
207  
208  
209 =head2 Raw Configuration HASH
210  
211 Configuration files are stored in a HASH, where the keys are the variable names and the values are a HASH of information for
212 that variable:
213  
214 'summary' => Summary string
215 'default' => Default value
216 'description' => Description string
217 'type' => Variable option type (s, i, f)
218 'value' => Variable value
219
220  
221 =cut
222  
223 2 ▶use strict ;
Subroutine BEGIN: called CC=1 SCAR=0.0
224  
225 2 our $VERSION = "0.11" ;
226  
227 #============================================================================================
228 # USES
229 #============================================================================================
230 2 ▶use App::Framework::Feature ;
Subroutine BEGIN: called CC=1 SCAR=0.0
231 2 ▶use App::Framework::Base ;
Subroutine BEGIN: called CC=1 SCAR=0.0
232 2 ▶use App::Framework::Base::SearchPath ;
Subroutine BEGIN: called CC=1 SCAR=0.0
233  
234 #============================================================================================
235 # OBJECT HIERARCHY
236 #============================================================================================
237 2 our @ISA = qw(App::Framework::Feature) ;
238  
239 #============================================================================================
240 # GLOBALS
241 #============================================================================================
242  
243 =head2 FIELDS
244  
245 The following fields should be defined either in the call to 'new()', as part of a 'set()' call, or called by their accessor method
246 (which is the same name as the field):
247  
248  
249 =over 4
250  
251 =item B<filename> - Name of config file
252  
253 User-specified config filename. This is searched for using the search path
254  
255  
256 =item B<path> - search path
257  
258 A comma seperated list (in scalar context), or an ARRAY ref list of paths to be searched (for a file).
259  
260 =item B<write_path> - search path for writing
261  
262 A comma seperated list (in scalar context), or an ARRAY ref list of paths to be searched (for a file) when writing. If not set, then
263 B<path> is used.
264  
265 =item B<file_path> - configuration file path
266  
267 Created when config file is read. Full path of configuration file accessed in last read or write.
268  
269  
270 =item B<sections> - section names list
271  
272 Created when config file is read. ARRAY ref list of any section names.
273  
274  
275 =item B<config> - configuration HASH ref
276  
277 Created when config file is read. This is a HASH ref to the raw configuration file entries
278  
279 =back
280  
281 =cut
282  
283 2 my %FIELDS = (
284 # user settings
285 'filename' => undef,
286
287 # Created during execution
288 'configuration' => {},
289 'file_path' => undef,
290 'sections' => [],
291
292 '_search_path' => undef,
293 ) ;
294  
295  
296 =head2 ADDITIONAL COMMAND LINE OPTIONS
297  
298 This feature adds the following additional command line options to any application:
299  
300 =over 4
301  
302 =item B<-config_path> - Config file path
303  
304 Comma/semicolon separated list of search paths for the config file
305  
306 =item B<-config_writepath> - Config file write path
307  
308 Comma/semicolon separated list of paths for writing the config file. Uses -config_path setting if not specified.
309  
310 =item B<-config> - Config filename
311  
312 Specify the configuration filename to use
313  
314 =item B<-config_write> - Write config file
315  
316 When specified, writes the configuration file using the write path
317  
318 =back
319  
320 =cut
321  
322  
323 2 my $OPT_CFGPATH = "config_path" ;
324 2 my $OPT_CFGWRPATH = "config_writepath" ;
325 2 my $OPT_CFG = "config" ;
326 2 my $OPT_CFGWR = "config_write" ;
327  
328 2 my $OPT_CFGPATH_AREF =
329 ["$OPT_CFGPATH=s", 'Config file path', 'Comma/semicolon separated list of search paths for the config file', ] ;
330 2 my $OPT_CFGWRPATH_AREF =
331 ["$OPT_CFGWRPATH=s", 'Config file write path', 'Comma/semicolon separated list of paths for writing the config file', ] ;
332 2 my $OPT_CFG_AREF =
333 ["$OPT_CFG=s", 'Config file name', 'Config filename'] ;
334 2 my $OPT_CFGWR_AREF =
335 ["$OPT_CFGWR", 'Write config file', 'When specified, writes the configuration file using the write path'] ;
336  
337 # Set of default options
338 2 my @EXTRA_OPTIONS = (
339 $OPT_CFGPATH_AREF,
340 $OPT_CFGWRPATH_AREF,
341 $OPT_CFG_AREF,
342 $OPT_CFGWR_AREF,
343 ) ;
344  
345 2 my @CONFIG_OPTIONS = (
346 $OPT_CFGPATH,
347 $OPT_CFGWRPATH,
348 $OPT_CFG,
349 $OPT_CFGWR,
350 ) ;
351  
352 #============================================================================================
353  
354 =head2 CONSTRUCTOR
355  
356 =over 4
357  
358 =cut
359  
360 #============================================================================================
361  
362  
363 =item B< new([%args]) >
364  
365 Create a new Config object.
366  
367 The %args are specified as they would be in the B<set> method, for example:
368  
369 'mmap_handler' => $mmap_handler
370  
371 The full list of possible arguments are :
372  
373 'fields' => Either ARRAY list of valid field names, or HASH of field names with default values
374  
375 =cut
376  
377 sub new
378 2 ▶{
Subroutine new: called CC=7 SCAR=19.5
Pod documented
379 2 my ($obj, %args) = @_ ;
380
381 2 ▶ my $class = ref($obj) || $obj ;
Condition66%2/3
ref $obj || $obj
l!l&&r!l&&!r
110
MC/DC0%0/2
ref $obj || $obj
ref $obj $obj
Truth table66%2/3
A || B
A= ref $objB= $obj
ABresult
1X1
011
000
382  
383 # create search path object
384 2 my $search_obj = App::Framework::Base::SearchPath->new(%args) ;
385
386 # Create object
387 2 my $this = $class->SUPER::new(%args,
388 'priority' => $App::Framework::Base::PRIORITY_SYSTEM + 15, # needs to be after options, but before data
389 'registered' => [qw/go_entry getopts_entry application_entry/],
390 '_search_path' => $search_obj,
391 ) ;
392
393 ## Map the search path object's methods into this object
394 2 foreach my $method (qw/path write_path read_filepath write_filepath/)
395 {
396 2 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
397 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
398
399 8 ▶ *{ __PACKAGE__."::${method}"} = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
400 13 my $this = shift ;
401 13 $this->_dbg_prt( ["Config: calling searchpath->$method() ", \@_] ) ;
402 13 return $search_obj->$method(@_) ;
403 8 };
404 }
405
406 ## If associated with an app, then add the app's variables to the search path
407 2 my $app = $this->app ;
408 2 ▶ if ($app)
Branch: 1 branch
BranchTrueFalseTotal
if ($app) 1 1 2
409 {
410 ## only interested in scalar values
411 1 my %vars = $app->vars() ;
412 1 my %app_vars ;
413 1 foreach my $var (keys %vars)
414 {
415 32 ▶ $app_vars{$var} = $vars{$var} if !ref($vars{$var}) || ref($vars{$var}) eq 'SCALAR' ;
Branch: 1 branch
BranchTrueFalseTotal
if not ref $vars{$var} or ref $vars{$var} eq "SCALAR" 19 13 32
Condition66%2/3
not ref $vars{$var} or ref $vars{$var} eq "SCALAR"
l!l&&r!l&&!r
19013
MC/DC50%1/2
not ref $vars{$var} or ref $vars{$var} eq "SCALAR"
not ref $vars{$var} ref $vars{$var} eq "SCALAR"
Truth table66%2/3
not(A) or B
A= not ref $vars{$var}B= ref $vars{$var} eq "SCALAR"
ABresult
1X1
011
000
416 }
417 1 $search_obj->env(\%app_vars) ;
418 }
419  
420 2 return($this) ;
421 }
422  
423  
424  
425 #============================================================================================
426  
427 =back
428  
429 =head2 CLASS METHODS
430  
431 =over 4
432  
433 =cut
434  
435 #============================================================================================
436  
437 #-----------------------------------------------------------------------------
438  
439 =item B< init_class([%args]) >
440  
441 Initialises the Config object class variables.
442  
443 =cut
444  
445 sub init_class
446 2 ▶{
Subroutine init_class: called CC=1 SCAR=0.0
Pod documented
447 2 my $class = shift ;
448 2 my (%args) = @_ ;
449  
450 # Add extra fields
451 2 $class->add_fields(\%FIELDS, \%args) ;
452  
453 # init class
454 2 $class->SUPER::init_class(%args) ;
455  
456 }
457  
458 #============================================================================================
459  
460 =back
461  
462 =head2 OBJECT DATA METHODS
463  
464 =over 4
465  
466 =cut
467  
468 #============================================================================================
469  
470 #----------------------------------------------------------------------------
471  
472 =item B<set(%args)>
473  
474 Overrides the parent 'set()' method to send the parameters off to the L<App::Framework::Base::SearchPath> object
475 as well as itself.
476  
477 =cut
478  
479 sub set
480 6 ▶{
Subroutine set: called CC=4 SCAR=14.0
Pod documented
481 6 my $this = shift ;
482 6 my (%args) = @_ ;
483  
484 6 ▶ if (keys %args)
Branch: 1 branch
BranchTrueFalseTotal
if (keys %args) 4 2 6
485 {
486  
487 4 $this->_dbg_prt( ["settings args = ", \%args] ) ;
488  
489 # send to search path obj (if created yet)
490 4 my $search_obj = $this->_search_path ;
491 4 ▶$this->_dbg_prt( ["settings args on search_obj\n"] ) if $search_obj ;
Branch: 1 branch
BranchTrueFalseTotal
if $search_obj 0 4 4
492 4 ▶ $search_obj->set(%args) if $search_obj ;
Branch: 1 branch
BranchTrueFalseTotal
if $search_obj 0 4 4
493
494 # handle the args
495 4 $this->SUPER::set(%args) ;
496 }
497  
498 }
499  
500 #============================================================================================
501  
502 =back
503  
504 =head2 OBJECT METHODS
505  
506 =over 4
507  
508 =cut
509  
510 #============================================================================================
511  
512  
513 #-----------------------------------------------------------------------------
514  
515 =item B< go_entry() >
516  
517 Application hook: When application calls go() set up config options.
518  
519 =cut
520  
521 sub go_entry
522 1 ▶{
Subroutine go_entry: called CC=9 SCAR=23.1
Pod documented
523 1 my $this = shift ;
524  
525 1 $this->_dbg_prt( ["Config: go_entry()\n"] ) ;
526  
527 ## must be under application to get here...
528 1 my $app = $this->app ;
529  
530 1 ▶ my $home = $ENV{'HOME'} || $ENV{'USERPROFILE'} || "$ENV{'HOMEDRIVE'}$ENV{'HOMEPATH'}" ;
Condition33%2/6
$ENV{'HOME'} || $ENV{'USERPROFILE'} || "$ENV{'HOMEDRIVE'}$ENV{'HOMEPATH'}"
l!l&&r!l&&!r
100
$ENV{"HOME"} or $ENV{"USERPROFILE"}
l!l&&r!l&&!r
100
MC/DC0%0/3
$ENV{'HOME'} || $ENV{'USERPROFILE'} || "$ENV{'HOMEDRIVE'}$ENV{'HOMEPATH'}"
$ENV{"HOME"} $ENV{"USERPROFILE"} "$ENV{'HOMEDRIVE'}$ENV{'HOMEPATH'}"
Truth table25%1/4
A or B || C
A= $ENV{"HOME"}B= $ENV{"USERPROFILE"}C= "$ENV{'HOMEDRIVE'}$ENV{'HOMEPATH'}"
ABCresult
1XX1
01X1
0011
0000
531 1 my $app_name = $app->name ;
532 1 my $app_path = $app->progpath ;
533
534 1 my $app_dir = ".$app_name" ;
535 1 my $sys = "/etc" ;
536 1 ▶ if ($^O =~ /MSWin/)
Branch: 1 branch
BranchTrueFalseTotal
if ($^O =~ /MSWin/) 0 1 1
537 {
538 0 $app_dir = "$app_name" ;
539 0 $sys = "c:/" ;
540 }
541
542 ## Set up write path, if not already set
543 1 my $write_path = $this->write_path() ;
544 1 $this->_dbg_prt( ["current write path=$write_path\n"] ) ;
545 1 ▶ unless ($write_path)
Branch: 1 branch
BranchTrueFalseTotal
unless ($write_path) 1 0 1
546 {
547 1 $this->_dbg_prt( ["set default write path\n"] ) ;
548 1 $this->write_path("$home/$app_dir;$sys/$app_name") ;
549 }
550
551 ## Set up search path, if not already set
552 1 my $path = $this->path() ;
553 1 $this->_dbg_prt( ["current path=$path\n"] ) ;
554 1 ▶ unless ($path)
Branch: 1 branch
BranchTrueFalseTotal
unless ($path) 1 0 1
555 {
556 1 $this->_dbg_prt( ["set default path\n"] ) ;
557 1 $this->path("$home/$app_dir;$sys/$app_name;$app_path/config") ;
558 }
559
560 ## Set up filename, if not already set
561 1 ▶ my $filename = $this->filename() || '' ;
Condition50%1/2
$this->filename || ''
l!l
01
MC/DC0%0/1
$this->filename || ''
$this->filename
Truth table50%1/2
A
A= $this->filename
Aresult
11
00
562 1 $this->_dbg_prt( ["current filename=$filename\n"] ) ;
563 1 ▶ unless ($filename)
Branch: 1 branch
BranchTrueFalseTotal
unless ($filename) 1 0 1
564 {
565 1 $this->_dbg_prt( ["set default filename\n"] ) ;
566 1 $this->filename("$app_name.conf") ;
567 }
568  
569 # Set defaults
570 1 $OPT_CFGPATH_AREF->[3] = $this->path() ;
571 1 $OPT_CFG_AREF->[3] = $this->filename() ;
572 1 $OPT_CFGWRPATH_AREF->[3] = $this->write_path() ;
573  
574 ## Set options
575 1 $this->_dbg_prt( ["$this go_entry - append_options\n"] ) ;
576 1 ▶$this->dump_callstack() if $this->debug ;
Branch: 1 branch
BranchTrueFalseTotal
if $this->debug 0 1 1
577 1 $app->feature('Options')->append_options(\@EXTRA_OPTIONS) ;
578  
579 }
580  
581  
582 #-----------------------------------------------------------------------------
583  
584 =item B< getopts_entry() >
585  
586 Application hook: When application calls getopts() initialise the object and read config.
587  
588 =cut
589  
590 sub getopts_entry
591 1 ▶{
Subroutine getopts_entry: called CC=10 SCAR=23.1
Pod documented
592 1 my $this = shift ;
593  
594 1 $this->_dbg_prt( ["Config: getopts_entry()\n"] ) ;
595  
596 ## must be under application to get here...
597 1 my $app = $this->app ;
598  
599 ## do first pass at getting options
600 1 my @saved_argv = @ARGV ;
601  
602 ## Allow any config command line options through, otherwise just get option defaults
603 1 @ARGV=() ;
604 1 ▶ for (my $argc=0; $argc < scalar(@saved_argv); ++$argc)
Branch: 1 branch
BranchTrueFalseTotal
if ($argc < scalar @saved_argv) 7 1 8
605 {
606 7 ▶ if ($saved_argv[$argc] =~ m/^\-($OPT_CFGPATH|$OPT_CFGWRPATH|$OPT_CFG)$/)
Branch: 1 branch
BranchTrueFalseTotal
if ($saved_argv[$argc] =~ /^\-($OPT_CFGPATH|$OPT_CFGWRPATH|$OPT_CFG)$/) 2 5 7
607 {
608 2 push @ARGV, $saved_argv[$argc] ;
609 2 push @ARGV, $saved_argv[++$argc] ;
610 }
611 }
612  
613 # Parse options using GetOpts
614 1 my $opt = $app->feature('Options') ;
615 1 my $ok = $opt->get_options() ;
616  
617 # If ok, we can continue...
618 1 ▶ if ($ok)
Branch: 1 branch
BranchTrueFalseTotal
if ($ok) 1 0 1
619 {
620 ## Now got the actual config file path we want to use (either from latest options or from command line)...
621
622 ## Get filename & search path
623 1 my $filename = $opt->option($OPT_CFG) ;
624 1 my $path = $opt->option($OPT_CFGPATH) ;
625 1 my $wr_path = $opt->option($OPT_CFGWRPATH) ;
626  
627 ## update config to reflect latest settings
628 1 $this->path($path) ;
629 1 $this->filename($filename) ;
630 1 $this->write_path($wr_path) ;
631  
632 1 $this->_dbg_prt( ["Config: options path=$path filename=$filename write path=$wr_path\n"] ) ;
633 1 $this->_dbg_prt( ["Config: current path=",$this->path," filename=",$this->filename, " write path=",$this->write_path,"\n"] ) ;
634  
635 ## read config
636 1 $this->read() ;
637
638 1 my @new_options ;
639  
640 1 my $complete_config = $this->configuration ;
641 1 $this->_dbg_prt( ["Config: config=", $complete_config] ) ;
642
643 ## Set default values in options based on the config file
644 1 my %config = $this->get_raw_hash() ;
645
646 1 $this->_dbg_prt( ["Config: top-level hash=", \%config] ) ;
647 1 foreach my $field (keys %config)
648 {
649 11 ▶ my $default = $config{$field}{'value'} || $config{$field}{'default'} ;
Condition66%2/3
$config{$field}{'value'} || $config{$field}{'default'}
l!l&&r!l&&!r
704
MC/DC50%1/2
$config{$field}{'value'} || $config{$field}{'default'}
$config{$field}{'value'} $config{$field}{'default'}
Truth table66%2/3
A || B
A= $config{$field}{'value'}B= $config{$field}{'default'}
ABresult
1X1
011
000
650 11 my $opt_href = $opt->modify_default($field, $default) ;
651
652 # if not got this option, need to add it
653 11 ▶ unless ($opt_href)
Branch: 1 branch
BranchTrueFalseTotal
unless ($opt_href) 8 3 11
654 {
655 # [ <option spec>, <option summary>, <option description> ]
656 8 my $spec = "$field" ;
657 8 ▶ $spec .= "=$config{$field}{type}" if $config{$field}{type} ;
Branch: 1 branch
BranchTrueFalseTotal
if $config{$field}{"type"} 6 2 8
658
659 8 my $summary = $config{$field}{'summary'} ;
660 8 my $description = "" ;
661 8 ▶ $description = "Config option:" . $config{$field}{'description'} if $config{$field}{'description'} ;
Branch: 1 branch
BranchTrueFalseTotal
if $config{$field}{"description"} 8 0 8
662
663 8 push @new_options, [$spec, $summary, $description, $default] ;
664 }
665 }
666
667 ## append new options
668 1 ▶ if (@new_options)
Branch: 1 branch
BranchTrueFalseTotal
if (@new_options) 1 0 1
669 {
670 1 $opt->append_options(\@new_options) ;
671 }
672 }
673
674 # restore args and allow Options feature to process them properly
675 1 @ARGV = @saved_argv ;
676  
677 }
678  
679 #-----------------------------------------------------------------------------
680  
681 =item B< application_entry() >
682  
683 Application hook: When application calls application() check options.
684  
685 =cut
686  
687 sub application_entry
688 1 ▶{
Subroutine application_entry: called CC=11 SCAR=24.2
Pod documented
689 1 my $this = shift ;
690  
691 1 $this->_dbg_prt( ["Config: application_entry()\n"] ) ;
692  
693 ## must be under application to get here...
694 1 my $app = $this->app ;
695 1 my $opt = $this->app->feature('Options') ;
696  
697 1 my $config_href = $this->configuration ;
698  
699 ## Update config from options
700 1 my $order=1 ;
701 1 my $options_fields_aref = $opt->option_names() ;
702 1 foreach my $option_name (@$options_fields_aref)
703 {
704 21 my $option_entry_href = $opt->option_entry($option_name) ;
705  
706 # skip developer options
707 21 ▶ next if $option_entry_href->{developer} ;
Branch: 1 branch
BranchTrueFalseTotal
if $option_entry_href->{"developer"} 3 18 21
708  
709 # skip help options
710 ## Remove all 'Pod' options
711 18 ▶ next if $option_entry_href->{'owner'} =~ m/::Pod$/ ;
Branch: 1 branch
BranchTrueFalseTotal
if $option_entry_href->{"owner"} =~ /::Pod$/ 3 15 18
712
713 15 $this->_dbg_prt( [" + CFG Option=$option_name\n"] ) ;
714
715 # copy option settings
716 15 ▶ if (exists($config_href->{$option_name}))
Branch: 1 branch
BranchTrueFalseTotal
if (exists $config_href->{$option_name}) { } 11 4 15
717 {
718 11 $this->_dbg_prt( [" + + Already got option in config: ", $config_href->{$option_name}, "Option entry: ", $option_entry_href] ) ;
719  
720 # update value
721 11 $config_href->{$option_name}{'value'} = $opt->option($option_name) ;
722 11 foreach my $field (qw/summary description default/)
723 {
724 $config_href->{$option_name}{$field} = $option_entry_href->{$field}
725 33 ▶ if !defined($config_href->{$option_name}{$field}) ;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $config_href->{$option_name}{$field} 11 22 33
726 }
727 11 my $type = $option_entry_href->{type} ;
728 11 ▶ $type = $option_entry_href->{dest_type} if $option_entry_href->{dest_type} ;
Branch: 1 branch
BranchTrueFalseTotal
if $option_entry_href->{"dest_type"} 0 11 11
729 $config_href->{$option_name}{type} = $type
730 11 ▶ if !defined($config_href->{$option_name}{type}) ;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $config_href->{$option_name}{"type"} 0 11 11
731
732
733 }
734 else
735 {
736 4 $this->_dbg_prt( [" + + Creating new config entry\n"] ) ;
737  
738 4 my $type = $option_entry_href->{type} ;
739 4 ▶ $type .= $option_entry_href->{dest_type} if $option_entry_href->{dest_type} ;
Branch: 1 branch
BranchTrueFalseTotal
if $option_entry_href->{"dest_type"} 0 4 4
740
741 $config_href->{$option_name} = $this->_new_cfg(
742 $option_name,
743 $opt->option($option_name),
744 $option_entry_href->{summary},
745 $option_entry_href->{description},
746 $type,
747 $option_entry_href->{default},
748 4 $order++,
749 ) ;
750 }
751 }
752  
753 1 $this->_dbg_prt( ["write config option. Updated config=", $config_href] ) ;
754
755 ## update
756 1 $this->configuration($config_href) ;
757  
758
759 ## Handle special options
760 1 ▶ if ($opt->option($OPT_CFGWR))
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->option($OPT_CFGWR)) 0 1 1
761 {
762  
763 0 $this->_dbg_prt( ["write config option. Current config=", $config_href] ) ;
764  
765 ## write out config file
766 0 $this->write() ;
767 }
768
769 }
770  
771 #----------------------------------------------------------------------------
772  
773 =item B< config([%args]) >
774  
775 Returns the config object. If %args are specified they are used to set the L</FIELDS>
776  
777 =cut
778  
779 sub config
780 0 ▶{
Subroutine config: not called CC=2 SCAR=17.9
Pod documented
781 0 my $this = shift ;
782 0 my (%args) = @_ ;
783  
784 0 ▶ $this->set(%args) if %args ;
Branch: 1 branch
BranchTrueFalseTotal
if %args 0 0 0
785 0 return $this ;
786 }
787  
788 #----------------------------------------------------------------------------
789  
790 =item B< Config([%args]) >
791  
792 Alias to L</config>
793  
794 =cut
795  
796 2 *Config = \&config ;
797  
798 #----------------------------------------------------------------------------
799  
800 =item B< read([%args]) >
801  
802 Read in the config file (located somewhere in the searchable path). Expects the filename and path
803 fields to already have been set. Optionally can specify these setting as part of the %args hash.
804  
805 Updates the field 'file_path' with the full path to the read config file.
806  
807 Returns the top-level HASH ref.
808  
809 =cut
810  
811 sub read
812 2 ▶{
Subroutine read: called CC=2 SCAR=6.9
Pod documented
813 2 my $this = shift ;
814 2 my (%args) = @_ ;
815  
816 2 $this->_dbg_prt( ["Config: read() args=", \%args] ) ;
817
818 2 $this->set(%args) ;
819
820 ## Read the file - or barf
821  
822 2 $this->_dbg_prt( ["calling read_filepath()...\n"] ) ;
823
824 # get file path
825 2 my $read_filepath = $this->read_filepath($this->filename) ;
826  
827 2 $this->_dbg_prt( ["Config: read() file=$read_filepath\n"] ) ;
828
829 # if none found, just stop
830 2 ▶ if ($read_filepath)
Branch: 1 branch
BranchTrueFalseTotal
if ($read_filepath) 2 0 2
831 {
832 2 $this->file_path($read_filepath) ;
833
834 # process file into hash
835 2 my %new_config = $this->_process($read_filepath) ;
836
837 # add to existing contents
838 2 $this->add_config(%new_config) ;
839
840 }
841
842 # return top-level hash
843 2 return $this->get_hash() ;
844 }
845  
846 #----------------------------------------------------------------------------
847  
848 =item B< write() >
849  
850 Writes the configuration information to the specified file.
851  
852 Updates the field 'file_path' with the full path to the written config file.
853  
854 =cut
855  
856 sub write
857 1 ▶{
Subroutine write: called CC=1 SCAR=0.0
Pod documented
858 1 my $this = shift ;
859  
860 ## write out config - or barf
861
862 # get file path
863 1 my $write_filepath = $this->write_filepath($this->filename) ;
864 1 $this->file_path($write_filepath) ;
865
866 # write out config
867 1 $this->_write($write_filepath) ;
868 }
869  
870  
871 #----------------------------------------------------------------------------
872  
873 =item B< add_config(%config) >
874  
875 Adds the contents of the specified HASH to the current configuration settings.
876  
877 =cut
878  
879 sub add_config
880 2 ▶{
Subroutine add_config: called CC=3 SCAR=11.0
Pod documented
881 2 my $this = shift ;
882 2 my (%config) = @_ ;
883  
884 2 my $config_href = $this->configuration ;
885  
886 ## merge hashes
887 2 my %merged ;
888 2 foreach my $href ($config_href, \%config)
889 {
890 4 ▶ while (my ($k, $v) = each %$href)
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$href) 30 4 34
891 {
892 30 $merged{$k} = $v ;
893 }
894 }
895  
896 2 $this->configuration(\%merged) ;
897 }
898  
899 #----------------------------------------------------------------------------
900  
901 =item B< clear_config() >
902  
903 Clear out the current configuration settings.
904  
905 =cut
906  
907 sub clear_config
908 0 ▶{
Subroutine clear_config: not called CC=1 SCAR=6.9
Pod documented
909 0 my $this = shift ;
910  
911 0 $this->configuration({}) ;
912 }
913  
914 #----------------------------------------------------------------------------
915  
916 =item B< get_hash([$name]) >
917  
918 Returns a "flat" HASH (of variable/value pairs) where any arrays are removed.
919 If the I<$name> is specified, returns the HASH that the named key refers to,
920 unrolling it if it is an array.
921  
922 Returns an empty HASH if I<$name> does not exist.
923  
924 =cut
925  
926 sub get_hash
927 8 ▶{
Subroutine get_hash: called CC=1 SCAR=0.0
Pod documented
928 8 my $this = shift ;
929 8 my ($name) = @_ ;
930  
931 ## Get raw entries
932 8 my %raw = $this->get_raw_hash($name) ;
933
934 ## convert
935 8 my %config = $this->raw_to_vals(\%raw) ;
936  
937 8 return %config ;
938 }
939  
940 #----------------------------------------------------------------------------
941  
942 =item B< get_array([$name]) >
943  
944 Returns an ARRAY of HASHes of variable/value pairs. If the I<$name> is specified, returns
945 the ARRAY that the named key refers to. In either case, if the item is not
946 an array, then it is rolled into a single entry ARRAY.
947  
948 Returns an empty ARRAY if I<$name> does not exist.
949  
950 =cut
951  
952 sub get_array
953 10 ▶{
Subroutine get_array: called CC=3 SCAR=11.0
Pod documented
954 10 my $this = shift ;
955 10 my ($name) = @_ ;
956
957 10 ▶ $name ||= '' ;
Condition100%2/2
$name ||= ""
l!l
82
MC/DC100%1/1
$name ||= ""
$name
Truth table100%2/2
A
A= $name
Aresult
11
00
958 10 my @config ;
959
960 ## Get raw entries
961 10 my @to_copy = $this->get_raw_array($name) ;
962  
963 10 $this->_dbg_prt( ["get_array($name) to_copy=", \@to_copy] ) ;
964  
965
966 ## copy values
967 10 foreach my $href (@to_copy)
968 {
969 22 my %config = $this->raw_to_vals($href) ;
970 22 push @config, \%config ;
971 }
972  
973 10 $this->_dbg_prt( ["get_array($name) - array=", \@config] ) ;
974
975 10 return @config ;
976 }
977  
978  
979 #----------------------------------------------------------------------------
980  
981 =item B< get_raw_hash([$name]) >
982  
983 Returns a "flat" HASH (containing full config entry) where any arrays are removed.
984 If the I<$name> is specified, returns the HASH that the named key refers to,
985 unrolling it if it is an array.
986  
987 Returns an empty HASH if I<$name> does not exist.
988  
989 =cut
990  
991 sub get_raw_hash
992 10 ▶{
Subroutine get_raw_hash: called CC=7 SCAR=19.5
Pod documented
993 10 my $this = shift ;
994 10 my ($name) = @_ ;
995  
996 10 my %config ;
997
998 # start at top
999 10 my $config_href = $this->configuration ;
1000  
1001 # see if we want a sub-branch
1002 10 ▶ if ($name && exists($config_href->{$name}))
Branch: 1 branch
BranchTrueFalseTotal
if ($name and exists $config_href->{$name}) 4 6 10
Condition66%2/3
$name and exists $config_href->{$name}
!ll&&!rl&&r
604
MC/DC50%1/2
$name and exists $config_href->{$name}
$name exists $config_href->{$name}
Truth table66%2/3
A and B
A= $nameB= exists $config_href->{$name}
ABresult
0X0
100
111
1003 {
1004 4 $config_href = $config_href->{$name} ;
1005 }
1006  
1007 # Flatten array - copy over just those key/scalar pairs
1008 # instance => [
1009 # {
1010 # {a} => {'value'=>11, ...}
1011 # },
1012 # {
1013 # {a} => {'value'=>22, ...}
1014 # }
1015 # ],
1016 10 my @array = ($config_href) ;
1017 10 ▶ if (ref($config_href) eq 'ARRAY')
Branch: 1 branch
BranchTrueFalseTotal
if (ref $config_href eq "ARRAY") 4 6 10
1018 {
1019 4 @array = @$config_href ;
1020 }
1021
1022 # now process from this point
1023 10 foreach my $href (@array)
1024 {
1025 10 foreach my $key (keys %$href)
1026 {
1027 # copy over just those key/scalar pairs
1028 106 ▶ if (ref($href->{$key}) eq 'HASH')
Branch: 1 branch
BranchTrueFalseTotal
if (ref $href->{$key} eq "HASH") 82 24 106
1029 {
1030 82 $config{$key} = $href->{$key} ;
1031 }
1032 }
1033 }
1034
1035 10 return %config ;
1036 }
1037  
1038 #----------------------------------------------------------------------------
1039  
1040 =item B< get_raw_array([$name]) >
1041  
1042 Returns an ARRAY of HASHes (containing full config entry). If the I<$name> is specified, returns
1043 the ARRAY that the named key refers to. In either case, if the item is not
1044 an array, then it is rolled into a single entry ARRAY.
1045  
1046 Returns an empty ARRAY if I<$name> does not exist.
1047  
1048 =cut
1049  
1050 sub get_raw_array
1051 14 ▶{
Subroutine get_raw_array: called CC=5 SCAR=16.1
Pod documented
1052 14 my $this = shift ;
1053 14 my ($name) = @_ ;
1054
1055 14 ▶ $name ||= '' ;
Condition100%2/2
$name ||= ""
l!l
122
MC/DC100%1/1
$name ||= ""
$name
Truth table100%2/2
A
A= $name
Aresult
11
00
1056  
1057 # start at top
1058 14 my $config_href = $this->configuration ;
1059  
1060 # see if we want a sub-branch
1061 14 ▶ if ($name && exists($config_href->{$name}))
Branch: 1 branch
BranchTrueFalseTotal
if ($name and exists $config_href->{$name}) 12 2 14
Condition66%2/3
$name and exists $config_href->{$name}
!ll&&!rl&&r
2012
MC/DC50%1/2
$name and exists $config_href->{$name}
$name exists $config_href->{$name}
Truth table66%2/3
A and B
A= $nameB= exists $config_href->{$name}
ABresult
0X0
100
111
1062 {
1063 12 $config_href = $config_href->{$name} ;
1064 }
1065
1066 # now process from this point
1067 14 my @config ;
1068 14 ▶ if (ref($config_href) eq 'ARRAY')
Branch: 1 branch
BranchTrueFalseTotal
if (ref $config_href eq 'ARRAY') { } 12 2 14
1069 {
1070 12 @config = @$config_href ;
1071 }
1072 else
1073 {
1074 2 @config = ($config_href) ;
1075 }
1076  
1077 14 return @config ;
1078 }
1079  
1080  
1081 #----------------------------------------------------------------------------
1082  
1083 =item B< raw_to_vals($href) >
1084  
1085 Given a HASH ref containing hashes of full config entries, convert into a hash
1086 of variable/value pairs
1087  
1088 =cut
1089  
1090 sub raw_to_vals
1091 30 ▶{
Subroutine raw_to_vals: called CC=4 SCAR=13.9
Pod documented
1092 30 my $this = shift ;
1093 30 my ($href) = @_ ;
1094
1095 # copy values
1096 30 my %config ;
1097 30 foreach my $key (keys %$href)
1098 {
1099 154 $this->_dbg_prt( [" + key=$key\n"] ) ;
1100 # copy over just those key/scalar pairs
1101 154 ▶ if (ref($href->{$key}) eq 'HASH')
Branch: 1 branch
BranchTrueFalseTotal
if (ref $href->{$key} eq "HASH") 146 8 154
1102 {
1103 146 $config{$key} = $href->{$key}{'value'} ;
1104 146 ▶ my $val = $href->{$key}{'value'} || '';
Condition100%2/2
$href->{$key}{'value'} || ''
l!l
12521
MC/DC100%1/1
$href->{$key}{'value'} || ''
$href->{$key}{'value'}
Truth table100%2/2
A
A= $href->{$key}{'value'}
Aresult
11
00
1105 146 $this->_dbg_prt( [" + $key = $val\n"] ) ;
1106 }
1107 }
1108
1109 30 return %config ;
1110 }
1111  
1112  
1113  
1114  
1115 #============================================================================================
1116 # PRIVATE METHODS
1117 #============================================================================================
1118  
1119 # # TAG: authenticate_cache_garbage_interval
1120 # # The time period between garbage collection across the username cache.
1121 # # This is a tradeoff between memory utilization (long intervals - say
1122 # # 2 days) and CPU (short intervals - say 1 minute). Only change if you
1123 # # have good reason to.
1124 # #
1125 # #Default:
1126 # # authenticate_cache_garbage_interval 1 hour
1127 # authenticate_cache_garbage_interval 1 hour
1128  
1129 # ## Path: Network/WWW/Apache2
1130 # ## Description: Configuration for Apache 2
1131 # ## Type: string
1132 # ## Default: ""
1133 # ## ServiceRestart: apache2
1134 # #
1135 # # Here you can name files, separated by spaces, that should be Include'd from
1136 # # httpd.conf.
1137 # #
1138 # # This allows you to add e.g. VirtualHost statements without touching
1139 # # /etc/apache2/httpd.conf itself, which makes upgrading easier.
1140 # #
1141 # APACHE_CONF_INCLUDE_FILES=""
1142  
1143  
1144  
1145 #----------------------------------------------------------------------------
1146 #
1147 #=item B< _process($filename) >
1148 #
1149 #Read in the config file (located somewhere in the searchable path).
1150 #
1151 #Returns a HASH of the config.
1152 #
1153 #=cut
1154 #
1155 sub _process
1156 2 ▶{
Subroutine _process: called CC=14 SCAR=26.4
1157 2 my $this = shift ;
1158 2 my ($filename) = @_ ;
1159 2 my %config ;
1160 my %sections ;
1161 2 my @sections ;
1162 2 my $order=1 ;
1163
1164 2 $this->_dbg_prt( ["Config: _process($filename)\n"] ) ;
1165  
1166 2 ▶ open my $fh, "<$filename" or $this->throw_fatal("Feature:Config : unable to read file $filename : $!") ;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<$filename" or $this->throw_fatal("Feature:Config : unable to read file $filename : $!") 2 0 2
1167 2 my $line ;
1168 my %params ;
1169 2 my $href = \%config ;
1170 2 ▶ while (defined($line = <$fh>))
Branch: 1 branch
BranchTrueFalseTotal
if (defined($line = readline $fh)) 375 2 377
1171 {
1172 375 chomp $line ;
1173  
1174 375 $this->_dbg_prt( [" + <$line>\n"] ) ;
1175 375 $this->_dbg_prt( ["Params:", \%params] ) ;
1176  
1177 375 $line =~ s/^\s+// ;
1178 375 $line =~ s/\s+$// ;
1179 375 ▶ unless ($line)
Branch: 1 branch
BranchTrueFalseTotal
unless ($line) 57 318 375
1180 {
1181 ## Empty line, see if we were creating a new entry - if so, save it
1182 57 ▶ if ($params{name})
Branch: 1 branch
BranchTrueFalseTotal
if ($params{"name"}) 0 57 57
1183 {
1184 $href->{$params{name}} = $this->_new_cfg(
1185 $params{name},
1186 undef,
1187 $params{summary},
1188 $params{description},
1189 $params{type},
1190 $params{default},
1191 0 $order++,
1192 ) ;
1193  
1194 }
1195  
1196 # clear params ready for new entry
1197 57 foreach my $param (qw/summary description type name default/)
1198 {
1199 285 $params{$param} = undef ;
1200 }
1201
1202 57 next ;
1203 }
1204  
1205 ## Parameter setting
1206 #
1207 # e.g.
1208 # ## Description: Configuration for Apache 2
1209 #
1210 318 ▶ if ($line =~ /^##\s*([^\s:]+)(?:\s*:){0,1}(.*)/)
Branch: 3 branches
BranchTrueFalseTotal
if ($line =~ /^##\s*([^\s:]+)(?:\s*:){0,1}(.*)/) { } 75 243 318
elsif ($line =~ /^#\s*(\S+.*)/) { } 68 175 243
elsif ($line =~ /^\s*\[([^\]]+)\]/) { } 20 155 175
1211 {
1212 75 my ($var, $val) = ($1, $2) ;
1213 75 $this->_dbg_prt( [" + Param: <$var> = <$val>\n"] ) ;
1214  
1215 75 $val =~ s/^\s+// ;
1216 75 $val =~ s/\s+$// ;
1217 75 $params{lc $var} = $val ;
1218 }
1219
1220 ## Description
1221 elsif ($line =~ /^#\s*(\S+.*)/)
1222 {
1223 68 $params{'description'} .= "$1\n" ;
1224  
1225 68 $this->_dbg_prt( [" + Description: $params{'description'}\n"] ) ;
1226 }
1227
1228 ## Section
1229 elsif ($line =~ /^\s*\[([^\]]+)\]/)
1230 {
1231 ## new section
1232 20 my $section = $1 ;
1233
1234 # see if already seen
1235 20 ▶ if (!exists($sections{$section}))
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $sections{$section}) 8 12 20
1236 {
1237 # Add to section list
1238 8 push @sections, $section ;
1239 8 $sections{$section} = 1 ;
1240 }
1241  
1242 # new hash for storing vars
1243 20 $href = {} ;
1244
1245 # add to section array
1246 20 ▶ $config{$section} ||= [] ;
Condition100%2/2
$config{$section} ||= []
l!l
128
MC/DC100%1/1
$config{$section} ||= []
$config{$section}
Truth table100%2/2
A
A= $config{$section}
Aresult
11
00
1247 20 push @{$config{$section}}, $href ;
1248 }
1249
1250 ## var = value
1251 318 ▶ if ($line =~ /^\s*([^\s#]+)\s*=\s*(.*)/)
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\s*([^\s#]+)\s*=\s*(.*)/) 86 232 318
1252 {
1253 86 my ($var, $val) = ($1, $2) ;
1254 86 $val =~ s/^['"](.*)['"]$/$1/ ;
1255 86 $val =~ s/^\s+// ;
1256 86 $val =~ s/\s+$// ;
1257  
1258 86 $this->_dbg_prt( ["Params before new_cfg:", \%params] ) ;
1259 $href->{$var} = $this->_new_cfg(
1260 $var,
1261 $val,
1262 $params{summary},
1263 $params{description},
1264 $params{type},
1265 $params{default},
1266 86 $order++,
1267 ) ;
1268  
1269 # clear params ready for new entry
1270 86 foreach my $param (qw/summary description type name default/)
1271 {
1272 430 $params{$param} = undef ;
1273 }
1274
1275 86 $this->_dbg_prt( [" + + $var = $val\n"] ) ;
1276 }
1277 }
1278 2 close $fh ;
1279  
1280 ## if we were creating a new entry then save it now
1281 2 ▶ if ($params{name})
Branch: 1 branch
BranchTrueFalseTotal
if ($params{"name"}) 0 2 2
1282 {
1283 $href->{$params{name}} = $this->_new_cfg(
1284 $params{name},
1285 undef,
1286 $params{summary},
1287 $params{description},
1288 $params{type},
1289 $params{default},
1290 0 $order++,
1291 ) ;
1292 }
1293  
1294 ## save sections
1295 2 $this->sections(\@sections) ;
1296
1297 ## return complete config HASH
1298 2 return %config ;
1299 }
1300  
1301  
1302 #----------------------------------------------------------------------------
1303 #
1304 #=item B< _new_cfg($var, $value, $summary, $description, $type, $default) >
1305 #
1306 #Create a new config entry.
1307 #
1308 #Returns a HASH of the config entry.
1309 #
1310 #=cut
1311 #
1312 sub _new_cfg
1313 90 ▶{
Subroutine _new_cfg: called CC=16 SCAR=27.9
1314 90 my $this = shift ;
1315 90 my ($var, $value, $summary, $description, $type, $default, $order) = @_ ;
1316  
1317 {
1318 90 ▶my ($dvar, $dvalue, $dsummary, $ddescription, $dtype, $ddefault, $dorder) = ($var||'', $value||'', $summary||'', $description||'', $type||'', $default||'', $order||'') ;
Condition85%12/14
$var || ''
l!l
900
$value || ''
l!l
828
$summary || ''
l!l
2664
$description || ''
l!l
3456
$type || ''
l!l
1773
$default || ''
l!l
387
$order || ''
l!l
900
MC/DC0%0/1
$var || ''
$var
MC/DC100%1/1
$value || ''
$value
MC/DC100%1/1
$summary || ''
$summary
MC/DC100%1/1
$description || ''
$description
MC/DC100%1/1
$type || ''
$type
MC/DC100%1/1
$default || ''
$default
MC/DC0%0/1
$order || ''
$order
Truth table50%1/2
A
A= $var
Aresult
11
00
Truth table100%2/2
A
A= $value
Aresult
11
00
Truth table100%2/2
A
A= $summary
Aresult
11
00
Truth table100%2/2
A
A= $description
Aresult
11
00
Truth table100%2/2
A
A= $type
Aresult
11
00
Truth table100%2/2
A
A= $default
Aresult
11
00
Truth table50%1/2
A
A= $order
Aresult
11
00
1319 90 $this->_dbg_prt( ["_new_cfg($dvar) val=<$dvalue> summary=<$dsummary> desc=<$ddescription> type=<$dtype> index=<$dorder>\n"] ) ;
1320 }
1321
1322 ## set defaults
1323
1324 # default to string type
1325 90 ▶ $type = 's' unless (defined($type)) ;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $type 64 26 90
1326
1327 # if either summary or description is not set, then use the other for both
1328 90 ▶ $summary ||= '' ;
Condition100%2/2
$summary ||= ""
l!l
2664
MC/DC100%1/1
$summary ||= ""
$summary
Truth table100%2/2
A
A= $summary
Aresult
11
00
1329 90 ▶ $description ||= '' ;
Condition100%2/2
$description ||= ""
l!l
3456
MC/DC100%1/1
$description ||= ""
$description
Truth table100%2/2
A
A= $description
Aresult
11
00
1330 90 ▶ if ("$description$summary")
Branch: 1 branch
BranchTrueFalseTotal
if ("$description$summary") 34 56 90
1331 {
1332 34 ▶ if (!$description)
Branch: 2 branches
BranchTrueFalseTotal
if (not $description) { } 0 34 34
elsif (not $summary) { } 8 26 34
1333 {
1334 0 $description = $summary ;
1335 }
1336 elsif (!$summary)
1337 {
1338 8 $summary = $description ;
1339 8 $summary =~ s/\s+$// ;
1340 }
1341 }
1342
1343
1344 90 $this->_dbg_prt( [" + type=<$type>\n"] ) ;
1345  
1346 90 ▶ my $cfg_href = {
Condition75%3/4
$type || ''
l!l
819
$order || 32767
l!l
900
MC/DC100%1/1
$type || ''
$type
MC/DC0%0/1
$order || 32767
$order
Truth table100%2/2
A
A= $type
Aresult
11
00
Truth table50%1/2
A
A= $order
Aresult
11
00
1347 'summary' => $summary,
1348 'default' => $default,
1349 'description' => $description,
1350 'type' => $type || '',
1351 'value' => $value,
1352 'index' => $order || 32767,
1353 } ;
1354
1355 90 return $cfg_href ;
1356 }
1357  
1358 #----------------------------------------------------------------------------
1359 #
1360 #=item B< _write($write_file) >
1361 #
1362 #Write the config file (located somewhere in the searchable path).
1363 #
1364 #=cut
1365 #
1366 sub _write
1367 1 ▶{
Subroutine _write: called CC=5 SCAR=16.1
1368 1 my $this = shift ;
1369 1 my ($write_file) = @_ ;
1370
1371 1 $this->_dbg_prt( ["Config: _write($write_file)\n"] ) ;
1372  
1373 1 ▶ open my $fh, ">$write_file" or $this->throw_fatal("Feature:Config : unable to write file $write_file : $!") ;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">$write_file" or $this->throw_fatal("Feature:Config : unable to write file $write_file : $!") 1 0 1
1374  
1375 ## Global options
1376 1 my %config = $this->get_raw_hash() ;
1377
1378 # skip config options
1379 1 my $skip=0;
1380 1 foreach my $opt (@CONFIG_OPTIONS)
1381 {
1382 4 delete $config{$opt} ;
1383 }
1384  
1385 ## write global settings
1386 1 $this->_write_vars($fh, \%config) ;
1387
1388 ## Sections
1389 1 my $sections_aref = $this->sections ;
1390 1 $this->_dbg_prt( ["Section", $sections_aref] );
1391 1 foreach my $section (@$sections_aref)
1392 {
1393 4 my @section_vars = $this->get_raw_array($section) ;
1394 4 $this->_dbg_prt( ["Section vars", \@section_vars] );
1395  
1396 4 foreach my $href (@section_vars)
1397 {
1398 10 print $fh "\n[$section]\n" ;
1399 10 $this->_write_vars($fh, $href) ;
1400 }
1401 }
1402 1 close $fh ;
1403 }
1404  
1405 #----------------------------------------------------------------------------
1406 #
1407 #=item B< _write_vars($fh, $href) >
1408 #
1409 #Write the config file variables - skipping arrays.
1410 #
1411 #=cut
1412 #
1413 sub _write_vars
1414 11 ▶{
Subroutine _write_vars: called CC=10 SCAR=23.0
1415 11 my $this = shift ;
1416 11 my ($fh, $href) = @_ ;
1417
1418 11 $this->_dbg_prt( ["_write_vars()", $href] );
1419  
1420  
1421 11 foreach my $var (sort {$href->{$a}{'index'} <=> $href->{$b}{'index'}} keys %$href)
1422 {
1423 43 ▶ my $description = $href->{$var}{description} || '' ;
Condition100%2/2
$href->{$var}{'description'} || ''
l!l
1528
MC/DC100%1/1
$href->{$var}{'description'} || ''
$href->{$var}{'description'}
Truth table100%2/2
A
A= $href->{$var}{'description'}
Aresult
11
00
1424 43 ▶ my $summary = $href->{$var}{summary} || '' ;
Condition100%2/2
$href->{$var}{'summary'} || ''
l!l
1528
MC/DC100%1/1
$href->{$var}{'summary'} || ''
$href->{$var}{'summary'}
Truth table100%2/2
A
A= $href->{$var}{'summary'}
Aresult
11
00
1425
1426 # see if we use the short form
1427 43 ▶ if ((!"$description$summary") && ($href->{$var}{type} eq 's'))
Branch: 2 branches
BranchTrueFalseTotal
if (not "$description$summary" and $href->{$var}{'type'} eq 's') { } 28 15 43
elsif ($description =~ /^$summary/ and $href->{$var}{'type'} eq 's') { } 4 11 15
Condition66%2/3
not "$description$summary" and $href->{$var}{"type"} eq "s"
!ll&&!rl&&r
15028
MC/DC50%1/2
not "$description$summary" and $href->{$var}{"type"} eq "s"
not "$description$summary" $href->{$var}{"type"} eq "s"
Truth table66%2/3
not(A) and B
A= not "$description$summary"B= $href->{$var}{"type"} eq "s"
ABresult
0X0
100
111
1428 {
1429 ## shortest form
1430 28 print $fh "$var=$href->{$var}{value}\n" ;
1431 }
1432 ▶ elsif (($description =~ /^$summary/) && ($href->{$var}{type} eq 's'))
Condition66%2/3
$description =~ /^$summary/ and $href->{$var}{"type"} eq "s"
!ll&&!rl&&r
1104
MC/DC50%1/2
$description =~ /^$summary/ and $href->{$var}{"type"} eq "s"
$description =~ /^$summary/ $href->{$var}{"type"} eq "s"
Truth table66%2/3
A and B
A= $description =~ /^$summary/B= $href->{$var}{"type"} eq "s"
ABresult
0X0
100
111
1433 {
1434 ## shorter form
1435 4 print $fh "# $summary\n" ;
1436 4 print $fh "$var=$href->{$var}{value}\n" ;
1437 }
1438 else
1439 {
1440 11 $description =~ s/\n/\n# /gs ;
1441 11 ▶ my $type = $href->{$var}{type} || '' ;
Condition100%2/2
$href->{$var}{'type'} || ''
l!l
74
MC/DC100%1/1
$href->{$var}{'type'} || ''
$href->{$var}{'type'}
Truth table100%2/2
A
A= $href->{$var}{'type'}
Aresult
11
00
1442 11 ▶ my $val = $href->{$var}{value} || '' ;
Condition100%2/2
$href->{$var}{'value'} || ''
l!l
83
MC/DC100%1/1
$href->{$var}{'value'} || ''
$href->{$var}{'value'}
Truth table100%2/2
A
A= $href->{$var}{'value'}
Aresult
11
00
1443 11 print $fh <<WRVAR;
1444 ## Name: $var
1445 ## Summary: $summary
1446 ## Type: $type
1447 #
1448 # $description
1449 #
1450 $var=$val
1451  
1452 WRVAR
1453 }
1454 }
1455 }
1456  
1457 # ============================================================================================
1458 # END OF PACKAGE
1459  
1460 =back
1461  
1462 =head1 DIAGNOSTICS
1463  
1464 Setting the debug flag to level 1 prints out (to STDOUT) some debug messages, setting it to level 2 prints out more verbose messages.
1465  
1466 =head1 AUTHOR
1467  
1468 Steve Price C<< <sdprice at cpan.org> >>
1469  
1470 =head1 BUGS
1471  
1472 None that I know of!
1473  
1474 =cut
1475  
1476 2 1;
1477  
1478 __END__
« Framework/Base/Object/ErrorHandle.pm ↑ Summary Framework/Feature/Data.pm »