Support BEGIN blocks in B::Deparse (& more)
[p5sagit/p5-mst-13.2.git] / ext / B / B / Deparse.pm
index 91c08e7..7d8e0b5 100644 (file)
@@ -1,5 +1,5 @@
 # B::Deparse.pm
-# Copyright (c) 1998 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.
 
@@ -7,10 +7,20 @@
 # but essentially none of his code remains.
 
 package B::Deparse;
-use Carp 'cluck';
-use B qw(class main_root main_start main_cv svref_2object);
-$VERSION = 0.53;
+use Carp 'cluck', 'croak';
+use B qw(class main_root main_start main_cv svref_2object opnumber cstring
+        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 OPpTARGET_MY
+        OPpCONST_ARYBASE OPpEXISTS_SUB
+        SVf_IOK SVf_NOK SVf_ROK SVf_POK
+         CVf_METHOD CVf_LOCKED CVf_LVALUE
+        PMf_KEEP PMf_GLOBAL PMf_CONTINUE PMf_EVAL PMf_ONCE PMf_SKIPWHITE
+        PMf_MULTILINE PMf_SINGLELINE PMf_FOLD PMf_EXTENDED);
+$VERSION = 0.60;
 use strict;
+use warnings ();
 
 # Changes between 0.50 and 0.51:
 # - fixed nulled leave with live enter in sort { }
@@ -26,24 +36,85 @@ use strict;
 # Changes between 0.51 and 0.52:
 # - added pp_threadsv (special variables under USE_THREADS)
 # - added documentation
-# Changes between 0.52 and 0.53
+# Changes between 0.52 and 0.53:
 # - many changes adding precedence contexts and associativity
 # - added `-p' and `-s' output style options
 # - various other minor fixes
+# Changes between 0.53 and 0.54:
+# - added support for new `for (1..100)' optimization,
+#   thanks to Gisle Aas
+# Changes between 0.54 and 0.55:
+# - added support for new qr// construct
+# - added support for new pp_regcreset OP
+# Changes between 0.55 and 0.56:
+# - tested on base/*.t, cmd/*.t, comp/*.t, io/*.t
+# - fixed $# on non-lexicals broken in last big rewrite
+# - added temporary fix for change in opcode of OP_STRINGIFY
+# - fixed problem in 0.54's for() patch in `for (@ary)'
+# - fixed precedence in conditional of ?:
+# - tweaked list paren elimination in `my($x) = @_'
+# - made continue-block detection trickier wrt. null ops
+# - fixed various prototype problems in pp_entersub
+# - added support for sub prototypes that never get GVs
+# - added unquoting for special filehandle first arg in truncate
+# - print doubled rv2gv (a bug) as `*{*GV}' instead of illegal `**GV'
+# - added semicolons at the ends of blocks
+# - added -l `#line' declaration option -- fixes cmd/subval.t 27,28
+# Changes between 0.56 and 0.561:
+# - fixed multiply-declared my var in pp_truncate (thanks to Sarathy)
+# - used new B.pm symbolic constants (done by Nick Ing-Simmons)
+# Changes between 0.561 and 0.57:
+# - stylistic changes to symbolic constant stuff
+# - handled scope in s///e replacement code
+# - added unquote option for expanding "" into concats, etc.
+# - split method and proto parts of pp_entersub into separate functions
+# - various minor cleanups
+# Changes after 0.57:
+# - added parens in \&foo (patch by Albert Dvornik)
+# Changes between 0.57 and 0.58:
+# - fixed `0' statements that weren't being printed
+# - added methods for use from other programs
+#   (based on patches from James Duncan and Hugo van der Sanden)
+# - 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
+# Changes between 0.59 and 0.60
+# - support for method attribues was added
+# - some warnings fixed
+# - separate recognition of constant subs
+# - rewrote continue block handling, now recoginizing for loops
+# - added more control of expanding control structures
 
 # Todo:
-# - {} around variables in strings ("${var}letters")
+# - finish tr/// changes
+# - add option for even more parens (generalize \&foo change)
+# - left/right context
+# - treat top-level block specially for incremental output
+# - copy comments (look at real text with $^P?)
+# - avoid semis in one-statement blocks
 # - associativity of &&=, ||=, ?:
 # - ',' => '=>' (auto-unquote?)
 # - break long lines ("\r" as discretionary break?)
+# - 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'?
+# - coordinate with Data::Dumper (both directions? see previous)
 # - version using op_next instead of op_first/sibling?
 # - avoid string copies (pass arrays, one big join?)
-# - auto-apply `-u'?
-# - while{} with one-statement continue => for(; XXX; XXX) {}?
-# - -uPackage:: descend recursively?
 # - here-docs?
 # - <DATA>?
 
+# Tests that will always fail:
+# comp/redef.t -- all (redefinition happens at compile time)
+
 # Object fields (were globals):
 #
 # avoid_local:
@@ -57,19 +128,34 @@ use strict;
 # curcv:
 # CV for current sub (or main program) being deparsed
 #
+# curcop:
+# COP for statement being deparsed
+#
 # curstash:
 # name of the current package for deparsed code
 #
 # subs_todo:
-# array of [cop_seq, GV, is_format?] for subs and formats we still
+# array of [cop_seq, CV, is_format?] for subs and formats we still
 # want to deparse
 #
+# protos_todo:
+# as above, but [name, prototype] for subs that never got a GV
+#
 # subs_done, forms_done:
 # keys are addresses of GVs for subs and formats we've already
 # deparsed (or at least put into subs_todo)
 #
+# subs_declared
+# keys are names of subs for which we've printed declarations.
+# That means we can omit parentheses from the arguments.
+#
 # parens: -p
+# linenums: -l
+# unquote: -q
 # cuddle: ` ' or `\n', depending on -sC
+# indent_size: -si
+# use_tabs: -sT
+# ex_const: -sv
 
 # A little explanation of how precedence contexts and associativity
 # work:
@@ -77,7 +163,7 @@ use strict;
 # deparse() calls each per-op subroutine with an argument $cx (short
 # for context, but not the same as the cx* in the perl core), which is
 # a number describing the op's parents in terms of precedence, whether
-# they're inside and expression or at statement level, etc.  (see
+# they're inside an expression or at statement level, etc.  (see
 # chart below). When ops with children call deparse on them, they pass
 # along their precedence. Fractional values are used to implement
 # associativity (`($x + $y) + $z' => `$x + $y + $y') and related
@@ -120,6 +206,7 @@ use strict;
 # \n - newline and indent
 # \t - increase indent
 # \b - decrease indent (`outdent')
