blib/lib/Templer/Global.pm untested

filter: Statementstmt 0.0% 0 / 46 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 78 CC 8 SCAR 34.6
CC
8
Cov
0%
_readGlobalCFG
56.0
new
6.0
field
2.0
CRAP 31.7
1  
2 =head1 NAME
3  
4 Templer::Global - Configuration-file parser for templer.
5  
6 =cut
7  
8 =head1 SYNOPSIS
9  
10 use strict;
11 use warnings;
12  
13 use Templer::Global;
14  
15 my $site = Templer::Global->new( file => "./templer.cfg" );
16 my $suffix = $site->field( "suffix" );
17  
18 =cut
19  
20 =head1 DESCRIPTION
21  
22 This class is responsible for parsing the top-level templer.cfg file
23 which we assume will be present in a templer-based site.
24  
25 The file is a simple key=value store, with comments being prefixed by
26 the hash ("#") character, and ignored.
27  
28 This object is created when templer is started so that the options may
29 be parsed/read. Once that happens the options are merged with the
30 command-line flags, and this object isn't touched again.
31  
32 =cut
33  
34 =head1 LICENSE
35  
36 This module is free software; you can redistribute it and/or modify it
37 under the terms of either:
38  
39 a) the GNU General Public License as published by the Free Software
40 Foundation; either version 2, or (at your option) any later version,
41 or
42  
43 b) the Perl "Artistic License".
44  
45 =cut
46  
47 =head1 AUTHOR
48  
49 Steve Kemp <steve@steve.org.uk>
50  
51 =cut
52  
53 =head1 COPYRIGHT AND LICENSE
54  
55 Copyright (C) 2012-2015 Steve Kemp <steve@steve.org.uk>.
56  
57 This library is free software. You can modify and or distribute it under
58 the same terms as Perl itself.
59  
60 =cut
61  
62 =head1 METHODS
63  
64 =cut
65  
66  
67 use strict;
68 use warnings;
69  
70  
71 package Templer::Global;
72  
73  
74 =head2 new
75  
76 Constructor.
77  
78 Any parameters specified in our single hash-argument are saved away,
79 the filename specified in the 'file' parameter will be opened and parsed.
80  
81 =cut
82  
83 sub new
84 {
85 my ( $proto, %supplied ) = (@_);
86 my $class = ref($proto) || $proto;
87  
88 my $self = {};
89  
90 #
91 # Allow user supplied values to override our defaults
92 #
93 foreach my $key ( keys %supplied )
94 {
95 $self->{ lc $key } = $supplied{ $key };
96 }
97  
98 bless( $self, $class );
99 $self->_readGlobalCFG( $self->{ 'file' } ) if ( $self->{ 'file' } );
100 return $self;
101 }
102  
103  
104  
105 =head2 _readGlobalCFG
106  
107 Read the specified configuration file. Called by the constructor if
108 a filename was specified.
109  
110 =cut
111  
112 sub _readGlobalCFG
113 {
114 my ( $self, $filename ) = (@_);
115  
116 #
117 # If the configuration file doesn't exist that's a shame.
118 #
119 return if ( !-e $filename );
120  
121 #
122 # Open the file, making sure we're UTF-8 safe.
123 #
124 open( my $handle, "<:utf8", $filename ) or
125 die "Failed to read '$filename' - $!";
126 binmode( $handle, ":utf8" );
127  
128 while ( my $line = <$handle> )
129 {
130  
131 # strip trailing newline.
132 $line =~ s/[\r\n]*//g;
133  
134 # skip comments.
135 next if ( $line =~ /^#/ );
136  
137 # If the line is : key = value
138 if ( $line =~ /^([^=]+)=(.*)$/ )
139 {
140 my $key = $1;
141 my $val = $2;
142 $key = lc($key);
143 $key =~ s/^\s+|\s+$//g;
144 $val =~ s/^\s+|\s+$//g;
145  
146 #
147 # command expansion?
148 #
149 if ( $val =~ /(.*)`([^`]+)`(.*)/ )
150 {
151  
152 # store
153 my $pre = $1;
154 my $cmd = $2;
155 my $post = $3;
156  
157 # get output
158 my $output = `$cmd`;
159 chomp($output);
160  
161 # build up replacement.
162 $val = $pre . $output . $post;
163 }
164  
165 #
166 # If the line is pre/post-build then save the values as an array
167 #
168 if ( $key =~ /^(pre|post)-build$/ )
169 {
170 push( @{ $self->{ $key } }, $val );
171 }
172 else
173 {
174  
175 #
176 # The general case is store the value in the key.
177 #
178 $self->{ $key } = $val;
179 }
180 print "Templer::Global set: $key => $val\n"
181 if ( $self->{ 'debug' } );
182 }
183 }
184 close($handle);
185 }
186  
187  
188  
189 =head2 field
190  
191 Retrieve a value from the file, by key.
192  
193 This is only called by templer to retrieve the pre/post-build
194 commands to execute.
195  
196 =cut
197  
198 sub field
199 {
200 my ( $self, $field ) = (@_);
201 return ( $self->{ $field } );
202 }
203  
204  
205 =head2 fields
206  
207 Retrieve all known key/value pairs.
208  
209 This is called by templer to retrieve all global settings, which
210 can then be merged with its defaults.
211  
212 =cut
213  
214 sub fields
215 {
216 my ($self) = (@_);
217  
218 %$self;
219 }
220  
221  
222 1;