Consolidated B::Deparse fixes (from Stephen McCamant)
[p5sagit/p5-mst-13.2.git] / ext / B / B / Deparse.pm
index b983d12..5c0be87 100644 (file)
@@ -1,5 +1,5 @@
 # B::Deparse.pm
-# Copyright (c) 1998,1999 Stephen McCamant. All rights reserved.
+# Copyright (c) 1998, 1999, 2000 Stephen McCamant. All rights reserved.
 # This module is free software; you can redistribute and/or modify
 # it under the same terms as Perl itself.
 
@@ -12,11 +12,11 @@ use B qw(class main_root main_start main_cv svref_2object opnumber
         OPf_WANT OPf_WANT_VOID OPf_WANT_SCALAR OPf_WANT_LIST
         OPf_KIDS OPf_REF OPf_STACKED OPf_SPECIAL
         OPpLVAL_INTRO OPpENTERSUB_AMPER OPpSLICE OPpCONST_BARE
-        OPpTRANS_SQUASH OPpTRANS_DELETE OPpTRANS_COMPLEMENT
+        OPpTRANS_SQUASH OPpTRANS_DELETE OPpTRANS_COMPLEMENT OPpTARGET_MY
         SVf_IOK SVf_NOK SVf_ROK SVf_POK
         PMf_KEEP PMf_GLOBAL PMf_CONTINUE PMf_EVAL PMf_ONCE
         PMf_MULTILINE PMf_SINGLELINE PMf_FOLD PMf_EXTENDED);
-$VERSION = 0.58;
+$VERSION = 0.591;
 use strict;
 
 # Changes between 0.50 and 0.51:
@@ -75,6 +75,13 @@ use strict;
 # - added -si and -sT to control indenting (also based on a patch from Hugo)
 # - added -sv to print something else instead of '???'
 # - preliminary version of utf8 tr/// handling
+# Changes after 0.58:
+# - uses of $op->ppaddr changed to new $op->name (done by Sarathy)
+# - added support for Hugo's new OP_SETSTATE (like nextstate) 
+# Changes between 0.58 and 0.59
+# - added support for Chip's OP_METHOD_NAMED
+# - added support for Ilya's OPpTARGET_MY optimization
+# - elided arrows before `()' subscripts when possible
 
 # Todo:
 # - finish tr/// changes
@@ -86,7 +93,7 @@ use strict;
 # - recognize `use utf8', `use integer', etc
 # - treat top-level block specially for incremental output
 # - interpret in high bit chars in string as utf8 \x{...} (when?)
-# - copy comments (look at real text with $^P) 
+# - copy comments (look at real text with $^P?) 
 # - avoid semis in one-statement blocks
 # - associativity of &&=, ||=, ?:
 # - ',' => '=>' (auto-unquote?)
@@ -94,6 +101,7 @@ use strict;
 # - configurable syntax highlighting: ANSI color, HTML, TeX, etc.
 # - more style options: brace style, hex vs. octal, quotes, ...
 # - print big ints as hex/octal instead of decimal (heuristic?)
+# - handle `my $x if 0'?
 # - include values of variables (e.g. set in BEGIN)
 # - coordinate with Data::Dumper (both directions? see previous)
 # - version using op_next instead of op_first/sibling?
@@ -219,8 +227,7 @@ sub next_todo {
        return "format $name =\n"
            . $self->deparse_format($ent->[1]->FORM). "\n";
     } else {
-       return "sub $name " .
-           $self->deparse_sub($ent->[1]->CV);
+       return "sub $name " . $self->deparse_sub($ent->[1]->CV);
     }
 }
 
@@ -244,18 +251,19 @@ sub walk_sub {
     walk_tree($op, sub {
        my $op = shift;
        if ($op->name eq "gv") {
+           my $gv = $self->gv_or_padgv($op);
            if ($op->next->name eq "entersub") {
-               next if $self->{'subs_done'}{$ {$op->gv}}++;
-               next if class($op->gv->CV) eq "SPECIAL";
-               $self->todo($op->gv, $op->gv->CV, 0);
-               $self->walk_sub($op->gv->CV);
+               return if $self->{'subs_done'}{$$gv}++;
+               return if class($gv->CV) eq "SPECIAL";
+               $self->todo($gv, $gv->CV, 0);
+               $self->walk_sub($gv->CV);
            } elsif ($op->next->name eq "enterwrite"
                     or ($op->next->name eq "rv2gv"
                         and $op->next->next->name eq "enterwrite")) {
-               next if $self->{'forms_done'}{$ {$op->gv}}++;
-               next if class($op->gv->FORM) eq "SPECIAL";
-               $self->todo($op->gv, $op->gv->FORM, 1);
-               $self->walk_sub($op->gv->FORM);
+               return if $self->{'forms_done'}{$$gv}++;
+               return if class($gv->FORM) eq "SPECIAL";
+               $self->todo($gv, $gv->FORM, 1);
+               $self->walk_sub($gv->FORM);
            }
        }
     });
@@ -369,7 +377,7 @@ sub compile {
        while (scalar(@{$self->{'subs_todo'}})) {
            push @text, $self->next_todo;
        }
-       print indent(join("", @text)), "\n" if @text;
+       print $self->indent(join("", @text)), "\n" if @text;
     }
 }
 
