blib/lib/Config/Terse.pm untested

filter: Statementstmt 0.0% 0 / 75 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 121 CC 18 SCAR 55.9
CC
18
Cov
0%
terse_config_load
342.0
BEGIN
2.0
BEGIN
2.0
CRAP 268.1
1 ##############################################################################
2 #
3 # Config::Terse is laconic configuration files parser.
4 # Vladi Belperchinov-Shabanski "Cade" <cade@biscom.net> <cade@datamax.bg>
5 #
6 # DISTRIBUTED UNDER GPLv2
7 #
8 ##############################################################################
9 package Config::Terse;
10 use Exporter;
11 use Carp qw( croak );
12 use Data::Dumper;
13 use strict;
14  
15 our @ISA = qw( Exporter );
16  
17 our @EXPORT = qw(
18 terse_config_load
19 );
20  
21 our $VERSION = '0.01';
22  
23 ##############################################################################
24  
25 sub terse_config_load
26 {
27 my $fn = shift;
28 my %opt = @_;
29  
30 $opt{ uc $_ } ||= $opt{ lc $_ } for qw( CASE ORDERED MAIN );
31  
32 my $opt_case;
33 $opt_case = 'UC';
34 $opt_case = 'LC' if $opt{ 'CASE' } =~ /L|LC|LO|LOW|LOWER/i;
35 $opt_case = 'NC' if $opt{ 'CASE' } =~ /N|NC|ASIS/i;
36  
37 my $opt_ordered = $opt{ 'ORDERED' };
38 my $opt_main = $opt{ 'MAIN' };
39  
40 my $kc = sub
41 {
42 my $s = shift;
43 return uc $s if $opt_case eq 'UC';
44 return lc $s if $opt_case eq 'LC';
45 return $s if $opt_case eq 'NC';
46 };
47  
48 my %h;
49  
50 if( $opt_ordered )
51 {
52 require Tie::IxHash;
53 tie %h, 'Tie::IxHash';
54 }
55  
56 my $section = $kc->( $opt_main );
57  
58 my $lh = $h{ $section } = {}; # last section hash
59
60 open( my $if, $fn );
61 while( my $line = <$if> )
62 {
63 next if $line =~ /^\s*[#;]/; # skip comments
64 $line =~ s/[\r\n]+$//; # trip trailing cr/lf
65 next unless $line =~ /\S/; # skip empty lines
66
67 if( $line =~ /^\s*=(\S+)(.*)/ )
68 {
69 my $section = $kc->( $1 );
70 my %sh; # section hash
71 tie %sh, 'Tie::IxHash' if $opt_ordered;
72 $lh = \%sh;
73  
74 my $args = $2;
75 my @args = split /\s+/, $args;
76  
77 my $lg; # last group
78 for my $arg ( @args )
79 {
80 if( $arg =~ /^\+(\S+)/ )
81 {
82 my $is = $kc->( $1 ); # inherit section
83 my $ih;
84 if( $lg )
85 {
86 croak "section [$lg:$section] cannot inherit from [$lg:$is] does not exist" unless exists $h{ $lg }{ $is };
87 $ih = $h{ $lg }{ $is };
88 }
89 else
90 {
91 croak "section [$section] cannot inherit from [$is] does not exist" unless exists $h{ $is };
92 $ih = $h{ $is };
93 }
94 %$lh = ( %$lh, %$ih );
95 next;
96 }
97 if( $arg =~ /^\@(\S+)/ )
98 {
99 $lg = $kc->( $1 );
100 $h{ $lg }{ $section } = \%sh;
101 }
102 }
103 $h{ $section } = \%sh unless $lg;
104  
105 next;
106 # end section code
107 }
108 if( $line =~ /^\s*(\S+)\s*(.*)/ )
109 {
110 my $k = $kc->( $1 ); # key
111 my $v = $2; # value
112 $lh->{ $k } = $v;
113 next;
114 # end key=value code
115 }
116 }
117 close( $if );
118  
119 return \%h;
120 }
121  
122 ##############################################################################
123  
124 =pod
125  
126 =head1 NAME
127  
128 Config::Terse is laconic configuration files parser.
129  
130 =head1 SYNOPSIS
131  
132 #!/usr/bin/perl
133 use strict;
134 use Config::Terse;
135  
136 my $cfg = terse_config_load( 'try.cfg' );
137  
138 use Data::Dumper;
139 print Dumper( $cfg );
140  
141 =head1 DESCRIPTION
142  
143 Config::Terse parses configuration files with very compact syntax, which
144 may seem rude or unfriendly. It provides sections with keyword/value pairs,
145 sections inheritance and named groups of sections.
146  
147 Each line in the config file contains whitespace-delimited key and value:
148  
149 key value
150 anotherkey other value
151 koe ne se chete
152  
153 Sections begin with equal sign on new line, followed by the section name:
154  
155 =newsection
156
157 sectionkey1 value
158 newkey value
159  
160 Sections may inherit other sections. Inherited sections are specified with
161 plus sign and name after the section name:
162  
163 =newsection +othersection1 +othersection2 ...
164  
165 Sections may be grouped in groupss. Group names are specified with "at" sign (@)
166 followed by the group name, after the section name:
167  
168 =apple @fruits
169
170 Inheritance and groups can be combined but order is important! All inherited
171 sections specified before group is taken from the main (root) sections.
172 Inherited sections after group name is taken from the same group (if such
173 exists.
174  
175 Sections can be added to multiple groups. They will be linked together and
176 changing one section key will be visible in the other groups.
177  
178 Here is an example:
179  
180 =green
181 color green
182
183 =tree @fruits
184 isatree yes
185
186 =apple +green @fruits +tree
187 name this is a green apple tree
188
189 The "apple" section will inherit "green" section, then will be put in the
190 "fruits" group and finally will inherit the "tree" section from "fruits".
191  
192 All section and key names are converted to upper case by default.
193  
194 The result perl hash structure for all the examples combined will be:
195  
196 $VAR1 = {
197 'GREEN' => {
198 'COLOR' => 'green'
199 },
200 'MAIN' => {
201 'ANOTHERKEY' => 'other value',
202 'KEY' => 'value',
203 'KOE' => 'ne se chete'
204 },
205 'FRUITS' => {
206 'TREE' => {
207 'ISATREE' => 'yes'
208 },
209 'APPLE' => {
210 'COLOR' => 'green',
211 'NAME' => 'this is a green apple tree',
212 'ISATREE' => 'yes'
213 }
214 },
215 'NEWSECTION' => {
216 'SECTIONKEY1' => 'value',
217 'NEWKEY' => 'value'
218 }
219 };
220  
221 Default section name is 'MAIN'. It is used for keys in files without any
222 sections or for keys in the leading part of a file where no section has been
223 defined yet. Default section name can be changed with 'MAIN' option and will
224 be modified by the 'CASE' option. See 'OPTIONS' section below.
225  
226 =head1 OPTIONS
227  
228 Few options can be added when loading new config file:
229  
230 # make all sections and keys names upper case (default)
231 my $cfg = terse_config_load( 'try.cfg', CASE => 'UC' );
232
233 # make all sections and keys names lower case
234 my $cfg = terse_config_load( 'try.cfg', CASE => 'LC' );
235  
236 # leave all sections and keys names asis, no case conversion
237 my $cfg = terse_config_load( 'try.cfg', CASE => 'NC' );
238  
239 # keep sections and keys in the order they were seen
240 my $cfg = terse_config_load( 'try.cfg', ORDERED => 1 );
241  
242 # set MAIN section name
243 my $cfg = terse_config_load( 'try.cfg', MAIN => '*' );
244  
245 # combined options
246 my $cfg = terse_config_load( 'try.cfg', CASE => 'LC',
247 MAIN => '*',
248 ORDERED => 1 );
249  
250 =head1 GITHUB REPOSITORY
251  
252 https://github.com/cade-vs/perl-config-terse
253
254 git clone git://github.com/cade-vs/perl-config-terse.git
255  
256 =head1 AUTHOR
257  
258 Vladi Belperchinov-Shabanski "Cade"
259  
260 <cade@biscom.net> <cade@datamax.bg> <cade@cpan.org>
261  
262 http://cade.datamax.bg
263  
264 =cut
265  
266 1;