Subject: | Variable interpolation change proposal. |
The changes of variable interpolation valid chars in variable names in
2.38 broke some of my programs. After fixing them I took a look at the
code in Interpolated.pm from 2.44 and made two small changes:
1.- Changed single quote handling in interpolate, from a substitution to
a random string to a split in quotes and handling only even parts:
abcd'def'ghi'jkl'mn=> split => interpolate in even ( abcd ghi mn ) join
again.
I think is a little clearer. Also, the rand() solution can have
collisions ( unlikely, but it can ).
Actually my solution will consider a misquoted tail ( abc'def ) as
quoted, when the previous one will not. I don't know wether this is a
problem ( thinking on << I don't do $X >> ). Also, as the previous one I
unquote before parsing, so << ${I_don't} ${you don't} >> will not expand
( but this behaviour was also present in the previous solution ).
2.- Change regexp from catching an optional open brace and then
conditionally catching the closing brace to matching an alternative
after $ , either an unquoted word definition or a quoted word
definition. This required a little change in the substitution code to
get either $2 or $3 as $var name. It's been done.
This change permits using diferent definitions for braced and unbraced
words. In the uploaded sample I use the pre-2.38 definition for unbrace,
which is similar to shell/perl notation, and anything not a closing
brace for braced.
This way you can do "$HOME/tmp/$BASENAME.log" but you are still allowed
to use "${foo-bar}", "${whatever you want.}" and even ${} ( empty name
). I think this could be useful.
Notes:
a.- Not being familiar with patchs I've uploaded the whole
Interpolated.pm file. Presently it passes all the tests in 2.44, I'm
working in some more interpolation tests.
b.- The regulr expresions uses '[^\}]*' for braced var names, using
'[a-zA-Z_0-9+.:,-]+' would catch 2.38+ names.
c.- The chunk after the trailing single quote can easily be considered
unquoted. All that would be needed is to factorize the code for the s///
into another func ( to avoid duplication ) and, after the loop, doing
'if (scalar(@values)%2!=0) { $values[-1]=~ substitute }.
Subject: | Interpolated.pm |
#
# Config::General::Interpolated - special Class based on Config::General
#
# Copyright (c) 2001 by Wei-Hon Chen <plasmaball@pchome.com.tw>.
# Copyright (c) 2000-2009 by Thomas Linden <tlinden |AT| cpan.org>.
# All Rights Reserved. Std. disclaimer applies.
# Artificial License, same as perl itself. Have fun.
#
package Config::General::Interpolated;
$Config::General::Interpolated::VERSION = "2.11";
use strict;
use Carp;
use Config::General;
use Exporter ();
# Import stuff from Config::General
use vars qw(@ISA @EXPORT);
@ISA = qw(Config::General Exporter);
sub new {
#
# overwrite new() with our own version
# and call the parent class new()
#
croak "Deprecated method Config::General::Interpolated::new() called.\n"
."Use Config::General::new() instead and set the -InterPolateVars flag.\n";
}
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. :)
my $regex = qr{
(^|\G|[^\\]) # $1: can be the beginning of the line
# or the beginning of next match
# but can't begin with a '\'
\$ # dollar sign
(?: # Group alternative
(?: \{ ([^\}]*) \} ) # $2, anything in braces.
|
( [a-zA-Z_]\w* ) # $3: A traditional variable name
)
}x;
return $regex;
}
sub _interpolate {
#
# interpolate a scalar value and keep the result
# on the varstack.
#
# called directly by Config::General::_parse_value()
#
my ($this, $config, $key, $value) = @_;
# some dirty trick to circumvent single quoted vars to be interpolated
# Quoted chunks will end up in odd positions.
# Note, a trailing lone single quote will be treated as quoted,
# i.e., "a'b'c'd" will treat d as quoted.
my @values = split /'/,$value, -1; ## get trailing fields also.
for (my $i = 0; $i<=$#values; $i+=2) {
$values[$i] =~ s{$this->{regex}}{
my $con = $1;
my $var = defined($2) ? $2 : $3;
my $var_lc = $this->{LowerCaseNames} ? lc($var) : $var;
if (exists $config->{__stack}->{$var_lc}) {
$con . $config->{__stack}->{$var_lc};
}
elsif ($this->{InterPolateEnv}) {
# may lead to vulnerabilities, by default flag turned off
if (defined($ENV{$var})) {
$con . $ENV{$var};
}
else {
$con;
}
}
else {
if ($this->{StrictVars}) {
croak "Use of uninitialized variable (\$$var) while loading config entry: $key = $value\n";
}
else {
# be cool
$con;
}
}
}egx;
}
return join("'",@values);
};
sub _interpolate_hash {
#
# interpolate a complete hash and keep the results
# on the varstack.
#
# called directly by Config::General::new()
#
my ($this, $config) = @_;
# bugfix rt.cpan.org#46184, moved code from _interpolate() to here.
if ($this->{InterPolateEnv} && defined(%ENV)) {
# may lead to vulnerabilities, by default flag turned off
for my $key (keys %ENV){
$config->{__stack}->{$key}=$ENV{$key};
}
}
$config = $this->_var_hash_stacker($config);
return $config;
}
sub _var_hash_stacker {
#
# build a varstack of a given hash ref
#
my ($this, $config) = @_;
foreach my $key (keys %{$config}) {
next if($key eq "__stack");
if (ref($config->{$key}) eq "ARRAY" ) {
$config->{$key} = $this->_var_array_stacker($config->{$key}, $key);
}
elsif (ref($config->{$key}) eq "HASH") {
my $tmphash = $config->{$key};
$tmphash->{__stack} = $config->{__stack};
$config->{$key} = $this->_var_hash_stacker($tmphash);
}
else {
# SCALAR
$config->{__stack}->{$key} = $config->{$key};
}
}
return $config;
}
sub _var_array_stacker {
#
# same as _var_hash_stacker but for arrayrefs
#
my ($this, $config, $key) = @_;
my @new;
foreach my $entry (@{$config}) {
if (ref($entry) eq "HASH") {
$entry = $this->_var_hash_stacker($entry);
}
elsif (ref($entry) eq "ARRAY") {
# ignore this. Arrays of Arrays cannot be created/supported
# with Config::General, because they are not accessible by
# any key (anonymous array-ref)
next;
}
else {
#### $config->{__stack}->{$key} = $config->{$key};
# removed. a array of scalars (eg: option = [1,2,3]) cannot
# be used for interpolation (which one shall we use?!), so
# we ignore those types of lists.
# found by fbicknel, fixes rt.cpan.org#41570
}
push @new, $entry;
}
return \@new;
}
sub _clean_stack {
#
# recursively empty the variable stack
#
my ($this, $config) = @_;
#return $config; # DEBUG
foreach my $key (keys %{$config}) {
if ($key eq "__stack") {
delete $config->{__stack};
next;
}
if (ref($config->{$key}) eq "ARRAY" ) {
$config->{$key} = $this->_clean_array_stack($config->{$key});
}
elsif (ref($config->{$key}) eq "HASH") {
$config->{$key} = $this->_clean_stack($config->{$key});
}
}
return $config;
}
sub _clean_array_stack {
#
# same as _var_hash_stacker but for arrayrefs
#
my ($this, $config) = @_;
my @new;
foreach my $entry (@{$config}) {
if (ref($entry) eq "HASH") {
$entry = $this->_clean_stack($entry);
}
elsif (ref($entry) eq "ARRAY") {
# ignore this. Arrays of Arrays cannot be created/supported
# with Config::General, because they are not accessible by
# any key (anonymous array-ref)
next;
}
push @new, $entry;
}
return \@new;
}
1;
__END__
=head1 NAME
Config::General::Interpolated - Parse variables within Config files
=head1 SYNOPSIS
use Config::General;
$conf = new Config::General(
-ConfigFile => 'configfile',
-InterPolateVars => 1
);
=head1 DESCRIPTION
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.
=head1 VARIABLES
Variables can be defined everywhere in the config and can be used
afterwards as the value of an option. Variables cannot be used as
keys or as part of keys.
If you define a variable inside
a block or a named block then it is only visible within this block or
within blocks which are defined inside this block. Well - let's take a
look to an example:
# sample config which uses variables
basedir = /opt/ora
user = t_space
sys = unix
<table intern>
instance = INTERN
owner = $user # "t_space"
logdir = $basedir/log # "/opt/ora/log"
sys = macos
<procs>
misc1 = ${sys}_${instance} # macos_INTERN
misc2 = $user # "t_space"
</procs>
</table>
This will result in the following structure:
{
'basedir' => '/opt/ora',
'user' => 't_space'
'sys' => 'unix',
'table' => {
'intern' => {
'sys' => 'macos',
'logdir' => '/opt/ora/log',
'instance' => 'INTERN',
'owner' => 't_space',
'procs' => {
'misc1' => 'macos_INTERN',
'misc2' => 't_space'
}
}
}
As you can see, the variable B<sys> has been defined twice. Inside
the <procs> block a variable ${sys} has been used, which then were
interpolated into the value of B<sys> defined inside the <table>
block, not the sys variable one level above. If sys were not defined
inside the <table> block then the "global" variable B<sys> would have
been used instead with the value of "unix".
Variables inside double quotes will be interpolated, but variables
inside single quotes will B<not> interpolated. This is the same
behavior as you know of Perl itself.
In addition you can surround variable names with curly braces to
avoid misinterpretation by the parser.
=head1 SEE ALSO
L<Config::General>
=head1 AUTHORS
Thomas Linden <tlinden |AT| cpan.org>
Autrijus Tang <autrijus@autrijus.org>
Wei-Hon Chen <plasmaball@pchome.com.tw>
=head1 COPYRIGHT
Copyright 2001 by Wei-Hon Chen E<lt>plasmaball@pchome.com.twE<gt>.
Copyright 2002-2009 by Thomas Linden <tlinden |AT| cpan.org>.
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
2.11
=cut