@@ -448,7 +456,7 @@ sub deparse_format {
        $op = $op->sibling; # skip nextstate
        my @exprs;
        $kid = $op->first->sibling; # skip pushmark
-       push @text, $kid->sv->PV;
+       push @text, $self->const_sv($kid)->PV;
        $kid = $kid->sibling;
        for (; not null $kid; $kid = $kid->sibling) {
            push @exprs, $self->deparse($kid, 0);
@@ -550,6 +558,18 @@ sub maybe_local {
     }
 }
 
+sub maybe_targmy {
+    my $self = shift;
+    my($op, $cx, $func, @args) = @_;
+    if ($op->private & OPpTARGET_MY) {
+       my $var = $self->padname($op->targ);
+       my $val = $func->($self, $op, 7, @args);
+       return $self->maybe_parens("$var = $val", $cx, 7);
+    } else {
+       return $func->($self, $op, $cx, @args);
+    }
+}
+
 sub padname_sv {
     my $self = shift;
     my $targ = shift;
@@ -751,14 +771,14 @@ sub pp_nextstate {
           and $seq > $self->{'subs_todo'}[0][0]) {
        push @text, $self->next_todo;
     }
-    my $stash = $op->stash->NAME;
+    my $stash = $op->stashpv;
     if ($stash ne $self->{'curstash'}) {
        push @text, "package $stash;\n";
        $self->{'curstash'} = $stash;
     }
     if ($self->{'linenums'}) {
        push @text, "\f#line " . $op->line . 
-         ' "' . substr($op->filegv->NAME, 2), qq'"\n';
+         ' "' . $op->file, qq'"\n';
     }
     return join("", @text);
 }
@@ -777,9 +797,9 @@ sub baseop {
 sub pp_stub { baseop(@_, "()") }
 sub pp_wantarray { baseop(@_, "wantarray") }
 sub pp_fork { baseop(@_, "fork") }
-sub pp_wait { baseop(@_, "wait") }
-sub pp_getppid { baseop(@_, "getppid") }
-sub pp_time { baseop(@_, "time") }
+sub pp_wait { maybe_targmy(@_, \&baseop, "wait") }
+sub pp_getppid { maybe_targmy(@_, \&baseop, "getppid") }
+sub pp_time { maybe_targmy(@_, \&baseop, "time") }
 sub pp_tms { baseop(@_, "times") }
 sub pp_ghostent { baseop(@_, "gethostent") }
 sub pp_gnetent { baseop(@_, "getnetent") }
@@ -813,15 +833,16 @@ sub pfixop {
 
 sub pp_preinc { pfixop(@_, "++", 23) }
 sub pp_predec { pfixop(@_, "--", 23) }
-sub pp_postinc { pfixop(@_, "++", 23, POSTFIX) }
-sub pp_postdec { pfixop(@_, "--", 23, POSTFIX) }
+sub pp_postinc { maybe_targmy(@_, \&pfixop, "++", 23, POSTFIX) }
+sub pp_postdec { maybe_targmy(@_, \&pfixop, "--", 23, POSTFIX) }
 sub pp_i_preinc { pfixop(@_, "++", 23) }
 sub pp_i_predec { pfixop(@_, "--", 23) }
-sub pp_i_postinc { pfixop(@_, "++", 23, POSTFIX) }
-sub pp_i_postdec { pfixop(@_, "--", 23, POSTFIX) }
-sub pp_complement { pfixop(@_, "~", 21) }
+sub pp_i_postinc { maybe_targmy(@_, \&pfixop, "++", 23, POSTFIX) }
+sub pp_i_postdec { maybe_targmy(@_, \&pfixop, "--", 23, POSTFIX) }
+sub pp_complement { maybe_targmy(@_. \&pfixop, "~", 21) }
 
-sub pp_negate {
+sub pp_negate { maybe_targmy(@_, \&real_negate) }
+sub real_negate {
     my $self = shift;
     my($op, $cx) = @_;
     if ($op->first->name =~ /^(i_)?negate$/) {
@@ -855,31 +876,31 @@ sub unop {
     }
 }
 
-sub pp_chop { unop(@_, "chop") }
-sub pp_chomp { unop(@_, "chomp") }
-sub pp_schop { unop(@_, "chop") }
-sub pp_schomp { unop(@_, "chomp") }
+sub pp_chop { maybe_targmy(@_, \&unop, "chop") }
+sub pp_chomp { maybe_targmy(@_, \&unop, "chomp") }
+sub pp_schop { maybe_targmy(@_, \&unop, "chop") }
+sub pp_schomp { maybe_targmy(@_, \&unop, "chomp") }
 sub pp_defined { unop(@_, "defined") }
 sub pp_undef { unop(@_, "undef") }
 sub pp_study { unop(@_, "study") }
 sub pp_ref { unop(@_, "ref") }
 sub pp_pos { maybe_local(@_, unop(@_, "pos")) }
 
-sub pp_sin { unop(@_, "sin") }
-sub pp_cos { unop(@_, "cos") }
-sub pp_rand { unop(@_, "rand") }
+sub pp_sin { maybe_targmy(@_, \&unop, "sin") }
+sub pp_cos { maybe_targmy(@_, \&unop, "cos") }
+sub pp_rand { maybe_targmy(@_, \&unop, "rand") }
 sub pp_srand { unop(@_, "srand") }
-sub pp_exp { unop(@_, "exp") }
-sub pp_log { unop(@_, "log") }
-sub pp_sqrt { unop(@_, "sqrt") }
-sub pp_int { unop(@_, "int") }
-sub pp_hex { unop(@_, "hex") }
-sub pp_oct { unop(@_, "oct") }
-sub pp_abs { unop(@_, "abs") }
-
-sub pp_length { unop(@_, "length") }
-sub pp_ord { unop(@_, "ord") }
-sub pp_chr { unop(@_, "chr") }
+sub pp_exp { maybe_targmy(@_, \&unop, "exp") }
+sub pp_log { maybe_targmy(@_, \&unop, "log") }
+sub pp_sqrt { maybe_targmy(@_, \&unop, "sqrt") }
+sub pp_int { maybe_targmy(@_, \&unop, "int") }
+sub pp_hex { maybe_targmy(@_, \&unop, "hex") }
+sub pp_oct { maybe_targmy(@_, \&unop, "oct") }
+sub pp_abs { maybe_targmy(@_, \&unop, "abs") }
+
+sub pp_length { maybe_targmy(@_, \&unop, "length") }
+sub pp_ord { maybe_targmy(@_, \&unop, "ord") }
+sub pp_chr { maybe_targmy(@_, \&unop, "chr") }
 
 sub pp_each { unop(@_, "each") }
 sub pp_values { unop(@_, "values") }
@@ -905,19 +926,19 @@ sub pp_tell { unop(@_, "tell") }
 sub pp_getsockname { unop(@_, "getsockname") }
 sub pp_getpeername { unop(@_, "getpeername") }
 
-sub pp_chdir { unop(@_, "chdir") }
-sub pp_chroot { unop(@_, "chroot") }
+sub pp_chdir { maybe_targmy(@_, \&unop, "chdir") }
+sub pp_chroot { maybe_targmy(@_, \&unop, "chroot") }
 sub pp_readlink { unop(@_, "readlink") }
-sub pp_rmdir { unop(@_, "rmdir") }
+sub pp_rmdir { maybe_targmy(@_, \&unop, "rmdir") }
 sub pp_readdir { unop(@_, "readdir") }
 sub pp_telldir { unop(@_, "telldir") }
 sub pp_rewinddir { unop(@_, "rewinddir") }
 sub pp_closedir { unop(@_, "closedir") }
-sub pp_getpgrp { unop(@_, "getpgrp") }
+sub pp_getpgrp { maybe_targmy(@_, \&unop, "getpgrp") }
 sub pp_localtime { unop(@_, "localtime") }
 sub pp_gmtime { unop(@_, "gmtime") }
 sub pp_alarm { unop(@_, "alarm") }
-sub pp_sleep { unop(@_, "sleep") }
+sub pp_sleep { maybe_targmy(@_, \&unop, "sleep") }
 
 sub pp_dofile { unop(@_, "do") }
 sub pp_entereval { unop(@_, "eval") }
@@ -964,7 +985,7 @@ sub pp_require {
     if (class($op) eq "UNOP" and $op->first->name eq "const"
        and $op->first->private & OPpCONST_BARE)
     {
-       my $name = $op->first->sv->PV;
+       my $name = $self->const_sv($op->first)->PV;
        $name =~ s[/][::]g;
        $name =~ s/\.pm//g;
        return "require($name)";
@@ -988,6 +1009,7 @@ sub pp_scalar {
 sub padval {
     my $self = shift;
     my $targ = shift;
+    #cluck "curcv was undef" unless $self->{curcv};
     return (($self->{'curcv'}->PADLIST->ARRAY)[1]->ARRAY)[$targ];
 }
 
@@ -1060,7 +1082,7 @@ sub pp_ucfirst { dq_unop(@_, "ucfirst") }
 sub pp_lcfirst { dq_unop(@_, "lcfirst") }
 sub pp_uc { dq_unop(@_, "uc") }
 sub pp_lc { dq_unop(@_, "lc") }
-sub pp_quotemeta { dq_unop(@_, "quotemeta") }
+sub pp_quotemeta { maybe_targmy(@_, \&dq_unop, "quotemeta") }
 
 sub loopex {
     my $self = shift;
@@ -1089,7 +1111,7 @@ sub ftst {
        # Genuine `-X' filetests are exempt from the LLAFR, but not
        # l?stat(); for the sake of clarity, give'em all parens
        return $self->maybe_parens_unop($name, $op->first, $cx);
-    } elsif (class($op) eq "GVOP") {
+    } elsif (class($op) eq "SVOP") {
        return $self->maybe_parens_func($name, $self->pp_gv($op, 1), $cx, 16);
     } else { # I don't think baseop filetests ever survive ck_ftst, but...
        return $name;
@@ -1174,7 +1196,7 @@ BEGIN {
 sub deparse_binop_left {
     my $self = shift;
     my($op, $left, $prec) = @_;
-    if ($left{assoc_class($op)}
+    if ($left{assoc_class($op)} && $left{assoc_class($left)}
        and $left{assoc_class($op)} == $left{assoc_class($left)})
     {
        return $self->deparse($left, $prec - .00001);
@@ -1207,7 +1229,7 @@ BEGIN {
 sub deparse_binop_right {
     my $self = shift;
     my($op, $right, $prec) = @_;
-    if ($right{assoc_class($op)}
+    if ($right{assoc_class($op)} && $right{assoc_class($right)}
        and $right{assoc_class($op)} == $right{assoc_class($right)})
     {
        return $self->deparse($right, $prec - .00001);
@@ -1234,23 +1256,23 @@ sub binop {
     return $self->maybe_parens("$left $opname$eq $right", $cx, $prec);
 }
 
-sub pp_add { binop(@_, "+", 18, ASSIGN) }
-sub pp_multiply { binop(@_, "*", 19, ASSIGN) }
-sub pp_subtract { binop(@_, "-",18,  ASSIGN) }
-sub pp_divide { binop(@_, "/", 19, ASSIGN) }
-sub pp_modulo { binop(@_, "%", 19, ASSIGN) }
-sub pp_i_add { binop(@_, "+", 18, ASSIGN) }
-sub pp_i_multiply { binop(@_, "*", 19, ASSIGN) }
-sub pp_i_subtract { binop(@_, "-", 18, ASSIGN) }
-sub pp_i_divide { binop(@_, "/", 19, ASSIGN) }
-sub pp_i_modulo { binop(@_, "%", 19, ASSIGN) }
-sub pp_pow { binop(@_, "**", 22, ASSIGN) }
-
-sub pp_left_shift { binop(@_, "<<", 17, ASSIGN) }
-sub pp_right_shift { binop(@_, ">>", 17, ASSIGN) }
-sub pp_bit_and { binop(@_, "&", 13, ASSIGN) }
-sub pp_bit_or { binop(@_, "|", 12, ASSIGN) }
-sub pp_bit_xor { binop(@_, "^", 12, ASSIGN) }
+sub pp_add { maybe_targmy(@_, \&binop, "+", 18, ASSIGN) }
+sub pp_multiply { maybe_targmy(@_, \&binop, "*", 19, ASSIGN) }
+sub pp_subtract { maybe_targmy(@_, \&binop, "-",18,  ASSIGN) }
+sub pp_divide { maybe_targmy(@_, \&binop, "/", 19, ASSIGN) }
+sub pp_modulo { maybe_targmy(@_, \&binop, "%", 19, ASSIGN) }
+sub pp_i_add { maybe_targmy(@_, \&binop, "+", 18, ASSIGN) }
+sub pp_i_multiply { maybe_targmy(@_, \&binop, "*", 19, ASSIGN) }
+sub pp_i_subtract { maybe_targmy(@_, \&binop, "-", 18, ASSIGN) }
+sub pp_i_divide { maybe_targmy(@_, \&binop, "/", 19, ASSIGN) }
+sub pp_i_modulo { maybe_targmy(@_, \&binop, "%", 19, ASSIGN) }
+sub pp_pow { maybe_targmy(@_, \&binop, "**", 22, ASSIGN) }
+
+sub pp_left_shift { maybe_targmy(@_, \&binop, "<<", 17, ASSIGN) }
+sub pp_right_shift { maybe_targmy(@_, \&binop, ">>", 17, ASSIGN) }
+sub pp_bit_and { maybe_targmy(@_, \&binop, "&", 13, ASSIGN) }
+sub pp_bit_or { maybe_targmy(@_, \&binop, "|", 12, ASSIGN) }
+sub pp_bit_xor { maybe_targmy(@_, \&binop, "^", 12, ASSIGN) }
 
 sub pp_eq { binop(@_, "==", 14) }
 sub pp_ne { binop(@_, "!=", 14) }
@@ -1281,7 +1303,8 @@ sub pp_aassign { binop(@_, "=", 7, SWAP_CHILDREN) }
 # `.' is special because concats-of-concats are optimized to save copying
 # by making all but the first concat stacked. The effect is as if the
 # programmer had written `($a . $b) .= $c', except legal.
-sub pp_concat {
+sub pp_concat { maybe_targmy(@_, \&real_concat) }
+sub real_concat {
     my $self = shift;
     my($op, $cx) = @_;
     my $left = $op->first;
@@ -1370,6 +1393,9 @@ sub logop {
 
 sub pp_and { logop(@_, "and", 3, "&&", 11, "if") }
 sub pp_or  { logop(@_, "or",  2, "||", 10, "unless") }
+
+# xor is syntactically a logop, but it's really a binop (contrary to
+# old versions of opcode.pl). Syntax is what matters here.
 sub pp_xor { logop(@_, "xor", 2, "",   0,  "") }
 
 sub logassignop {
@@ -1407,20 +1433,20 @@ sub listop {
 }
 
 sub pp_bless { listop(@_, "bless") }
-sub pp_atan2 { listop(@_, "atan2") }
+sub pp_atan2 { maybe_targmy(@_, \&listop, "atan2") }
 sub pp_substr { maybe_local(@_, listop(@_, "substr")) }
 sub pp_vec { maybe_local(@_, listop(@_, "vec")) }
-sub pp_index { listop(@_, "index") }
-sub pp_rindex { listop(@_, "rindex") }
-sub pp_sprintf { listop(@_, "sprintf") }
+sub pp_index { maybe_targmy(@_, \&listop, "index") }
+sub pp_rindex { maybe_targmy(@_, \&listop, "rindex") }
+sub pp_sprintf { maybe_targmy(@_, \&listop, "sprintf") }
 sub pp_formline { listop(@_, "formline") } # see also deparse_format
-sub pp_crypt { listop(@_, "crypt") }
+sub pp_crypt { maybe_targmy(@_, \&listop, "crypt") }
 sub pp_unpack { listop(@_, "unpack") }
 sub pp_pack { listop(@_, "pack") }
-sub pp_join { listop(@_, "join") }
+sub pp_join { maybe_targmy(@_, \&listop, "join") }
 sub pp_splice { listop(@_, "splice") }
-sub pp_push { listop(@_, "push") }
-sub pp_unshift { listop(@_, "unshift") }
+sub pp_push { maybe_targmy(@_, \&listop, "push") }
+sub pp_unshift { maybe_targmy(@_, \&listop, "unshift") }
 sub pp_reverse { listop(@_, "reverse") }
 sub pp_warn { listop(@_, "warn") }
 sub pp_die { listop(@_, "die") }
@@ -1443,7 +1469,7 @@ sub pp_recv { listop(@_, "recv") }
 sub pp_seek { listop(@_, "seek") }
 sub pp_fcntl { listop(@_, "fcntl") }
 sub pp_ioctl { listop(@_, "ioctl") }
-sub pp_flock { listop(@_, "flock") }
+sub pp_flock { maybe_targmy(@_, \&listop, "flock") }
 sub pp_socket { listop(@_, "socket") }
 sub pp_sockpair { listop(@_, "sockpair") }
 sub pp_bind { listop(@_, "bind") }
@@ -1453,23 +1479,23 @@ sub pp_accept { listop(@_, "accept") }
 sub pp_shutdown { listop(@_, "shutdown") }
 sub pp_gsockopt { listop(@_, "getsockopt") }
 sub pp_ssockopt { listop(@_, "setsockopt") }
-sub pp_chown { listop(@_, "chown") }
-sub pp_unlink { listop(@_, "unlink") }
-sub pp_chmod { listop(@_, "chmod") }
-sub pp_utime { listop(@_, "utime") }
-sub pp_rename { listop(@_, "rename") }
-sub pp_link { listop(@_, "link") }
-sub pp_symlink { listop(@_, "symlink") }
-sub pp_mkdir { listop(@_, "mkdir") }
+sub pp_chown { maybe_targmy(@_, \&listop, "chown") }
+sub pp_unlink { maybe_targmy(@_, \&listop, "unlink") }
+sub pp_chmod { maybe_targmy(@_, \&listop, "chmod") }
+sub pp_utime { maybe_targmy(@_, \&listop, "utime") }
+sub pp_rename { maybe_targmy(@_, \&listop, "rename") }
+sub pp_link { maybe_targmy(@_, \&listop, "link") }
+sub pp_symlink { maybe_targmy(@_, \&listop, "symlink") }
+sub pp_mkdir { maybe_targmy(@_, \&listop, "mkdir") }
 sub pp_open_dir { listop(@_, "opendir") }
 sub pp_seekdir { listop(@_, "seekdir") }
-sub pp_waitpid { listop(@_, "waitpid") }
-sub pp_system { listop(@_, "system") }
-sub pp_exec { listop(@_, "exec") }
-sub pp_kill { listop(@_, "kill") }
-sub pp_setpgrp { listop(@_, "setpgrp") }
-sub pp_getpriority { listop(@_, "getpriority") }
-sub pp_setpriority { listop(@_, "setpriority") }
+sub pp_waitpid { maybe_targmy(@_, \&listop, "waitpid") }
+sub pp_system { maybe_targmy(@_, \&listop, "system") }
+sub pp_exec { maybe_targmy(@_, \&listop, "exec") }
+sub pp_kill { maybe_targmy(@_, \&listop, "kill") }
+sub pp_setpgrp { maybe_targmy(@_, \&listop, "setpgrp") }
+sub pp_getpriority { maybe_targmy(@_, \&listop, "getpriority") }
+sub pp_setpriority { maybe_targmy(@_, \&listop, "setpriority") }
 sub pp_shmget { listop(@_, "shmget") }
 sub pp_shmctl { listop(@_, "shmctl") }
 sub pp_shmread { listop(@_, "shmread") }
@@ -1513,7 +1539,7 @@ sub pp_truncate {
     my $fh;
     if ($op->flags & OPf_SPECIAL) {
        # $kid is an OP_CONST
-       $fh = $kid->sv->PV;
+       $fh = $self->const_sv($kid)->PV;
     } else {
        $fh = $self->deparse($kid, 6);
         $fh = "+$fh" if not $parens and substr($fh, 0, 1) eq "(";
@@ -1547,8 +1573,7 @@ sub indirop {
        $expr = $self->deparse($kid, 6);
        push @exprs, $expr;
     }
-    return $self->maybe_parens_func($name,
-                                   $indir . join(", ", @exprs),
+    return $self->maybe_parens_func($name, $indir . join(", ", @exprs),
                                    $cx, 5);
 }
 
@@ -1627,6 +1652,13 @@ sub pp_list {
     }
 }
 
+sub is_ifelse_cont {
+    my $op = shift;
+    return ($op->name eq "null" and class($op) eq "UNOP"
+           and $op->first->name =~ /^(and|cond_expr)$/
+           and is_scope($op->first->first->sibling));
+}
+
 sub pp_cond_expr {
     my $self = shift;
     my($op, $cx) = @_;
@@ -1634,36 +1666,34 @@ sub pp_cond_expr {
     my $true = $cond->sibling;
     my $false = $true->sibling;
     my $cuddle = $self->{'cuddle'};
-    unless ($cx == 0 and is_scope($true) and is_scope($false)) {
+    unless ($cx == 0 and (is_scope($true) and $true->name ne "null") and
+           (is_scope($false) || is_ifelse_cont($false))) {
        $cond = $self->deparse($cond, 8);
        $true = $self->deparse($true, 8);
        $false = $self->deparse($false, 8);
        return $self->maybe_parens("$cond ? $true : $false", $cx, 8);
-    } 
+    }
+
     $cond = $self->deparse($cond, 1);
     $true = $self->deparse($true, 0);    
-    if ($false->name eq "lineseq") { # braces w/o scope => elsif
-       my $head = "if ($cond) {\n\t$true\n\b}";
-       my @elsifs;
-       while (!null($false) and $false->name eq "lineseq") {
-           my $newop = $false->first->sibling->first;
-           my $newcond = $newop->first;
-           my $newtrue = $newcond->sibling;
-           $false = $newtrue->sibling; # last in chain is OP_AND => no else
-           $newcond = $self->deparse($newcond, 1);
-           $newtrue = $self->deparse($newtrue, 0);
-           push @elsifs, "elsif ($newcond) {\n\t$newtrue\n\b}";
-       }
-       if (!null($false)) {        
-           $false = $cuddle . "else {\n\t" .
-             $self->deparse($false, 0) . "\n\b}\cK";
-       } else {
-           $false = "\cK";
-       }
-       return $head . join($cuddle, "", @elsifs) . $false; 
+    my $head = "if ($cond) {\n\t$true\n\b}";
+    my @elsifs;
+    while (!null($false) and is_ifelse_cont($false)) {
+       my $newop = $false->first;
+       my $newcond = $newop->first;
+       my $newtrue = $newcond->sibling;
+       $false = $newtrue->sibling; # last in chain is OP_AND => no else
+       $newcond = $self->deparse($newcond, 1);
+       $newtrue = $self->deparse($newtrue, 0);
+       push @elsifs, "elsif ($newcond) {\n\t$newtrue\n\b}";
+    }
+    if (!null($false)) {           
+       $false = $cuddle . "else {\n\t" .
+         $self->deparse($false, 0) . "\n\b}\cK";
+    } else {
+       $false = "\cK";
     }
-    $false = $self->deparse($false, 0);
-    return "if ($cond) {\n\t$true\n\b}${cuddle}else {\n\t$false\n\b}\cK";
+    return $head . join($cuddle, "", @elsifs) . $false; 
 }
 
 sub pp_leaveloop {
@@ -1788,7 +1818,7 @@ sub pp_null {
     } elsif ($op->first->name eq "enter") {
        return $self->pp_leave($op, $cx);
     } elsif ($op->targ == OP_STRINGIFY) {
-       return $self->dquote($op);
+       return $self->dquote($op, $cx);
     } elsif (!null($op->first->sibling) and
             $op->first->sibling->name eq "readline" and
             $op->first->sibling->flags & OPf_STACKED) {
@@ -1853,22 +1883,34 @@ sub pp_threadsv {
     return $self->maybe_local($op, $cx, "\$" .  $threadsv_names[$op->targ]);
 }    
 
+sub gv_or_padgv {
+    my $self = shift;
+    my $op = shift;
+    if (class($op) eq "PADOP") {
+       return $self->padval($op->padix);
+    } else { # class($op) eq "SVOP"
+       return $op->gv;
+    }
+}
+
 sub pp_gvsv {
     my $self = shift;
     my($op, $cx) = @_;
-    return $self->maybe_local($op, $cx, "\$" . $self->gv_name($op->gv));
+    my $gv = $self->gv_or_padgv($op);
+    return $self->maybe_local($op, $cx, "\$" . $self->gv_name($gv));
 }
 
 sub pp_gv {
     my $self = shift;
     my($op, $cx) = @_;
-    return $self->gv_name($op->gv);
+    my $gv = $self->gv_or_padgv($op);
+    return $self->gv_name($gv);
 }
 
 sub pp_aelemfast {
     my $self = shift;
     my($op, $cx) = @_;
-    my $gv = $op->gv;
+    my $gv = $self->gv_or_padgv($op);
     return "\$" . $self->gv_name($gv) . "[" . $op->private . "]";
 }
 
@@ -1904,13 +1946,31 @@ sub pp_rv2av {
     my($op, $cx) = @_;
     my $kid = $op->first;
     if ($kid->name eq "const") { # constant list
-       my $av = $kid->sv;
+       my $av = $self->const_sv($kid);
        return "(" . join(", ", map(const($_), $av->ARRAY)) . ")";
     } else {
        return $self->maybe_local($op, $cx, $self->rv2x($op, $cx, "\@"));
     }
  }
 
+sub is_subscriptable {
+    my $op = shift;
+    if ($op->name =~ /^[ahg]elem/) {
+       return 1;
+    } elsif ($op->name eq "entersub") {
+       my $kid = $op->first;
+       return 0 unless null $kid->sibling;
+       $kid = $kid->first;
+       $kid = $kid->sibling until null $kid->sibling;
+       return 0 if is_scope($kid);
+       $kid = $kid->first;
+       return 0 if $kid->name eq "gv";
+       return 0 if is_scalar($kid);
+       return is_subscriptable($kid);  
+    } else {
+       return 0;
+    }
+}
 
 sub elem {
     my $self = shift;
@@ -1927,8 +1987,7 @@ sub elem {
        $array = $self->deparse($array, 24);
     } else {
        # $x[20][3]{hi} or expr->[20]
-       my $arrow;
-       $arrow = "->" if $array->name !~ /^[ah]elem$/;
+       my $arrow = is_subscriptable($array) ? "" : "->";
        return $self->deparse($array, 24) . $arrow .
            $left . $self->deparse($idx, 1) . $right;
     }
@@ -1985,10 +2044,8 @@ sub slice {
     return "\@" . $array . $left . $list . $right;
 }
 
-sub pp_aslice { maybe_local(@_, slice(@_, "[", "]", 
-                                     "rv2av", "padav")) }
-sub pp_hslice { maybe_local(@_, slice(@_, "{", "}",
-                                     "rv2hv", "padhv")) }
+sub pp_aslice { maybe_local(@_, slice(@_, "[", "]", "rv2av", "padav")) }
+sub pp_hslice { maybe_local(@_, slice(@_, "{", "}", "rv2hv", "padhv")) }
 
 sub pp_lslice {
     my $self = shift;
@@ -2028,7 +2085,7 @@ sub method {
        # as the left side of -> always is, while in the former
        # the list is in list context as method arguments always are.
        # (Good thing there aren't method prototypes!)
-       $meth = $kid->sibling->first;
+       $meth = $kid->sibling;
        $kid = $kid->first->sibling; # skip pushmark
        $obj = $kid;
        $kid = $kid->sibling;
@@ -2041,13 +2098,20 @@ sub method {
        for (; not null $kid->sibling; $kid = $kid->sibling) {
            push @exprs, $self->deparse($kid, 6);
        }
-       $meth = $kid->first;
+       $meth = $kid;
     }
     $obj = $self->deparse($obj, 24);
-    if ($meth->name eq "const") {
-       $meth = $meth->sv->PV; # needs to be bare
+    if ($meth->name eq "method_named") {
+       $meth = $self->const_sv($meth)->PV;
     } else {
-       $meth = $self->deparse($meth, 1);
+       $meth = $meth->first;
+       if ($meth->name eq "const") {
+           # As of 5.005_58, this case is probably obsoleted by the
+           # method_named case above
+           $meth = $self->const_sv($meth)->PV; # needs to be bare
+       } else {
+           $meth = $self->deparse($meth, 1);
+       }
     }
     my $args = join(", ", @exprs);     
     $kid = $obj . "->" . $meth;
@@ -2157,7 +2221,7 @@ sub pp_entersub {
        $amper = "&";
        $kid = "{" . $self->deparse($kid, 0) . "}";
     } elsif ($kid->first->name eq "gv") {
-       my $gv = $kid->first->gv;
+       my $gv = $self->gv_or_padgv($kid->first);
        if (class($gv->CV) ne "SPECIAL") {
            $proto = $gv->CV->PV if $gv->CV->FLAGS & SVf_POK;
        }
@@ -2168,7 +2232,8 @@ sub pp_entersub {
        $kid = $self->deparse($kid, 24);
     } else {
        $prefix = "";
-       $kid = $self->deparse($kid, 24) . "->";
+       my $arrow = is_subscriptable($kid->first) ? "" : "->";
+       $kid = $self->deparse($kid, 24) . $arrow;
     }
     my $args;
     if (defined $proto and not $amper) {
@@ -2188,9 +2253,9 @@ sub pp_entersub {
     } else {
        if (defined $proto and $proto eq "") {
            return $kid;
-       } elsif ($proto eq "\$") {
+       } elsif (defined $proto and $proto eq "\$") {
            return $self->maybe_parens_func($kid, $args, $cx, 16);
-       } elsif ($proto or $simple) {
+       } elsif (defined($proto) && $proto or $simple) {
            return $self->maybe_parens_func($kid, $args, $cx, 5);
        } else {
            return "$kid(" . $args . ")";
@@ -2301,13 +2366,23 @@ sub const {
     }
 }
 
+sub const_sv {
+    my $self = shift;
+    my $op = shift;
+    my $sv = $op->sv;
+    # the constant could be in the pad (under useithreads)
+    $sv = $self->padval($op->targ) unless $$sv;
+    return $sv;
+}
+
 sub pp_const {
     my $self = shift;
     my($op, $cx) = @_;
 #    if ($op->private & OPpCONST_BARE) { # trouble with `=>' autoquoting 
-#      return $op->sv->PV;
+#      return $self->const_sv($op)->PV;
 #    }
-    return const($op->sv);
+    my $sv = $self->const_sv($op);
+    return const($sv);
 }
 
 sub dq {
@@ -2315,7 +2390,7 @@ sub dq {
     my $op = shift;
     my $type = $op->name;
     if ($type eq "const") {
-       return uninterp(escape_str(unback($op->sv->PV)));
+       return uninterp(escape_str(unback($self->const_sv($op)->PV)));
     } elsif ($type eq "concat") {
        return $self->dq($op->first) . $self->dq($op->last);
     } elsif ($type eq "uc") {
@@ -2344,14 +2419,15 @@ sub pp_backtick {
 
 sub dquote {
     my $self = shift;
-    my($op, $cx) = shift;
-    return $self->deparse($op->first->sibling, $cx) if $self->{'unquote'};
-    # skip ex-stringify, pushmark
-    return single_delim("qq", '"', $self->dq($op->first->sibling)); 
+    my($op, $cx) = @_;
+    my $kid = $op->first->sibling; # skip ex-stringify, pushmark
+    return $self->deparse($kid, $cx) if $self->{'unquote'};
+    $self->maybe_targmy($kid, $cx,
+                       sub {single_delim("qq", '"', $self->dq($_[1]))});
 }
 
 # OP_STRINGIFY is a listop, but it only ever has one arg
-sub pp_stringify { dquote(@_) }
+sub pp_stringify { maybe_targmy(@_, \&dquote) }
 
 # tr/// and s/// (and tr[][], tr[]//, tr###, etc)
 # note that tr(from)/to/ is OK, but not tr/from/(to)
@@ -2603,7 +2679,7 @@ sub re_dq {
     my $op = shift;
     my $type = $op->name;
     if ($type eq "const") {
-       return uninterp($op->sv->PV);
+       return uninterp($self->const_sv($op)->PV);
     } elsif ($type eq "concat") {
        return $self->re_dq($op->first) . $self->re_dq($op->last);
     } elsif ($type eq "uc") {