1package Config::IniRegEx; 2 3use warnings; 4use strict; 5 6use Config::IniFiles; 7our $VERSION = '0.01'; 8 9=head1 NAME 10 11Config::IniRegEx - Ini workaround, regex search for parameters and sections. 12 13=head1 SYNOPSIS 14 15use Config::IniRegEx; 16 17my $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 33sub New { 34 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 my $nocase = $_[1]; 40 $nocase = 0 if ( not defined $_[1] ); 41 my $DataRef = _ParseAndPopulate($_[0],$nocase); 42 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 my $objref = { 47 'filename' => $_[0], 48 'nocase' => $nocase, 49 'data' => $DataRef, 50 }; 51 bless $objref,$class; 52 return $objref; 53} # ---------- end of subroutine New ---------- 54 55 56sub _ParseAndPopulate 57{ 58 # Get filename from argument of this functions 59 my ($filename, $icase) = @_; 60 # To store the data available in a ini file. 61 my %ini; 62 63 # Tie that filename into ini hash, it will store in a hash of hash form. 64 tie %ini, 'Config::IniFiles', ( -file => "$filename", -nocase => $icase ); 65 66 # If the filename given is not available 67 if ( ! %ini ) { 68 return undef; 69 } 70 # Returning the reference of a hash. 71 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. 76sub ValRegex 77{ 78 my ( $Pack, $Section, $Parameter ) = @_; 79 my $countofmatch = 0; 80 my %outhash ; 81 undef %outhash; 82 83 # Check whether the section exist first. 84 if ( tied( %{$Pack->{'data'}})->SectionExists($Section) ) { 85 # take the parameters available in that section. 86 foreach ( keys %{$Pack->{'data'}->{$Section}} ) { 87 # Check if the parameters are matching. 88 if ( $_ =~ /^$Parameter$/ ) { 89 # If it matches, Store this into the output hash. 90 $outhash{$_} = $Pack->{'data'}->{$Section}->{$_}; 91 # Have a matched count 92 $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 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 111sub SectionRegex 112{ 113 my ( $Pack, $Section ) = @_; 114 my $countofmatch = 0; 115 my @opsec; 116 undef @opsec; 117 118 if ( !$Section ) { 119 #Section argument is not available 120 return @opsec; 121 } 122 123 foreach ( tied( %{$Pack->{'data'}})->Sections() ) { 124 if ( $_ =~ /^$Section$/ ) { 125 push @opsec, $_; 126 $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 return @opsec; 135} 136 137 138 139#5. SectionExists ( regex ) 140# 141#5.1 returns number of matches. 142# ZERO for no match. 143sub SectionExistsRegex 144{ 145 my ( $Pack, $Section ) = @_; 146 my ( $countofmatch ) = ( 0 ); 147 148 if ( !$Section ) { 149 #Section arguement is not available 150 return 0; 151 } 152 153 foreach ( tied( %{$Pack->{'data'}})->Sections() ) { 154 if ( $_ =~ /^$Section$/ ) { 155 $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 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 172sub ParameterRegex 173{ 174 my ( $Pack , $Section) = @_; 175 my %ophash; 176 undef %ophash; 177 178 if ( !$Section ) { 179 #Section arguement is not available 180 return %ophash; 181 } 182 183 # Get all the sections 184 foreach ( tied( %{$Pack->{'data'}})->Sections() ) { 185 # Check for a match 186 if ( $_ =~ /^$Section$/ ) { 187 $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 return %ophash; 196} 197 198sub post 199{ 200 my ($pack) = shift; 201 my $ref = {'a' => 'b'}; 202 $pack->{'hash'} = $ref; 203 204 return; 205} 206 207sub AUTOLOAD 208{ 209 210 my ( $Pack ) = shift; 211 my $program = our $AUTOLOAD ; 212 $program =~ s/.*:://; 213 ######## what to do ???????????? 214 print tied( %{$Pack->{'data'}})->$program; 215} 216 217 218=cut 219 220=head1 DESCRIPTION 221 222Config::IniRegEx is dependent on Config::IniFiles. Using that module it does the ini configuration 223file parsing, with an addon facility of regex kind of search. 224 225Each function explained below, searches for a different thing based up on the given regular expression. 226And it is a exact regex match. 227 228When a function of a Config::IniFiles is called then it will be called using the autoload functionality, 229i.e all the functions of the Config::IniFiles can be called with this object itself. ( creating an 230object for Config::IniRegEx is enough, can access all the functions available at Config::IniFiles also. ). 231 232This module aims out doing a regex search for Sections, and Parameters of the Ini configuration file. 233It does the Perl regex matching, nothing external. So whoever knows the Perl basic regex can use this 234feature. 235 236=head1 FUNCTIONS 237 238The 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 355Sasi, C<< <sasi.asterisk at gmail.com> >> 356 357=head1 COPYRIGHT & LICENSE 358 359Copyright 2009 Sasi, all rights reserved. 360 361This program is free software; you can redistribute it and/or modify it 362under the same terms as Perl itself. 363 364 365=cut 366 3671; # End of Config::IniRegEx 368