From: Father Chrysostomos Date: Wed, 7 Dec 2011 07:07:49 +0000 (-0800) Subject: Deparse funny keys/values in hh properly X-Git-Tag: accepted/trunk/20130322.191538~1840 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=035146a3ca31411488f684bee10fdd30d32ba7c3;p=platform%2Fupstream%2Fperl.git Deparse funny keys/values in hh properly 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? --- 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 $_;