/usr/share/perl5/Config/General/Interpolated.pm is in libconfig-general-perl 2.63-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 | #
# Config::General::Interpolated - special Class based on Config::General
#
# Copyright (c) 2001 by Wei-Hon Chen <plasmaball@pchome.com.tw>.
# Copyright (c) 2000-2014 by Thomas Linden <tlinden |AT| cpan.org>.
# All Rights Reserved. Std. disclaimer applies.
# Artistic License, same as perl itself. Have fun.
#
package Config::General::Interpolated;
$Config::General::Interpolated::VERSION = "2.15";
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
(\{)? # $2: optional opening curly
([a-zA-Z0-9_\-\.:\+,]+) # $3: capturing variable name (fix of #33447)
(?(2) # $4: if there's the opening curly...
\} # ... match closing curly
)
}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) = @_;
my $quote_counter = 100;
# some dirty trick to circumvent single quoted vars to be interpolated
# we remove all quotes and replace them with unique random literals,
# which will be replaced after interpolation with the original quotes
# fixes bug rt#35766
my %quotes;
if(! $this->{AllowSingleQuoteInterpolation} ) {
$value =~ s/(\'[^\']+?\')/
my $key = "QUOTE" . ($quote_counter++) . "QUOTE";
$quotes{ $key } = $1;
$key;
/gex;
}
$value =~ s{$this->{regex}}{
my $con = $1;
my $var = $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;
}
}
elsif ($this->{StrictVars}) {
croak "Use of uninitialized variable (\$$var) while loading config entry: $key = $value\n";
}
else {
# be cool
$con;
}
}egx;
# re-insert unaltered quotes
# fixes bug rt#35766
foreach my $quote (keys %quotes) {
$value =~ s/$quote/$quotes{$quote}/;
}
return $value;
};
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}) {
# 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 = Config::General->new(
-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-2014 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.15
=cut
|