From 3937d2423ec91d89b323de27b13667185a52a89e Mon Sep 17 00:00:00 2001 From: Thomas von Dein Date: Tue, 7 Jan 2025 10:36:02 +0100 Subject: [PATCH] add new flag AlwaysQuoteOutput for write quoting --- General.pm | 12 +++++++++--- t/cfg.59 | 1 + t/run.t | 11 ++++++++++- 3 files changed, 20 insertions(+), 4 deletions(-) create mode 100644 t/cfg.59 diff --git a/General.pm b/General.pm index e9f19c3..726de63 100644 --- a/General.pm +++ b/General.pm @@ -32,7 +32,7 @@ use Carp::Heavy; use Carp; use Exporter; -$Config::General::VERSION = "2.65"; +$Config::General::VERSION = "2.66"; use base qw(Exporter); our @EXPORT_OK = qw(ParseConfig SaveConfig SaveConfigString); @@ -96,7 +96,8 @@ sub new { NormalizeValue => 0, Plug => {}, UseApacheIfDefine => 0, - Define => {} + Define => {}, + AlwaysQuoteOutput => 0 }; # create the class instance @@ -1424,7 +1425,7 @@ sub _write_scalar { $line =~ s/([#\$\\\"])/\\$1/g; } - if ($line =~ /\s/) { + if ($line =~ /^\s/ || $line =~ /\s$/ || $this->{AlwaysQuoteOutput}) { # quote lines containing whitespace $line = "\"$line\""; } @@ -2119,6 +2120,11 @@ Same as B<-NormalizeBlock> but applied on options only. Same as B<-NormalizeBlock> but applied on values only. +=item B<-AlwaysQuoteOutput> + +If set to true, then values containing whitespace will always quoted +when calling C or C. + =back diff --git a/t/cfg.59 b/t/cfg.59 new file mode 100644 index 0000000..2ce4857 --- /dev/null +++ b/t/cfg.59 @@ -0,0 +1 @@ +foo = "bar baz" diff --git a/t/run.t b/t/run.t index 4dbf233..26d6120 100644 --- a/t/run.t +++ b/t/run.t @@ -8,7 +8,7 @@ use Data::Dumper; -use Test::More tests => 78; +use Test::More tests => 79; #use Test::More qw(no_plan); # ahem, we deliver the test code with a local copy of @@ -791,3 +791,12 @@ foreach my $def (keys %defs) { my %hash = $conf->getall(); is_deeply \%hash, $expected58, "UseApacheIfDefine, -Define => $def"; } + +# force quoting +my $cfg59 = "t/cfg.59"; +my $expected59 = qq(foo "bar baz" +); # newline is important here, as we check write output +my $conf = Config::General->new(-ConfigFile => $cfg59, + -AlwaysQuoteOutput => 1); +my $got59 = $conf->save_string(); +is_deeply \$expected59, \$got59, "quotes";