File Coverage

blib/lib/AppConfig/MyFile.pm
Criterion Covered Total %
statement 41 51 80.3
branch 5 16 31.2
condition 1 3 33.3
subroutine 7 9 77.7
pod 0 3 0.0
total 54 82 65.8


line stmt bran cond sub pod time code
1             #============================================================================
2             #
3             # AppConfig::MyFile.pm
4             #
5             # Perl5 module to read configuration files and use the contents therein
6             # to update variable values in an AppConfig::State object.
7             #
8             # Written by Andy Wardley
9             #
10             # Copyright (C) 1997,1998 Canon Research Centre Europe Ltd.
11             # All Rights Reserved.
12             #
13             # This module is free software; you can redistribute it and/or modify it
14             # under the same terms as Perl itself.
15             #
16             #----------------------------------------------------------------------------
17             #
18             # $Id$
19             #
20             #============================================================================
21              
22             package AppConfig::MyFile;
23              
24             require 5.004;
25              
26 1     1   8614 use AppConfig;
  1         3  
  1         41  
27 1     1   7 use AppConfig::State;
  1         1  
  1         29  
28              
29 1     1   6 use strict;
  1         7  
  1         38  
30 1     1   7 use vars qw( $VERSION );
  1         2  
  1         672  
31              
32             $VERSION = sprintf("%d.%02d", q$Revision$ =~ /(\d+)\.(\d+)/);
33              
34              
35              
36             #========================================================================
37             # ----- PUBLIC METHODS -----
38             #========================================================================
39              
40             #========================================================================
41             #
42             # new($state, $file, [$file, ...])
43             #
44             # Module constructor. The first, mandatory parameter should be a
45             # reference to an AppConfig::State object to which all actions should
46             # be applied. The remaining parameters are assumed to be file names or
47             # file handles for reading and are passed to parse().
48             #
49             # Returns a reference to a newly created AppConfig::File object.
50             #
51             #========================================================================
52              
53             sub new {
54 1     1 0 2 my $class = shift;
55 1         2 my $state = shift;
56            
57              
58 1         11 my $self = {
59             STATE => $state, # AppConfig::State ref
60             DEBUG => $state->_debug(), # store local copy of debug
61             PEDANTIC => $state->_pedantic, # and pedantic flags
62             };
63              
64 1         42 bless $self, $class;
65              
66             # call parse(@_) to parse any files specified as further params
67 1 50       5 $self->parse(@_)
68             if @_;
69              
70 1         9 return $self;
71             }
72              
73              
74              
75             #========================================================================
76             #
77             # parse($file, [file, ...])
78             #
79             # Reads and parses a config file...
80             #
81             # Returns undef on system error, 0 if all files were parsed but generated
82             # one or more warnings, 1 if all files parsed without warnings.
83             #
84             #========================================================================
85              
86             sub parse {
87 1     1 0 2 my $self = shift;
88 1         2 my $warnings = 0;
89 1         2 my $file;
90              
91              
92             # take a local copy of the state to avoid much hash dereferencing
93 1         4 my ($state, $debug, $pedantic) = @$self{ qw( STATE DEBUG PEDANTIC ) };
94              
95             # we want to install a custom error handler into the AppConfig::State
96             # which appends filename and line info to error messages and then
97             # calls the previous handler; we start by taking a copy of the
98             # current handler..
99 1         4 my $errhandler = $state->_ehandler();
100              
101             # ...and if it doesn't exist, we craft a default handler
102 0     0   0 $errhandler = sub { warn(sprintf(shift, @_), "\n") }
103 1 50       15 unless defined $errhandler;
104              
105             # install a closure as a new error handler
106             $state->_ehandler(
107             sub {
108             # modify the error message
109 0     0   0 my $format = shift;
110 0 0       0 $format .= ref $file
111             ? " at line $."
112             : " at $file line $.";
113              
114             # chain call to prevous handler
115 0         0 &$errhandler($format, @_);
116             }
117 1         8 );
118              
119             # trawl through all files passed as params
120 1         13 FILE: while ($file = shift) {
121              
122             # local/lexical vars ensure opened files get closed
123 1         2 my $handle;
124 1         3 local *FH;
125              
126             # if the file is a reference, we assume it's a file handle, if
127             # not, we assume it's a filename and attempt to open it
128 1         2 $handle = $file;
129 1 50       5 if (ref($file)) {
130 1         2 $handle = $file;
131              
132             # DEBUG
133 1 50       3 print STDERR "reading from file handle: $file\n" if $debug;
134             }
135             else {
136             # open and read config file
137 0 0       0 open(FH, $file) or do {
138             # restore original error handler and report error
139 0         0 $state->_ehandler($errhandler);
140 0         0 $state->_error("$file: $!");
141              
142 0         0 return undef;
143             };
144 0         0 $handle = \*FH;
145              
146             # DEBUG
147 0 0       0 print STDERR "reading file: $file\n" if $debug;
148             }
149              
150             # your code goes here...
151 1         8 while (<$handle>) {
152 1         35 print;
153             };
154             }
155              
156             # restore original error handler
157 1         4 $state->_ehandler($errhandler);
158            
159             # return $warnings => 0, $success => 1
160 1 50       20 return $warnings ? 0 : 1;
161             }
162              
163              
164              
165             #========================================================================
166             # ----- AppConfig PUBLIC METHOD -----
167             #========================================================================
168              
169             package AppConfig;
170              
171              
172             #========================================================================
173             #
174             # myfile(@files)
175             #
176             # The myfile() method is called to...
177             #
178             # Propagates the return value from AppConfig::MyFile->parse().
179             #
180             #========================================================================
181              
182             sub myfile {
183 1     1 0 126 my $self = shift;
184 1         3 my $state = $self->{ STATE };
185 1         2 my $myfile;
186              
187              
188             # create an AppConfig::MyFile object if one isn't defined
189 1   33     14 $myfile = $self->{ MYFILE } ||= AppConfig::MyFile->new($state);
190              
191             # call on the AppConfig::File object to process files.
192 1         5 $myfile->parse(@_);
193             }
194              
195              
196              
197             1;
198              
199             __END__