i 2.00 - fixed a bug in the ::Extended::keys() method, which

caused a beloved "use of uninitialized ..." message.
	   Reported by Danial Pearce <danial@infoxchange.net.au>.

         - Removed all deprecated methods (in fact, they are still
	   there for shouting out a warn that its deprecated. But
	   the pod sections are removed. These are NoMultiOptions()
	   and save().

	 - added two new parameters to new(): -InterPolateVars and
	   -ExtendedAccess, which allows one to use the functionalites
	   of the supplied submodules without the need to decide
	   for one of them. This makes it possible to use variable
	   interpolation and oop access in the same time. Suggested
	   by Jared Rhine <jared@wordzoo.com>.

	 - added new parameter -BaseHash which makes it possible
	   to supply your own hash which stores the parsed contents
	   of the config. This can be a tied hash o the like.
	   Suggested by Jared Rhine <jared@wordzoo.com> too.

	 - switched to release 2.00 because the above is a major
	   change.


git-svn-id: http://dev.catalyst.perl.org/repos/Config-General/trunk@26 be1acefe-a474-0410-9a34-9b3221f2030f
This commit is contained in:
Thomas von Dein
2009-10-10 16:20:14 +00:00
parent c3eced799c
commit 76502a240e
4 changed files with 203 additions and 133 deletions

View File

@@ -17,7 +17,7 @@ use strict;
use Carp;
use Exporter;
$Config::General::VERSION = "1.36";
$Config::General::VERSION = "2.00";
use vars qw(@ISA @EXPORT);
@ISA = qw(Exporter);
@@ -53,7 +53,15 @@ sub new {
DefaultConfig => {},
BaseHash => {},
level => 1,
InterPolateVars => 0,
ExtendedAccess => 0,
parsed => 0
};
# create the class instance
@@ -93,8 +101,17 @@ sub new {
$self->{content} = ();
}
delete $conf{-DefaultConfig};
delete $conf{-BaseHash}; # ignore BaseHash if a default one was given
}
if (exists $conf{-BaseHash}) {
if ($conf{-BaseHash}) {
# we do not check for ref() output because the hash could
# be something we are not expecting, a tied hash for example
$self->{DefaultConfig} = $conf{-BaseHash};
}
delete $conf{-BaseHash};
}
# handle options which may either be true or false
# allowing "human" logic about what is true and what is not
@@ -124,35 +141,63 @@ sub new {
else {
# this happens if $#param == -1,1 thus no param was given to new!
$self->{config} = {};
return $self;
}
### use Data::Dumper; print Dumper($self); exit;
# process as usual
if (exists $self->{StringContent}) {
# consider the supplied string as config file
$self->_read($self->{StringContent}, "SCALAR");
$self->{config} = $self->_parse($self->{DefaultConfig}, $self->{content});
}
elsif (ref($configfile) eq "HASH") {
# initialize with given hash
$self->{config} = $configfile;
$self->{parsed} = 1;
}
elsif (ref($configfile) eq "GLOB") {
# use the file the glob points to
$self->_read($configfile);
$self->{config} = $self->_parse($self->{DefaultConfig}, $self->{content});
# process as usual
if (!$self->{parsed}) {
if (exists $self->{StringContent}) {
# consider the supplied string as config file
$self->_read($self->{StringContent}, "SCALAR");
$self->{config} = $self->_parse($self->{DefaultConfig}, $self->{content});
}
elsif (ref($configfile) eq "HASH") {
# initialize with given hash
$self->{config} = $configfile;
$self->{parsed} = 1;
}
elsif (ref($configfile) eq "GLOB") {
# use the file the glob points to
$self->_read($configfile);
$self->{config} = $self->_parse($self->{DefaultConfig}, $self->{content});
}
else {
# open the file and read the contents in
$self->{configfile} = $configfile;
# look if is is an absolute path and save the basename if it is absolute
($self->{configpath}) = $configfile =~ /^(\/.*)\//;
$self->_open($self->{configfile});
# now, we parse immdediately, getall simply returns the whole hash
$self->{config} = $self->_parse($self->{DefaultConfig}, $self->{content});
}
}
else {
# open the file and read the contents in
$self->{configfile} = $configfile;
# look if is is an absolute path and save the basename if it is absolute
($self->{configpath}) = $configfile =~ /^(\/.*)\//;
$self->_open($self->{configfile});
# now, we parse immdediately, getall simply returns the whole hash
$self->{config} = $self->_parse($self->{DefaultConfig}, $self->{content});
#
# Submodule handling. Parsing is already done at this point.
#
if ($self->{InterPolateVars}) {
eval {
require Config::General::Interpolated;
};
if ($@) {
croak $@;
}
$self->{regex} = Config::General::Interpolated::_set_regex();
$self->{config} = Config::General::Interpolated::_vars($self, $self->{config}, {});
}
if ($self->{ExtendedAccess}) {
#
# we are blessing here again, to get into the ::Extended namespace
# for inheriting the methods available overthere, which we doesn't have.
#
bless($self, "Config::General::Extended");
eval {
require Config::General::Extended;
};
if ($@) {
croak $@;
}
}
return $self;
@@ -354,7 +399,16 @@ sub _parse {
delete $config->{$option};
push @{$config->{$option}}, $savevalue;
}
push @{$config->{$option}}, $this->_parse_value($option, $value); # it's already an array, just push
eval {
# check if arrays are supported by the underlying hash
my $i = scalar @{$config->{$option}};
};
if ($@) {
$config->{$option} = $this->_parse_value($option, $value);
}
else {
push @{$config->{$option}}, $this->_parse_value($option, $value); # it's already an array, just push
}
}
}
}
@@ -506,9 +560,7 @@ sub NoMultiOptions {
# Since we do parsing from within new(), we must
# call it again if one turns NoMultiOptions on!
#
my($this) = @_;
$this->{AllowMultiOptions} = 0;
$this->{config} = $this->_parse({}, $this->{content});
croak "The NoMultiOptions() method is deprecated. Set 'AllowMultiOptions' to 'no' instead!";
}
@@ -729,6 +781,8 @@ sub SaveConfigString {
}
}
# keep this one
1;
@@ -979,15 +1033,32 @@ causes the module to preset the resulting config hash with the given values,
which allows you to set default values for particular config options directly.
=item B<-InterPolateVars>
If set to a true value, variable interpolation will be done on your config
input. See L<Config::General::Interpolated> for more informations.
=item B<-ExtendedAccess>
If set to a true value, you can use object oriented (extended) methods to
access the parsed config. See L<Config::General::Extended> for more informations.
=item B<-BaseHash>
You can supply a reference to a 'hash' which will be used to store the parsed
contents of your config file instead of the default standard perl hash.
This is a way to affect the way, variable storing will be done. You could, for
example supply a tied hash, say Tie::DxHash, which preserves ordering of the
keys in the config (which a standard perl hash won't do). Or, you could supply
a hash tied to a DBM file to save the parsed variables to disk.
There are many more things to do in tie-land, see L<tie> to get some interesting
ideas.
=back
=item NoMultiOptions()
This method only exists for compatibility reasons and is deprecated.
Now you should set the parameter to the B<new()> method B<-AllowMultiOptions> to "no".
see B<METHODS>.
=item getall()
@@ -995,11 +1066,6 @@ see B<METHODS>.
Returns a hash structure which represents the whole config.
=item save()
B<Deprectated. Use save_file() instead!>
=item save_file()
Writes the config hash back to the harddisk. This method takes one or two
@@ -1514,7 +1580,7 @@ Thomas Linden <tom@daemon.de>
=head1 VERSION
1.36
2.00
=cut