From 1ffbf956658c860531b3b4c9f4b4bbf288a9a3c8 Mon Sep 17 00:00:00 2001 From: Thomas von Dein Date: Sat, 10 Oct 2009 16:10:12 +0000 Subject: [PATCH] 1.22: - added a new option to new(): -LowerCaseNames, which lowercases all option-names (feature request) git-svn-id: http://dev.catalyst.perl.org/repos/Config-General/trunk@10 be1acefe-a474-0410-9a34-9b3221f2030f --- General.pm | 51 ++++++++++++++++++++++++++++++++++++++++++++++----- t/cfg.7 | 29 +++++------------------------ t/cfg.8 | 27 +++++++++++++++++++++++++++ t/run.t | 15 ++++++++------- 4 files changed, 86 insertions(+), 36 deletions(-) create mode 100644 t/cfg.8 diff --git a/General.pm b/General.pm index 64d4d0c..9499270 100644 --- a/General.pm +++ b/General.pm @@ -6,10 +6,14 @@ # given file and return it as hash # structure # -# Copyright (c) 2000 Thomas Linden . +# Copyright (c) 2000-2001 Thomas Linden . # All Rights Reserved. Std. disclaimer applies. # Artificial License, same as perl itself. Have fun. # +# Changes from 1.21: - added a new option to new(): -LowerCaseNames, which +# lowercases all option-names (feature request) +# Changes from 1.20: - lines with just one "#" became an option array named +# "#" with empty entries, very weird, fixed # Changes from 1.19: - added an if(exists... to new() for checking of the # existence of -AllowMultiOptions. # - use now "local $_" because it caused weird results @@ -26,7 +30,7 @@ use FileHandle; use strict; use Carp; -$Config::General::VERSION = "1.20"; +$Config::General::VERSION = "1.22"; sub new { # @@ -50,6 +54,11 @@ sub new { $self->{NoMultiOptions} = 1; } } + if (exists $conf{-LowerCaseNames}) { + if ($conf{-LowerCaseNames}) { + $self->{LowerCaseNames} = 1; + } + } } elsif ($#param == 0) { # use of the old style @@ -210,6 +219,7 @@ sub _parse { if ($blockname) { $block = $grab; } + $block = lc($block) if $this->{LowerCaseNames}; # only for blocks lc(), if configured via new() undef @newcontent; next; } @@ -217,6 +227,7 @@ sub _parse { croak "EndBlock \"<\/$1>\" has no StartBlock statement (level: $this->{level}, chunk $chunk)!\n"; } else { # insert key/value pair into actual node + $option = lc($option) if $this->{LowerCaseNames}; if ($this->{NoMultiOptions}) { # configurable via special method ::NoMultiOptions() if (exists $config->{$option}) { croak "Option $config->{$option} occurs more than once (level: $this->{level}, chunk $chunk)!\n"; @@ -244,7 +255,7 @@ sub _parse { } elsif (/^<\/(.+?)>$/) { if ($block_level) { # this endblock is not the one we are searching for, decrement and push - $block_level--; # if it is 0 the the endblock was the one we searched for, see below + $block_level--; # if it is 0, then the endblock was the one we searched for, see below push @newcontent, $_; # push onto new content stack } else { # calling myself recursively, end of $block reached, $block_level is 0 @@ -386,8 +397,9 @@ Possible ways to call B: $conf = new Config::General(\%somehash); $conf = new Config::General( - -file => "rcfile", + -file => "rcfile", -AllowMultiOptions => "no" + -LowerCaseNames => "yes" ); $conf = new Config::General( @@ -410,6 +422,9 @@ still supported. Possible parameters are: -hash - a hash reference. -AllowMultiOptions - if the value is "no", then multiple identical options are disallowed. + -LowerCaseNames - if true (1 or "yes") then all options found + in the config will be converted to lowercase. + This allows you to provide case-in-sensitive configs =item NoMultiOptions() @@ -542,6 +557,32 @@ The hash which the method B returns look like that: 'user' => 'hans' }; +If you have turned on B<-LowerCaseNames> (see new()) then blocks as in the +following example: + + + + Owner root + + + +would produce the following hash structure: + + $VAR1 = { + 'dir' => { + 'attributes' => { + 'owner => "root", + } + } + }; + +As you can see, the keys inside the config hash are normalized. + +Please note, that the above config block would result in a +valid hash structure, even if B<-LowerCaseNames> is not set! +This is because I does not +use the blocknames to check if a block ends, instead it uses an internal +state counter, which indicates a block end. If the module cannot find an end-block statement, then this block will be ignored. @@ -740,7 +781,7 @@ Thomas Linden =head1 VERSION -1.20 +1.22 =cut diff --git a/t/cfg.7 b/t/cfg.7 index 4ab7384..ef23a32 100644 --- a/t/cfg.7 +++ b/t/cfg.7 @@ -1,27 +1,8 @@ - - +# Case insensitive block test + + + name stein age 25 - - name bird - age 31 - - -domain b0fh.org -domain l0pht.com -domain infonexus.com -message < -host = blah.blubber - + diff --git a/t/cfg.8 b/t/cfg.8 new file mode 100644 index 0000000..4ab7384 --- /dev/null +++ b/t/cfg.8 @@ -0,0 +1,27 @@ + + + name stein + age 25 + + + name bird + age 31 + + +domain b0fh.org +domain l0pht.com +domain infonexus.com +message < +host = blah.blubber + diff --git a/t/run.t b/t/run.t index 2402714..09bb992 100644 --- a/t/run.t +++ b/t/run.t @@ -4,17 +4,18 @@ # the Conf.pm source directory. # Under normal circumstances every test should run. -BEGIN { $| = 1; print "1..7\n";} +BEGIN { $| = 1; print "1..8\n";} use lib "blib/lib"; use Config::General; +use Data::Dumper; print "ok\n"; print STDERR "\n1 .. ok # loading Config::General\n"; -foreach (2..6) { +foreach (2..7) { &p("t/cfg." . $_, $_); } -my $conf = new Config::General("t/cfg.7"); +my $conf = new Config::General("t/cfg.8"); my %hash = $conf->getall; $conf->save("t/cfg.out", %hash); @@ -25,14 +26,14 @@ my $a = \%hash; my $b = \%copyhash; # now see if the saved hash is still the same as the -# one we got from cfg.7 +# one we got from cfg.8 if (&comp($a,$b)) { print "ok\n"; - print STDERR "7 .. ok # Writing Config Hash to disk and compare with original\n"; + print STDERR "8 .. ok # Writing Config Hash to disk and compare with original\n"; } else { - print "7 not ok\n"; - print STDERR "7 .. not ok\n"; + print "8 not ok\n"; + print STDERR "8 .. not ok\n"; }