From 035146a3ca31411488f684bee10fdd30d32ba7c3 Mon Sep 17 00:00:00 2001 From: Father Chrysostomos Date: Tue, 6 Dec 2011 23:07:49 -0800 Subject: [PATCH] Deparse funny keys/values in hh properly MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Simply putting '...' or q(...) around a string is not sufficient to quote it properly, allowing this sort of ‘fun’: $ perl -l -MO=Deparse -e'BEGIN{$^H{"'\''.'\''"} = "um);\n exit("}print' BEGIN { $/ = "\n"; $\ = "\n"; } sub BEGIN { $^H{q['.']} = "um);\n exit("; } BEGIN { $^H{''.''} = q(um); exit(); } print $_; -e syntax OK And B::Deparse already has a facility for quoting strings, so why not use it? --- dist/B-Deparse/Deparse.pm | 9 +++++++-- dist/B-Deparse/t/deparse.t | 2 ++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/dist/B-Deparse/Deparse.pm b/dist/B-Deparse/Deparse.pm index 24d17af..c7bdec4 100644 --- a/dist/B-Deparse/Deparse.pm +++ b/dist/B-Deparse/Deparse.pm @@ -1511,8 +1511,13 @@ sub declare_hinthash { for my $key (keys %$to) { next if $ignored_hints{$key}; if (!exists $from->{$key} or $from->{$key} ne $to->{$key}) { - push @decls, qq(\$^H{'$key'} = ) - . (defined $to->{$key} ? qq(q($to->{$key})) : 'undef') + push @decls, + qq(\$^H{) . single_delim("q", "'", $key) . qq(} = ) + . ( + defined $to->{$key} + ? single_delim("q", "'", $to->{$key}) + : 'undef' + ) . qq(;); } } diff --git a/dist/B-Deparse/t/deparse.t b/dist/B-Deparse/t/deparse.t index 7b11ccf..d47498c 100644 --- a/dist/B-Deparse/t/deparse.t +++ b/dist/B-Deparse/t/deparse.t @@ -807,3 +807,5 @@ BEGIN { $^H{'foo'} = undef; } } print $_; } +BEGIN { $^H{q[']} = '('; } +print $_; -- 2.7.4