diff --git a/Changelog b/Changelog index c6f2392..af86bc8 100644 --- a/Changelog +++ b/Changelog @@ -1,4 +1,25 @@ - 1.32 - fixed bug in _parse_value() which caused perl to complain + 1.34 - Danial Pearce reported a bug + in _store(), which caused the module to create scalar + entries even if the entry contained newlines. While + Danial supplied a patch to fix this - thx(TM) - I + did not apply it, because I "outsourced" this kind of + stuff to the subroutine _write_scalar(), see next. + + - added internal methods _write_scalar() and _write_hash() + to simplify _store(), which did the same thing more + than once, which is a good time to create a sub which + does the job. + + - fixed cut'n paste bug in General/Extended.pm reported by + Danial Pearce , which caused + Config::General::Extended::is_scalar() to return true even + when the key you pass in is an array. + + - added new method Config::General::Extended::delete() suggested + by Danial Pearce , which deletes + the given key from the config. + + 1.33 - fixed bug in _parse_value() which caused perl to complain with "Use of uninitialized value in..." if a value was empty. diff --git a/General.pm b/General.pm index ce168ca..a5a1e0c 100644 --- a/General.pm +++ b/General.pm @@ -583,39 +583,19 @@ sub _store { foreach my $entry (sort keys %config) { if (ref($config{$entry}) eq "ARRAY") { foreach my $line (@{$config{$entry}}) { - # patch submitted by Peder Stray to catch - # arrays of hashes. - if (ref($line) eq "HASH") { - $config_string .= $indent . "<" . $entry . ">\n"; - $config_string .= $this->_store($level + 1, %{$line}); - $config_string .= $indent . "\n"; - } - else { - $line =~ s/#/\\#/g; - $config_string .= $indent . $entry . " " . $line . "\n"; - } + if (ref($line) eq "HASH") { + $config_string .= $this->_write_hash($level, $entry, $line); + } + else { + $config_string .= $this->_write_scalar($level, $entry, $line); + } } } elsif (ref($config{$entry}) eq "HASH") { - $config_string .= $indent . "<" . $entry . ">\n"; - $config_string .= $this->_store($level + 1, %{$config{$entry}}); - $config_string .= $indent . "\n"; + $config_string .= $this->_write_hash($level, $entry, $config{$entry}); } else { - # scalar - if ($config{$entry} =~ /\n/) { - # it is a here doc - my @lines = split /\n/, $config{$entry}; - $config_string .= $indent . $entry . " <_write_scalar($level, $entry, $config{$entry}); } } @@ -623,6 +603,53 @@ sub _store { } +sub _write_scalar { + # + # internal sub, which writes a scalar + # it returns it, in fact + # + my($this, $level, $entry, $line) = @_; + + my $indent = " " x $level; + + my $config_string; + + if ($line =~ /\n/) { + # it is a here doc + my @lines = split /\n/, $line; + $config_string .= $indent . $entry . " <\n"; + $config_string .= $this->_store($level + 1, %{$line}); + $config_string .= $indent . "\n"; + + return $config_string +} + + # # Procedural interface @@ -881,9 +908,9 @@ The resulting config structure would look like this after parsing: This method allows the user (or, the "maintainer" of the configfile for your application) to set multiple pre-defined values for one option. -Please beware, that all occurences of thos variables will be handled this +Please beware, that all occurencies of those variables will be handled this way, there is no way to distinguish between variables in different scopes. -That means, that if "Mode" would also occur inside a named block, it would +That means, if "Mode" would also occur inside a named block, it would also parsed this way. Values which are not defined in the hash-ref supplied to the parameter B<-FlagBits> @@ -1440,7 +1467,7 @@ Thomas Linden =head1 VERSION -1.33 +1.34 =cut diff --git a/General/Extended.pm b/General/Extended.pm index d2c8519..f41a811 100644 --- a/General/Extended.pm +++ b/General/Extended.pm @@ -22,7 +22,7 @@ use vars qw(@ISA); use strict; -$Config::General::Extended::VERSION = "1.3"; +$Config::General::Extended::VERSION = "1.4"; sub obj { @@ -126,7 +126,7 @@ sub is_scalar { # returns true if the given key contains a scalar(or number) # my($this, $key) = @_; - if (exists $this->{config}->{$key} && !ref(exists $this->{config}->{$key})) { + if (exists $this->{config}->{$key} && !ref($this->{config}->{$key})) { return 1; } return; @@ -162,6 +162,22 @@ sub keys { } } + +sub delete { + # + # delete the given key from the config, if any + # and return what is deleted (just as 'delete $hash{key}' does) + # + my($this, $key) = @_; + if (exists $this->{config}->{$key}) { + return delete $this->{config}->{$key}; + } + else { + return undef; + } +} + + sub save { # # save the config back to disk @@ -437,6 +453,13 @@ config above you yould do that: You can use this method in B loops as seen in an example above(obj() ). + +=item delete ('key') + +This method removes the given key and all associated data from the internal +hash structure. If 'key' contained data, then this data will be returned, +otherwise undef will be returned. + =back @@ -494,7 +517,7 @@ Thomas Linden =head1 VERSION -1.3 +1.4 =cut diff --git a/README b/README index 6d26ef5..d33997d 100644 --- a/README +++ b/README @@ -37,6 +37,7 @@ INSTALLATION to read the complete documentation, type: perldoc Config::General perldoc Config::General::Extended + perldoc Config::General::Interpolated see some example config files which can be parsed with Config::Genreal in the subdirectory @@ -44,14 +45,19 @@ INSTALLATION COPYRIGHT - Copyright (c) 2000-2002 Thomas Linden + Config::General + Config::General::Extended + Copyright (c) 2000-2002 Thomas Linden + + Config::General::Interpolated + Copyright (c) 2001 Wei-Hon Chen This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. BUGS - none known yet. + make test does currently not catch all possible scenarios. AUTHOR @@ -59,4 +65,4 @@ AUTHOR VERSION - 1.33 + 1.34