blib/lib/Config/Extend/MySQL.pm

filter: Statementstmt 86.7% 72 / 83 Branchbran 70.0% 28 / 40 Conditioncond 68.7% 11 / 16 MC/DCmcdc 44.4% 4 / 9 Subroutinesub 76.4% 13 / 17 Podpod 100% 1 / 1 totaltotal 77.7% 129 / 166 CC 26 SCAR 21.8
CC
26
Cov
80%
__new_from
16.7
new
12.2
__read_config
10.7
CRAP 8.8
↑ Summary
1 package Config::Extend::MySQL;
2 3 ▶use 5.006; # read the CAVEATS if you really want a version that works on 5.5
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use File::Basename qw(dirname);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use File::Spec::Functions qw(catfile rel2abs);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use File::Read;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use UNIVERSAL::require;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11  
12 {
13 3 ▶ no strict "vars";
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 $VERSION = '0.05';
15 }
16  
17 3 ▶use constant USE_IO_STRING => $] <= 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 3 my %skip;
20  
21  
22 =head1 NAME
23  
24 Config::Extend::MySQL - Extend your favourite .INI parser module to read MySQL configuration file
25  
26 =head1 VERSION
27  
28 Version 0.05
29  
30 =head1 SYNOPSIS
31  
32 use Config::Extend::MySQL;
33  
34 # read MySQL config using Config::IniFiles
35 my $config = Config::Extend::MySQL->new({ from => $file, using => "Config::IniFiles" });
36  
37 # read MySQL config using Config::Tiny
38 my $config = Config::Extend::MySQL->new({ from => $file, using => "Config::Tiny" });
39  
40 # use the resulting object as you usually do
41 ...
42  
43  
44 =head1 DESCRIPTION
45  
46 This module extends other C<Config::> modules so they can read MySQL
47 configuration files. It works by slurping and preprocessing the files
48 before letting your favourite C<Config::> module parse the result.
49  
50 Currently supported modules are C<Config::IniFiles>, C<Config::INI::Reader>
51 and C<Config::Tiny>.
52  
53 =head2 Rationale
54  
55 This module was written out of a need of reading MySQL configuration
56 files from random machines. At first, the author thought they were just
57 classical C<.INI> files, but soon discovered that they include additional
58 features like C<!include> and C<!includedir>, and bare boolean options,
59 which without surprise make most common modules choke or die.
60  
61 Hence this module which simply slurps all the files, recursing though the
62 C<!include> and C<!includedir> directives, inlining their content in
63 memory, and transforms the bare boolean options into explicitly assigned
64 options.
65  
66 As to why this module extends other modules instead of being on its own,
67 it's because the author was too lazy to think of yet another API and
68 preferred to use the modules he already know. And given he use several
69 of them, depending on the context, it was just as easy to avoid being
70 too thighly coupled to a particular module.
71  
72  
73 =head1 METHODS
74  
75 =head2 new()
76  
77 Create and return an object
78  
79 B<Usage>
80  
81 my $config = Config::Extend::MySQL->new({ from => $file, using => $module });
82  
83 B<Options>
84  
85 =over
86  
87 =item *
88  
89 C<from> - the path to the main MySQL configuration file
90  
91 =item *
92  
93 C<using> - the module name to use as backend for parsing the configuration file
94  
95 =back
96  
97 B<Examples>
98  
99 # read MySQL config using Config::IniFiles
100 my $config = Config::Extend::MySQL->new({ from => $file, using => "Config::IniFiles" });
101 # $config ISA Config::Extend::MySQL, ISA Config::IniFiles
102  
103 # read MySQL config using Config::Tiny
104 my $config = Config::Extend::MySQL->new({ from => $file, using => "Config::Tiny" });
105 # $config ISA Config::Extend::MySQL, ISA Config::Tiny
106  
107 =cut
108  
109 16 ▶sub new {
Subroutine new: called CC=12 SCAR=25.0
Pod documented
110 16 my ($class, $args) = @_;
111  
112 16 ▶ croak "error: Arguments must be given as a hash reference"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $args eq "HASH" 1 15 16
113 unless ref $args eq "HASH";
114 croak "error: Missing required argument 'from'"
115 15 ▶ unless exists $args->{from};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args->{"from"} 1 14 15
116 croak "error: Empty argument 'from'"
117 14 ▶ unless defined $args->{from} and length $args->{from};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args->{"from"} and length $args->{"from"} 2 12 14
Condition100%3/3
defined $args->{"from"} and length $args->{"from"}
!ll&&!rl&&r
1112
MC/DC100%2/2
defined $args->{"from"} and length $args->{"from"}
defined $args->{"from"} length $args->{"from"}
Truth table100%3/3
A and B
A= defined $args->{"from"}B= length $args->{"from"}
ABresult
0X0
100
111
118  
119 # check that the file exists and contains something
120 12 my $file = $args->{from};
121 12 ▶ croak "fatal: No such file '$file'" unless -f $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $file 1 11 12
122 11 ▶ carp "warning: File '$file' is empty" and return if -s _ == 0;
Branch: 1 branch
BranchTrueFalseTotal
if -s _ == 0 1 10 11
Condition60%3/5
-s _ == 0 && (carp("warning: File '${file}' is empty") && (return))
!ll&&!rl&&r
1010
carp("warning: File '${file}' is empty") and return
!ll
01
MC/DC0%0/2
-s _ == 0 && (carp("warning: File '${file}' is empty") && (return))
-s _ == 0 carp("warning: File '${file}' is empty")
Truth table33%1/3
A && B
A= -s _ == 0B= carp("warning: File '${file}' is empty")
ABresult
0X0
100
111
123  
124 # read the file and resolve the MySQL-isms
125 10 my $content = __read_config(file => $file);
126  
127 10 my $fh = undef;
128 10 if (USE_IO_STRING) {
129 require IO::String;
130 $fh = IO::String->new(\$content);
131 }
132 else {
133 10 ▶ open($fh, "<", \$content)
Branch: 1 branch
BranchTrueFalseTotal
open $fh, "<", \$content or croak("fatal: Can't read in-memory buffer: $!") 10 0 10
134 or croak "fatal: Can't read in-memory buffer: $!";
135 }
136  
137 # create the object using the given Config:: module
138 10 ▶ my $backend = defined $args->{using} ? $args->{using} : "Config::Tiny";
Branch: 1 branch
BranchTrueFalseTotal
defined $args->{'using'} ? : 10 0 10
139 10 ▶ $backend->require or croak "fatal: Can't load module $args->{using}: $@";
Branch: 1 branch
BranchTrueFalseTotal
$backend->require or croak(qq[fatal: Can't load module $args->{"using"}: $@]) 9 1 10
140 9 @Config::Extend::MySQL::ISA = ($backend);
141 9 ▶ my $self = __new_from($backend, $fh, \$content)
Branch: 1 branch
BranchTrueFalseTotal
my $self = __new_from($backend, $fh, \$content) or croak("fatal: Backend module failed to parse '${file}'") 9 0 9
142 or croak "fatal: Backend module failed to parse '$file'";
143 9 bless $self, $class;
144  
145 # store the names to skip when reading directories
146 9 my @skip_names = qw(. .. CVS);
147 9 @skip{@skip_names} = (1) x @skip_names;
148  
149 9 return $self
150 }
151  
152  
153 9 ▶sub __new_from {
Subroutine __new_from: called CC=6 SCAR=28.1
154 9 my ($backend, $fh, $content_r) = @_;
155  
156 9 ▶ if ($backend eq "Config::IniFiles") {
Branch: 4 branches
BranchTrueFalseTotal
if ($backend eq 'Config::IniFiles') { } 0 9 9
elsif ($backend eq 'Config::Format::Ini') { } 0 9 9
elsif ($backend eq 'Config::Simple') { } 0 9 9
elsif ($backend eq 'Config::Tiny' or $backend eq 'Config::INI::Reader') { } 9 0 9
157 0 ▶ local $SIG{__WARN__} = sub {}; # avoid a warning from stat() on this $fh
Subroutine __ANON__: not called CC=1 SCAR=6.9
158 0 ▶ local *IO::String::FILENO = sub { -1 };
Subroutine __ANON__: not called CC=1 SCAR=6.9
159 0 return Config::IniFiles->new(-file => $fh)
160 }
161 elsif ($backend eq "Config::Format::Ini") {
162 0 ▶ local $SIG{__WARN__} = sub {}; # avoid "slurp redefined" warning
Subroutine __ANON__: not called CC=1 SCAR=6.9
163 0 ▶ local *Config::Format::Ini::slurp = sub { return ${$_[0]} };
Subroutine __ANON__: not called CC=1 SCAR=6.9
164 0 return Config::Format::Ini::read_ini($content_r)
165 }
166 elsif ($backend eq "Config::Simple") {
167 # can't get Config::Simple to play nicely because it want to
168 # seek() and flock() the filehandle. seek() works on in-memory
169 # filehandles, but flock() doesn't, and can't be faked/replaced
170  
171 #my $obj = Config::Simple->new(syntax => "ini");
172 #$obj->{_DATA} = $obj->parse_ini_file($fh);
173 #return $obj
174  
175 0 return Config::Simple->new($fh)
176 }
177 ▶ elsif ($backend eq "Config::Tiny" or $backend eq "Config::INI::Reader") {
Condition33%1/3
$backend eq "Config::Tiny" or $backend eq "Config::INI::Reader"
l!l&&r!l&&!r
900
MC/DC0%0/2
$backend eq "Config::Tiny" or $backend eq "Config::INI::Reader"
$backend eq "Config::Tiny" $backend eq "Config::INI::Reader"
Truth table33%1/3
A or B
A= $backend eq "Config::Tiny"B= $backend eq "Config::INI::Reader"
ABresult
1X1
011
000
178 9 return $backend->read_string($$content_r)
179 }
180 }
181  
182  
183 21 ▶sub __read_config {
Subroutine __read_config: called CC=10 SCAR=23.7
184 21 my ($what, $path) = @_;
185 21 my $content = "";
186 21 my $opts = {}; #{ err_mode => "quiet" };
187  
188 21 ▶ if ($what eq "file") {
Branch: 2 branches
BranchTrueFalseTotal
if ($what eq 'file') { } 19 2 21
elsif ($what eq 'dir') { } 2 0 2
189 19 my $base_dir = dirname($path);
190 19 $content = read_file($opts, $path);
191  
192 # handle single param (without value)
193 19 $content =~ s{^ \s* (\w+ (?:-\w+)* ) \s* $}{$1 = yes}xgm;
194  
195 # handle includes
196 19 $content =~ s{^ \s* !include(dir)? \s+ (.+) \s* $}
197 5 ▶ { __read_config($1 || "file", rel2abs($2, $base_dir)) }xgme;
Condition100%2/2
$1 || 'file'
l!l
23
MC/DC100%1/1
$1 || 'file'
$1
Truth table100%2/2
A
A= $1
Aresult
11
00
198 }
199 elsif ($what eq "dir") {
200 2 ▶ opendir(my $dirh, $path) or return "";
Branch: 1 branch
BranchTrueFalseTotal
opendir my $dirh, $path or return "" 2 0 2
201  
202 2 ▶ while (my $file = readdir($dirh)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $file = readdir $dirh)) 10 2 12
203 # skip invisible files and directories we shouldn't
204 # recurse into, like ../ or CVS/
205 10 ▶ next if $skip{$file} or index($file, ".") == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $skip{$file} or index($file, ".") == 0 4 6 10
Condition66%2/3
$skip{$file} or index($file, ".") == 0
l!l&&r!l&&!r
406
MC/DC50%1/2
$skip{$file} or index($file, ".") == 0
$skip{$file} index($file, ".") == 0
Truth table66%2/3
A or B
A= $skip{$file}B= index($file, ".") == 0
ABresult
1X1
011
000
206  
207 6 my $filepath = catfile($path, $file);
208  
209 6 ▶ if (-f $filepath) {
Branch: 2 branches
BranchTrueFalseTotal
if (-f $filepath) { } 6 0 6
elsif (-d _) { } 0 0 0
210 6 $content .= __read_config(file => $filepath)
211 }
212 elsif (-d _) {
213 0 $content .= __read_config(dir => $filepath)
214 }
215 }
216  
217 2 closedir($dirh);
218 }
219  
220 21 return $content
221 }
222  
223  
224 =head1 DIAGNOSTICS
225  
226 =over
227  
228 =item C<Arguments must be given as a hash reference>
229  
230 B<(E)> As the message says, the arguments must be given to the
231 function or method as a hash reference.
232  
233 =item C<Backend module failed to parse '%s'>
234  
235 B<(F)> The backend module was unable to parse the given file.
236 See L<"CAVEATS"> for some hints.
237  
238 =item C<Can't load module %s: %s>
239  
240 B<(F)> The backend module could not be loaded.
241  
242 =item C<Can't read in-memory buffer: %s>
243  
244 B<(F)> This should not happen.
245  
246 =item C<Empty argument '%s'>
247  
248 B<(E)> The given argument was empty, but a value is required.
249  
250 =item C<File '%s' is empty>
251  
252 B<(W)> The file is empty.
253  
254 =item C<Missing required argument '%s'>
255  
256 B<(E)> You forgot to supply a mandatory argument.
257  
258 =item C<No such file '%s'">
259  
260 B<(F)> The given path does not point to an existing file.
261  
262 =back
263  
264  
265 =head1 CAVEATS
266  
267 The different supported modules don't parse C<.INI> files exactly the
268 same ways, and have different behaviours:
269  
270 =over
271  
272 =item *
273  
274 C<Config::IniFiles> doesn't want to create an object from an empty file.
275  
276 =item *
277  
278 C<Config::INI::Reader> by default doesn't allow the pound sign (C<#>)
279 for beginning comments.
280  
281 =item *
282  
283 when assigning the same option twice, C<Config::Tiny> replaces the old
284 value with the new one, C<Config::IniFiles> appends it with a newline.
285  
286 =back
287  
288 And probably many more.
289  
290 Also note that in order to keep the code simple, this module wants
291 Perl 5.6 or newer. However, a patch to make it work on Perl 5.5.3 is
292 included in the distribution (F<patches/patch-for-perl5.5.diff>).
293  
294  
295 =head1 SEE ALSO
296  
297 L<Config::IniFiles>
298  
299 L<Config::INI::Reader>
300  
301 L<Config::Tiny>
302  
303  
304 =head1 AUTHOR
305  
306 SE<eacute>bastien Aperghis-Tramoni, C<< <sebastien at aperghis.net> >>
307  
308 =head1 BUGS
309  
310 Please report any bugs or feature requests
311 to C<bug-config-extend-mysql at rt.cpan.org>, or through the web interface
312 at L<http://rt.cpan.org/Dist/Display.html?Queue=Config-Extend-MySQL>.
313 I will be notified, and then you'll automatically be notified of
314 progress on your bug as I make changes.
315  
316  
317 =head1 SUPPORT
318  
319 You can find documentation for this module with the perldoc command.
320  
321 perldoc Config::Extend::MySQL
322  
323  
324 You can also look for information at:
325  
326 =over 4
327  
328 =item * RT: CPAN's request tracker
329  
330 L<http://rt.cpan.org/Dist/Display.html?Queue=Config-Extend-MySQL>
331  
332 =item * AnnoCPAN: Annotated CPAN documentation
333  
334 L<http://annocpan.org/dist/Config-Extend-MySQL>
335  
336 =item * CPAN Ratings
337  
338 L<http://cpanratings.perl.org/d/Config-Extend-MySQL>
339  
340 =item * Search CPAN
341  
342 L<http://search.cpan.org/dist/Config-Extend-MySQL>
343  
344 =back
345  
346  
347 =head1 COPYRIGHT & LICENSE
348  
349 Copyright 2008 SE<eacute>bastien Aperghis-Tramoni, all rights reserved.
350  
351 This program is free software; you can redistribute it and/or modify it
352 under the same terms as Perl itself.
353  
354  
355 =cut
356  
357 3 1; # End of Config::Extend::MySQL
↑ Summary