develooper Front page | perl.perl5.porters | Postings from July 2001

[PATCH B::Deparse] hash key auto-quoting

Thread Next
From:
Robin Houston
Date:
July 2, 2001 15:02
Subject:
[PATCH B::Deparse] hash key auto-quoting
Message ID:
E15HBmR-0006mc-00.2001-07-02-23-02-55@mail18.svr.pol.co.uk
B::Deparse would incorrectly deparse code like C<$hash{warn()}> or
C<$hash{+warn}> as C<$hash{warn}>, which isn't the same thing at all.
The patch below should fix this problem.

Thanks to Richard Soderberg for telling me about this problem.

  .robin.

PS. I hope the patch isn't mangled by this mailer. Does anyone have
     a port of mutt to darwin?

--- ext/B/B/Deparse.pm.one	Mon Jul  2 21:34:06 2001
+++ ext/B/B/Deparse.pm	Mon Jul  2 21:49:08 2001
@@ -2668,6 +2668,16 @@
      # expression would be parenthesized as well.]
      #
      $idx =~ s/^\((.*)\)$/$1/ if $self->{'parens'};
+
+    # Hash-element braces will autoquote a bareword inside themselves.
+    # We need to make sure that C<$hash{warn()}> doesn't come out as
+    # C<$hash{warn}>, which has a quite different meaning. Currently
+    # B::Deparse will always quote strings, even if the string was a
+    # bareword in the original (i.e. the OPpCONST_BARE flag is ignored
+    # for constant strings.) So we can cheat slightly here - if we see
+    # a bareword, we know that it is supposed to be a function call.
+    #
+    $idx =~ s/^([A-Za-z_]\w*)$/$1()/;

      return "\$" . $array . $left . $idx . $right;
  }

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About