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