From c395e5d596ac2264ac6a2626c0a55754a5613191 Mon Sep 17 00:00:00 2001 From: Thomas von Dein Date: Sat, 10 Oct 2009 16:08:33 +0000 Subject: [PATCH] 1.19: - you can escape "#" characters using a backslash: "\#" which will now no more treated as a comment. - comments inside here-documents will now remain in the here-doc value. git-svn-id: http://dev.catalyst.perl.org/repos/Config-General/trunk@7 be1acefe-a474-0410-9a34-9b3221f2030f --- General.pm | 43 +++++++++++++++++++++++++++++++------------ README | 19 ++----------------- t/cfg.2 | 5 ++++- t/cfg.4 | 1 + t/cfg.5 | 4 ++-- t/cfg.7 | 2 +- 6 files changed, 41 insertions(+), 33 deletions(-) diff --git a/General.pm b/General.pm index c3306a0..e2c66dd 100644 --- a/General.pm +++ b/General.pm @@ -10,6 +10,10 @@ # All Rights Reserved. Std. disclaimer applies. # Artificial License, same as perl itself. Have fun. # +# Changes from 1.18: - you can escape "#" characters using a backslash: "\#" +# which will now no more treated as a comment. +# - comments inside here-documents will now remain in the +# here-doc value. # namespace package Config::General; @@ -18,7 +22,7 @@ use FileHandle; use strict; use Carp; -$Config::General::VERSION = "1.18"; +$Config::General::VERSION = "1.19"; sub new { # @@ -95,9 +99,12 @@ sub _open { open $fh, "<$configfile" or croak "Could not open $configfile!($!)\n"; while (<$fh>) { chomp; - next if (/^\s*$/ || /^\s*#/); # ignore whitespace(s) and lines beginning with # - if (/^([^#]+?)#/) { - $_ = $1; # remove trailing comment + + # patch by "Manuel Valente" : + if (!$hierend) { + s/(?{content}}, $longline; # push it onto the content stack undef $longline; @@ -185,8 +192,8 @@ sub _parse { my ($option,$value) = split /\s*=\s*|\s+/, $_, 2; # option/value assignment, = is optional my $indichar = chr(182); # ¶, inserted by _open, our here-doc indicator - $value =~ s/^$indichar// if($value); # a here-doc begin, remove indicator - $value =~ s/^"// if($value); # remove leading and trailing " + $value =~ s/^$indichar// if($value); # a here-doc begin, remove indicator + $value =~ s/^"// if($value); # remove leading and trailing " $value =~ s/"$// if($value); if (!$block) { # not inside a block @ the moment if (/^<([^\/]+?.*?)>$/) { # look if it is a block @@ -296,6 +303,7 @@ sub _store { foreach my $entry (sort keys %config) { if (ref($config{$entry}) eq "ARRAY") { foreach my $line (@{$config{$entry}}) { + $line =~ s/#/\\#/g; print $fh $indent . $entry . " " . $line . "\n"; } } @@ -316,6 +324,7 @@ sub _store { print $fh $indent . "EOF\n"; } else { + $config{$entry} =~ s/#/\\#/g; print $fh $indent . $entry . " " . $config{$entry} . "\n"; } } @@ -690,13 +699,23 @@ Example: */ In this example the second options of user and db will be ignored. Please beware of the fact, -the if the Module finds a B string which is the start of a comment block, but no matching +if the Module finds a B string which is the start of a comment block, but no matching end block, it will ignore the whole rest of the config file! +B If you require the B<#> character (number sign) to remain in the option value, then +you can use a backlsash in front of it, to escape it. Example: + + bgcolor = \#ffffcc + +In this example the value of $config{bgcolor} will be "#ffffcc", Config::General will not treat +the number sign as the begin of a comment because of the leading backslash. + +Inside here-documents escaping of number signs is NOT required! + =head1 COPYRIGHT -Copyright (c) 2000 Thomas Linden +Copyright (c) 2000-2001 Thomas Linden This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. @@ -714,7 +733,7 @@ Thomas Linden =head1 VERSION -1.18 +1.19 =cut diff --git a/README b/README index 0925156..b3298d4 100644 --- a/README +++ b/README @@ -44,22 +44,7 @@ INSTALLATION COPYRIGHT - Copyright (c) 2000 Thomas Linden - - This library is free software; you can redistribute it - and/or modify it under the same terms as Perl itself. - - -BUGS - none known yet. - - -AUTHOR - Thomas Linden - - -COPYRIGHT - Copyright (c) 2000 Thomas Linden + Copyright (c) 2000-2001 Thomas Linden This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. @@ -74,4 +59,4 @@ AUTHOR VERSION - 1.17 + 1.19 diff --git a/t/cfg.2 b/t/cfg.2 index f222b7c..afdef44 100644 --- a/t/cfg.2 +++ b/t/cfg.2 @@ -1,11 +1,14 @@ # Nested block test + name stein age 25 + color \#000000 name bird age 31 + color \#ffffff - \ No newline at end of file + diff --git a/t/cfg.4 b/t/cfg.4 index a2068da..546cfcf 100644 --- a/t/cfg.4 +++ b/t/cfg.4 @@ -3,4 +3,5 @@ message <