File Coverage

blib/lib/LocalConf/Parser.pm
Criterion Covered Total %
statement 11 33 33.3
branch 0 16 0.0
condition 0 6 0.0
subroutine 4 5 80.0
pod 1 1 100.0
total 16 61 26.2


line stmt bran cond sub pod time code
1             package LocalConf::Parser;
2              
3 1     1   68474 use 5.006;
  1         4  
4 1     1   6 use strict;
  1         2  
  1         20  
5 1     1   6 use warnings;
  1         3  
  1         53  
6              
7 1     1   6 use Exporter qw(import);
  1         2  
  1         749  
8             our @EXPORT = qw(conf_parser);
9              
10             =head1 NAME
11              
12             LocalConf::Parser - read config to an hashref from local conf files.
13              
14             =head1 VERSION
15              
16             Version 0.01
17              
18             =cut
19              
20             our $VERSION = '0.01';
21              
22              
23             =head1 SYNOPSIS
24              
25             Quick summary of what the module does.
26              
27             use config::parser;
28              
29             my $confref = conf_parser($config_file_loc);
30             ...
31              
32             =head1 EXPORT
33              
34             conf_parser
35              
36             =head1 METHOD
37              
38             =head2 conf_parser
39              
40             =cut
41              
42             sub conf_parser {
43 0     0 1   (my $config ) = @_;
44 0           my %config_env;
45 0 0         open CONFIG, "<", $config
46             or die("Cannot read config file " . $config);
47 0           while () {
48             # skip blank lines
49 0 0         next if /^\s*$/;
50             # skip comments
51 0 0         next if /^\s*#/;
52             # parse line like 'key = "value"'
53 0           /^\s*([\w_.-]+)\s*=\s*"([~\w,:\/;._\s\d!=-]*)"/;
54 0           my ($key, $value) = ($1, $2);
55 0 0 0       if (!$key && !$value || !defined($value)) {
      0        
56 0           die "Can't parse config file " . $config . " line ${.}. Ignoring.";
57 0           next;
58             }
59             # values with commas will be split and each item parsed
60 0 0         if ($value =~ /,|=/) {
61 0           for my $item (split /,/, $value) {
62             # skip empty values
63 0 0         next unless $item;
64 0 0         if ( $item =~ /([\w_.\d\/:-]+)\s*=\s*([\w_.\d\/:-]*)/ ) {
65             # turn 'foo = "bar=jazz,blah=grub"' into
66             # $config_env{foo} = { "bar" => "jazz", "blah" => "grub" }
67 0           $config_env{$key}{$1} = $2;
68             } else {
69             # turn 'foo = "bar,blah,grub" into
70             # $config_env{foo} = ( "bar", "blah", "grub" );
71 0           push @{$config_env{$key}}, $item;
  0            
72             }
73             }
74             } else {
75             # regular 'key = "value"' line
76 0           $config_env{$key} = $value;
77             }
78             }
79             # mark config file as read
80 0           my $config_read = 1;
81 0 0         close CONFIG or trap("Cannot close config file " . $config);
82             # return reference to config data
83 0           return \%config_env;
84             }
85              
86             =head1 AUTHOR
87              
88             nickniu, C<< >>
89              
90             =head1 BUGS
91              
92             Please report any bugs or feature requests to C, or through
93             the web interface at L. I will be notified, and then you'll
94             automatically be notified of progress on your bug as I make changes.
95              
96              
97              
98              
99             =head1 SUPPORT
100              
101             You can find documentation for this module with the perldoc command.
102              
103             perldoc config::parser
104              
105              
106             You can also look for information at:
107              
108             =over 4
109              
110             =item * RT: CPAN's request tracker (report bugs here)
111              
112             L
113              
114             =item * AnnoCPAN: Annotated CPAN documentation
115              
116             L
117              
118             =item * CPAN Ratings
119              
120             L
121              
122             =item * Search CPAN
123              
124             L
125              
126             =back
127              
128              
129             =head1 ACKNOWLEDGEMENTS
130              
131              
132             =head1 LICENSE AND COPYRIGHT
133              
134             Copyright 2020 nickniu.
135              
136             This program is free software; you can redistribute it and/or modify it
137             under the terms of the the Artistic License (2.0). You may obtain a
138             copy of the full license at:
139              
140             L
141              
142             Any use, modification, and distribution of the Standard or Modified
143             Versions is governed by this Artistic License. By using, modifying or
144             distributing the Package, you accept this license. Do not use, modify,
145             or distribute the Package, if you do not accept this license.
146              
147             If your Modified Version has been derived from a Modified Version made
148             by someone other than you, you are nevertheless required to ensure that
149             your Modified Version complies with the requirements of this license.
150              
151             This license does not grant you the right to use any trademark, service
152             mark, tradename, or logo of the Copyright Holder.
153              
154             This license includes the non-exclusive, worldwide, free-of-charge
155             patent license to make, have made, use, offer to sell, sell, import and
156             otherwise transfer the Package with respect to any patent claims
157             licensable by the Copyright Holder that are necessarily infringed by the
158             Package. If you institute patent litigation (including a cross-claim or
159             counterclaim) against any party alleging that the Package constitutes
160             direct or contributory patent infringement, then this Artistic License
161             to you shall terminate on the date that such litigation is filed.
162              
163             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
164             AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
165             THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
166             PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
167             YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
168             CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
169             CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
170             EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
171              
172              
173             =cut
174              
175             1; # End of config::parser