+# \f - flush left (no indent)
 # \cK - kill following semicolon, if any
 
 sub null {
@@ -129,30 +216,122 @@ sub null {
 
 sub todo {
     my $self = shift;
-    my($gv, $cv, $is_form) = @_;
+    my($cv, $is_form) = @_;
     my $seq;
     if (!null($cv->START) and is_state($cv->START)) {
        $seq = $cv->START->cop_seq;
     } else {
        $seq = 0;
     }
-    push @{$self->{'subs_todo'}}, [$seq, $gv, $is_form];
+    push @{$self->{'subs_todo'}}, [$seq, $cv, $is_form];
 }
 
 sub next_todo {
     my $self = shift;
     my $ent = shift @{$self->{'subs_todo'}};
-    my $name = $self->gv_name($ent->[1]);
+    my $cv = $ent->[1];
+    my $gv = $cv->GV;
+    my $name = $self->gv_name($gv);
     if ($ent->[2]) {
        return "format $name =\n"
            . $self->deparse_format($ent->[1]->FORM). "\n";
     } else {
-       return "sub $name " .
-           $self->deparse_sub($ent->[1]->CV);
+       $self->{'subs_declared'}{$name} = 1;
+       if ($name eq "BEGIN") {
+           my $use_dec = $self->begin_is_use($cv);
+           return $use_dec if defined ($use_dec);
+       }
+        return "sub $name " . $self->deparse_sub($cv);
     }
 }
 
-sub OPf_KIDS () { 4 }
+# Return a "use" declaration for this BEGIN block, if appropriate
+sub begin_is_use {
+    my ($self, $cv) = @_;
+    my $root = $cv->ROOT;
+#require B::Debug;
+#B::walkoptree($cv->ROOT, "debug");
+    my $lineseq = $root->first;
+    return if $lineseq->name ne "lineseq";
+
+    my $req_op = $lineseq->first->sibling;
+    return if $req_op->name ne "require";
+
+    my $module;
+    if ($req_op->first->private & OPpCONST_BARE) {
+       # Actually it should always be a bareword
+       $module = $self->const_sv($req_op->first)->PV;
+       $module =~ s[/][::]g;
+       $module =~ s/.pm$//;
+    }
+    else {
+       $module = const($self->const_sv($req_op->first));
+    }
+
+    my $version;
+    my $version_op = $req_op->sibling;
+    return if class($version_op) eq "NULL";
+    if ($version_op->name eq "lineseq") {
+       # We have a version parameter; skip nextstate & pushmark
+       my $constop = $version_op->first->next->next;
+
+       return unless $self->const_sv($constop)->PV eq $module;
+       $constop = $constop->sibling;
+
+       $version = $self->const_sv($constop)->int_value;
+       $constop = $constop->sibling;
+       return if $constop->name ne "method_named";
+       return if $self->const_sv($constop)->PV ne "VERSION";
+    }
+
+    $lineseq = $version_op->sibling;
+    return if $lineseq->name ne "lineseq";
+    my $entersub = $lineseq->first->sibling;
+    if ($entersub->name eq "stub") {
+       return "use $module $version ();\n" if defined $version;
+       return "use $module ();\n";
+    }
+    return if $entersub->name ne "entersub";
+
+    # See if there are import arguments
+    my $args = '';
+
+    my $constop = $entersub->first->sibling; # Skip over pushmark
+    return unless $self->const_sv($constop)->PV eq $module;
+
+    # Pull out the arguments
+    for ($constop=$constop->sibling; $constop->name eq "const";
+               $constop = $constop->sibling) {
+       $args .= ", " if length($args);
+       $args .= $self->deparse($constop, 6);
+    }
+
+    my $use = 'use';
+    my $method_named = $constop;
+    return if $method_named->name ne "method_named";
+    my $method_name = $self->const_sv($method_named)->PV;
+
+    if ($method_name eq "unimport") {
+       $use = 'no';
+    }
+
+    # Certain pragmas are dealt with using hint bits,
+    # so we ignore them here
+    if ($module eq 'strict' || $module eq 'integer'
+       || $module eq 'bytes') {
+       return "";
+    }
+
+    if (defined $version && length $args) {
+       return "$use $module $version ($args);\n";
+    } elsif (defined $version) {
+       return "$use $module $version;\n";
+    } elsif (length $args) {
+       return "$use $module ($args);\n";
+    } else {
+       return "$use $module;\n";
+    }
+}
 
 sub walk_tree {
     my($op, $sub) = @_;
@@ -173,45 +352,79 @@ sub walk_sub {
     return if !$op or null $op;
     walk_tree($op, sub {
        my $op = shift;
-       if ($op->ppaddr eq "pp_gv") {
-           if ($op->next->ppaddr eq "pp_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);
-           } elsif ($op->next->ppaddr eq "pp_enterwrite"
-                    or ($op->next->ppaddr eq "pp_rv2gv"
-                        and $op->next->next->ppaddr eq "pp_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);
+       if ($op->name eq "gv") {
+           my $gv = $self->gv_or_padgv($op);
+           if ($op->next->name eq "entersub") {
+               return if $self->{'subs_done'}{$$gv}++;
+               return if class($gv->CV) eq "SPECIAL";
+               $self->todo($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")) {
+               return if $self->{'forms_done'}{$$gv}++;
+               return if class($gv->FORM) eq "SPECIAL";
+               $self->todo($gv->FORM, 1);
+               $self->walk_sub($gv->FORM);
            }
        }
     });
 }
 
 sub stash_subs {
-    my $self = shift;
-    my $pack = shift;
-    my(%stash, @ret);
-    { no strict 'refs'; %stash = svref_2object(\%{$pack . "::"})->ARRAY }
-    my($key, $val);
-    while (($key, $val) = each %stash) {
-       next unless class($val) eq "GV";
-       if (class($val->CV) ne "SPECIAL") {
-           next if $self->{'subs_done'}{$$val}++;
-           $self->todo($val, $val->CV, 0);
-           $self->walk_sub($val->CV);
-       }
-       if (class($val->FORM) ne "SPECIAL") {
-           next if $self->{'forms_done'}{$$val}++;
-           $self->todo($val, $val->FORM, 1);
-           $self->walk_sub($val->FORM);
+    my ($self, $pack) = @_;
+    my (@ret, $stash);
+    if (!defined $pack) {
+       $pack = '';
+       $stash = \%::;
+    }
+    else {
+       $pack =~ s/(::)?$/::/;
+       no strict 'refs';
+       $stash = \%$pack;
+    }
+    my %stash = svref_2object($stash)->ARRAY;
+    while (my ($key, $val) = each %stash) {
+       next if $key eq 'main::';       # avoid infinite recursion
+       next if $key eq 'B::';          # don't automatically scan B
+       my $class = class($val);
+       if ($class eq "PV") {
+           # Just a prototype
+           push @{$self->{'protos_todo'}}, [$pack . $key, $val->PV];
+       } elsif ($class eq "IV") {
+           # Just a name
+           push @{$self->{'protos_todo'}}, [$pack . $key, undef];          
+       } elsif ($class eq "GV") {
+           if (class(my $cv = $val->CV) ne "SPECIAL") {
+               next unless $cv->FILE eq $0 || $self->{'files'}{$cv->FILE};
+               next if $self->{'subs_done'}{$$val}++;
+               $self->todo($val->CV, 0);
+               $self->walk_sub($val->CV);
+           }
+           if (class($val->FORM) ne "SPECIAL") {
+               next if $self->{'forms_done'}{$$val}++;
+               $self->todo($val->FORM, 1);
+               $self->walk_sub($val->FORM);
+           }
+           if (class($val->HV) ne "SPECIAL" && $key =~ /::$/) {
+               $self->stash_subs($pack . $key);
+           }
        }
     }
 }
 
+sub print_protos {
+    my $self = shift;
+    my $ar;
+    my @ret;
+    foreach $ar (@{$self->{'protos_todo'}}) {
+       my $proto = (defined $ar->[1] ? " (". $ar->[1] . ")" : "");
+       push @ret, "sub " . $ar->[0] .  "$proto;\n";
+    }
+    delete $self->{'protos_todo'};
+    return @ret;
+}
+
 sub style_opts {
     my $self = shift;
     my $opts = shift;
@@ -219,86 +432,287 @@ sub style_opts {
     while (length($opt = substr($opts, 0, 1))) {
        if ($opt eq "C") {
            $self->{'cuddle'} = " ";
+           $opts = substr($opts, 1);
+       } elsif ($opt eq "i") {
+           $opts =~ s/^i(\d+)//;
+           $self->{'indent_size'} = $1;
+       } elsif ($opt eq "T") {
+           $self->{'use_tabs'} = 1;
+           $opts = substr($opts, 1);
+       } elsif ($opt eq "v") {
+           $opts =~ s/^v([^.]*)(.|$)//;
+           $self->{'ex_const'} = $1;
        }
-       $opts = substr($opts, 1);
     }
 }
 
+sub new {
+    my $class = shift;
+    my $self = bless {}, $class;
+    $self->{'subs_todo'} = [];
+    $self->{'files'} = {};
+    $self->{'curstash'} = "main";
+    $self->{'curcop'} = undef;
+    $self->{'cuddle'} = "\n";
+    $self->{'indent_size'} = 4;
+    $self->{'use_tabs'} = 0;
+    $self->{'expand'} = 0;
+    $self->{'unquote'} = 0;
+    $self->{'linenums'} = 0;
+    $self->{'parens'} = 0;
+    $self->{'ex_const'} = "'???'";
+
+    $self->{'ambient_arybase'} = 0;
+    $self->{'ambient_warnings'} = "\0"x12;
+    $self->{'ambient_hints'} = 0;
+    $self->init();
+
+    while (my $arg = shift @_) {
+       if ($arg =~ /^-f(.*)/) {
+           $self->{'files'}{$1} = 1;
+       } elsif ($arg eq "-p") {
+           $self->{'parens'} = 1;
+       } elsif ($arg eq "-l") {
+           $self->{'linenums'} = 1;
+       } elsif ($arg eq "-q") {
+           $self->{'unquote'} = 1;
+       } elsif (substr($arg, 0, 2) eq "-s") {
+           $self->style_opts(substr $arg, 2);
+       } elsif ($arg =~ /^-x(\d)$/) {
+           $self->{'expand'} = $1;
+       }
+    }
+    return $self;
+}
+
+sub WARN_MASK () {
+    # Mask out the bits that C<use vars> uses
+    $warnings::Bits{all} | $warnings::DeadBits{all};
+}
+
+# Initialise the contextual information, either from
+# defaults provided with the ambient_pragmas method,
+# or from perl's own defaults otherwise.
+sub init {
+    my $self = shift;
+
+    $self->{'arybase'}  = $self->{'ambient_arybase'};
+    $self->{'warnings'} = $self->{'ambient_warnings'} & WARN_MASK;
+    $self->{'hints'}    = $self->{'ambient_hints'} & 0xFF;
+
+    # also a convenient place to clear out subs_declared
+    delete $self->{'subs_declared'};
+}
+
 sub compile {
     my(@args) = @_;
     return sub { 
-       my $self = bless {};
-       my $arg;
-       $self->{'subs_todo'} = [];
-       $self->stash_subs("main");
-       $self->{'curcv'} = main_cv;
-       $self->{'curstash'} = "main";
-       $self->{'cuddle'} = "\n";
-       while ($arg = shift @args) {
-           if (substr($arg, 0, 2) eq "-u") {
-               $self->stash_subs(substr($arg, 2));
-           } elsif ($arg eq "-p") {
-               $self->{'parens'} = 1;
-           } elsif (substr($arg, 0, 2) eq "-s") {
-               $self->style_opts(substr $arg, 2);
+       my $self = B::Deparse->new(@args);
+       my @BEGINs  = B::begin_av->isa("B::AV") ? B::begin_av->ARRAY : ();
+       my @INITs   = B::init_av->isa("B::AV") ? B::init_av->ARRAY : ();
+       my @ENDs    = B::end_av->isa("B::AV") ? B::end_av->ARRAY : ();
+       for my $block (@BEGINs, @INITs, @ENDs) {
+           if ($block->FILE eq $0 || $self->{'files'}{$block->FILE}) {
+               $self->todo($block, 0);
+               $self->walk_sub($block);
            }
        }
+       $self->stash_subs();
+       $self->{'curcv'} = main_cv;
        $self->walk_sub(main_cv, main_start);
+       print $self->print_protos;
        @{$self->{'subs_todo'}} =
-           sort {$a->[0] <=> $b->[0]} @{$self->{'subs_todo'}};
-       print indent($self->deparse(main_root, 0)), "\n" unless null main_root;
+         sort {$a->[0] <=> $b->[0]} @{$self->{'subs_todo'}};
+       print $self->indent($self->deparse(main_root, 0)), "\n"
+         unless null main_root;
        my @text;
        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;
     }
 }
 
+sub coderef2text {
+    my $self = shift;
+    my $sub = shift;
+    croak "Usage: ->coderef2text(CODEREF)" unless ref($sub) eq "CODE";
+
+    $self->init();
+    return $self->indent($self->deparse_sub(svref_2object($sub)));
+}
+
+sub ambient_pragmas {
+    my $self = shift;
+    my ($arybase, $hint_bits, $warning_bits) = (0, 0, "\0"x12);
+
+    while (@_ > 1) {
+       my $name = shift();
+       my $val  = shift();
+
+       if ($name eq 'strict') {
+           require strict;
+
+           if ($val eq 'none') {
+               $hint_bits &= ~strict::bits(qw/refs subs vars/);
+               next();
+           }
+
+           my @names;
+           if ($val eq "all") {
+               @names = qw/refs subs vars/;
+           }
+           elsif (ref $val) {
+               @names = @$val;
+           }
+           else {
+               @names = split' ', $val;
+           }
+           $hint_bits |= strict::bits(@names);
+       }
+
+       elsif ($name eq '$[') {
+           $arybase = $val;
+       }
+
+       elsif ($name eq 'integer'
+           || $name eq 'bytes'
+           || $name eq 'utf8') {
+           require "$name.pm";
+           if ($val) {
+               $hint_bits |= ${$::{"${name}::"}{"hint_bits"}};
+           }
+           else {
+               $hint_bits &= ~${$::{"${name}::"}{"hint_bits"}};
+           }
+       }
+
+       elsif ($name eq 're') {
+           require re;
+           if ($val eq 'none') {
+               $hint_bits &= ~re::bits(qw/taint eval asciirange/);
+               next();
+           }
+
+           my @names;
+           if ($val eq 'all') {
+               @names = qw/taint eval asciirange/;
+           }
+           elsif (ref $val) {
+               @names = @$val;
+           }
+           else {
+               @names = split' ',$val;
+           }
+           $hint_bits |= re::bits(@names);
+       }
+
+       elsif ($name eq 'warnings') {
+           if ($val eq 'none') {
+               $warning_bits = "\0"x12;
+               next();
+           }
+
+           my @names;
+           if (ref $val) {
+               @names = @$val;
+           }
+           else {
+               @names = split/\s+/, $val;
+           }
+
+           $warning_bits |= warnings::bits(@names);
+       }
+
+       elsif ($name eq 'warning_bits') {
+           $warning_bits = $val;
+       }
+
+       elsif ($name eq 'hint_bits') {
+           $hint_bits = $val;
+       }
+
+       else {
+           croak "Unknown pragma type: $name";
+       }
+    }
+    if (@_) {
+       croak "The ambient_pragmas method expects an even number of args";
+    }
+
+    $self->{'ambient_arybase'} = $arybase;
+    $self->{'ambient_warnings'} = $warning_bits;
+    $self->{'ambient_hints'} = $hint_bits;
+}
+
 sub deparse {
     my $self = shift;
     my($op, $cx) = @_;
-#    cluck if class($op) eq "NULL";
-    my $meth = $op->ppaddr;
+
+    Carp::confess("Null op in deparse") if !defined($op)
+                                       || class($op) eq "NULL";
+    my $meth = "pp_" . $op->name;
     return $self->$meth($op, $cx);
 }
 
 sub indent {
+    my $self = shift;
     my $txt = shift;
     my @lines = split(/\n/, $txt);
     my $leader = "";
+    my $level = 0;
     my $line;
     for $line (@lines) {
-       if (substr($line, 0, 1) eq "\t") {
-           $leader = $leader . "    ";
-           $line = substr($line, 1);
-       } elsif (substr($line, 0, 1) eq "\b") {
-           $leader = substr($leader, 0, length($leader) - 4);
+       my $cmd = substr($line, 0, 1);
+       if ($cmd eq "\t" or $cmd eq "\b") {
+           $level += ($cmd eq "\t" ? 1 : -1) * $self->{'indent_size'};
+           if ($self->{'use_tabs'}) {
+               $leader = "\t" x ($level / 8) . " " x ($level % 8);
+           } else {
+               $leader = " " x $level;
+           }
            $line = substr($line, 1);
        }
+       if (substr($line, 0, 1) eq "\f") {
+           $line = substr($line, 1); # no indent
+       } else {
+           $line = $leader . $line;
+       }
        $line =~ s/\cK;?//g;
-       $line = $leader . $line;
     }
     return join("\n", @lines);
 }
 
-sub SVf_POK () {0x40000}
-
 sub deparse_sub {
     my $self = shift;
     my $cv = shift;
     my $proto = "";
+Carp::confess("SPECIAL in deparse_sub") if $cv->isa("B::SPECIAL");
+    local $self->{'curcop'} = $self->{'curcop'};
     if ($cv->FLAGS & SVf_POK) {
        $proto = "(". $cv->PV . ") ";
     }
+    if ($cv->CvFLAGS & (CVf_METHOD|CVf_LOCKED|CVf_LVALUE)) {
+        $proto .= ": ";
+        $proto .= "lvalue " if $cv->CvFLAGS & CVf_LVALUE;
+        $proto .= "locked " if $cv->CvFLAGS & CVf_LOCKED;
+        $proto .= "method " if $cv->CvFLAGS & CVf_METHOD;
+    }
+
     local($self->{'curcv'}) = $cv;
-    local($self->{'curstash'}) = $self->{'curstash'};
+    local(@$self{qw'curstash warnings hints'})
+               = @$self{qw'curstash warnings hints'};
     if (not null $cv->ROOT) {
        # skip leavesub
        return $proto . "{\n\t" . 
            $self->deparse($cv->ROOT->first, 0) . "\n\b}\n"; 
-    } else { # XSUB?
-       return $proto  . "{}\n";
+    }
+    my $sv = $cv->const_sv;
+    if ($$sv) {
+       # uh-oh. inlinable sub... format it differently
+       return $proto . "{ " . const($sv) . " }\n";
+    } else { # XSUB? (or just a declaration)
+       return "$proto;\n";
     }
 }
 
@@ -307,7 +721,8 @@ sub deparse_format {
     my $form = shift;
     my @text;
     local($self->{'curcv'}) = $form;
-    local($self->{'curstash'}) = $self->{'curstash'};
+    local(@$self{qw'curstash warnings hints'})
+               = @$self{'curstash warnings hints'};
     my $op = $form->ROOT;
     my $kid;
     $op = $op->first->first; # skip leavewrite, lineseq
@@ -315,7 +730,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);
@@ -326,47 +741,38 @@ sub deparse_format {
     return join("", @text) . ".";
 }
 
-# the aassign in-common check messes up SvCUR (always setting it
-# to a value >= 100), but it's probably safe to assume there
-# won't be any NULs in the names of my() variables. (with
-# stash variables, I wouldn't be so sure)
-sub padname_fix {
-    my $str = shift;
-    $str = substr($str, 0, index($str, "\0")) if index($str, "\0") != -1;
-    return $str;
-}
-
 sub is_scope {
     my $op = shift;
-    return $op->ppaddr eq "pp_leave" || $op->ppaddr eq "pp_scope"
-      || $op->ppaddr eq "pp_lineseq"
-       || ($op->ppaddr eq "pp_null" && class($op) eq "UNOP" 
-           && (is_scope($op->first) || $op->first->ppaddr eq "pp_enter"));
+    return $op->name eq "leave" || $op->name eq "scope"
+      || $op->name eq "lineseq"
+       || ($op->name eq "null" && class($op) eq "UNOP" 
+           && (is_scope($op->first) || $op->first->name eq "enter"));
 }
 
 sub is_state {
-    my $name = $_[0]->ppaddr;
-    return $name eq "pp_nextstate" || $name eq "pp_dbstate";
+    my $name = $_[0]->name;
+    return $name eq "nextstate" || $name eq "dbstate" || $name eq "setstate";
 }
 
 sub is_miniwhile { # check for one-line loop (`foo() while $y--')
     my $op = shift;
     return (!null($op) and null($op->sibling) 
-           and $op->ppaddr eq "pp_null" and class($op) eq "UNOP"
-           and (($op->first->ppaddr =~ /^pp_(and|or)$/
-                 and $op->first->first->sibling->ppaddr eq "pp_lineseq")
-                or ($op->first->ppaddr eq "pp_lineseq"
+           and $op->name eq "null" and class($op) eq "UNOP"
+           and (($op->first->name =~ /^(and|or)$/
+                 and $op->first->first->sibling->name eq "lineseq")
+                or ($op->first->name eq "lineseq"
                     and not null $op->first->first->sibling
-                    and $op->first->first->sibling->ppaddr eq "pp_unstack")
+                    and $op->first->first->sibling->name eq "unstack")
                 ));
 }
 
 sub is_scalar {
     my $op = shift;
-    return ($op->ppaddr eq "pp_rv2sv" or
-           $op->ppaddr eq "pp_padsv" or
-           $op->ppaddr eq "pp_gv" or # only in array/hash constructs
-           !null($op->first) && $op->first->ppaddr eq "pp_gvsv");
+    return ($op->name eq "rv2sv" or
+           $op->name eq "padsv" or
+           $op->name eq "gv" or # only in array/hash constructs
+           $op->flags & OPf_KIDS && !null($op->first)
+             && $op->first->name eq "gvsv");
 }
 
 sub maybe_parens {
@@ -416,18 +822,32 @@ sub maybe_parens_func {
     }
 }
 
-sub OPp_LVAL_INTRO () { 128 }
-
 sub maybe_local {
     my $self = shift;
     my($op, $cx, $text) = @_;
-    if ($op->private & OPp_LVAL_INTRO and not $self->{'avoid_local'}{$$op}) {
-       return $self->maybe_parens_func("local", $text, $cx, 16);
+    if ($op->private & OPpLVAL_INTRO and not $self->{'avoid_local'}{$$op}) {
+        if (want_scalar($op)) {
+           return "local $text";
+       } else {
+           return $self->maybe_parens_func("local", $text, $cx, 16);
+       }
     } else {
        return $text;
     }
 }
 
+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;
@@ -437,8 +857,12 @@ sub padname_sv {
 sub maybe_my {
     my $self = shift;
     my($op, $cx, $text) = @_;
-    if ($op->private & OPp_LVAL_INTRO and not $self->{'avoid_local'}{$$op}) {
-       return $self->maybe_parens_func("my", $text, $cx, 16);
+    if ($op->private & OPpLVAL_INTRO and not $self->{'avoid_local'}{$$op}) {
+       if (want_scalar($op)) {
+           return "my $text";
+       } else {
+           return $self->maybe_parens_func("my", $text, $cx, 16);
+       }
     } else {
        return $text;
     }
@@ -479,6 +903,11 @@ sub pp_regcmaybe { # see also regcomp
     return "XXX";
 }
 
+sub pp_regcreset { # see also regcomp
+    cluck "unexpected OP_REGCRESET";
+    return "XXX";
+}
+
 sub pp_substcont { # see also subst
     cluck "unexpected OP_SUBSTCONT";
     return "XXX";
@@ -524,70 +953,73 @@ sub pp_entertry { # see also leavetry
     return "XXX";
 }
 
-# leave and scope/lineseq should probably share code
-sub pp_leave {
+sub lineseq {
     my $self = shift;
-    my($op, $cx) = @_;
-    my ($kid, $expr);
-    my @exprs;
-    local($self->{'curstash'}) = $self->{'curstash'};
-    $kid = $op->first->sibling; # skip enter
-    if (is_miniwhile($kid)) {
-       my $top = $kid->first;
-       my $name = $top->ppaddr;
-       if ($name eq "pp_and") {
-           $name = "while";
-       } elsif ($name eq "pp_or") {
-           $name = "until";
-       } else { # no conditional -> while 1 or until 0
-           return $self->deparse($top->first, 1) . " while 1";
-       }
-       my $cond = $top->first;
-       my $body = $cond->sibling->first; # skip lineseq
-       $cond = $self->deparse($cond, 1);
-       $body = $self->deparse($body, 1);
-       return "$body $name $cond";
-    }
-    for (; !null($kid); $kid = $kid->sibling) {
+    my(@ops) = @_;
+    my($expr, @exprs);
+    for (my $i = 0; $i < @ops; $i++) {
        $expr = "";
-       if (is_state $kid) {
-           $expr = $self->deparse($kid, 0);
-           $kid = $kid->sibling;
-           last if null $kid;
+       if (is_state $ops[$i]) {
+           $expr = $self->deparse($ops[$i], 0);
+           $i++;
+           last if $i > $#ops;
        }
-       $expr .= $self->deparse($kid, 0);
-       push @exprs, $expr if $expr;
-    }
-    if ($cx > 0) { # inside an expression
-       return "do { " . join(";\n", @exprs) . " }";
-    } else {
-       return join(";\n", @exprs);     
+       if (!is_state $ops[$i] and $ops[$i+1] and !null($ops[$i+1]) and
+           $ops[$i+1]->name eq "leaveloop" and $self->{'expand'} < 3)
+       {
+           push @exprs, $expr . $self->for_loop($ops[$i], 0);
+           $i++;
+           next;
+       }
+       $expr .= $self->deparse($ops[$i], 0);
+       push @exprs, $expr if length $expr;
     }
+    for(@exprs[0..@exprs-1]) { s/;\n\z// }
+    return join(";\n", @exprs);
 }
 
-sub pp_scope {
-    my $self = shift;
-    my($op, $cx) = @_;
-    my ($kid, $expr);
-    my @exprs;
-    for ($kid = $op->first; !null($kid); $kid = $kid->sibling) {
-       $expr = "";
-       if (is_state $kid) {
-           $expr = $self->deparse($kid, 0);
-           $kid = $kid->sibling;
-           last if null $kid;
+sub scopeop {
+    my($real_block, $self, $op, $cx) = @_;
+    my $kid;
+    my @kids;
+
+    local(@$self{qw'curstash warnings hints'})
+               = @$self{qw'curstash warnings hints'} if $real_block;
+    if ($real_block) {
+       $kid = $op->first->sibling; # skip enter
+       if (is_miniwhile($kid)) {
+           my $top = $kid->first;
+           my $name = $top->name;
+           if ($name eq "and") {
+               $name = "while";
+           } elsif ($name eq "or") {
+               $name = "until";
+           } else { # no conditional -> while 1 or until 0
+               return $self->deparse($top->first, 1) . " while 1";
+           }
+           my $cond = $top->first;
+           my $body = $cond->sibling->first; # skip lineseq
+           $cond = $self->deparse($cond, 1);
+           $body = $self->deparse($body, 1);
+           return "$body $name $cond";
        }
-       $expr .= $self->deparse($kid, 0);
-       push @exprs, $expr if $expr;
+    } else {
+       $kid = $op->first;
+    }
+    for (; !null($kid); $kid = $kid->sibling) {
+       push @kids, $kid;
     }
     if ($cx > 0) { # inside an expression, (a do {} while for lineseq)
-       return "do { " . join(";\n", @exprs) . " }";
+       return "do { " . $self->lineseq(@kids) . " }";
     } else {
-       return join(";\n", @exprs);     
+       my $lineseq = $self->lineseq(@kids);
+       return (length ($lineseq) ? "$lineseq;" : "");
     }
 }
 
-sub pp_lineseq { pp_scope(@_) }
+sub pp_scope { scopeop(0, @_); }
+sub pp_lineseq { scopeop(0, @_); }
+sub pp_leave { scopeop(1, @_); }
 
 # The BEGIN {} is used here because otherwise this code isn't executed
 # when you run B::Deparse on itself.
@@ -598,8 +1030,9 @@ BEGIN { map($globalnames{$_}++, "SIG", "STDIN", "STDOUT", "STDERR", "INC",
 sub gv_name {
     my $self = shift;
     my $gv = shift;
+Carp::confess() if $gv->isa("B::CV");
     my $stash = $gv->STASH->NAME;
-    my $name = $gv->NAME;
+    my $name = $gv->SAFENAME;
     if ($stash eq $self->{'curstash'} or $globalnames{$name}
        or $name =~ /^[^A-Za-z_]/)
     {
@@ -607,32 +1040,120 @@ sub gv_name {
     } else {
        $stash = $stash . "::";
     }
-    if ($name =~ /^([\cA-\cZ])$/) {
-       $name = "^" . chr(64 + ord($1));
+    if ($name =~ /^\^../) {
+        $name = "{$name}";       # ${^WARNING_BITS} etc
     }
     return $stash . $name;
 }
 
-# Notice how subs and formats are inserted between statements here
-sub pp_nextstate {
-    my $self = shift;
-    my($op, $cx) = @_;
-    my @text;
-    @text = $op->label . ": " if $op->label;
+# Recurses down the tree, looking for a COP
+sub find_cop {
+    my ($self, $op) = @_;
+    if ($op->flags & OPf_KIDS) {
+       for (my $o=$op->first; $$o; $o=$o->sibling) {
+           return $o if is_state($o);
+           my $r = $self->find_cop($o);
+           return $r if defined $r;
+       }
+    }
+    return undef;
+}
+
+# Returns a list of subs which should be inserted before the COP
+sub cop_subs {
+    my ($self, $op, $out_seq) = @_;
     my $seq = $op->cop_seq;
+    # If we have nephews, then our sequence number indicates
+    # the cop_seq of the end of some sort of scope.
+    if (class($op->sibling) ne "NULL" && $op->sibling->flags & OPf_KIDS
+       and my $ncop = $self->find_cop($op->sibling)) {
+       $seq = $ncop->cop_seq;
+    }
+    $seq = $out_seq if defined($out_seq) && $out_seq < $seq;
+    return $self->seq_subs($seq);
+}
+
+sub seq_subs {
+    my ($self, $seq) = @_;
+    my @text;
+#push @text, "# ($seq)\n";
+
     while (scalar(@{$self->{'subs_todo'}})
           and $seq > $self->{'subs_todo'}[0][0]) {
        push @text, $self->next_todo;
     }
-    my $stash = $op->stash->NAME;
+    return @text;
+}
+
+# Notice how subs and formats are inserted between statements here;
+# also $[ assignments and pragmas.
+sub pp_nextstate {
+    my $self = shift;
+    my($op, $cx) = @_;
+    $self->{'curcop'} = $op;
+    my @text;
+    @text = $op->label . ": " if $op->label;
+#push @text, "# ", $op->cop_seq, "\n";
+    push @text, $self->cop_subs($op);
+    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 . 
+         ' "' . $op->file, qq'"\n';
+    }
+
+    if ($self->{'arybase'} != $op->arybase) {
+       push @text, '$[ = '. $op->arybase .";\n";
+       $self->{'arybase'} = $op->arybase;
+    }
+
+    my $warnings = $op->warnings;
+    my $warning_bits;
+    if ($warnings->isa("B::SPECIAL") && $$warnings == 4) {
+       $warning_bits = $warnings::Bits{"all"};
+    }
+    elsif ($warnings->isa("B::SPECIAL")) {
+        $warning_bits = "\0"x12;
+    }
+    else {
+       $warning_bits = $warnings->PV & WARN_MASK;
+    }
+
+    if ($self->{'warnings'} ne $warning_bits) {
+       push @text, declare_warnings($self->{'warnings'}, $warning_bits);
+       $self->{'warnings'} = $warning_bits;
+    }
+
+    if ($self->{'hints'} != $op->private) {
+       push @text, declare_hints($self->{'hints'}, $op->private);
+       $self->{'hints'} = $op->private;
+    }
+
     return join("", @text);
 }
 
+sub declare_warnings {
+    my ($from, $to) = @_;
+    if ($to eq warnings::bits("all")) {
+       return "use warnings;\n";
+    }
+    elsif ($to eq "\0"x12) {
+       return "no warnings;\n";
+    }
+    return "BEGIN {\${^WARNING_BITS} = ".cstring($to)."}\n";
+}
+
+sub declare_hints {
+    my ($from, $to) = @_;
+    my $bits = $to;
+    return sprintf "BEGIN {\$^H &= ~0xFF; \$^H |= %x}\n", $bits;
+}
+
 sub pp_dbstate { pp_nextstate(@_) }
+sub pp_setstate { pp_nextstate(@_) }
 
 sub pp_unstack { return "" } # see also leaveloop
 
@@ -645,9 +1166,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") }
@@ -681,18 +1202,19 @@ 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->ppaddr =~ /^pp_(i_)?negate$/) {
+    if ($op->first->name =~ /^(i_)?negate$/) {
        # avoid --$x
        $self->pfixop($op, $cx, "-", 21.5);
     } else {
@@ -711,11 +1233,9 @@ sub pp_not {
     }
 }
 
-sub OPf_SPECIAL () { 128 }
-
 sub unop {
     my $self = shift;
-    my($op, $cx, $name, $prec, $flags) = (@_, 0, 0);
+    my($op, $cx, $name) = @_;
     my $kid;
     if ($op->flags & OPf_KIDS) {
        $kid = $op->first;
@@ -725,36 +1245,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_ucfirst { unop(@_, "ucfirst") }
-sub pp_lcfirst { unop(@_, "lcfirst") }
-sub pp_uc { unop(@_, "uc") }
-sub pp_lc { unop(@_, "lc") }
-sub pp_quotemeta { unop(@_, "quotemeta") }
+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") }
@@ -770,7 +1285,6 @@ sub pp_prototype { unop(@_, "prototype") }
 sub pp_close { unop(@_, "close") }
 sub pp_fileno { unop(@_, "fileno") }
 sub pp_umask { unop(@_, "umask") }
-sub pp_binmode { unop(@_, "binmode") }
 sub pp_untie { unop(@_, "untie") }
 sub pp_tied { unop(@_, "tied") }
 sub pp_dbmclose { unop(@_, "dbmclose") }
@@ -780,19 +1294,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") }
@@ -814,39 +1328,58 @@ sub pp_lock { unop(@_, "lock") }
 sub pp_exists {
     my $self = shift;
     my($op, $cx) = @_;
+    my $arg;
+    if ($op->private & OPpEXISTS_SUB) {
+       # Checking for the existence of a subroutine
+       return $self->maybe_parens_func("exists",
+                               $self->pp_rv2cv($op->first, 16), $cx, 16);
+    }
+    if ($op->flags & OPf_SPECIAL) {
+       # Array element, not hash element
+       return $self->maybe_parens_func("exists",
+                               $self->pp_aelem($op->first, 16), $cx, 16);
+    }
     return $self->maybe_parens_func("exists", $self->pp_helem($op->first, 16),
                                    $cx, 16);
 }
 
-sub OPpSLICE () { 64 }
-
 sub pp_delete {
     my $self = shift;
     my($op, $cx) = @_;
     my $arg;
     if ($op->private & OPpSLICE) {
+       if ($op->flags & OPf_SPECIAL) {
+           # Deleting from an array, not a hash
+           return $self->maybe_parens_func("delete",
+                                       $self->pp_aslice($op->first, 16),
+                                       $cx, 16);
+       }
        return $self->maybe_parens_func("delete",
                                        $self->pp_hslice($op->first, 16),
                                        $cx, 16);
     } else {
+       if ($op->flags & OPf_SPECIAL) {
+           # Deleting from an array, not a hash
+           return $self->maybe_parens_func("delete",
+                                       $self->pp_aelem($op->first, 16),
+                                       $cx, 16);
+       }
        return $self->maybe_parens_func("delete",
                                        $self->pp_helem($op->first, 16),
                                        $cx, 16);
     }
 }
 
-sub OPp_CONST_BARE () { 64 }
-
 sub pp_require {
     my $self = shift;
     my($op, $cx) = @_;
-    if (class($op) eq "UNOP" and $op->first->ppaddr eq "pp_const"
-       and $op->first->private & OPp_CONST_BARE)
+    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)";
+       return "require $name";
     } else {   
        $self->unop($op, $cx, "require");
     }
@@ -867,20 +1400,19 @@ 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];
 }
 
-sub OPf_REF () { 16 }
-
 sub pp_refgen {
     my $self = shift;  
     my($op, $cx) = @_;
     my $kid = $op->first;
-    if ($kid->ppaddr eq "pp_null") {
+    if ($kid->name eq "null") {
        $kid = $kid->first;
-       if ($kid->ppaddr eq "pp_anonlist" || $kid->ppaddr eq "pp_anonhash") {
-           my($pre, $post) = @{{"pp_anonlist" => ["[","]"],
-                                "pp_anonhash" => ["{","}"]}->{$kid->ppaddr}};
+       if ($kid->name eq "anonlist" || $kid->name eq "anonhash") {
+           my($pre, $post) = @{{"anonlist" => ["[","]"],
+                                "anonhash" => ["{","}"]}->{$kid->name}};
            my($expr, @exprs);
            $kid = $kid->first->sibling; # skip pushmark
            for (; !null($kid); $kid = $kid->sibling) {
@@ -889,16 +1421,25 @@ sub pp_refgen {
            }
            return $pre . join(", ", @exprs) . $post;
        } elsif (!null($kid->sibling) and 
-                $kid->sibling->ppaddr eq "pp_anoncode") {
+                $kid->sibling->name eq "anoncode") {
            return "sub " .
                $self->deparse_sub($self->padval($kid->sibling->targ));
-       } elsif ($kid->ppaddr eq "pp_pushmark"
-                and $kid->sibling->ppaddr =~ /^pp_(pad|rv2)[ah]v$/
-                and not $kid->sibling->flags & OPf_REF) {
-           # The @a in \(@a) isn't in ref context, but only when the
-           # parens are there.
-           return "\\(" . $self->deparse($kid->sibling, 1) . ")";
-       }
+       } elsif ($kid->name eq "pushmark") {
+            my $sib_name = $kid->sibling->name;
+            if ($sib_name =~ /^(pad|rv2)[ah]v$/
+                and not $kid->sibling->flags & OPf_REF)
+            {
+                # The @a in \(@a) isn't in ref context, but only when the
+                # parens are there.
+                return "\\(" . $self->deparse($kid->sibling, 1) . ")";
+            } elsif ($sib_name eq 'entersub') {
+                my $text = $self->deparse($kid->sibling, 1);
+                # Always show parens for \(&func()), but only with -p otherwise
+                $text = "($text)" if $self->{'parens'}
+                                 or $kid->sibling->private & OPpENTERSUB_AMPER;
+                return "\\$text";
+            }
+        }
     }
     $self->pfixop($op, $cx, "\\", 20);
 }
@@ -909,13 +1450,31 @@ sub pp_readline {
     my $self = shift;
     my($op, $cx) = @_;
     my $kid = $op->first;
-    $kid = $kid->first if $kid->ppaddr eq "pp_rv2gv"; # <$fh>
-    if ($kid->ppaddr eq "pp_rv2gv") {
-       $kid = $kid->first;
-    }
+    $kid = $kid->first if $kid->name eq "rv2gv"; # <$fh>
     return "<" . $self->deparse($kid, 1) . ">";
 }
 
+# Unary operators that can occur as pseudo-listops inside double quotes
+sub dq_unop {
+    my $self = shift;
+    my($op, $cx, $name, $prec, $flags) = (@_, 0, 0);
+    my $kid;
+    if ($op->flags & OPf_KIDS) {
+       $kid = $op->first;
+       # If there's more than one kid, the first is an ex-pushmark.
+       $kid = $kid->sibling if not null $kid->sibling;
+       return $self->maybe_parens_unop($name, $kid, $cx);
+    } else {
+       return $name .  ($op->flags & OPf_SPECIAL ? "()" : "");       
+    }
+}
+
+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 { maybe_targmy(@_, \&dq_unop, "quotemeta") }
+
 sub loopex {
     my $self = shift;
     my ($op, $cx, $name) = @_;
@@ -943,7 +1502,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;
@@ -983,19 +1542,17 @@ sub pp_ftbinary { ftst(@_, "-B") }
 sub SWAP_CHILDREN () { 1 }
 sub ASSIGN () { 2 } # has OP= variant
 
-sub OPf_STACKED () { 64 }
-
 my(%left, %right);
 
 sub assoc_class {
     my $op = shift;
-    my $name = $op->ppaddr;
-    if ($name eq "pp_concat" and $op->first->ppaddr eq "pp_concat") {
+    my $name = $op->name;
+    if ($name eq "concat" and $op->first->name eq "concat") {
        # avoid spurious `=' -- see comment in pp_concat
-       return "pp_concat";
+       return "concat";
     }
-    if ($name eq "pp_null" and class($op) eq "UNOP"
-       and $op->first->ppaddr =~ /^pp_(and|x?or)$/
+    if ($name eq "null" and class($op) eq "UNOP"
+       and $op->first->name =~ /^(and|x?or)$/
        and null $op->first->sibling)
     {
        # Like all conditional constructs, OP_ANDs and OP_ORs are topped
@@ -1012,25 +1569,25 @@ sub assoc_class {
 # $a + $b + $c is equivalent to ($a + $b) + $c
 
 BEGIN {
-    %left = ('pp_multiply' => 19, 'pp_i_multiply' => 19,
-            'pp_divide' => 19, 'pp_i_divide' => 19,
-            'pp_modulo' => 19, 'pp_i_modulo' => 19,
-            'pp_repeat' => 19,
-            'pp_add' => 18, 'pp_i_add' => 18,
-            'pp_subtract' => 18, 'pp_i_subtract' => 18,
-            'pp_concat' => 18,
-            'pp_left_shift' => 17, 'pp_right_shift' => 17,
-            'pp_bit_and' => 13,
-            'pp_bit_or' => 12, 'pp_bit_xor' => 12,
-            'pp_and' => 3,
-            'pp_or' => 2, 'pp_xor' => 2,
+    %left = ('multiply' => 19, 'i_multiply' => 19,
+            'divide' => 19, 'i_divide' => 19,
+            'modulo' => 19, 'i_modulo' => 19,
+            'repeat' => 19,
+            'add' => 18, 'i_add' => 18,
+            'subtract' => 18, 'i_subtract' => 18,
+            'concat' => 18,
+            'left_shift' => 17, 'right_shift' => 17,
+            'bit_and' => 13,
+            'bit_or' => 12, 'bit_xor' => 12,
+            'and' => 3,
+            'or' => 2, 'xor' => 2,
            );
 }
 
 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);
@@ -1043,27 +1600,27 @@ sub deparse_binop_left {
 # $a = $b = $c is equivalent to $a = ($b = $c)
 
 BEGIN {
-    %right = ('pp_pow' => 22,
-             'pp_sassign=' => 7, 'pp_aassign=' => 7,
-             'pp_multiply=' => 7, 'pp_i_multiply=' => 7,
-             'pp_divide=' => 7, 'pp_i_divide=' => 7,
-             'pp_modulo=' => 7, 'pp_i_modulo=' => 7,
-             'pp_repeat=' => 7,
-             'pp_add=' => 7, 'pp_i_add=' => 7,
-             'pp_subtract=' => 7, 'pp_i_subtract=' => 7,
-             'pp_concat=' => 7,
-             'pp_left_shift=' => 7, 'pp_right_shift=' => 7,
-             'pp_bit_and=' => 7,
-             'pp_bit_or=' => 7, 'pp_bit_xor=' => 7,
-             'pp_andassign' => 7,
-             'pp_orassign' => 7,
+    %right = ('pow' => 22,
+             'sassign=' => 7, 'aassign=' => 7,
+             'multiply=' => 7, 'i_multiply=' => 7,
+             'divide=' => 7, 'i_divide=' => 7,
+             'modulo=' => 7, 'i_modulo=' => 7,
+             'repeat=' => 7,
+             'add=' => 7, 'i_add=' => 7,
+             'subtract=' => 7, 'i_subtract=' => 7,
+             'concat=' => 7,
+             'left_shift=' => 7, 'right_shift=' => 7,
+             'bit_and=' => 7,
+             'bit_or=' => 7, 'bit_xor=' => 7,
+             'andassign' => 7,
+             'orassign' => 7,
             );
 }
 
 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);
@@ -1090,23 +1647,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) }
@@ -1137,14 +1694,15 @@ 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;
     my $right = $op->last;
     my $eq = "";
     my $prec = 18;
-    if ($op->flags & OPf_STACKED and $op->first->ppaddr ne "pp_concat") {
+    if ($op->flags & OPf_STACKED and $op->first->name ne "concat") {
        $eq = "=";
        $prec = 7;
     }
@@ -1205,11 +1763,14 @@ sub logop {
     my ($op, $cx, $lowop, $lowprec, $highop, $highprec, $blockname) = @_;
     my $left = $op->first;
     my $right = $op->first->sibling;
-    if ($cx == 0 and is_scope($right) and $blockname) { # if ($a) {$b}
+    if ($cx == 0 and is_scope($right) and $blockname
+       and $self->{'expand'} < 7)
+    { # if ($a) {$b}
        $left = $self->deparse($left, 1);
        $right = $self->deparse($right, 0);
        return "$blockname ($left) {\n\t$right\n\b}\cK";
-    } elsif ($cx == 0 and $blockname and not $self->{'parens'}) { # $b if $a
+    } elsif ($cx == 0 and $blockname and not $self->{'parens'}
+            and $self->{'expand'} < 7) { # $b if $a
        $right = $self->deparse($right, 1);
        $left = $self->deparse($left, 1);
        return "$right $blockname $left";
@@ -1225,7 +1786,10 @@ sub logop {
 }
 
 sub pp_and { logop(@_, "and", 3, "&&", 11, "if") }
-sub pp_or {  logop(@_, "or",  2, "||", 10, "unless") }
+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 {
@@ -1263,20 +1827,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") }
@@ -1286,6 +1850,7 @@ sub pp_return { listop(@_, "return") }
 sub pp_open { listop(@_, "open") }
 sub pp_pipe_op { listop(@_, "pipe") }
 sub pp_tie { listop(@_, "tie") }
+sub pp_binmode { listop(@_, "binmode") }
 sub pp_dbmopen { listop(@_, "dbmopen") }
 sub pp_sselect { listop(@_, "select") }
 sub pp_select { listop(@_, "select") }
@@ -1297,10 +1862,9 @@ sub pp_syswrite { listop(@_, "syswrite") }
 sub pp_send { listop(@_, "send") }
 sub pp_recv { listop(@_, "recv") }
 sub pp_seek { listop(@_, "seek") }
-sub pp_truncate { listop(@_, "truncate") }
 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") }
@@ -1310,23 +1874,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") }
@@ -1357,6 +1921,32 @@ sub pp_glob {
     }
 }
 
+# Truncate is special because OPf_SPECIAL makes a bareword first arg
+# be a filehandle. This could probably be better fixed in the core
+# by moving the GV lookup into ck_truc.
+
+sub pp_truncate {
+    my $self = shift;
+    my($op, $cx) = @_;
+    my(@exprs);
+    my $parens = ($cx >= 5) || $self->{'parens'};
+    my $kid = $op->first->sibling;
+    my $fh;
+    if ($op->flags & OPf_SPECIAL) {
+       # $kid is an OP_CONST
+       $fh = $self->const_sv($kid)->PV;
+    } else {
+       $fh = $self->deparse($kid, 6);
+        $fh = "+$fh" if not $parens and substr($fh, 0, 1) eq "(";
+    }
+    my $len = $self->deparse($kid->sibling, 6);
+    if ($parens) {
+       return "truncate($fh, $len)";
+    } else {
+       return "truncate $fh, $len";
+    }
+}
+
 sub indirop {
     my $self = shift;
     my($op, $cx, $name) = @_;
@@ -1378,8 +1968,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);
 }
 
@@ -1395,7 +1984,7 @@ sub mapop {
     $kid = $kid->first->sibling; # skip a pushmark
     my $code = $kid->first; # skip a null
     if (is_scope $code) {
-       $code = "{" . $self->deparse($code, 1) . "} ";
+       $code = "{" . $self->deparse($code, 0) . "} ";
     } else {
        $code = $self->deparse($code, 24) . ", ";
     }
@@ -1415,30 +2004,30 @@ sub pp_list {
     my($op, $cx) = @_;
     my($expr, @exprs);
     my $kid = $op->first->sibling; # skip pushmark
-    return $self->deparse($kid, $cx) if null $kid->sibling;
     my $lop;
     my $local = "either"; # could be local(...) or my(...)
     for ($lop = $kid; !null($lop); $lop = $lop->sibling) {
        # This assumes that no other private flags equal 128, and that
        # OPs that store things other than flags in their op_private,
        # like OP_AELEMFAST, won't be immediate children of a list.
-       unless ($lop->private & OPp_LVAL_INTRO or $lop->ppaddr eq "pp_undef")
+       unless ($lop->private & OPpLVAL_INTRO or $lop->name eq "undef")
        {
            $local = ""; # or not
            last;
        }
-       if ($lop->ppaddr =~ /^pp_pad[ash]v$/) { # my()
+       if ($lop->name =~ /^pad[ash]v$/) { # my()
            ($local = "", last) if $local eq "local";
            $local = "my";
-       } elsif ($lop->ppaddr ne "pp_undef") { # local()
+       } elsif ($lop->name ne "undef") { # local()
            ($local = "", last) if $local eq "my";
            $local = "local";
        }
     }
     $local = "" if $local eq "either"; # no point if it's all undefs
+    return $self->deparse($kid, $cx) if null $kid->sibling and not $local;
     for (; !null($kid); $kid = $kid->sibling) {
        if ($local) {
-           if (class($kid) eq "UNOP" and $kid->first->ppaddr eq "pp_gvsv") {
+           if (class($kid) eq "UNOP" and $kid->first->name eq "gvsv") {
                $lop = $kid->first;
            } else {
                $lop = $kid;
@@ -1458,6 +2047,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) = @_;
@@ -1465,55 +2061,68 @@ sub pp_cond_expr {
     my $true = $cond->sibling;
     my $false = $true->sibling;
     my $cuddle = $self->{'cuddle'};
-    $cond = $self->deparse($cond, 1);
-    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))
+           and $self->{'expand'} < 7) {
+       $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->ppaddr eq "pp_lineseq") { # braces w/o scope => elsif
-       my $head = "if ($cond) {\n\t$true\n\b}";
-       my @elsifs;
-       while (!null($false) and $false->ppaddr eq "pp_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 {
+sub loop_common {
     my $self = shift;
-    my($op, $cx) = @_;
+    my($op, $cx, $init) = @_;
     my $enter = $op->first;
     my $kid = $enter->sibling;
-    local($self->{'curstash'}) = $self->{'curstash'};
+    local(@$self{qw'curstash warnings hints'})
+               = @$self{qw'curstash warnings hints'};
     my $head = "";
     my $bare = 0;
-    if ($kid->ppaddr eq "pp_lineseq") { # bare or infinite loop 
+    my $body;
+    my $cond = undef;
+    my $out_seq = $self->{'curcop'}->cop_seq;;
+    if ($kid->name eq "lineseq") { # bare or infinite loop 
        if (is_state $kid->last) { # infinite
            $head = "for (;;) "; # shorter than while (1)
+           $cond = "";
        } else {
            $bare = 1;
        }
-    } elsif ($enter->ppaddr eq "pp_enteriter") { # foreach
+       $body = $kid;
+    } elsif ($enter->name eq "enteriter") { # foreach
        my $ary = $enter->first->sibling; # first was pushmark
        my $var = $ary->sibling;
-       $ary = $self->deparse($ary, 1);
+       if ($enter->flags & OPf_STACKED
+           and not null $ary->first->sibling->sibling)
+       {
+           $ary = $self->deparse($ary->first->sibling, 9) . " .. " .
+             $self->deparse($ary->first->sibling->sibling, 9);
+       } else {
+           $ary = $self->deparse($ary, 1);
+       }
        if (null $var) {
            if ($enter->flags & OPf_SPECIAL) { # thread special var
                $var = $self->pp_threadsv($enter, 1);
@@ -1528,64 +2137,72 @@ sub pp_leaveloop {
                    $var = "my " . $var;
                }
            }
-       } elsif ($var->ppaddr eq "pp_rv2gv") {
+       } elsif ($var->name eq "rv2gv") {
            $var = $self->pp_rv2sv($var, 1);
-       } elsif ($var->ppaddr eq "pp_gv") {
+       } elsif ($var->name eq "gv") {
            $var = "\$" . $self->deparse($var, 1);
        }
        $head = "foreach $var ($ary) ";
-       $kid = $kid->first->first->sibling; # skip OP_AND and OP_ITER
-    } elsif ($kid->ppaddr eq "pp_null") { # while/until
+       $body = $kid->first->first->sibling; # skip OP_AND and OP_ITER
+    } elsif ($kid->name eq "null") { # while/until
        $kid = $kid->first;
-       my $name = {"pp_and" => "while", "pp_or" => "until"}
-                   ->{$kid->ppaddr};
-       $head = "$name (" . $self->deparse($kid->first, 1) . ") ";
-       $kid = $kid->first->sibling;
-    } elsif ($kid->ppaddr eq "pp_stub") { # bare and empty
+       my $name = {"and" => "while", "or" => "until"}->{$kid->name};
+       $cond = $self->deparse($kid->first, 1);
+       $head = "$name ($cond) ";
+       $body = $kid->first->sibling;
+    } elsif ($kid->name eq "stub") { # bare and empty
        return "{;}"; # {} could be a hashref
     }
-    # The third-to-last kid is the continue block if the pointer used
-    # by `next BLOCK' points to its first OP, which happens to be the
-    # the op_next of the head of the _previous_ statement. 
-    # Unless it's a bare loop, in which case it's last, since there's
-    # no unstack or extra nextstate.
-    my($cont, $precont);
-    if ($bare) {
-       $cont = $kid->first;
-       while (!null($cont->sibling)) {
-           $precont = $cont;
-           $cont = $cont->sibling;
-       }       
-    } else {
-       $cont = $kid->first;
-       while (!null($cont->sibling->sibling->sibling)) {
-           $precont = $cont;
-           $cont = $cont->sibling;
+    # If there isn't a continue block, then the next pointer for the loop
+    # will point to the unstack, which is kid's penultimate child, except
+    # in a bare loop, when it will point to the leaveloop. When neither of
+    # these conditions hold, then the third-to-last child in the continue
+    # block (or the last in a bare loop).
+    my $cont_start = $enter->nextop;
+    my $cont;
+    if ($$cont_start != $$op and $ {$cont_start->sibling} != $ {$body->last}) {
+       if ($bare) {
+           $cont = $body->last;
+       } else {
+           $cont = $body->first;
+           while (!null($cont->sibling->sibling->sibling)) {
+               $cont = $cont->sibling;
+           }
        }
-    }
-#    cluck $self->{'curcv'}->GV->NAME unless $precont;
-    if ($precont and $ {$precont->next} == $ {$enter->nextop}) {
-       my $state = $kid->first;
+       my $state = $body->first;
        my $cuddle = $self->{'cuddle'};
-       my($expr, @exprs);
+       my @states;
        for (; $$state != $$cont; $state = $state->sibling) {
-           $expr = "";
-           if (is_state $state) {
-               $expr = $self->deparse($state, 0);
-               $state = $state->sibling;
-               last if null $kid;
-           }
-           $expr .= $self->deparse($state, 0);
-           push @exprs, $expr if $expr;
+           push @states, $state;
+       }
+       $body = $self->lineseq(@states);
+       if (defined $cond and not is_scope $cont and $self->{'expand'} < 3) {
+           $head = "for ($init; $cond; " . $self->deparse($cont, 1) .") ";
+           $cont = "\cK";
+       } else {
+           $cont = $cuddle . "continue {\n\t" .
+             $self->deparse($cont, 0) . "\n\b}\cK";
        }
-       $kid = join(";\n", @exprs);
-       $cont = $cuddle . "continue {\n\t" .
-         $self->deparse($cont, 0) . "\n\b}\cK";
     } else {
+       return "" if !defined $body;
        $cont = "\cK";
-       $kid = $self->deparse($kid, 0);
+       $body = $self->deparse($body, 0);
     }
-    return $head . "{\n\t" . $kid . "\n\b}" . $cont;
+    $body .= "\n";
+    # If we have say C<{my $x=2; sub x{$x}}>, the sub must go inside
+    # the loop. So we insert any subs which are due here.
+    $body .= join"", $self->seq_subs($out_seq);
+
+    return $head . "{\n\t" . $body . "\b}" . $cont;
+}
+
+sub pp_leaveloop { loop_common(@_, "") }
+
+sub for_loop {
+    my $self = shift;
+    my($op, $cx) = @_;
+    my $init = $self->deparse($op, 1);
+    return $self->loop_common($op->sibling, $cx, $init);
 }
 
 sub pp_leavetry {
@@ -1593,28 +2210,29 @@ sub pp_leavetry {
     return "eval {\n\t" . $self->pp_leave(@_) . "\n\b}";
 }
 
-sub OP_CONST () { 5 }
-sub OP_STRINGIFY () { 65 }
+BEGIN { eval "sub OP_CONST () {" . opnumber("const") . "}" }
+BEGIN { eval "sub OP_STRINGIFY () {" . opnumber("stringify") . "}" }
 
 sub pp_null {
     my $self = shift;
     my($op, $cx) = @_;
     if (class($op) eq "OP") {
-       return "'???'" if $op->targ == OP_CONST; # old value is lost
-    } elsif ($op->first->ppaddr eq "pp_pushmark") {
+       # old value is lost
+       return $self->{'ex_const'} if $op->targ == OP_CONST;
+    } elsif ($op->first->name eq "pushmark") {
        return $self->pp_list($op, $cx);
-    } elsif ($op->first->ppaddr eq "pp_enter") {
+    } 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->ppaddr eq "pp_readline" and
+            $op->first->sibling->name eq "readline" and
             $op->first->sibling->flags & OPf_STACKED) {
        return $self->maybe_parens($self->deparse($op->first, 7) . " = "
                                   . $self->deparse($op->first->sibling, 7),
                                   $cx, 7);
     } elsif (!null($op->first->sibling) and
-            $op->first->sibling->ppaddr eq "pp_trans" and
+            $op->first->sibling->name eq "trans" and
             $op->first->sibling->flags & OPf_STACKED) {
        return $self->maybe_parens($self->deparse($op->first, 20) . " =~ "
                                   . $self->deparse($op->first->sibling, 20),
@@ -1627,8 +2245,7 @@ sub pp_null {
 sub padname {
     my $self = shift;
     my $targ = shift;
-    my $str = $self->padname_sv($targ)->PV;
-    return padname_fix($str);
+    return $self->padname_sv($targ)->PVX;
 }
 
 sub padany {
@@ -1661,32 +2278,44 @@ 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;
-    return "\$" . $self->gv_name($gv) . "[" . $op->private . "]";
+    my $gv = $self->gv_or_padgv($op);
+    return "\$" . $self->gv_name($gv) . "[" .
+                 ($op->private + $self->{'arybase'}) . "]";
 }
 
 sub rv2x {
     my $self = shift;
     my($op, $cx, $type) = @_;
     my $kid = $op->first;
-    my $scope = is_scope($kid);
-    $kid = $self->deparse($kid, 0);
-    return $type . ($scope ? "{$kid}" : $kid);
+    my $str = $self->deparse($kid, 0);
+    return $type . (is_scalar($kid) ? $str : "{$str}");
 }
 
 sub pp_rv2sv { maybe_local(@_, rv2x(@_, "\$")) }
@@ -1697,10 +2326,11 @@ sub pp_rv2gv { maybe_local(@_, rv2x(@_, "*")) }
 sub pp_av2arylen {
     my $self = shift;
     my($op, $cx) = @_;
-    if ($op->first->ppaddr eq "pp_padav") {
+    if ($op->first->name eq "padav") {
        return $self->maybe_local($op, $cx, '$#' . $self->padany($op->first));
     } else {
-       return $self->maybe_local($op, $cx, $self->rv2x($op->first, '$#'));
+       return $self->maybe_local($op, $cx,
+                                 $self->rv2x($op->first, $cx, '$#'));
     }
 }
 
@@ -1711,23 +2341,41 @@ sub pp_rv2av {
     my $self = shift;
     my($op, $cx) = @_;
     my $kid = $op->first;
-    if ($kid->ppaddr eq "pp_const") { # constant list
-       my $av = $kid->sv;
+    if ($kid->name eq "const") { # constant list
+       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;
     my ($op, $cx, $left, $right, $padname) = @_;
     my($array, $idx) = ($op->first, $op->first->sibling);
-    unless ($array->ppaddr eq $padname) { # Maybe this has been fixed  
+    unless ($array->name eq $padname) { # Maybe this has been fixed    
        $array = $array->first; # skip rv2av (or ex-rv2av in _53+)
     }
-    if ($array->ppaddr eq $padname) {
+    if ($array->name eq $padname) {
        $array = $self->padany($array);
     } elsif (is_scope($array)) { # ${expr}[0]
        $array = "{" . $self->deparse($array, 0) . "}";
@@ -1735,24 +2383,42 @@ sub elem {
        $array = $self->deparse($array, 24);
     } else {
        # $x[20][3]{hi} or expr->[20]
-       my $arrow;
-       $arrow = "->" if $array->ppaddr !~ /^pp_[ah]elem$/;
+       my $arrow = is_subscriptable($array) ? "" : "->";
        return $self->deparse($array, 24) . $arrow .
            $left . $self->deparse($idx, 1) . $right;
     }
     $idx = $self->deparse($idx, 1);
+
+    # Outer parens in an array index will confuse perl
+    # if we're interpolating in a regular expression, i.e.
+    # /$x$foo[(-1)]/ is *not* the same as /$x$foo[-1]/
+    #
+    # If $self->{parens}, then an initial '(' will
+    # definitely be paired with a final ')'. If
+    # !$self->{parens}, the misleading parens won't
+    # have been added in the first place.
+    #
+    # [You might think that we could get "(...)...(...)"
+    # where the initial and final parens do not match
+    # each other. But we can't, because the above would
+    # only happen if there's an infix binop between the
+    # two pairs of parens, and *that* means that the whole
+    # expression would be parenthesized as well.]
+    #
+    $idx =~ s/^\((.*)\)$/$1/ if $self->{'parens'};
+
     return "\$" . $array . $left . $idx . $right;
 }
 
-sub pp_aelem { maybe_local(@_, elem(@_, "[", "]", "pp_padav")) }
-sub pp_helem { maybe_local(@_, elem(@_, "{", "}", "pp_padhv")) }
+sub pp_aelem { maybe_local(@_, elem(@_, "[", "]", "padav")) }
+sub pp_helem { maybe_local(@_, elem(@_, "{", "}", "padhv")) }
 
 sub pp_gelem {
     my $self = shift;
     my($op, $cx) = @_;
     my($glob, $part) = ($op->first, $op->last);
     $glob = $glob->first; # skip rv2gv
-    $glob = $glob->first if $glob->ppaddr eq "pp_rv2gv"; # this one's a bug
+    $glob = $glob->first if $glob->name eq "rv2gv"; # this one's a bug
     my $scope = is_scope($glob);
     $glob = $self->deparse($glob, 0);
     $part = $self->deparse($part, 1);
@@ -1772,16 +2438,16 @@ sub slice {
     }
     $array = $last;
     $array = $array->first
-       if $array->ppaddr eq $regname or $array->ppaddr eq "pp_null";
+       if $array->name eq $regname or $array->name eq "null";
     if (is_scope($array)) {
        $array = "{" . $self->deparse($array, 0) . "}";
-    } elsif ($array->ppaddr eq $padname) {
+    } elsif ($array->name eq $padname) {
        $array = $self->padany($array);
     } else {
        $array = $self->deparse($array, 24);
     }
     $kid = $op->first->sibling; # skip pushmark
-    if ($kid->ppaddr eq "pp_list") {
+    if ($kid->name eq "list") {
        $kid = $kid->first->sibling; # skip list, pushmark
        for (; !null $kid; $kid = $kid->sibling) {
            push @elems, $self->deparse($kid, 6);
@@ -1793,10 +2459,8 @@ sub slice {
     return "\@" . $array . $left . $list . $right;
 }
 
-sub pp_aslice { maybe_local(@_, slice(@_, "[", "]", 
-                                     "pp_rv2av", "pp_padav")) }
-sub pp_hslice { maybe_local(@_, slice(@_, "{", "}",
-                                     "pp_rv2hv", "pp_padhv")) }
+sub pp_aslice { maybe_local(@_, slice(@_, "[", "]", "rv2av", "padav")) }
+sub pp_hslice { maybe_local(@_, slice(@_, "{", "}", "rv2hv", "padhv")) }
 
 sub pp_lslice {
     my $self = shift;
@@ -1809,48 +2473,153 @@ sub pp_lslice {
     return "($list)" . "[$idx]";
 }
 
-sub OPpENTERSUB_AMPER () { 8 }
-
-sub OPf_WANT () { 3 }
-sub OPf_WANT_VOID () { 1 }
-sub OPf_WANT_SCALAR () { 2 }
-sub OPf_WANT_LIST () { 2 }
-
 sub want_scalar {
     my $op = shift;
     return ($op->flags & OPf_WANT) == OPf_WANT_SCALAR;
 }
 
-sub pp_entersub {
+sub want_list {
+    my $op = shift;
+    return ($op->flags & OPf_WANT) == OPf_WANT_LIST;
+}
+
+sub method {
     my $self = shift;
     my($op, $cx) = @_;
-    my $prefix = "";
-    my $amper = "";
-    my $proto = undef;
-    my $simple = 0;
-    my($kid, $args, @exprs);
-    if (not null $op->first->sibling) { # method
-       $kid = $op->first->sibling; # skip pushmark
-       my $obj = $self->deparse($kid, 24);
+    my $kid = $op->first->sibling; # skip pushmark
+    my($meth, $obj, @exprs);
+    if ($kid->name eq "list" and want_list $kid) {
+       # When an indirect object isn't a bareword but the args are in
+       # parens, the parens aren't part of the method syntax (the LLAFR
+       # doesn't apply), but they make a list with OPf_PARENS set that
+       # doesn't get flattened by the append_elem that adds the method,
+       # making a (object, arg1, arg2, ...) list where the object
+       # usually is. This can be distinguished from 
+       # `($obj, $arg1, $arg2)->meth()' (which is legal if $arg2 is an
+       # object) because in the later the list is in scalar context
+       # 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;
+       $kid = $kid->first->sibling; # skip pushmark
+       $obj = $kid;
+       $kid = $kid->sibling;
+       for (; not null $kid; $kid = $kid->sibling) {
+           push @exprs, $self->deparse($kid, 6);
+       }
+    } else {
+       $obj = $kid;
        $kid = $kid->sibling;
        for (; not null $kid->sibling; $kid = $kid->sibling) {
            push @exprs, $self->deparse($kid, 6);
        }
-       my $meth = $kid->first;
-       if ($meth->ppaddr eq "pp_const") {
-           $meth = $meth->sv->PV; # needs to be bare
+       $meth = $kid;
+    }
+    $obj = $self->deparse($obj, 24);
+    if ($meth->name eq "method_named") {
+       $meth = $self->const_sv($meth)->PV;
+    } else {
+       $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);
        }
-       $args = join(", ", @exprs);     
-       $kid = $obj . "->" . $meth;
-       if ($args) {
-           return $kid . "(" . $args . ")"; # parens mandatory
+    }
+    my $args = join(", ", @exprs);     
+    $kid = $obj . "->" . $meth;
+    if ($args) {
+       return $kid . "(" . $args . ")"; # parens mandatory
+    } else {
+       return $kid;
+    }
+}
+
+# returns "&" if the prototype doesn't match the args,
+# or ("", $args_after_prototype_demunging) if it does.
+sub check_proto {
+    my $self = shift;
+    my($proto, @args) = @_;
+    my($arg, $real);
+    my $doneok = 0;
+    my @reals;
+    # An unbackslashed @ or % gobbles up the rest of the args
+    $proto =~ s/([^\\]|^)([@%])(.*)$/$1$2/;
+    while ($proto) {
+       $proto =~ s/^ *([\\]?[\$\@&%*]|;)//;
+       my $chr = $1;
+       if ($chr eq "") {
+           return "&" if @args;
+       } elsif ($chr eq ";") {
+           $doneok = 1;
+       } elsif ($chr eq "@" or $chr eq "%") {
+           push @reals, map($self->deparse($_, 6), @args);
+           @args = ();
        } else {
-           return $kid; # toke.c fakes parens
-       }
+           $arg = shift @args;
+           last unless $arg;
+           if ($chr eq "\$") {
+               if (want_scalar $arg) {
+                   push @reals, $self->deparse($arg, 6);
+               } else {
+                   return "&";
+               }
+           } elsif ($chr eq "&") {
+               if ($arg->name =~ /^(s?refgen|undef)$/) {
+                   push @reals, $self->deparse($arg, 6);
+               } else {
+                   return "&";
+               }
+           } elsif ($chr eq "*") {
+               if ($arg->name =~ /^s?refgen$/
+                   and $arg->first->first->name eq "rv2gv")
+                 {
+                     $real = $arg->first->first; # skip refgen, null
+                     if ($real->first->name eq "gv") {
+                         push @reals, $self->deparse($real, 6);
+                     } else {
+                         push @reals, $self->deparse($real->first, 6);
+                     }
+                 } else {
+                     return "&";
+                 }
+           } elsif (substr($chr, 0, 1) eq "\\") {
+               $chr = substr($chr, 1);
+               if ($arg->name =~ /^s?refgen$/ and
+                   !null($real = $arg->first) and
+                   ($chr eq "\$" && is_scalar($real->first)
+                    or ($chr eq "\@"
+                        && $real->first->sibling->name
+                        =~ /^(rv2|pad)av$/)
+                    or ($chr eq "%"
+                        && $real->first->sibling->name
+                        =~ /^(rv2|pad)hv$/)
+                    #or ($chr eq "&" # This doesn't work
+                    #   && $real->first->name eq "rv2cv")
+                    or ($chr eq "*"
+                        && $real->first->name eq "rv2gv")))
+                 {
+                     push @reals, $self->deparse($real, 6);
+                 } else {
+                     return "&";
+                 }
+           }
+       }
     }
-    # else, not a method
+    return "&" if $proto and !$doneok; # too few args and no `;'
+    return "&" if @args;               # too many args
+    return ("", join ", ", @reals);
+}
+
+sub pp_entersub {
+    my $self = shift;
+    my($op, $cx) = @_;
+    return $self->method($op, $cx) unless null $op->first->sibling;
+    my $prefix = "";
+    my $amper = "";
+    my($kid, @exprs);
     if ($op->flags & OPf_SPECIAL) {
        $prefix = "do ";
     } elsif ($op->private & OPpENTERSUB_AMPER) {
@@ -1861,96 +2630,35 @@ sub pp_entersub {
     for (; not null $kid->sibling; $kid = $kid->sibling) {
        push @exprs, $kid;
     }
+    my $simple = 0;
+    my $proto = undef;
     if (is_scope($kid)) {
        $amper = "&";
        $kid = "{" . $self->deparse($kid, 0) . "}";
-    } elsif ($kid->first->ppaddr eq "pp_gv") {
-       my $gv = $kid->first->gv;
+    } elsif ($kid->first->name eq "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;
        }
-       $simple = 1;
+       $simple = 1; # only calls of named functions can be prototyped
        $kid = $self->deparse($kid, 24);
     } elsif (is_scalar $kid->first) {
        $amper = "&";
        $kid = $self->deparse($kid, 24);
     } else {
        $prefix = "";
-       $kid = $self->deparse($kid, 24) . "->";
-    }
-    if (defined $proto and not $amper) {
-       my($arg, $real);
-       my $doneok = 0;
-       my @args = @exprs;
-       my @reals;
-       $proto =~ s/([^\\]|^)([@%])(.*)$/$1$2/;
-       while ($proto) {
-           $proto =~ s/^ *([\\]?[\$\@&%*]|;)//;
-           my $chr = $1;
-           if ($chr eq "") {
-               undef $proto if @args;
-           } elsif ($chr eq ";") {
-               $doneok = 1;
-           } elsif ($chr eq "@" or $chr eq "%") {
-               push @reals, map($self->deparse($_, 6), @args);
-               @args = ();
-           } else {
-               $arg = shift @args;
-               undef $proto, last unless $arg;
-               if ($chr eq "\$") {
-                   if (want_scalar $arg) {
-                       push @reals, $self->deparse($arg, 6);
-                   } else {
-                       undef $proto;
-                   }
-               } elsif ($chr eq "&") {
-                   if ($arg->ppaddr =~ /pp_(s?refgen|undef)/) {
-                       push @reals, $self->deparse($arg, 6);
-                   } else {
-                       undef $proto;
-                   }
-               } elsif ($chr eq "*") {
-                   if ($arg->ppaddr =~ /^pp_s?refgen$/
-                       and $arg->first->first->ppaddr eq "pp_rv2gv")
-                   {
-                       $real = $arg->first->first; # skip refgen, null
-                       if ($real->first->ppaddr eq "pp_gv") {
-                           push @reals, $self->deparse($real, 6);
-                       } else {
-                           push @reals, $self->deparse($real->first, 6);
-                       }
-                   } else {
-                       undef $proto;
-                   }
-               } elsif (substr($chr, 0, 1) eq "\\") {
-                   $chr = substr($chr, 1);
-                   if ($arg->ppaddr =~ /^pp_s?refgen$/ and
-                       !null($real = $arg->first) and
-                       ($chr eq "\$" && is_scalar($real->first)
-                        or ($chr eq "\@"
-                            && $real->first->sibling->ppaddr 
-                            =~ /^pp_(rv2|pad)av$/)
-                        or ($chr eq "%"
-                            && $real->first->sibling->ppaddr
-                            =~ /^pp_(rv2|pad)hv$/)
-                        #or ($chr eq "&" # This doesn't work
-                        #   && $real->first->ppaddr eq "pp_rv2cv")
-                        or ($chr eq "*"
-                            && $real->first->ppaddr eq "pp_rv2gv")))
-                   {
-                       push @reals, $self->deparse($real, 6);
-                   } else {
-                       undef $proto;
-                   }
-               }
-           }
-       }
-       undef $proto if $proto and !$doneok;
-       undef $proto if @args;
-       $args = join(", ", @reals);
-       $amper = "";
-       unless (defined $proto) {
-           $amper = "&";
+       my $arrow = is_subscriptable($kid->first) ? "" : "->";
+       $kid = $self->deparse($kid, 24) . $arrow;
+    }
+
+    # Doesn't matter how many prototypes there are, if
+    # they haven't happened yet!
+    my $declared = exists $self->{'subs_declared'}{$kid};
+
+    my $args;
+    if ($declared and defined $proto and not $amper) {
+       ($amper, $args) = $self->check_proto($proto, @exprs);
+       if ($amper eq "&") {
            $args = join(", ", map($self->deparse($_, 6), @exprs));
        }
     } else {
@@ -1963,11 +2671,26 @@ sub pp_entersub {
            return $prefix . $amper. $kid;
        }
     } else {
-       if (defined $proto and $proto eq "") {
+       # glob() invocations can be translated into calls of
+       # CORE::GLOBAL::glob with an second parameter, a number.
+       # Reverse this.
+       if ($kid eq "CORE::GLOBAL::glob") {
+           $kid = "glob";
+           $args =~ s/\s*,[^,]+$//;
+       }
+
+       # It's a syntax error to call CORE::GLOBAL::foo without a prefix,
+       # so it must have been translated from a keyword call. Translate
+       # it back.
+       $kid =~ s/^CORE::GLOBAL:://;
+
+        if (!$declared) {
+           return "$kid(" . $args . ")";
+       } elsif (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 . ")";
@@ -1994,8 +2717,9 @@ sub re_uninterp {
 }
 
 # character escapes, but not delimiters that might need to be escaped
-sub escape_str { # ASCII
+sub escape_str { # ASCII, UTF8
     my($str) = @_;
+    $str =~ s/(.)/ord($1)>255 ? sprintf("\\x{%x}", ord($1)) : $1/eg;
     $str =~ s/\a/\\a/g;
 #    $str =~ s/\cH/\\b/g; # \b means someting different in a regex 
     $str =~ s/\t/\\t/g;
@@ -2015,6 +2739,16 @@ sub unback {
     return $str;
 }
 
+# Remove backslashes which precede literal control characters,
+# to avoid creating ambiguity when we escape the latter.
+sub re_unback {
+    my($str) = @_;
+
+    # the insane complexity here is due to the behaviour of "\c\"
+    $str =~ s/(^|[^\\]|\\c\\)(?<!\\c)\\(\\\\)*(?=[\0-\031\177-\377])/$1$2/g;
+    return $str;
+}
+
 sub balanced_delim {
     my($str) = @_;
     my @str = split //, $str;
@@ -2028,6 +2762,7 @@ sub balanced_delim {
            } elsif ($c eq $close) {
                $cnt--;
                if ($cnt < 0) {
+                   # qq()() isn't ")("
                    $fail = 1;
                    last;
                }
@@ -2041,70 +2776,97 @@ sub balanced_delim {
 
 sub single_delim {
     my($q, $default, $str) = @_;
-    return "$default$str$default" if index($str, $default) == -1;
+    return "$default$str$default" if $default and index($str, $default) == -1;
     my($succeed, $delim);
     ($succeed, $str) = balanced_delim($str);
     return "$q$str" if $succeed;
     for $delim ('/', '"', '#') {
        return "$q$delim" . $str . $delim if index($str, $delim) == -1;
     }
-    $str =~ s/$default/\\$default/g;
-    return "$default$str$default";
+    if ($default) {
+       $str =~ s/$default/\\$default/g;
+       return "$default$str$default";
+    } else {
+       $str =~ s[/][\\/]g;
+       return "$q/$str/";
+    }
 }
 
-sub SVf_IOK () {0x10000}
-sub SVf_NOK () {0x20000}
-sub SVf_ROK () {0x80000}
-
 sub const {
     my $sv = shift;
     if (class($sv) eq "SPECIAL") {
-       return ('undef', '1', '0')[$$sv-1];
+       return ('undef', '1', '0')[$$sv-1]; # sv_undef, sv_yes, sv_no
+    } elsif (class($sv) eq "NULL") {
+       return 'undef';
     } elsif ($sv->FLAGS & SVf_IOK) {
-       return $sv->IV;
+       return $sv->int_value;
     } elsif ($sv->FLAGS & SVf_NOK) {
        return $sv->NV;
-    } elsif ($sv->FLAGS & SVf_ROK) {
+    } elsif ($sv->FLAGS & SVf_ROK && $sv->can("RV")) {
        return "\\(" . const($sv->RV) . ")"; # constant folded
     } else {
        my $str = $sv->PV;
-       if ($str =~ /[^ -~]/) { # ASCII
+       if ($str =~ /[^ -~]/) { # ASCII for non-printing
            return single_delim("qq", '"', uninterp escape_str unback $str);
        } else {
-           $str =~ s/\\/\\\\/g;
-           return single_delim("q", "'", $str);
+           return single_delim("q", "'", unback $str);
        }
     }
 }
 
+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 & OPp_CONST_BARE) { # trouble with `=>' autoquoting 
-#      return $op->sv->PV;
+    if ($op->private & OPpCONST_ARYBASE) {
+        return '$[';
+    }
+#    if ($op->private & OPpCONST_BARE) { # trouble with `=>' autoquoting 
+#      return $self->const_sv($op)->PV;
 #    }
-    return const($op->sv);
+    my $sv = $self->const_sv($op);
+#    return const($sv);
+    my $c = const $sv; 
+    return $c =~ /^-\d/ ? $self->maybe_parens($c, $cx, 21) : $c;
 }
 
 sub dq {
     my $self = shift;
     my $op = shift;
-    my $type = $op->ppaddr;
-    if ($type eq "pp_const") {
-       return uninterp(escape_str(unback($op->sv->PV)));
-    } elsif ($type eq "pp_concat") {
-       return $self->dq($op->first) . $self->dq($op->last);
-    } elsif ($type eq "pp_uc") {
+    my $type = $op->name;
+    if ($type eq "const") {
+       return '$[' if $op->private & OPpCONST_ARYBASE;
+       return uninterp(escape_str(unback($self->const_sv($op)->as_string)));
+    } elsif ($type eq "concat") {
+       my $first = $self->dq($op->first);
+       my $last  = $self->dq($op->last);
+       # Disambiguate "${foo}bar", "${foo}{bar}", "${foo}[1]"
+       if ($last =~ /^[A-Z\\\^\[\]_?]/) {
+           $first =~ s/([\$@])\^$/${1}{^}/;  # "${^}W" etc
+        }
+       elsif ($last =~ /^[{\[\w]/) {
+           $first =~ s/([\$@])([A-Za-z_]\w*)$/${1}{$2}/;
+       }
+       return $first . $last;
+    } elsif ($type eq "uc") {
        return '\U' . $self->dq($op->first->sibling) . '\E';
-    } elsif ($type eq "pp_lc") {
+    } elsif ($type eq "lc") {
        return '\L' . $self->dq($op->first->sibling) . '\E';
-    } elsif ($type eq "pp_ucfirst") {
+    } elsif ($type eq "ucfirst") {
        return '\u' . $self->dq($op->first->sibling);
-    } elsif ($type eq "pp_lcfirst") {
+    } elsif ($type eq "lcfirst") {
        return '\l' . $self->dq($op->first->sibling);
-    } elsif ($type eq "pp_quotemeta") {
+    } elsif ($type eq "quotemeta") {
        return '\Q' . $self->dq($op->first->sibling) . '\E';
-    } elsif ($type eq "pp_join") {
+    } elsif ($type eq "join") {
        return $self->deparse($op->last, 26); # was join($", @ary)
     } else {
        return $self->deparse($op, 26);
@@ -2120,13 +2882,15 @@ sub pp_backtick {
 
 sub dquote {
     my $self = shift;
-    my $op = shift;
-    # 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(@_) }
+# OP_STRINGIFY is a listop, but it only ever has one arg
+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)
@@ -2186,14 +2950,15 @@ sub pchr { # ASCII
 
 sub collapse {
     my(@chars) = @_;
-    my($c, $str, $tr);
+    my($str, $c, $tr) = ("");
     for ($c = 0; $c < @chars; $c++) {
        $tr = $chars[$c];
        $str .= pchr($tr);
        if ($c <= $#chars - 2 and $chars[$c + 1] == $tr + 1 and
            $chars[$c + 2] == $tr + 2)
        {
-           for (; $c <= $#chars and $chars[$c + 1] == $chars[$c] + 1; $c++) {}
+           for (; $c <= $#chars-1 and $chars[$c + 1] == $chars[$c] + 1; $c++)
+             {}
            $str .= "-";
            $str .= pchr($chars[$c]);
        }
@@ -2201,14 +2966,12 @@ sub collapse {
     return $str;
 }
 
-sub OPpTRANS_SQUASH () { 16 }
-sub OPpTRANS_DELETE () { 32 }
-sub OPpTRANS_COMPLEMENT () { 64 }
+# XXX This has trouble with hyphens in the replacement (tr/bac/-AC/),
+# and backslashes.
 
-sub pp_trans {
-    my $self = shift;
-    my($op, $cx) = @_;
-    my(@table) = unpack("s256", $op->pv);
+sub tr_decode_byte {
+    my($table, $flags) = @_;
+    my(@table) = unpack("s256", $table);
     my($c, $tr, @from, @to, @delfrom, $delhyphen);
     if ($table[ord "-"] != -1 and 
        $table[ord("-") - 1] == -1 || $table[ord("-") + 1] == -1)
@@ -2230,10 +2993,8 @@ sub pp_trans {
            push @delfrom, $c;
        }
     }
-    my $flags;
     @from = (@from, @delfrom);
-    if ($op->private & OPpTRANS_COMPLEMENT) {
-       $flags .= "c";
+    if ($flags & OPpTRANS_COMPLEMENT) {
        my @newfrom = ();
        my %from;
        @from{@from} = (1) x @from;
@@ -2242,16 +3003,136 @@ sub pp_trans {
        }
        @from = @newfrom;
     }
-    if ($op->private & OPpTRANS_DELETE) {
-       $flags .= "d";
-    } else {
+    unless ($flags & OPpTRANS_DELETE || !@to) {
        pop @to while $#to and $to[$#to] == $to[$#to -1];
     }
-    $flags .= "s" if $op->private & OPpTRANS_SQUASH;
     my($from, $to);
     $from = collapse(@from);
     $to = collapse(@to);
     $from .= "-" if $delhyphen;
+    return ($from, $to);
+}
+
+sub tr_chr {
+    my $x = shift;
+    if ($x == ord "-") {
+       return "\\-";
+    } else {
+       return chr $x;
+    }
+}
+
+# XXX This doesn't yet handle all cases correctly either
+
+sub tr_decode_utf8 {
+    my($swash_hv, $flags) = @_;
+    my %swash = $swash_hv->ARRAY;
+    my $final = undef;
+    $final = $swash{'FINAL'}->IV if exists $swash{'FINAL'};
+    my $none = $swash{"NONE"}->IV;
+    my $extra = $none + 1;
+    my(@from, @delfrom, @to);
+    my $line;
+    foreach $line (split /\n/, $swash{'LIST'}->PV) {
+       my($min, $max, $result) = split(/\t/, $line);
+       $min = hex $min;
+       if (length $max) {
+           $max = hex $max;
+       } else {
+           $max = $min;
+       }
+       $result = hex $result;
+       if ($result == $extra) {
+           push @delfrom, [$min, $max];            
+       } else {
+           push @from, [$min, $max];
+           push @to, [$result, $result + $max - $min];
+       }
+    }
+    for my $i (0 .. $#from) {
+       if ($from[$i][0] == ord '-') {
+           unshift @from, splice(@from, $i, 1);
+           unshift @to, splice(@to, $i, 1);
+           last;
+       } elsif ($from[$i][1] == ord '-') {
+           $from[$i][1]--;
+           $to[$i][1]--;
+           unshift @from, ord '-';
+           unshift @to, ord '-';
+           last;
+       }
+    }
+    for my $i (0 .. $#delfrom) {
+       if ($delfrom[$i][0] == ord '-') {
+           push @delfrom, splice(@delfrom, $i, 1);
+           last;
+       } elsif ($delfrom[$i][1] == ord '-') {
+           $delfrom[$i][1]--;
+           push @delfrom, ord '-';
+           last;
+       }
+    }
+    if (defined $final and $to[$#to][1] != $final) {
+       push @to, [$final, $final];
+    }
+    push @from, @delfrom;
+    if ($flags & OPpTRANS_COMPLEMENT) {
+       my @newfrom;
+       my $next = 0;
+       for my $i (0 .. $#from) {
+           push @newfrom, [$next, $from[$i][0] - 1];
+           $next = $from[$i][1] + 1;
+       }
+       @from = ();
+       for my $range (@newfrom) {
+           if ($range->[0] <= $range->[1]) {
+               push @from, $range;
+           }
+       }
+    }
+    my($from, $to, $diff);
+    for my $chunk (@from) {
+       $diff = $chunk->[1] - $chunk->[0];
+       if ($diff > 1) {
+           $from .= tr_chr($chunk->[0]) . "-" . tr_chr($chunk->[1]);
+       } elsif ($diff == 1) {
+           $from .= tr_chr($chunk->[0]) . tr_chr($chunk->[1]);
+       } else {
+           $from .= tr_chr($chunk->[0]);
+       }
+    }
+    for my $chunk (@to) {
+       $diff = $chunk->[1] - $chunk->[0];
+       if ($diff > 1) {
+           $to .= tr_chr($chunk->[0]) . "-" . tr_chr($chunk->[1]);
+       } elsif ($diff == 1) {
+           $to .= tr_chr($chunk->[0]) . tr_chr($chunk->[1]);
+       } else {
+           $to .= tr_chr($chunk->[0]);
+       }
+    }
+    #$final = sprintf("%04x", $final) if defined $final;
+    #$none = sprintf("%04x", $none) if defined $none;
+    #$extra = sprintf("%04x", $extra) if defined $extra;    
+    #print STDERR "final: $final\n none: $none\nextra: $extra\n";
+    #print STDERR $swash{'LIST'}->PV;
+    return (escape_str($from), escape_str($to));
+}
+
+sub pp_trans {
+    my $self = shift;
+    my($op, $cx) = @_;
+    my($from, $to);
+    if (class($op) eq "PVOP") {
+       ($from, $to) = tr_decode_byte($op->pv, $op->private);
+    } else { # class($op) eq "SVOP"
+       ($from, $to) = tr_decode_utf8($op->sv->RV, $op->private);
+    }
+    my $flags = "";
+    $flags .= "c" if $op->private & OPpTRANS_COMPLEMENT;
+    $flags .= "d" if $op->private & OPpTRANS_DELETE;
+    $to = "" if $from eq $to and $flags eq "";
+    $flags .= "s" if $op->private & OPpTRANS_SQUASH;
     return "tr" . double_delim($from, $to) . $flags;
 }
 
@@ -2259,22 +3140,32 @@ sub pp_trans {
 sub re_dq {
     my $self = shift;
     my $op = shift;
-    my $type = $op->ppaddr;
-    if ($type eq "pp_const") {
-       return uninterp($op->sv->PV);
-    } elsif ($type eq "pp_concat") {
-       return $self->re_dq($op->first) . $self->re_dq($op->last);
-    } elsif ($type eq "pp_uc") {
+    my $type = $op->name;
+    if ($type eq "const") {
+       return '$[' if $op->private & OPpCONST_ARYBASE;
+       return re_uninterp(escape_str(re_unback($self->const_sv($op)->as_string)));
+    } elsif ($type eq "concat") {
+       my $first = $self->re_dq($op->first);
+       my $last  = $self->re_dq($op->last);
+       # Disambiguate "${foo}bar", "${foo}{bar}", "${foo}[1]"
+       if ($last =~ /^[A-Z\\\^\[\]_?]/) {
+           $first =~ s/([\$@])\^$/${1}{^}/;
+       }
+       elsif ($last =~ /^[{\[\w]/) {
+           $first =~ s/([\$@])([A-Za-z_]\w*)$/${1}{$2}/;
+       }
+       return $first . $last;
+    } elsif ($type eq "uc") {
        return '\U' . $self->re_dq($op->first->sibling) . '\E';
-    } elsif ($type eq "pp_lc") {
+    } elsif ($type eq "lc") {
        return '\L' . $self->re_dq($op->first->sibling) . '\E';
-    } elsif ($type eq "pp_ucfirst") {
+    } elsif ($type eq "ucfirst") {
        return '\u' . $self->re_dq($op->first->sibling);
-    } elsif ($type eq "pp_lcfirst") {
+    } elsif ($type eq "lcfirst") {
        return '\l' . $self->re_dq($op->first->sibling);
-    } elsif ($type eq "pp_quotemeta") {
+    } elsif ($type eq "quotemeta") {
        return '\Q' . $self->re_dq($op->first->sibling) . '\E';
-    } elsif ($type eq "pp_join") {
+    } elsif ($type eq "join") {
        return $self->deparse($op->last, 26); # was join($", @ary)
     } else {
        return $self->deparse($op, 26);
@@ -2285,25 +3176,11 @@ sub pp_regcomp {
     my $self = shift;
     my($op, $cx) = @_;
     my $kid = $op->first;
-    $kid = $kid->first if $kid->ppaddr eq "pp_regcmaybe";
+    $kid = $kid->first if $kid->name eq "regcmaybe";
+    $kid = $kid->first if $kid->name eq "regcreset";
     return $self->re_dq($kid);
 }
 
-sub OPp_RUNTIME () { 64 }
-
-sub PMf_ONCE () { 0x2 }
-sub PMf_SKIPWHITE () { 0x10 }
-sub PMf_FOLD () { 0x20 }
-sub PMf_CONST () { 0x40 }
-sub PMf_KEEP () { 0x80 }
-sub PMf_GLOBAL () { 0x100 }
-sub PMf_CONTINUE () { 0x200 }
-sub PMf_EVAL () { 0x400 }
-sub PMf_MULTILINE () { 0x1000 }
-sub PMf_SINGLELINE () { 0x2000 }
-sub PMf_LOCALE () { 0x4000 }
-sub PMf_EXTENDED () { 0x8000 }
-
 # osmic acid -- see osmium tetroxide
 
 my %matchwords;
@@ -2311,9 +3188,9 @@ map($matchwords{join "", sort split //, $_} = $_, 'cig', 'cog', 'cos', 'cogs',
     'cox', 'go', 'is', 'ism', 'iso', 'mig', 'mix', 'osmic', 'ox', 'sic', 
     'sig', 'six', 'smog', 'so', 'soc', 'sog', 'xi'); 
 
-sub pp_match {
+sub matchop {
     my $self = shift;
-    my($op, $cx) = @_;
+    my($op, $cx, $name, $delim) = @_;
     my $kid = $op->first;
     my ($binop, $var, $re) = ("", "", "");
     if ($op->flags & OPf_STACKED) {
@@ -2322,7 +3199,7 @@ sub pp_match {
        $kid = $kid->sibling;
     }
     if (null $kid) {
-       $re = re_uninterp(escape_str($op->precomp));
+       $re = re_uninterp(escape_str(re_unback($op->precomp)));
     } else {
        $re = $self->deparse($kid, 1);
     }
@@ -2339,7 +3216,7 @@ sub pp_match {
        $re =~ s/\?/\\?/g;
        $re = "?$re?";
     } else {
-       $re = single_delim("m", "/", $re);
+       $re = single_delim($name, $delim, $re);
     }
     $re = $re . $flags;
     if ($binop) {
@@ -2349,7 +3226,9 @@ sub pp_match {
     }
 }
 
-sub pp_pushre { pp_match(@_) }
+sub pp_match { matchop(@_, "m", "/") }
+sub pp_pushre { matchop(@_, "m", "/") }
+sub pp_qr { matchop(@_, "qr", "") }
 
 sub pp_split {
     my $self = shift;
@@ -2362,6 +3241,15 @@ sub pp_split {
     for (; !null($kid); $kid = $kid->sibling) {
        push @exprs, $self->deparse($kid, 6);
     }
+
+    # handle special case of split(), and split(" ") that compiles to /\s+/
+    $kid = $op->first;
+    if ($kid->flags & OPf_SPECIAL
+       && $exprs[0] eq '/\\s+/'
+       && $kid->pmflags & PMf_SKIPWHITE ) {
+           $exprs[0] = '" "';
+    }
+
     $expr = "split(" . join(", ", @exprs) . ")";
     if ($ary) {
        return $self->maybe_parens("$ary = $expr", $cx, 7);
@@ -2396,14 +3284,18 @@ sub pp_subst {
        $kid = $kid->sibling;
     } else {
        $repl = $op->pmreplroot->first; # skip substcont
-       while ($repl->ppaddr eq "pp_entereval") {
+       while ($repl->name eq "entereval") {
            $repl = $repl->first;
            $flags .= "e";
        }
-       $repl = $self->dq($repl);
+       if ($op->pmflags & PMf_EVAL) {
+           $repl = $self->deparse($repl, 0);
+       } else {
+           $repl = $self->dq($repl);   
+       }
     }
     if (null $kid) {
-       $re = re_uninterp(escape_str($op->precomp));
+       $re = re_uninterp(escape_str(re_unback($op->precomp)));
     } else {
        $re = $self->deparse($kid, 1);
     }
@@ -2433,7 +3325,8 @@ B::Deparse - Perl compiler backend to produce perl code
 
 =head1 SYNOPSIS
 
-B<perl> B<-MO=Deparse>[B<,-u>I<PACKAGE>][B<,-p>][B<,-s>I<LETTERS>] I<prog.pl>
+B<perl> B<-MO=Deparse>[B<,-u>I<PACKAGE>][B<,-p>][B<,-q>][B<,-l>]
+        [B<,-s>I<LETTERS>][B<,-x>I<LEVEL>] I<prog.pl>
 
 =head1 DESCRIPTION
 
@@ -2458,6 +3351,11 @@ the '-MO=Deparse', separated by a comma but not any white space.
 
 =over 4
 
+=item B<-l>
+
+Add '#line' declarations to the output based on the line and file
+locations of the original code.
+
 =item B<-p>
 
 Print extra parentheses. Without this option, B::Deparse includes
@@ -2481,24 +3379,39 @@ C<B::Deparse,-p> will print
 which probably isn't what you intended (the C<'???'> is a sign that
 perl optimized away a constant value).
 
-=item B<-u>I<PACKAGE>
+=item B<-q>
+
+Expand double-quoted strings into the corresponding combinations of
+concatenation, uc, ucfirst, lc, lcfirst, quotemeta, and join. For
+instance, print
+
+    print "Hello, $world, @ladies, \u$gentlemen\E, \u\L$me!";
+
+as
+
+    print 'Hello, ' . $world . ', ' . join($", @ladies) . ', '
+          . ucfirst($gentlemen) . ', ' . ucfirst(lc $me . '!');
 
-Normally, B::Deparse deparses the main code of a program, all the subs
-called by the main program (and all the subs called by them,
-recursively), and any other subs in the main:: package. To include
-subs in other packages that aren't called directly, such as AUTOLOAD,
-DESTROY, other subs called automatically by perl, and methods, which
-aren't resolved to subs until runtime, use the B<-u> option. The
-argument to B<-u> is the name of a package, and should follow directly
-after the 'u'. Multiple B<-u> options may be given, separated by
-commas.  Note that unlike some other backends, B::Deparse doesn't
-(yet) try to guess automatically when B<-u> is needed -- you must
-invoke it yourself.
+Note that the expanded form represents the way perl handles such
+constructions internally -- this option actually turns off the reverse
+translation that B::Deparse usually does. On the other hand, note that
+C<$x = "$y"> is not the same as C<$x = $y>: the former makes the value
+of $y into a string before doing the assignment.
+
+=item B<-f>I<FILE>
+
+Normally, B::Deparse deparses the main code of a program, and all the subs
+defined in the same file. To include subs defined in other files, pass the
+B<-f> option with the filename. You can pass the B<-f> option several times, to
+include more than one secondary file.  (Most of the time you don't want to
+use it at all.)  You can also use this option to include subs which are
+defined in the scope of a B<#line> directive with two parameters.
 
 =item B<-s>I<LETTERS>
 
-Tweak the style of B::Deparse's output. At the moment, only one style
-option is implemented:
+Tweak the style of B::Deparse's output. The letters should follow
+directly after the 's', with no space or punctuation. The following
+options are available:
 
 =over 4
 
@@ -2523,17 +3436,261 @@ instead of
 
 The default is not to cuddle.
 
+=item B<i>I<NUMBER>
+
+Indent lines by multiples of I<NUMBER> columns. The default is 4 columns.
+
+=item B<T>
+
+Use tabs for each 8 columns of indent. The default is to use only spaces.
+For instance, if the style options are B<-si4T>, a line that's indented
+3 times will be preceded by one tab and four spaces; if the options were
+B<-si8T>, the same line would be preceded by three tabs.
+
+=item B<v>I<STRING>B<.>
+
+Print I<STRING> for the value of a constant that can't be determined
+because it was optimized away (mnemonic: this happens when a constant
+is used in B<v>oid context). The end of the string is marked by a period.
+The string should be a valid perl expression, generally a constant.
+Note that unless it's a number, it probably needs to be quoted, and on
+a command line quotes need to be protected from the shell. Some
+conventional values include 0, 1, 42, '', 'foo', and
+'Useless use of constant omitted' (which may need to be
+B<-sv"'Useless use of constant omitted'.">
+or something similar depending on your shell). The default is '???'.
+If you're using B::Deparse on a module or other file that's require'd,
+you shouldn't use a value that evaluates to false, since the customary
+true constant at the end of a module will be in void context when the
+file is compiled as a main program.
+
 =back
 
+=item B<-x>I<LEVEL>
+
+Expand conventional syntax constructions into equivalent ones that expose
+their internal operation. I<LEVEL> should be a digit, with higher values
+meaning more expansion. As with B<-q>, this actually involves turning off
+special cases in B::Deparse's normal operations.
+
+If I<LEVEL> is at least 3, for loops will be translated into equivalent
+while loops with continue blocks; for instance
+
+    for ($i = 0; $i < 10; ++$i) {
+        print $i;
+    }
+
+turns into
+
+    $i = 0;
+    while ($i < 10) {
+        print $i;
+    } continue {
+        ++$i
+    }
+
+Note that in a few cases this translation can't be perfectly carried back
+into the source code -- if the loop's initializer declares a my variable,
+for instance, it won't have the correct scope outside of the loop.
+
+If I<LEVEL> is at least 7, if statements will be translated into equivalent
+expressions using C<&&>, C<?:> and C<do {}>; for instance
+
+    print 'hi' if $nice;
+    if ($nice) {
+        print 'hi';
+    }
+    if ($nice) {
+        print 'hi';
+    } else {
+        print 'bye';
+    }
+
+turns into
+
+    $nice and print 'hi';
+    $nice and do { print 'hi' };
+    $nice ? do { print 'hi' } : do { print 'bye' };
+
+Long sequences of elsifs will turn into nested ternary operators, which
+B::Deparse doesn't know how to indent nicely.
+
 =back
 
+=head1 USING B::Deparse AS A MODULE
+
+=head2 Synopsis
+
+    use B::Deparse;
+    $deparse = B::Deparse->new("-p", "-sC");
+    $body = $deparse->coderef2text(\&func);
+    eval "sub func $body"; # the inverse operation
+
+=head2 Description
+
+B::Deparse can also be used on a sub-by-sub basis from other perl
+programs.
+
+=head2 new
+
+    $deparse = B::Deparse->new(OPTIONS)
+
+Create an object to store the state of a deparsing operation and any
+options. The options are the same as those that can be given on the
+command line (see L</OPTIONS>); options that are separated by commas
+after B<-MO=Deparse> should be given as separate strings. Some
+options, like B<-u>, don't make sense for a single subroutine, so
+don't pass them.
+
+=head2 ambient_pragmas
+
+    $deparse->ambient_pragmas(strict => 'all', '$[' => $[);
+
+The compilation of a subroutine can be affected by a few compiler
+directives, B<pragmas>. These are:
+
+=over 4
+
+=item *
+
+use strict;
+
+=item *
+
+use warnings;
+
+=item *
+
+Assigning to the special variable $[
+
+=item *
+
+use integer;
+
+=item *
+
+use bytes;
+
+=item *
+
+use utf8;
+
+=item *
+
+use re;
+
+=back
+
+Ordinarily, if you use B::Deparse on a subroutine which has
+been compiled in the presence of one or more of these pragmas,
+the output will include statements to turn on the appropriate
+directives. So if you then compile the code returned by coderef2text, 
+it will behave the same way as the subroutine which you deparsed.
+
+However, you may know that you intend to use the results in a
+particular context, where some pragmas are already in scope. In
+this case, you use the B<ambient_pragmas> method to describe the
+assumptions you wish to make.
+
+The parameters it accepts are:
+
+=over 4
+
+=item strict
+
+Takes a string, possibly containing several values separated
+by whitespace. The special values "all" and "none" mean what you'd
+expect.
+
+    $deparse->ambient_pragmas(strict => 'subs refs');
+
+=item $[
+
+Takes a number, the value of the array base $[.
+
+=item bytes
+
+=item utf8
+
+=item integer
+
+If the value is true, then the appropriate pragma is assumed to
+be in the ambient scope, otherwise not.
+
+=item re
+
+Takes a string, possibly containing a whitespace-separated list of
+values. The values "all" and "none" are special. It's also permissible
+to pass an array reference here.
+
+    $deparser->ambient_pragmas(re => 'eval');
+
+
+=item warnings
+
+Takes a string, possibly containing a whitespace-separated list of
+values. The values "all" and "none" are special, again. It's also
+permissible to pass an array reference here.
+
+    $deparser->ambient_pragmas(warnings => [qw[void io]]);
+
+If one of the values is the string "FATAL", then all the warnings
+in that list will be considered fatal, just as with the B<warnings>
+pragma itself. Should you need to specify that some warnings are
+fatal, and others are merely enabled, you can pass the B<warnings>
+parameter twice:
+
+    $deparser->ambient_pragmas(
+       warnings => 'all',
+       warnings => [FATAL => qw/void io/],
+    );
+
+See L<perllexwarn> for more information about lexical warnings. 
+
+=item hint_bits
+
+=item warning_bits
+
+These two parameters are used to specify the ambient pragmas in
+the format used by the special variables $^H and ${^WARNING_BITS}.
+
+They exist principally so that you can write code like:
+
+    { my ($hint_bits, $warning_bits);
+    BEGIN {($hint_bits, $warning_bits) = ($^H, ${^WARNING_BITS})}
+    $deparser->ambient_pragmas (
+       hint_bits    => $hint_bits,
+       warning_bits => $warning_bits,
+       '$['         => 0 + $[
+    ); }
+
+which specifies that the ambient pragmas are exactly those which
+are in scope at the point of calling.
+
+=back
+
+=head2 coderef2text
+
+    $body = $deparse->coderef2text(\&func)
+    $body = $deparse->coderef2text(sub ($$) { ... })
+
+Return source code for the body of a subroutine (a block, optionally
+preceded by a prototype in parens), given a reference to the
+sub. Because a subroutine can have no names, or more than one name,
+this method doesn't return a complete subroutine definition -- if you
+want to eval the result, you should prepend "sub subname ", or "sub "
+for an anonymous function constructor. Unless the sub was defined in
+the main:: package, the code will include a package declaration.
+
 =head1 BUGS
 
 See the 'to do' list at the beginning of the module file.
 
 =head1 AUTHOR
 
-Stephen McCamant <alias@mcs.com>, based on an earlier version by
-Malcolm Beattie <mbeattie@sable.ox.ac.uk>.
+Stephen McCamant <smcc@CSUA.Berkeley.EDU>, based on an earlier
+version by Malcolm Beattie <mbeattie@sable.ox.ac.uk>, with
+contributions from Gisle Aas, James Duncan, Albert Dvornik, Hugo van
+der Sanden, Gurusamy Sarathy, and Nick Ing-Simmons.
 
 =cut