- added contributed sub module Config::General::Interpolated
	   by "Wei-Hon Chen" <plasmaball@pchome.com.tw> with
	   help from "Autrijus Tang" <autrijus@autrijus.org>
	   which makes it possible to use variables inside
	   config files.
	 - _read() accepts now c-comments inside c-comments if
	   they are on a single line.
	 - _read() is now more tolerant to here-identifiers
	   (the ends of here-docs), whitespaces right after
	   such an identifier are allowed (i.e. "EOF  ").
	 - _read() does now behave somewhat different with
	   C-comments, they will be the first thing being
	   processed in a config, so the parser really
	   ignores everything inside C-comments. Previously
	   it did not do that, for example here-docs has
	   not been ignored.


git-svn-id: http://dev.catalyst.perl.org/repos/Config-General/trunk@16 be1acefe-a474-0410-9a34-9b3221f2030f
This commit is contained in:
Thomas von Dein
2009-10-10 16:14:18 +00:00
parent 2c5ee3d876
commit b906f49050
4 changed files with 272 additions and 35 deletions

View File

@@ -17,7 +17,7 @@ use FileHandle;
use strict;
use Carp;
$Config::General::VERSION = "1.27";
$Config::General::VERSION = "1.28";
sub new {
#
@@ -157,18 +157,39 @@ sub _read {
foreach (@stuff) {
chomp;
# patch by "Manuel Valente" <manuel@ripe.net>:
if (!$hierend) {
if (/(\s*\/\*.*\*\/\s*)/) {
# single c-comment on one line
s/\s*\/\*.*\*\/\s*//;
}
elsif (/^\s*\/\*/) { # the beginning of a C-comment ("/*"), from now on ignore everything.
if (/\*\/\s*$/) { # C-comment end is already there, so just ignore this line!
$c_comment = 0;
}
else {
$c_comment = 1;
}
}
elsif (/\*\//) {
if (!$c_comment) {
warn "invalid syntax: found end of C-comment without previous start!\n";
}
$c_comment = 0; # the current C-comment ends here, go on
s/^.*\*\///; # if there is still stuff, it will be read
}
next if($c_comment); # ignore EVERYTHING from now on
if (!$hierend) { # patch by "Manuel Valente" <manuel@ripe.net>:
s/(?<!\\)#.+$//; # Remove comments
next if /^#/; # Remove lines beginning with "#"
next if /^\s*$/; # Skip empty lines
s/\\#/#/g; # remove the \ char in front of masked "#"
}
if (/^\s*(\S+?)(\s*=\s*|\s+)<<(.+?)$/) { # we are @ the beginning of a here-doc
$hier = $1; # $hier is the actual here-doc
$hier = $1; # $hier is the actual here-doc
$hierend = $3; # the here-doc end string, i.e. "EOF"
}
elsif (defined $hierend && /^(\s*)\Q$hierend\E$/) { # the current here-doc ends here
elsif (defined $hierend && /^(\s*)\Q$hierend\E\s*$/) { # the current here-doc ends here (allow spaces)
my $indent = $1; # preserve indentation
$hier .= " " . chr(182); # append a "<22>" to the here-doc-name, so _parse will also preserver indentation
if ($indent) {
@@ -185,29 +206,16 @@ sub _read {
undef $hier;
undef $hierend;
}
elsif (/^\s*\/\*/) { # the beginning of a C-comment ("/*"), from now on ignore everything.
if (/\*\/\s*$/) { # C-comment end is already there, so just ignore this line!
$c_comment = 0;
}
else {
$c_comment = 1;
}
}
elsif (/\*\//) {
if (!$c_comment) {
warn "invalid syntax: found end of C-comment without previous start!\n";
}
$c_comment = 0; # the current C-comment ends here, go on
}
elsif (/\\$/) { # a multiline option, indicated by a trailing backslash
chop;
s/^\s*//;
$longline .= $_ if(!$c_comment); # store in $longline
$longline .= $_; # store in $longline
}
else { # any "normal" config lines
if ($longline) { # previous stuff was a longline and this is the last line of the longline
s/^\s*//;
$longline .= $_ if(!$c_comment);
$longline .= $_;
push @{$this->{content}}, $longline; # push it onto the content stack
undef $longline;
}
@@ -216,23 +224,21 @@ sub _read {
}
else {
# look for include statement(s)
if (!$c_comment) {
my $incl_file;
if (/^\s*<<include (.+?)>>\s*$/i || (/^\s*include (.+?)\s*$/i && $this->{UseApacheInclude})) {
$incl_file = $1;
if ($this->{IncludeRelative} && $this->{configpath} && $incl_file !~ /^\//) {
# include the file from within location of $this->{configfile}
$this->_open($this->{configpath} . "/" . $incl_file);
}
else {
# include the file from within pwd, or absolute
$this->_open($incl_file);
}
my $incl_file;
if (/^\s*<<include (.+?)>>\s*$/i || (/^\s*include (.+?)\s*$/i && $this->{UseApacheInclude})) {
$incl_file = $1;
if ($this->{IncludeRelative} && $this->{configpath} && $incl_file !~ /^\//) {
# include the file from within location of $this->{configfile}
$this->_open($this->{configpath} . "/" . $incl_file);
}
else {
push @{$this->{content}}, $_;
# include the file from within pwd, or absolute
$this->_open($incl_file);
}
}
else {
push @{$this->{content}}, $_;
}
}
}
}
@@ -979,6 +985,11 @@ There is a way to access a parsed config the OO-way.
Use the module B<Config::General::Extended>, which is
supplied with the Config::General distribution.
=head1 VARIABLE INTERPOLATION
You can use variables inside your configfiles if you like. To do
that you have to use the module B<Config::General::Interpolated>,
which is supplied with the Config::General distribution.
=head1 SEE ALSO
@@ -1011,7 +1022,7 @@ Thomas Linden <tom@daemon.de>
=head1 VERSION
1.27
1.28
=cut