From 41b311f7a01efa5a8dd9598d0bc218c72b72c388 Mon Sep 17 00:00:00 2001 From: Thomas von Dein Date: Sat, 10 Oct 2009 16:23:39 +0000 Subject: [PATCH] 2.07 - fixed cpan bugid #1351, SaveConfig contained a deprecated function call which caused the module to croak. - added feature request, if in extended mode (OOP turned on with -ExtendedAccess => 1 access to non-existent keys caused a croak. While this is still the default behavior it is now possible to turn this off using -StrictObjects => 0. - added this to the related pod section in ::Extended. - fixed bug in new() which caused a couple of errors if the ConfigFile parameter is not set, or is set to undef. In this case it will now simply create an empty object. - fixed related bug in save_file() which will save "" to a file now if the config is uninitialized (i.e. the case mentioned below arrived). git-svn-id: http://dev.catalyst.perl.org/repos/Config-General/trunk@33 be1acefe-a474-0410-9a34-9b3221f2030f --- Changelog | 15 +++++++++++++++ General.pm | 42 +++++++++++++++++++++++++++++++----------- General/Extended.pm | 12 +++++++++++- README | 2 +- 4 files changed, 58 insertions(+), 13 deletions(-) diff --git a/Changelog b/Changelog index fc4cc92..95a1fec 100644 --- a/Changelog +++ b/Changelog @@ -1,3 +1,18 @@ + 2.07 - fixed cpan bugid #1351, SaveConfig contained a deprecated + function call which caused the module to croak. + - added feature request, if in extended mode (OOP turned + on with -ExtendedAccess => 1 access to non-existent keys + caused a croak. While this is still the default behavior + it is now possible to turn this off using -StrictObjects => 0. + - added this to the related pod section in ::Extended. + - fixed bug in new() which caused a couple of errors + if the ConfigFile parameter is not set, or is set to + undef. In this case it will now simply create an empty + object. + - fixed related bug in save_file() which will save "" to + a file now if the config is uninitialized (i.e. the case + mentioned below arrived). + 2.06 - added -SplitPolicy, -SplitDelimiter and -StoreDelimiter - removed whitespace support in keys in the default parser SplitPolicy 'guess', which was introduced in 2.02. Now diff --git a/General.pm b/General.pm index 008e4f5..5493b07 100644 --- a/General.pm +++ b/General.pm @@ -17,7 +17,7 @@ use strict; use Carp; use Exporter; -$Config::General::VERSION = "2.06"; +$Config::General::VERSION = "2.07"; use vars qw(@ISA @EXPORT); @ISA = qw(Exporter); @@ -67,6 +67,8 @@ sub new { CComments => 1, # by default turned on + StrictObjects => 1, # be strict on non-existent keys in OOP mode + parsed => 0 }; @@ -188,13 +190,20 @@ sub new { $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}); + if ($configfile) { + # 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 { + # hm, no valid config file given, so try it as an empty object + $self->{config} = {}; + $self->{parsed} = 1; + } } } @@ -660,7 +669,13 @@ sub save_file { $config_string = $this->_store(0,%{$config}); } - print $fh $config_string; + if ($config_string) { + print $fh $config_string; + } + else { + # empty config for whatever reason, I don't care + print $fh ""; + } close $fh; } @@ -805,7 +820,7 @@ sub SaveConfig { croak "The second parameter must be a reference to a hash!"; } else { - (new Config::General($hash))->save($file); + (new Config::General($hash))->save_file($file); } } } @@ -1100,6 +1115,11 @@ input. See L for more informations. If set to a true value, you can use object oriented (extended) methods to access the parsed config. See L for more informations. +=item B<-StrictObjects> + +By default this is turned on, which causes Config::General to croak with an +error if you try to access a non-existent key using the oop-way. If you turn +B<-StrictObjects> off (by setting to 0 or "no") it will just return undef. =item B<-SplitPolicy> @@ -1671,7 +1691,7 @@ Thomas Linden =head1 VERSION -2.06 +2.07 =cut diff --git a/General/Extended.pm b/General/Extended.pm index b91fe63..eb68f93 100644 --- a/General/Extended.pm +++ b/General/Extended.pm @@ -242,7 +242,13 @@ sub AUTOLOAD { } } else { - croak "\"$key\" does not exist within current object\n"; + if ($this->{StrictObjects}) { + croak "\"$key\" does not exist within current object\n"; + } + else { + # be cool + return undef; + } } } @@ -466,6 +472,10 @@ This notation supports only scalar values! You need to make sure, that the block does not contain any subblock or multiple identical options(which will become an array after parsing)! +If you access a non-existent key this way, Config::General will croak an error. +You can turn this behavior off by setting B<-StrictObjects> to 0 or "no". In +this case undef will be returned. + Of course you can use this kind of methods for writing data too: $person->name("Neustein"); diff --git a/README b/README index c97408a..f626acd 100644 --- a/README +++ b/README @@ -98,4 +98,4 @@ AUTHOR VERSION - 2.06 + 2.07 \ No newline at end of file