File Coverage

blib/lib/Config/IniRegEx.pm
Criterion Covered Total %
statement 22 75 29.3
branch 3 22 13.6
condition n/a
subroutine 5 11 45.4
pod 5 6 83.3
total 35 114 30.7


line stmt bran cond sub pod time code
1             package Config::IniRegEx;
2              
3 2     2   90229 use warnings;
  2         5  
  2         69  
4 2     2   11 use strict;
  2         3  
  2         67  
5              
6 2     2   2599 use Config::IniFiles;
  2         121415  
  2         1644  
7             our $VERSION = '0.01';
8              
9             =head1 NAME
10              
11             Config::IniRegEx - Ini workaround, regex search for parameters and sections.
12              
13             =head1 SYNOPSIS
14              
15             use Config::IniRegEx;
16              
17             my $foo = Config::IniRegEx->New();
18              
19             $foo->ValRegex('section_name', 'parameter_regex');
20              
21             $foo->SectionExistsRegex('section_regex');
22              
23             $foo->SectionRegex('section_regex');
24              
25             $foo->ParameterRegex('section_regex');
26              
27              
28             =cut
29              
30             ########## functions intended for internal use should start with _ .
31              
32              
33             sub New {
34 1     1 1 13 my ( $class ) = shift;
35              
36             # returns undef. When
37             # 1. Ini config does not exists
38             # 2. Ini config is not in form to process such as Wrong Format. ( depends on Config::IniFiles )
39 1         2 my $nocase = $_[1];
40 1 50       4 $nocase = 0 if ( not defined $_[1] );
41 1         4 my $DataRef = _ParseAndPopulate($_[0],$nocase);
42 1 50       6 return undef if ( not defined $DataRef );
43              
44             # Have the filename, options and data tied with the reference, so that it can be
45             # blessed for this class, and these can be accessed from the object of this class.
46 1         6 my $objref = {
47             'filename' => $_[0],
48             'nocase' => $nocase,
49             'data' => $DataRef,
50             };
51 1         3 bless $objref,$class;
52 1         3 return $objref;
53             } # ---------- end of subroutine New ----------
54              
55              
56             sub _ParseAndPopulate
57             {
58             # Get filename from argument of this functions
59 1     1   2 my ($filename, $icase) = @_;
60             # To store the data available in a ini file.
61 1         2 my %ini;
62              
63             # Tie that filename into ini hash, it will store in a hash of hash form.
64 1         15 tie %ini, 'Config::IniFiles', ( -file => "$filename", -nocase => $icase );
65              
66             # If the filename given is not available
67 1 50       4325 if ( ! %ini ) {
68 0         0 return undef;
69             }
70             # Returning the reference of a hash.
71 1         18 return \%ini;
72             }
73              
74             # Return undef, if there is no section with the given name.
75             # expected: Section should be text and parameter could be regex.
76             sub ValRegex
77             {
78 0     0 1   my ( $Pack, $Section, $Parameter ) = @_;
79 0           my $countofmatch = 0;
80 0           my %outhash ;
81 0           undef %outhash;
82              
83             # Check whether the section exist first.
84 0 0         if ( tied( %{$Pack->{'data'}})->SectionExists($Section) ) {
  0            
85             # take the parameters available in that section.
86 0           foreach ( keys %{$Pack->{'data'}->{$Section}} ) {
  0            
87             # Check if the parameters are matching.
88 0 0         if ( $_ =~ /^$Parameter$/ ) {
89             # If it matches, Store this into the output hash.
90 0           $outhash{$_} = $Pack->{'data'}->{$Section}->{$_};
91             # Have a matched count
92 0           $countofmatch++;
93             }
94             }
95             }
96              
97             # return's undef when
98             # 1. Section does not exist
99             # 2. Section exists, but no match for parameter.
100             # else return's the matched set of parameter and its value as a hash.
101 0           return %outhash;
102             }
103              
104              
105             #4. SectionRegex()
106             #
107             #4.1 returns the section names which matches to the supplied regex.
108             #4.2 If there is no argument, then call the default Sections function, and do the default action.
109             #4.3 nocase should be considered.
110              
111             sub SectionRegex
112             {
113 0     0 1   my ( $Pack, $Section ) = @_;
114 0           my $countofmatch = 0;
115 0           my @opsec;
116 0           undef @opsec;
117              
118 0 0         if ( !$Section ) {
119             #Section argument is not available
120 0           return @opsec;
121             }
122              
123 0           foreach ( tied( %{$Pack->{'data'}})->Sections() ) {
  0            
124 0 0         if ( $_ =~ /^$Section$/ ) {
125 0           push @opsec, $_;
126 0           $countofmatch++;
127             }
128             }
129              
130             # return's undef when
131             # 1. section argument is not given
132             # 2. section does not matches.
133             # else return's an array of matched sections
134 0           return @opsec;
135             }
136              
137              
138              
139             #5. SectionExists ( regex )
140             #
141             #5.1 returns number of matches.
142             # ZERO for no match.
143             sub SectionExistsRegex
144             {
145 0     0 1   my ( $Pack, $Section ) = @_;
146 0           my ( $countofmatch ) = ( 0 );
147              
148 0 0         if ( !$Section ) {
149             #Section arguement is not available
150 0           return 0;
151             }
152              
153 0           foreach ( tied( %{$Pack->{'data'}})->Sections() ) {
  0            
154 0 0         if ( $_ =~ /^$Section$/ ) {
155 0           $countofmatch++;
156             }
157             }
158              
159             # return's 0 when
160             # 1. When the section argument is not passed
161             # 2. when the section does not match
162             # else return's number of match
163 0           return $countofmatch;
164             }
165              
166              
167             #6. Parameters ( regex )
168             #
169             #6.1 argument is section regex,
170             #6.2 returns hash of hash, where the hash key is section name, and the nested hash key is parameter name.
171              
172             sub ParameterRegex
173             {
174 0     0 1   my ( $Pack , $Section) = @_;
175 0           my %ophash;
176 0           undef %ophash;
177              
178 0 0         if ( !$Section ) {
179             #Section arguement is not available
180 0           return %ophash;
181             }
182              
183             # Get all the sections
184 0           foreach ( tied( %{$Pack->{'data'}})->Sections() ) {
  0            
185             # Check for a match
186 0 0         if ( $_ =~ /^$Section$/ ) {
187 0           $ophash{$_} = $Pack->{'data'}->{$_};
188             }
189             }
190              
191             # returns undef. When
192             # 1. section argument is not given
193             # 2. when the section does not match
194             # else returns hash of hash, where the hash key is section name, and the nested hash key is parameter name.
195 0           return %ophash;
196             }
197              
198             sub post
199             {
200 0     0 0   my ($pack) = shift;
201 0           my $ref = {'a' => 'b'};
202 0           $pack->{'hash'} = $ref;
203              
204 0           return;
205             }
206              
207             sub AUTOLOAD
208             {
209              
210 0     0     my ( $Pack ) = shift;
211 0           my $program = our $AUTOLOAD ;
212 0           $program =~ s/.*:://;
213             ######## what to do ????????????
214 0           print tied( %{$Pack->{'data'}})->$program;
  0            
215             }
216              
217              
218             =cut
219              
220             =head1 DESCRIPTION
221              
222             Config::IniRegEx is dependent on Config::IniFiles. Using that module it does the ini configuration
223             file parsing, with an addon facility of regex kind of search.
224              
225             Each function explained below, searches for a different thing based up on the given regular expression.
226             And it is a exact regex match.
227              
228             When a function of a Config::IniFiles is called then it will be called using the autoload functionality,
229             i.e all the functions of the Config::IniFiles can be called with this object itself. ( creating an
230             object for Config::IniRegEx is enough, can access all the functions available at Config::IniFiles also. ).
231              
232             This module aims out doing a regex search for Sections, and Parameters of the Ini configuration file.
233             It does the Perl regex matching, nothing external. So whoever knows the Perl basic regex can use this
234             feature.
235              
236             =head1 FUNCTIONS
237              
238             The following functions are available.
239              
240             =head2 New
241            
242             New (filename,nocase)
243            
244             Returns a new configuration object (or "undef" if the configuration file has an error)
245             my $object = Config::IniRegEx->New('config_filename', [nocase] );
246              
247             Arguments
248             First argument is absolute path of the ini configuration file which you want
249             to manipulate.
250             Second argument could be 0 or 1.
251             0 to handle the config file in a case-sensitive manner
252             1 to handle the config file in a case-insensitive manner
253             By default, config files are case-sensitive.
254              
255             For Example
256            
257             my $object = Config::IniRegEx->New("sample.ini");
258              
259             =head2 ValRegex
260            
261             %val_hash = $foo->ValRegex('section_name','parameter_regex');
262              
263             section_name - should be text, ( not a regular expression )
264             parameter_regex - can be Perl regex
265              
266             return's undef when
267             1. Section does not exist
268             2. Section exists, but no match for parameter.
269             else return's the matched set of parameter and its value as a hash.
270              
271             For Example
272              
273             %val_hash = $foo->ValRegex('sizes','size_._side');
274              
275             %val_hash will contain
276             size_a_side => 100
277             size_b_side => 55
278              
279             =head2 SectionRegex
280              
281             @box_sections = $foo->SectionRegex('section_name_regex');
282              
283              
284             section_name_regex - regex to match section name
285              
286             return's undef when
287             1. section argument is not given
288             2. section does not matches.
289             else return's an array of matched section names.
290              
291             For Example
292              
293             @box_sections = $foo->SectionRegex("box_.*");
294              
295             @size_of_all_sides will contain
296             ( 'box_day_first', 'box_day_second','box_day_third' )
297              
298              
299             =head2 SectionExistsRegex
300              
301              
302             $section_available = $foo->SectionExistsRegex('section_name_regex');
303              
304             section_name_regex - regex to match section name
305              
306             return's 0 when
307             1. When the section argument is not passed
308             2. when the section does not match
309             else return's number of match
310              
311             For Example
312              
313             $section_available = $foo->SectionExistsRegex('box_.*');
314              
315             $section_available will contain
316             3
317              
318              
319             =head2 ParameterRegex
320              
321             %matched_hash = $foo->ParameterRegex('section_name_regex');
322              
323             section_name_regex - regex to match section name
324              
325             returns undef. When
326             1. section argument is not given
327             2. when the section does not match
328             else returns hash of hash, where the hash key is section name, and the nested hash key is parameter name.
329              
330             For Example
331              
332             %matched_hash = $foo->ParameterRegex("box_day_.*");
333              
334             %matched_hash will contain
335             ( 'box_day_first' => {
336             'expected_from' => Tue,
337             'expected_to' => Thu
338             },
339              
340             'box_day_second' => {
341             'expected_from' => Mon,
342             'expected_to' => Fri
343             },
344              
345             'box_day_third' => {
346             'expected_from' => Mon,
347             'expected_to' => Sat
348             },
349             )
350              
351              
352              
353             =head1 AUTHOR
354              
355             Sasi, C<< >>
356              
357             =head1 COPYRIGHT & LICENSE
358              
359             Copyright 2009 Sasi, all rights reserved.
360              
361             This program is free software; you can redistribute it and/or modify it
362             under the same terms as Perl itself.
363              
364              
365             =cut
366              
367             1; # End of Config::IniRegEx