Subject: | 'DontClobber' prefix in file parse only changes value if variable is not set. |
Suggested feature:
Add '?' prefix for file parsing, allowing non-clobbering variable
assignment.
Example:
# $foo is set to <undef>
?foo = bar # $foo is set to "bar"
?foo = baz # $foo remains set to "bar"
More complicated example:
bar = $(foo)
?foo = 'apple'
?bar = $(foo)sauce
If $foo begins undef, then we end up with $foo = 'apple' and $bar =
'applesauce'. If $foo starts with a value, 'orange', then we end with
both $foo and $bar = 'orange'
The first patch is a minor change, but only implements not-clobbering of
<undef>/false values (as in the examples above).
The second patch is probably more useful, but is also larger and less
well written. It adds potential clobber-protection for any non-DEFAULT
values. This required a new function, get_default in State.pm.
Let me know if you have any questions. If I have time || anyone is
interested, I'll try to update the documentation and provide diffs for
that as well.
Subject: | AppConfig.patch |
*** lib/AppConfig/File.pm Wed Feb 4 02:28:28 2004
--- lib/AppConfig/File.pm.new Tue Jun 6 03:04:17 2006
***************
*** 197,203 ****
};
# strip any leading '+/-' from the variable
! $variable =~ s/^([\-+]?)//;
$flag = $1;
# $variable gets any $prefix
--- 197,203 ----
};
# strip any leading '+/-' from the variable
! $variable =~ s/^([\-+?]?)//;
$flag = $1;
# $variable gets any $prefix
***************
*** 224,229 ****
--- 224,235 ----
elsif ($flag eq '+') {
$value = 1 unless defined $value;
}
+
+ # don't clobber those prefixed with '?'
+ elsif ($flag eq '?') {
+ my $orig_value = $state->get($variable);
+ next if(defined $orig_value && length $orig_value);
+ }
# determine if any extra arguments were expected
if ($nargs) {
Subject: | AppConfig.better.patch |
*** File.pm.old Tue Jun 6 04:14:02 2006
--- src/lib/AppConfig/File.pm Tue Jun 6 04:30:16 2006
***************
*** 196,203 ****
};
};
! # strip any leading '+/-' from the variable
! $variable =~ s/^([\-+]?)//;
$flag = $1;
# $variable gets any $prefix
--- 196,203 ----
};
};
! # strip any leading '+/-/?' from the variable
! $variable =~ s/^([\-+\?]?)//;
$flag = $1;
# $variable gets any $prefix
***************
*** 223,228 ****
--- 223,236 ----
# those prefixed '+' get set to 1
elsif ($flag eq '+') {
$value = 1 unless defined $value;
+ }
+
+ # don't clobber non-default values for those prefixed with '?'
+ elsif ($flag eq '?') {
+ my $orig_value = $state->get($variable);
+ my $default = $state->_get_default($variable);
+ next if(defined $orig_value xor defined $default or (
+ defined $orig_value and $orig_value ne $default));
}
# determine if any extra arguments were expected
*** State.pm.old Wed Feb 4 02:11:23 2004
--- src/lib/AppConfig/State.pm Tue Jun 6 05:13:00 2006
***************
*** 48,54 ****
# need access to AppConfig::ARGCOUNT_*
use AppConfig qw(:argcount);
! $VERSION = sprintf("%d.%02d", q$Revision: 1.61 $ =~ /(\d+)\.(\d+)/);
$DEBUG = 0;
# internal per-variable hashes that AUTOLOAD should provide access to
--- 48,54 ----
# need access to AppConfig::ARGCOUNT_*
use AppConfig qw(:argcount);
! $VERSION = sprintf("%d.%02d", q$Revision: 1.62 $ =~ /(\d+)\.(\d+)/);
$DEBUG = 0;
# internal per-variable hashes that AUTOLOAD should provide access to
***************
*** 604,632 ****
# check the variable exists
if (exists($self->{ VARIABLE }->{ $variable })) {
! # set variable value to the default scalar, an empty list or empty
# hash array, depending on its ARGCOUNT value
my $argcount = $self->{ ARGCOUNT }->{ $variable };
$argcount = AppConfig::ARGCOUNT_ONE unless defined $argcount;
if ($argcount == AppConfig::ARGCOUNT_NONE) {
! return $self->{ VARIABLE }->{ $variable }
! = $self->{ DEFAULT }->{ $variable } || 0;
}
elsif ($argcount == AppConfig::ARGCOUNT_LIST) {
my $deflist = $self->{ DEFAULT }->{ $variable };
! return $self->{ VARIABLE }->{ $variable } =
! [ ref $deflist eq 'ARRAY' ? @$deflist : ( ) ];
}
elsif ($argcount == AppConfig::ARGCOUNT_HASH) {
my $defhash = $self->{ DEFAULT }->{ $variable };
! return $self->{ VARIABLE }->{ $variable } =
! { ref $defhash eq 'HASH' ? %$defhash : () };
}
else {
! return $self->{ VARIABLE }->{ $variable }
! = $self->{ DEFAULT }->{ $variable };
}
}
else {
--- 604,655 ----
# check the variable exists
if (exists($self->{ VARIABLE }->{ $variable })) {
! # set variable value to the default value for this variable
! $self->{ VARIABLE }->{ $variable} = _get_default($self, $variable);
! }
! else {
! $self->_error("$variable: no such variable");
! return 0;
! }
! }
!
! #------------------------------------------------------------------------
! # get_default($variable)
! #
! # Gets the default value for the variable specified or undef if it doesn't have
! # a default. The default value is returned.
! #------------------------------------------------------------------------
!
!
! sub _get_default {
! my $self = shift;
! my $variable = shift;
!
! # _varname returns variable name after aliasing and case conversion
! $variable = $self->_varname($variable);
!
! # check the variable exists
! if (exists($self->{ VARIABLE }->{ $variable })) {
!
! # get variable value to the default scalar, an empty list or empty
# hash array, depending on its ARGCOUNT value
my $argcount = $self->{ ARGCOUNT }->{ $variable };
$argcount = AppConfig::ARGCOUNT_ONE unless defined $argcount;
if ($argcount == AppConfig::ARGCOUNT_NONE) {
! return $self->{ DEFAULT }->{ $variable } || 0;
}
elsif ($argcount == AppConfig::ARGCOUNT_LIST) {
my $deflist = $self->{ DEFAULT }->{ $variable };
! return [ ref $deflist eq 'ARRAY' ? @$deflist : ( ) ];
}
elsif ($argcount == AppConfig::ARGCOUNT_HASH) {
my $defhash = $self->{ DEFAULT }->{ $variable };
! return { ref $defhash eq 'HASH' ? %$defhash : () };
}
else {
! return $self->{ DEFAULT }->{ $variable };
}
}
else {