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

@@ -14,15 +14,17 @@ use Config::General 1.18;
use FileHandle;
use Carp;
use vars qw(@ISA);
use Exporter ();
use vars qw(@ISA @EXPORT);
# inherit new() and so on from Config::General
@ISA = qw(Config::General);
@ISA = qw(Config::General Exporter);
@EXPORT = qw(obj value hash array is_hash is_array is_scalar exists keys delete configfile);
use strict;
$Config::General::Extended::VERSION = "1.5";
$Config::General::Extended::VERSION = "1.6";
sub obj {
@@ -154,12 +156,12 @@ sub keys {
# it contains keys (so it must be a hash!)
#
my($this, $key) = @_;
if (exists $this->{config}->{$key} && ref($this->{config}->{$key}) eq "HASH") {
return map { $_ } keys %{$this->{config}->{$key}};
}
elsif (!$key) {
if (!$key) {
return map { $_ } keys %{$this->{config}};
}
elsif (exists $this->{config}->{$key} && ref($this->{config}->{$key}) eq "HASH") {
return map { $_ } keys %{$this->{config}->{$key}};
}
else {
return ();
}
@@ -181,19 +183,21 @@ sub delete {
}
sub save {
#
# save the config back to disk
#
my($this,$file) = @_;
my $fh = new FileHandle;
if (!$file) {
$file = $this->{configfile};
}
$this->save_file($file);
}
#
# removed, use save() of General.pm now
# sub save {
# #
# # save the config back to disk
# #
# my($this,$file) = @_;
# my $fh = new FileHandle;
#
# if (!$file) {
# $file = $this->{configfile};
# }
#
# $this->save_file($file);
# }
sub configfile {
@@ -253,63 +257,27 @@ sub DESTROY {
Config::General::Extended - Extended access to Config files
=head1 SYNOPSIS
use Config::General::Extended;
$conf = new Config::General::Extended("rcfile");
# or
$conf = new Config::General::Extended(\%somehash);
use Config::General;
$conf = new Config::General(
-file => 'configfile',
-ExtendedAccess => 1
);
=head1 DESCRIPTION
This module is a subclass of B<Config::General>. You can use it if
you want OO access to parts of your config file. The following methods
are directly inherited from Config::General: B<new() getall()>.
This is an internal module which makes it possible to use object
oriented methods to access parts of your config file.
Please refer to the L<Config::General>, if you want to learn about the usage
of the two methods mentioned above. The possible format of config files supported
by this module is also well described in L<Config::General>.
Normally you don't call it directly.
=head1 METHODS
=over
=item new(filename) or new(\%somehash)
This method returns a B<Config::General> object (a hash blessed into "Config::General::Extended"
namespace. All further methods must be used from that returned object. see below.
Read a more detailed discussion on how to use the new() method in L<Config::General>.
=item NoMultiOptions()
This method only exists for compatibility reasons.
Now you should set the new() flag B<-AllowMultiOptions>
to "no".
Refer to L<Config::General> for details about this method.
=item getall()
Returns a hash structure which represents the whole config.
If you use this method, then it would be probably
better to use the simpler module B<Config::General>. It is just mentioned here
for completeness.
=item save()
Writes the current config hash back to the harddisk.
It takes an optional argument: B<filename>. If you omit a filename, save() will
use the filename configured by the method B<configfile()> or B<new()> (see below).
B<Important>: the method save() is now superseded by B<save_file()> and B<save_string()>.
Refer to L<Config::General> for details. You can use these new methods to save
a config either to a string or to a file.
=item configfile('filename')
Set the filename to be used by B<save> to "filename". It returns the current
@@ -522,7 +490,7 @@ Thomas Linden <tom@daemon.de>
=head1 VERSION
1.5
1.6
=cut

View File

@@ -1,13 +1,16 @@
package Config::General::Interpolated;
$Config::General::Interpolated::VERSION = "1.0";
$Config::General::Interpolated::VERSION = "1.1";
use strict;
use Carp;
use Config::General;
use Exporter ();
# Import stuff from Config::General
use vars qw(@ISA);
@ISA = qw(Config::General);
use vars qw(@ISA @EXPORT);
@ISA = qw(Config::General Exporter);
@EXPORT=qw(_set_regex _vars);
sub new {
#
@@ -17,10 +20,24 @@ sub new {
my $class = shift;
my $self = $class->SUPER::new(@_);
$self->{regex} = $self->_set_regex();
$self->{config} = $self->_vars($self->{config}, {});
return $self;
}
sub _set_regex {
#
# set the regex for finding vars
#
# the following regex is provided by Autrijus Tang
# <autrijus@autrijus.org>, and I made some modifications.
# thanx, autrijus. :)
$self->{regex} = qr{
my $regex = qr{
(^|[^\\]) # can be the beginning of the line
# but can't begin with a '\'
\$ # dollar sign
@@ -31,17 +48,12 @@ sub new {
\} # ... match closing curly
)
}x;
$self->{config} = $self->vars($self->{config}, {});
return $self;
return $regex;
}
sub vars {
sub _vars {
my ($this, $config, $stack) = @_;
my %varstack;
@@ -90,7 +102,7 @@ sub vars {
# traverse the hierarchy part
while (my ($key, $value) = each %{$config}) {
# this is not a scalar recursive call to myself
$this->vars($value, {%{$stack}, %varstack})
_vars($value, {%{$stack}, %varstack})
if ref($value) eq 'HASH';
}
@@ -109,21 +121,20 @@ Config::General::Interpolated - Parse variables within Config files
=head1 SYNOPSIS
use Config::General::Interpolated;
$conf = new Config::General::Interpolated("rcfile");
# or
$conf = new Config::General::Interpolated(\%somehash);
use Config::General;
$conf = new Config::General(
-file => 'configfile',
-InterPolateVars => 1
);
=head1 DESCRIPTION
This module is a subclass of B<Config::General>. You can use it if
your config file contains perl-style variables (i.e. C<$variable>
or C<${variable}>). The following methods are directly inherited
from Config::General: B<new() getall()>.
This is an internal module which makes it possible to interpolate
perl style variables in your config file (i.e. C<$variable>
or C<${variable}>).
Normally you don't call it directly.
Please refer to the L<Config::General> for the module's usage and the
format of config files.
=head1 VARIABLES
@@ -193,15 +204,16 @@ L<Config::General>
=head1 COPYRIGHT
Copyright 2001 by Wei-Hon Chen E<lt>plasmaball@pchome.com.twE<gt>.
Copyright 2002 by Thomas Linden <tom@daemon.de>.
This program is free software; you can redistribute it and/or
This program is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.
See L<http://www.perl.com/perl/misc/Artistic.html>
=head1 VERSION
This document describes version 1.0 of B<Config::General::Interpolated>.
This document describes version 1.1 of B<Config::General::Interpolated>.
=cut