| 1 | package Config::IniRegEx; | ||||||||||
| 2 | |||||||||||
| 3 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 2 | ▶ | use Config::IniFiles; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 2 | 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 | 1 | ▶ | sub New { | ||||||||
|
Subroutine
New: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||
| 34 | 1 | 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 | my $nocase = $_[1]; | |||||||||
| 40 | 1 | ▶ | $nocase = 0 if ( not defined $_[1] ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 41 | 1 | my $DataRef = _ParseAndPopulate($_[0],$nocase); | |||||||||
| 42 | 1 | ▶ | return undef if ( not defined $DataRef ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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 | my $objref = { | |||||||||
| 47 | 'filename' => $_[0], | ||||||||||
| 48 | 'nocase' => $nocase, | ||||||||||
| 49 | 'data' => $DataRef, | ||||||||||
| 50 | }; | ||||||||||
| 51 | 1 | bless $objref,$class; | |||||||||
| 52 | 1 | return $objref; | |||||||||
| 53 | } # ---------- end of subroutine New ---------- | ||||||||||
| 54 | |||||||||||
| 55 | |||||||||||
| 56 | sub _ParseAndPopulate | ||||||||||
| 57 | 1 | ▶ | { | ||||||||
|
Subroutine
_ParseAndPopulate: called CC=2 SCAR=7.2
| |||||||||||
| 58 | # Get filename from argument of this functions | ||||||||||
| 59 | 1 | my ($filename, $icase) = @_; | |||||||||
| 60 | # To store the data available in a ini file. | ||||||||||
| 61 | 1 | my %ini; | |||||||||
| 62 | |||||||||||
| 63 | # Tie that filename into ini hash, it will store in a hash of hash form. | ||||||||||
| 64 | 1 | tie %ini, 'Config::IniFiles', ( -file => "$filename", -nocase => $icase ); | |||||||||
| 65 | |||||||||||
| 66 | # If the filename given is not available | ||||||||||
| 67 | 1 | ▶ | if ( ! %ini ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 68 | 0 | return undef; | |||||||||
| 69 | } | ||||||||||
| 70 | # Returning the reference of a hash. | ||||||||||
| 71 | 1 | 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 | 0 | ▶ | { | ||||||||
|
Subroutine
ValRegex: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||
| 78 | 0 | 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 | ▶ | if ( tied( %{$Pack->{'data'}})->SectionExists($Section) ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 85 | # take the parameters available in that section. | ||||||||||
| 86 | 0 | foreach ( keys %{$Pack->{'data'}->{$Section}} ) { | |||||||||
| 87 | # Check if the parameters are matching. | ||||||||||
| 88 | 0 | ▶ | if ( $_ =~ /^$Parameter$/ ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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 | 0 | ▶ | { | ||||||||
|
Subroutine
SectionRegex: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||
| 113 | 0 | my ( $Pack, $Section ) = @_; | |||||||||
| 114 | 0 | my $countofmatch = 0; | |||||||||
| 115 | 0 | my @opsec; | |||||||||
| 116 | 0 | undef @opsec; | |||||||||
| 117 | |||||||||||
| 118 | 0 | ▶ | if ( !$Section ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 119 | #Section argument is not available | ||||||||||
| 120 | 0 | return @opsec; | |||||||||
| 121 | } | ||||||||||
| 122 | |||||||||||
| 123 | 0 | foreach ( tied( %{$Pack->{'data'}})->Sections() ) { | |||||||||
| 124 | 0 | ▶ | if ( $_ =~ /^$Section$/ ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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 | 0 | ▶ | { | ||||||||
|
Subroutine
SectionExistsRegex: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||
| 145 | 0 | my ( $Pack, $Section ) = @_; | |||||||||
| 146 | 0 | my ( $countofmatch ) = ( 0 ); | |||||||||
| 147 | |||||||||||
| 148 | 0 | ▶ | if ( !$Section ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 149 | #Section arguement is not available | ||||||||||
| 150 | 0 | return 0; | |||||||||
| 151 | } | ||||||||||
| 152 | |||||||||||
| 153 | 0 | foreach ( tied( %{$Pack->{'data'}})->Sections() ) { | |||||||||
| 154 | 0 | ▶ | if ( $_ =~ /^$Section$/ ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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 | 0 | ▶ | { | ||||||||
|
Subroutine
ParameterRegex: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||
| 174 | 0 | my ( $Pack , $Section) = @_; | |||||||||
| 175 | 0 | my %ophash; | |||||||||
| 176 | 0 | undef %ophash; | |||||||||
| 177 | |||||||||||
| 178 | 0 | ▶ | if ( !$Section ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 179 | #Section arguement is not available | ||||||||||
| 180 | 0 | return %ophash; | |||||||||
| 181 | } | ||||||||||
| 182 | |||||||||||
| 183 | # Get all the sections | ||||||||||
| 184 | 0 | foreach ( tied( %{$Pack->{'data'}})->Sections() ) { | |||||||||
| 185 | # Check for a match | ||||||||||
| 186 | 0 | ▶ | if ( $_ =~ /^$Section$/ ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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 | 0 | ▶ | { | ||||||||
|
Subroutine
post: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 200 | 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 | 1 | ▶ | { | ||||||||
|
Subroutine
AUTOLOAD: called CC=1 SCAR=0.0
| |||||||||||
| 209 | |||||||||||
| 210 | 1 | my ( $Pack ) = shift; | |||||||||
| 211 | 1 | my $program = our $AUTOLOAD ; | |||||||||
| 212 | 1 | $program =~ s/.*:://; | |||||||||
| 213 | ######## what to do ???????????? | ||||||||||
| 214 | 1 | print tied( %{$Pack->{'data'}})->$program; | |||||||||
| 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<< <sasi.asterisk at gmail.com> >> | ||||||||||
| 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 | 2 | 1; # End of Config::IniRegEx | |||||||||