# but essentially none of his code remains.
package B::Deparse;
-use Carp 'cluck', 'croak';
-use B qw(class main_root main_start main_cv svref_2object opnumber cstring
+use Carp;
+use B qw(class main_root main_start main_cv svref_2object opnumber perlstring
OPf_WANT OPf_WANT_VOID OPf_WANT_SCALAR OPf_WANT_LIST
- OPf_KIDS OPf_REF OPf_STACKED OPf_SPECIAL
+ OPf_KIDS OPf_REF OPf_STACKED OPf_SPECIAL OPf_MOD
OPpLVAL_INTRO OPpOUR_INTRO OPpENTERSUB_AMPER OPpSLICE OPpCONST_BARE
OPpTRANS_SQUASH OPpTRANS_DELETE OPpTRANS_COMPLEMENT OPpTARGET_MY
OPpCONST_ARYBASE OPpEXISTS_SUB OPpSORT_NUMERIC OPpSORT_INTEGER
OPpSORT_REVERSE
- SVf_IOK SVf_NOK SVf_ROK SVf_POK SVpad_OUR
- CVf_METHOD CVf_LOCKED CVf_LVALUE
+ SVf_IOK SVf_NOK SVf_ROK SVf_POK SVpad_OUR SVf_FAKE
+ CVf_METHOD CVf_LOCKED CVf_LVALUE CVf_ASSERTION
PMf_KEEP PMf_GLOBAL PMf_CONTINUE PMf_EVAL PMf_ONCE PMf_SKIPWHITE
PMf_MULTILINE PMf_SINGLELINE PMf_FOLD PMf_EXTENDED);
-$VERSION = 0.60;
+$VERSION = 0.63;
use strict;
+use vars qw/$AUTOLOAD/;
use warnings ();
# Changes between 0.50 and 0.51:
# - package declarations using cop_stash
# - subs, formats and code sorted by cop_seq
# Changes between 0.51 and 0.52:
-# - added pp_threadsv (special variables under USE_THREADS)
+# - added pp_threadsv (special variables under USE_5005THREADS)
# - added documentation
# Changes between 0.52 and 0.53:
# - many changes adding precedence contexts and associativity
# - separate recognition of constant subs
# - rewrote continue block handling, now recoginizing for loops
# - added more control of expanding control structures
+# Changes between 0.60 and 0.61 (mostly by Robin Houston)
+# - many bug-fixes
+# - support for pragmas and 'use'
+# - support for the little-used $[ variable
+# - support for __DATA__ sections
+# - UTF8 support
+# - BEGIN, CHECK, INIT and END blocks
+# - scoping of subroutine declarations fixed
+# - compile-time output from the input program can be suppressed, so that the
+# output is just the deparsed code. (a change to O.pm in fact)
+# - our() declarations
+# - *all* the known bugs are now listed in the BUGS section
+# - comprehensive test mechanism (TEST -deparse)
+# Changes between 0.62 and 0.63 (mostly by Rafael Garcia-Suarez)
+# - bug-fixes
+# - new switch -P
+# - support for command-line switches (-l, -0, etc.)
# Todo:
# (See also BUGS section at the end of this file)
# keys are names of subs for which we've printed declarations.
# That means we can omit parentheses from the arguments.
#
+# subs_deparsed
+# Keeps track of fully qualified names of all deparsed subs.
+#
# parens: -p
# linenums: -l
# unquote: -q
$seq = 0;
}
push @{$self->{'subs_todo'}}, [$seq, $cv, $is_form];
+ unless ($is_form || class($cv->STASH) eq 'SPECIAL') {
+ $self->{'subs_deparsed'}{$cv->STASH->NAME."::".$cv->GV->NAME} = 1;
+ }
}
sub next_todo {
return unless $self->const_sv($constop)->PV eq $module;
$constop = $constop->sibling;
- $version = $self->const_sv($constop)->int_value;
+ $version = $self->const_sv($constop);
+ if (class($version) ne "PVMG") {
+ # version is either an integer or a double
+ $version = $version->PV;
+ } else {
+ # version specified as a v-string
+ $version = 'v'.join '.', map ord, split //, $version->PV;
+ }
$constop = $constop->sibling;
return if $constop->name ne "method_named";
return if $self->const_sv($constop)->PV ne "VERSION";
$self->{'files'}{$1} = 1;
} elsif ($arg eq "-p") {
$self->{'parens'} = 1;
+ } elsif ($arg eq "-P") {
+ $self->{'noproto'} = 1;
} elsif ($arg eq "-l") {
$self->{'linenums'} = 1;
} elsif ($arg eq "-q") {
return $self;
}
-sub WARN_MASK () {
- # Mask out the bits that C<use vars> uses
- $warnings::Bits{all} | $warnings::DeadBits{all};
+{
+ # Mask out the bits that L<warnings::register> uses
+ my $WARN_MASK;
+ BEGIN {
+ $WARN_MASK = $warnings::Bits{all} | $warnings::DeadBits{all};
+ }
+ sub WARN_MASK () {
+ return $WARN_MASK;
+ }
}
# Initialise the contextual information, either from
my(@args) = @_;
return sub {
my $self = B::Deparse->new(@args);
+ # First deparse command-line args
+ if (defined $^I) { # deparse -i
+ print q(BEGIN { $^I = ).perlstring($^I).qq(; }\n);
+ }
+ if ($^W) { # deparse -w
+ print qq(BEGIN { \$^W = $^W; }\n);
+ }
+ if ($/ ne "\n" or defined $O::savebackslash) { # deparse -l and -0
+ my $fs = perlstring($/) || 'undef';
+ my $bs = perlstring($O::savebackslash) || 'undef';
+ print qq(BEGIN { \$/ = $fs; \$\\ = $bs; }\n);
+ }
my @BEGINs = B::begin_av->isa("B::AV") ? B::begin_av->ARRAY : ();
+ my @CHECKs = B::check_av->isa("B::AV") ? B::check_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) {
+ for my $block (@BEGINs, @CHECKs, @INITs, @ENDs) {
$self->todo($block, 0);
}
$self->stash_subs();
# Print __DATA__ section, if necessary
no strict 'refs';
- if (defined *{$self->{'curstash'}."::DATA"}{IO}) {
+ my $laststash = defined $self->{'curcop'}
+ ? $self->{'curcop'}->stash->NAME : $self->{'curstash'};
+ if (defined *{$laststash."::DATA"}{IO}) {
print "__DATA__\n";
- print readline(*{$self->{'curstash'}."::DATA"});
+ print readline(*{$laststash."::DATA"});
}
}
}
sub coderef2text {
my $self = shift;
my $sub = shift;
- croak "Usage: ->coderef2text(CODEREF)" unless ref($sub) eq "CODE";
+ croak "Usage: ->coderef2text(CODEREF)" unless UNIVERSAL::isa($sub, "CODE");
$self->init();
return $self->indent($self->deparse_sub(svref_2object($sub)));
elsif ($name eq 're') {
require re;
if ($val eq 'none') {
- $hint_bits &= ~re::bits(qw/taint eval asciirange/);
+ $hint_bits &= ~re::bits(qw/taint eval/);
next();
}
my @names;
if ($val eq 'all') {
- @names = qw/taint eval asciirange/;
+ @names = qw/taint eval/;
}
elsif (ref $val) {
@names = @$val;
elsif ($name eq 'warnings') {
if ($val eq 'none') {
- $warning_bits = "\0"x12;
+ $warning_bits = $warnings::NONE;
next();
}
@names = split/\s+/, $val;
}
- $warning_bits = "\0"x12 if !defined ($warning_bits);
+ $warning_bits = $warnings::NONE if !defined ($warning_bits);
$warning_bits |= warnings::bits(@names);
}
if ($cv->FLAGS & SVf_POK) {
$proto = "(". $cv->PV . ") ";
}
- if ($cv->CvFLAGS & (CVf_METHOD|CVf_LOCKED|CVf_LVALUE)) {
+ if ($cv->CvFLAGS & (CVf_METHOD|CVf_LOCKED|CVf_LVALUE|CVf_ASSERTION)) {
$proto .= ": ";
$proto .= "lvalue " if $cv->CvFLAGS & CVf_LVALUE;
$proto .= "locked " if $cv->CvFLAGS & CVf_LOCKED;
$proto .= "method " if $cv->CvFLAGS & CVf_METHOD;
+ $proto .= "assertion " if $cv->CvFLAGS & CVf_ASSERTION;
}
local($self->{'curcv'}) = $cv;
my @text;
local($self->{'curcv'}) = $form;
local($self->{'curcvlex'});
+ local($self->{'in_format'}) = 1;
local(@$self{qw'curstash warnings hints'})
- = @$self{'curstash warnings hints'};
+ = @$self{qw'curstash warnings hints'};
my $op = $form->ROOT;
my $kid;
+ return "\f." if $op->first->name eq 'stub'
+ || $op->first->name eq 'nextstate';
$op = $op->first->first; # skip leavewrite, lineseq
while (not null $op) {
$op = $op->sibling; # skip nextstate
if ($op->private & (OPpLVAL_INTRO|$our_intro)
and not $self->{'avoid_local'}{$$op}) {
my $our_local = ($op->private & OPpLVAL_INTRO) ? "local" : "our";
+ if( $our_local eq 'our' ) {
+ die "Unexpected our($text)\n" unless $text =~ /^\W(\w+::)*\w+\z/;
+ $text =~ s/(\w+::)+//;
+ }
if (want_scalar($op)) {
return "$our_local $text";
} else {
# The following OPs don't have functions:
# pp_padany -- does not exist after parsing
-# pp_rcatline -- does not exist
-
-sub pp_enter { # see also leave
- cluck "unexpected OP_ENTER";
- return "XXX";
-}
-
-sub pp_pushmark { # see also list
- cluck "unexpected OP_PUSHMARK";
- return "XXX";
-}
-
-sub pp_leavesub { # see also deparse_sub
- cluck "unexpected OP_LEAVESUB";
- return "XXX";
-}
-
-sub pp_leavewrite { # see also deparse_format
- cluck "unexpected OP_LEAVEWRITE";
- return "XXX";
-}
-
-sub pp_method { # see also entersub
- cluck "unexpected OP_METHOD";
- return "XXX";
-}
-sub pp_regcmaybe { # see also regcomp
- cluck "unexpected OP_REGCMAYBE";
- 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";
-}
-
-sub pp_grepstart { # see also grepwhile
- cluck "unexpected OP_GREPSTART";
- return "XXX";
-}
-
-sub pp_mapstart { # see also mapwhile
- cluck "unexpected OP_MAPSTART";
- return "XXX";
-}
-
-sub pp_flip { # see also flop
- cluck "unexpected OP_FLIP";
- return "XXX";
-}
-
-sub pp_iter { # see also leaveloop
- cluck "unexpected OP_ITER";
- return "XXX";
-}
-
-sub pp_enteriter { # see also leaveloop
- cluck "unexpected OP_ENTERITER";
- return "XXX";
-}
-
-sub pp_enterloop { # see also leaveloop
- cluck "unexpected OP_ENTERLOOP";
- return "XXX";
+sub AUTOLOAD {
+ if ($AUTOLOAD =~ s/^.*::pp_//) {
+ warn "unexpected OP_".uc $AUTOLOAD;
+ return "XXX";
+ } else {
+ die "Undefined subroutine $AUTOLOAD called";
+ }
}
-sub pp_leaveeval { # see also entereval
- cluck "unexpected OP_LEAVEEVAL";
- return "XXX";
-}
-
-sub pp_entertry { # see also leavetry
- cluck "unexpected OP_ENTERTRY";
- return "XXX";
-}
+sub DESTROY {} # Do not AUTOLOAD
# $root should be the op which represents the root of whatever
# we're sequencing here. If it's undefined, then we don't append
}
my $body = join(";\n", grep {length} @exprs);
my $subs = "";
- if (defined $root && defined $limit_seq) {
+ if (defined $root && defined $limit_seq && !$self->{'in_format'}) {
$subs = join "\n", $self->seq_subs($limit_seq);
}
return join(";\n", grep {length} $body, $subs);
Carp::confess() if $gv->isa("B::CV");
my $stash = $gv->STASH->NAME;
my $name = $gv->SAFENAME;
- if ($stash eq $self->{'curstash'} or $globalnames{$name}
+ if (($stash eq 'main' && $globalnames{$name})
+ or ($stash eq $self->{'curstash'} && !$globalnames{$name})
or $name =~ /^[^A-Za-z_]/)
{
$stash = "";
} else {
$stash = $stash . "::";
}
- if ($name =~ /^\^../) {
- $name = "{$name}"; # ${^WARNING_BITS} etc
+ if ($name =~ /^(\^..|{)/) {
+ $name = "{$name}"; # ${^WARNING_BITS}, etc and ${
}
return $stash . $name;
}
sub populate_curcvlex {
my $self = shift;
for (my $cv = $self->{'curcv'}; class($cv) eq "CV"; $cv = $cv->OUTSIDE) {
- my @padlist = $cv->PADLIST->ARRAY;
+ my $padlist = $cv->PADLIST;
+ # an undef CV still in lexical chain
+ next if class($padlist) eq "SPECIAL";
+ my @padlist = $padlist->ARRAY;
my @ns = $padlist[0]->ARRAY;
for (my $i=0; $i<@ns; ++$i) {
next;
}
my $name = $ns[$i]->PVX;
- my $seq_st = $ns[$i]->NVX;
- my $seq_en = int($ns[$i]->IVX);
+ my ($seq_st, $seq_en) =
+ ($ns[$i]->FLAGS & SVf_FAKE)
+ ? (0, 999999)
+ : ($ns[$i]->NVX, $ns[$i]->IVX);
push @{$self->{'curcvlex'}{$name}}, [$seq_st, $seq_en];
}
# Recurses down the tree, looking for pad variable introductions and COPs
sub find_scope {
my ($self, $op, $scope_st, $scope_en) = @_;
-Carp::cluck() if !defined $op;
+ carp("Undefined op in find_scope") if !defined $op;
return ($scope_st, $scope_en) unless $op->flags & OPf_KIDS;
for (my $o=$op->first; $$o; $o=$o->sibling) {
my $warnings = $op->warnings;
my $warning_bits;
if ($warnings->isa("B::SPECIAL") && $$warnings == 4) {
- $warning_bits = $warnings::Bits{"all"};
+ $warning_bits = $warnings::Bits{"all"} & WARN_MASK;
}
elsif ($warnings->isa("B::SPECIAL") && $$warnings == 5) {
- $warning_bits = "\0"x12;
+ $warning_bits = $warnings::NONE;
}
elsif ($warnings->isa("B::SPECIAL")) {
$warning_bits = undef;
elsif (($to & WARN_MASK) eq "\0"x length($to)) {
return "no warnings;\n";
}
- return "BEGIN {\${^WARNING_BITS} = ".cstring($to)."}\n";
+ return "BEGIN {\${^WARNING_BITS} = ".perlstring($to)."}\n";
}
sub declare_hints {
sub padval {
my $self = shift;
my $targ = shift;
- #cluck "curcv was undef" unless $self->{curcv};
return (($self->{'curcv'}->PADLIST->ARRAY)[1]->ARRAY)[$targ];
}
return $self->unop($op, $cx, "readline");
}
+sub pp_rcatline {
+ my $self = shift;
+ my($op) = @_;
+ return "<" . $self->gv_name($op->gv) . ">";
+}
+
# Unary operators that can occur as pseudo-listops inside double quotes
sub dq_unop {
my $self = shift;
($left, $right) = ($right, $left);
}
$left = $self->deparse_binop_left($op, $left, $prec);
- $left = "($left)" if $flags & LIST_CONTEXT && $left =~ /^\$/;
+ $left = "($left)" if $flags & LIST_CONTEXT
+ && $left !~ /^(my|our|local|)[\@\(]/;
$right = $self->deparse_binop_right($op, $right, $prec);
return $self->maybe_parens("$left $opname$eq $right", $cx, $prec);
}
sub pp_and { logop(@_, "and", 3, "&&", 11, "if") }
sub pp_or { logop(@_, "or", 2, "||", 10, "unless") }
+sub pp_dor { logop(@_, "err", 2, "//", 10, "") }
# 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_andassign { logassignop(@_, "&&=") }
-sub pp_orassign { logassignop(@_, "||=") }
+sub pp_orassign { logassignop(@_, "||=") }
+sub pp_dorassign { logassignop(@_, "//=") }
sub listop {
my $self = shift;
my $kid = $op->first->sibling;
return $name if null $kid;
my $first;
+ $name = "socketpair" if $name eq "sockpair";
if (defined prototype("CORE::$name")
&& prototype("CORE::$name") =~ /^;?\*/
&& $kid->name eq "rv2gv") {
$kid = $kid->sibling;
for (; !null($kid); $kid = $kid->sibling) {
$expr = $self->deparse($kid, 6);
- push @exprs, $expr if $expr;
+ push @exprs, $expr if defined $expr;
}
return $self->maybe_parens_func($name, $code . join(", ", @exprs), $cx, 5);
}
my $body;
my $cond = undef;
if ($kid->name eq "lineseq") { # bare or infinite loop
- if (is_state $kid->last) { # infinite
+ if ($kid->last->name eq "unstack") { # infinite
$head = "while (1) "; # Can't use for(;;) if there's a continue
$cond = "";
} else {
$var = $self->pp_threadsv($enter, 1);
} else { # regular my() variable
$var = $self->pp_padsv($enter, 1);
- if ($self->padname_sv($enter->targ)->IVX ==
- $kid->first->first->sibling->last->cop_seq)
- {
- # If the scope of this variable closes at the last
- # statement of the loop, it must have been
- # declared here.
- $var = "my " . $var;
- }
}
} elsif ($var->name eq "rv2gv") {
$var = $self->pp_rv2sv($var, 1);
+ if ($enter->private & OPpOUR_INTRO) {
+ # our declarations don't have package names
+ $var =~ s/^(.).*::/$1/;
+ $var = "our $var";
+ }
} elsif ($var->name eq "gv") {
$var = "\$" . $self->deparse($var, 1);
}
return "{;}"; # {} could be a hashref
}
# 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
+ # will point to the unstack, which is kid's last 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
+ # these conditions hold, then the second-to-last child is the continue
# block (or the last in a bare loop).
my $cont_start = $enter->nextop;
my $cont;
- if ($$cont_start != $$op && ${$cont_start->sibling} != ${$body->last}) {
+ if ($$cont_start != $$op && ${$cont_start} != ${$body->last}) {
if ($bare) {
$cont = $body->last;
} else {
$cont = $body->first;
- while (!null($cont->sibling->sibling->sibling)) {
+ while (!null($cont->sibling->sibling)) {
$cont = $cont->sibling;
}
}
BEGIN { eval "sub OP_CONST () {" . opnumber("const") . "}" }
BEGIN { eval "sub OP_STRINGIFY () {" . opnumber("stringify") . "}" }
+BEGIN { eval "sub OP_RV2SV () {" . opnumber("rv2sv") . "}" }
+BEGIN { eval "sub OP_LIST () {" . opnumber("list") . "}" }
sub pp_null {
my $self = shift;
- my($op, $cx) = @_;
+ my($op, $cx, $flags) = @_;
if (class($op) eq "OP") {
# old value is lost
return $self->{'ex_const'} if $op->targ == OP_CONST;
. $self->deparse($op->first->sibling, 20),
$cx, 20);
} elsif ($op->flags & OPf_SPECIAL && $cx == 0 && !$op->targ) {
- return "do {\n\t". $self->deparse($op->first, $cx) ."\n\b};";
+ if ($flags) {
+ return $self->deparse($op->first, $cx);
+ }
+ else {
+ return "do {\n\t". $self->deparse($op->first, $cx) ."\n\b};";
+ }
+ } elsif (!null($op->first->sibling) and
+ $op->first->sibling->name eq "null" and
+ class($op->first->sibling) eq "UNOP" and
+ $op->first->sibling->first->flags & OPf_STACKED and
+ $op->first->sibling->first->name eq "rcatline") {
+ return $self->maybe_parens($self->deparse($op->first, 18) . " .= "
+ . $self->deparse($op->first->sibling, 18),
+ $cx, 18);
} else {
return $self->deparse($op->first, $cx);
}
sub rv2x {
my $self = shift;
my($op, $cx, $type) = @_;
+
+ if (class($op) eq 'NULL' || !$op->can("first")) {
+ carp("Unexpected op in pp_rv2x");
+ return 'XXX';
+ }
my $kid = $op->first;
my $str = $self->deparse($kid, 0);
return $self->stash_variable($type, $str) if is_scalar($kid);
}
# skip down to the old, ex-rv2cv
-sub pp_rv2cv { $_[0]->rv2x($_[1]->first->first->sibling, $_[2], "&") }
+sub pp_rv2cv {
+ my ($self, $op, $cx) = @_;
+ if (!null($op->first) && $op->first->name eq 'null' &&
+ $op->first->targ eq OP_LIST)
+ {
+ return $self->rv2x($op->first->first->sibling, $cx, "&")
+ }
+ else {
+ return $self->rv2x($op, $cx, "")
+ }
+}
sub pp_rv2av {
my $self = shift;
#
$idx =~ s/^\((.*)\)$/$1/ if $self->{'parens'};
+ # Hash-element braces will autoquote a bareword inside themselves.
+ # We need to make sure that C<$hash{warn()}> doesn't come out as
+ # C<$hash{warn}>, which has a quite different meaning. Currently
+ # B::Deparse will always quote strings, even if the string was a
+ # bareword in the original (i.e. the OPpCONST_BARE flag is ignored
+ # for constant strings.) So we can cheat slightly here - if we see
+ # a bareword, we know that it is supposed to be a function call.
+ #
+ $idx =~ s/^([A-Za-z_]\w*)$/$1()/;
+
return "\$" . $array . $left . $idx . $right;
}
} else {
$obj = $kid;
$kid = $kid->sibling;
- for (; not null $kid->sibling; $kid = $kid->sibling) {
+ for (; !null ($kid->sibling) && $kid->name ne "method_named";
+ $kid = $kid->sibling) {
push @exprs, $self->deparse($kid, 6);
}
$meth = $kid;
# or ("", $args_after_prototype_demunging) if it does.
sub check_proto {
my $self = shift;
+ return "&" if $self->{'noproto'};
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/;
+ 1 while $proto =~ s/(?<!\\)([@%])[^\]]+$/$1/;
while ($proto) {
- $proto =~ s/^ *([\\]?[\$\@&%*]|;)//;
+ $proto =~ s/^(\\?[\$\@&%*]|\\\[[\$\@&%*]+\]|;)//;
my $chr = $1;
if ($chr eq "") {
return "&" if @args;
return "&";
}
} elsif (substr($chr, 0, 1) eq "\\") {
- $chr = substr($chr, 1);
+ $chr =~ tr/\\[]//d;
if ($arg->name =~ /^s?refgen$/ and
!null($real = $arg->first) and
- ($chr eq "\$" && is_scalar($real->first)
- or ($chr eq "\@"
+ ($chr =~ /\$/ && is_scalar($real->first)
+ or ($chr =~ /@/
+ && class($real->first->sibling) ne 'NULL'
&& $real->first->sibling->name
=~ /^(rv2|pad)av$/)
- or ($chr eq "%"
+ or ($chr =~ /%/
+ && class($real->first->sibling) ne 'NULL'
&& $real->first->sibling->name
=~ /^(rv2|pad)hv$/)
- #or ($chr eq "&" # This doesn't work
+ #or ($chr =~ /&/ # This doesn't work
# && $real->first->name eq "rv2cv")
- or ($chr eq "*"
+ or ($chr =~ /\*/
&& $real->first->name eq "rv2gv")))
{
push @reals, $self->deparse($real, 6);
my $prefix = "";
my $amper = "";
my($kid, @exprs);
- if ($op->flags & OPf_SPECIAL) {
+ if ($op->flags & OPf_SPECIAL && !($op->flags & OPf_MOD)) {
$prefix = "do ";
} elsif ($op->private & OPpENTERSUB_AMPER) {
$amper = "&";
}
$simple = 1; # only calls of named functions can be prototyped
$kid = $self->deparse($kid, 24);
- } elsif (is_scalar $kid->first) {
+ } elsif (is_scalar ($kid->first) && $kid->first->name ne 'rv2cv') {
$amper = "&";
$kid = $self->deparse($kid, 24);
} else {
# Doesn't matter how many prototypes there are, if
# they haven't happened yet!
- my $declared = exists $self->{'subs_declared'}{$kid};
- if (!$declared && defined($proto)) {
- # Avoid "too early to check prototype" warning
- ($amper, $proto) = ('&');
+ my $declared;
+ {
+ no strict 'refs';
+ no warnings 'uninitialized';
+ $declared = exists $self->{'subs_declared'}{$kid}
+ || (
+ defined &{ %{$self->{'curstash'}."::"}->{$kid} }
+ && !exists
+ $self->{'subs_deparsed'}{$self->{'curstash'}."::".$kid}
+ && defined prototype $self->{'curstash'}."::".$kid
+ );
+ if (!$declared && defined($proto)) {
+ # Avoid "too early to check prototype" warning
+ ($amper, $proto) = ('&');
+ }
}
my $args;
}
} else {
# glob() invocations can be translated into calls of
- # CORE::GLOBAL::glob with an second parameter, a number.
+ # CORE::GLOBAL::glob with a second parameter, a number.
# Reverse this.
if ($kid eq "CORE::GLOBAL::glob") {
$kid = "glob";
return $str;
}
-# the same, but treat $|, $), $( and $ at the end of the string differently
-sub re_uninterp {
- my($str) = @_;
-
+{
+my $bal;
+BEGIN {
use re "eval";
# Matches any string which is balanced with respect to {braces}
- my $bal = qr(
+ $bal = qr(
(?:
[^\\{}]
| \\\\
| \{(??{$bal})\}
)*
)x;
+}
+
+# the same, but treat $|, $), $( and $ at the end of the string differently
+sub re_uninterp {
+ my($str) = @_;
$str =~ s/
( ^|\G # $1
| \\[uUlLQE]
)
- /length($4) ? "$1$2$4" : "$1$2\\$3"/xeg;
+ /defined($4) && length($4) ? "$1$2$4" : "$1$2\\$3"/xeg;
+
+ return $str;
+}
+
+# This is for regular expressions with the /x modifier
+# We have to leave comments unmangled.
+sub re_uninterp_extended {
+ my($str) = @_;
+
+ $str =~ s/
+ ( ^|\G # $1
+ | [^\\]
+ )
+
+ ( # $2
+ (?:\\\\)*
+ )
+
+ ( # $3
+ ( \(\?\??\{$bal\}\) # $4 (skip over (?{}) and (??{}) blocks)
+ | \#[^\n]* # (skip over comments)
+ )
+ | [\$\@]
+ (?!\||\)|\(|$|\s)
+ | \\[uUlLQE]
+ )
+
+ /defined($4) && length($4) ? "$1$2$4" : "$1$2\\$3"/xeg;
return $str;
}
+}
+
+my %unctrl = # portable to to EBCDIC
+ (
+ "\c@" => '\c@', # unused
+ "\cA" => '\cA',
+ "\cB" => '\cB',
+ "\cC" => '\cC',
+ "\cD" => '\cD',
+ "\cE" => '\cE',
+ "\cF" => '\cF',
+ "\cG" => '\cG',
+ "\cH" => '\cH',
+ "\cI" => '\cI',
+ "\cJ" => '\cJ',
+ "\cK" => '\cK',
+ "\cL" => '\cL',
+ "\cM" => '\cM',
+ "\cN" => '\cN',
+ "\cO" => '\cO',
+ "\cP" => '\cP',
+ "\cQ" => '\cQ',
+ "\cR" => '\cR',
+ "\cS" => '\cS',
+ "\cT" => '\cT',
+ "\cU" => '\cU',
+ "\cV" => '\cV',
+ "\cW" => '\cW',
+ "\cX" => '\cX',
+ "\cY" => '\cY',
+ "\cZ" => '\cZ',
+ "\c[" => '\c[', # unused
+ "\c\\" => '\c\\', # unused
+ "\c]" => '\c]', # unused
+ "\c_" => '\c_', # unused
+ );
# character escapes, but not delimiters that might need to be escaped
sub escape_str { # ASCII, UTF8
my($str) = @_;
- $str =~ s/(.)/ord($1)>255 ? sprintf("\\x{%x}", ord($1)) : $1/eg;
+ $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/\cH/\\b/g; # \b means something different in a regex
$str =~ s/\t/\\t/g;
$str =~ s/\n/\\n/g;
$str =~ s/\e/\\e/g;
$str =~ s/\f/\\f/g;
$str =~ s/\r/\\r/g;
- $str =~ s/([\cA-\cZ])/'\\c' . chr(ord('@') + ord($1))/ge;
- $str =~ s/([\0\033-\037\177-\377])/'\\' . sprintf("%03o", ord($1))/ge;
+ $str =~ s/([\cA-\cZ])/$unctrl{$1}/ge;
+ $str =~ s/([[:^print:]])/sprintf("\\%03o", ord($1))/ge;
+ return $str;
+}
+
+# For regexes with the /x modifier.
+# Leave whitespace unmangled.
+sub escape_extended_re {
+ my($str) = @_;
+ $str =~ s/(.)/ord($1) > 255 ? sprintf("\\x{%x}", ord($1)) : $1/eg;
+ $str =~ s/([[:^print:]])/
+ ($1 =~ y! \t\n!!) ? $1 : sprintf("\\%03o", ord($1))/ge;
+ $str =~ s/\n/\n\f/g;
return $str;
}
my($str) = @_;
# the insane complexity here is due to the behaviour of "\c\"
- $str =~ s/(^|[^\\]|\\c\\)(?<!\\c)\\(\\\\)*(?=[\0-\031\177-\377])/$1$2/g;
+ $str =~ s/(^|[^\\]|\\c\\)(?<!\\c)\\(\\\\)*(?=[[:^print:]])/$1$2/g;
return $str;
}
sub single_delim {
my($q, $default, $str) = @_;
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 ('/', '"', '#') {
+ if ($q ne 'qr') {
+ (my $succeed, $str) = balanced_delim($str);
+ return "$q$str" if $succeed;
+ }
+ for my $delim ('/', '"', '#') {
return "$q$delim" . $str . $delim if index($str, $delim) == -1;
}
if ($default) {
sub const {
my $sv = shift;
if (class($sv) eq "SPECIAL") {
- return ('undef', '1', '0')[$$sv-1]; # sv_undef, sv_yes, sv_no
+ return ('undef', '1', '(!1)')[$$sv-1]; # sv_undef, sv_yes, sv_no
} elsif (class($sv) eq "NULL") {
return 'undef';
} elsif ($sv->FLAGS & SVf_IOK) {
my $first = $self->dq($op->first);
my $last = $self->dq($op->last);
- # Disambiguate "${foo}bar", "${foo}{bar}", "${foo}[1]"
+ # Disambiguate "${foo}bar", "${foo}{bar}", "${foo}[1]", "$foo\::bar"
($last =~ /^[A-Z\\\^\[\]_?]/ &&
$first =~ s/([\$@])\^$/${1}{^}/) # "${^}W" etc
- || ($last =~ /^[{\[\w_]/ &&
+ || ($last =~ /^[:'{\[\w_]/ &&
$first =~ s/([\$@])([A-Za-z_]\w*)$/${1}{$2}/);
return $first . $last;
# Like dq(), but different
sub re_dq {
my $self = shift;
- my $op = shift;
+ my ($op, $extended) = @_;
+
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)));
+ my $unbacked = re_unback($self->const_sv($op)->as_string);
+ return re_uninterp_extended(escape_extended_re($unbacked))
+ if $extended;
+ return re_uninterp(escape_str($unbacked));
} elsif ($type eq "concat") {
- my $first = $self->re_dq($op->first);
- my $last = $self->re_dq($op->last);
+ my $first = $self->re_dq($op->first, $extended);
+ my $last = $self->re_dq($op->last, $extended);
+
# 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}/;
- }
+ ($last =~ /^[A-Z\\\^\[\]_?]/ &&
+ $first =~ s/([\$@])\^$/${1}{^}/) # "${^}W" etc
+ || ($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';
+ return '\U' . $self->re_dq($op->first->sibling, $extended) . '\E';
} elsif ($type eq "lc") {
- return '\L' . $self->re_dq($op->first->sibling) . '\E';
+ return '\L' . $self->re_dq($op->first->sibling, $extended) . '\E';
} elsif ($type eq "ucfirst") {
- return '\u' . $self->re_dq($op->first->sibling);
+ return '\u' . $self->re_dq($op->first->sibling, $extended);
} elsif ($type eq "lcfirst") {
- return '\l' . $self->re_dq($op->first->sibling);
+ return '\l' . $self->re_dq($op->first->sibling, $extended);
} elsif ($type eq "quotemeta") {
- return '\Q' . $self->re_dq($op->first->sibling) . '\E';
+ return '\Q' . $self->re_dq($op->first->sibling, $extended) . '\E';
} elsif ($type eq "join") {
return $self->deparse($op->last, 26); # was join($", @ary)
} else {
}
}
-sub pp_regcomp {
+sub pure_string {
+ my ($self, $op) = @_;
+ return 0 if null $op;
+ my $type = $op->name;
+
+ if ($type eq 'const') {
+ return 1;
+ }
+ elsif ($type =~ /^[ul]c(first)?$/ || $type eq 'quotemeta') {
+ return $self->pure_string($op->first->sibling);
+ }
+ elsif ($type eq 'join') {
+ my $join_op = $op->first->sibling; # Skip pushmark
+ return 0 unless $join_op->name eq 'null' && $join_op->targ eq OP_RV2SV;
+
+ my $gvop = $join_op->first;
+ return 0 unless $gvop->name eq 'gvsv';
+ return 0 unless '"' eq $self->gv_name($self->gv_or_padgv($gvop));
+
+ return 0 unless ${$join_op->sibling} eq ${$op->last};
+ return 0 unless $op->last->name =~ /^(rv2|pad)av$/;
+ }
+ elsif ($type eq 'concat') {
+ return $self->pure_string($op->first)
+ && $self->pure_string($op->last);
+ }
+ elsif (is_scalar($op) || $type =~ /^[ah]elem(fast)?$/) {
+ return 1;
+ }
+ else {
+ return 0;
+ }
+
+ return 1;
+}
+
+sub regcomp {
my $self = shift;
- my($op, $cx) = @_;
+ my($op, $cx, $extended) = @_;
my $kid = $op->first;
$kid = $kid->first if $kid->name eq "regcmaybe";
$kid = $kid->first if $kid->name eq "regcreset";
- return $self->re_dq($kid);
+ return ($self->re_dq($kid, $extended), 1) if $self->pure_string($kid);
+ return ($self->deparse($kid, $cx), 0);
+}
+
+sub pp_regcomp {
+ my ($self, $op, $cx) = @_;
+ return (($self->regcomp($op, $cx, 0))[0]);
}
# osmic acid -- see osmium tetroxide
$var = $self->deparse($kid, 20);
$kid = $kid->sibling;
}
+ my $quote = 1;
+ my $extended = ($op->pmflags & PMf_EXTENDED);
if (null $kid) {
- $re = re_uninterp(escape_str(re_unback($op->precomp)));
+ my $unbacked = re_unback($op->precomp);
+ if ($extended) {
+ $re = re_uninterp_extended(escape_extended_re($unbacked));
+ } else {
+ $re = re_uninterp(escape_str(re_unback($op->precomp)));
+ }
+ } elsif ($kid->name ne 'regcomp') {
+ carp("found ".$kid->name." where regcomp expected");
} else {
- $re = $self->deparse($kid, 1);
+ ($re, $quote) = $self->regcomp($kid, 1, $extended);
}
my $flags = "";
$flags .= "c" if $op->pmflags & PMf_CONTINUE;
if ($op->pmflags & PMf_ONCE) { # only one kind of delimiter works here
$re =~ s/\?/\\?/g;
$re = "?$re?";
- } else {
+ } elsif ($quote) {
$re = single_delim($name, $delim, $re);
}
- $re = $re . $flags;
+ $re = $re . $flags if $quote;
if ($binop) {
return $self->maybe_parens("$var =~ $re", $cx, 20);
} else {
my($op, $cx) = @_;
my($kid, @exprs, $ary, $expr);
$kid = $op->first;
- if ($ {$kid->pmreplroot}) {
+ # under ithreads pmreplroot is an integer, not an SV
+ my $replroot = $kid->pmreplroot;
+ if ( ( ref($replroot) && $$replroot ) ||
+ ( !ref($replroot) && $replroot ) ) {
$ary = $self->stash_variable('@', $self->gv_name($kid->pmreplroot));
}
for (; !null($kid); $kid = $kid->sibling) {
$flags .= "e";
}
if ($op->pmflags & PMf_EVAL) {
- $repl = $self->deparse($repl, 0);
+ $repl = $self->deparse($repl, 0, 1);
} else {
$repl = $self->dq($repl);
}
}
+ my $extended = ($op->pmflags & PMf_EXTENDED);
if (null $kid) {
- $re = re_uninterp(escape_str(re_unback($op->precomp)));
+ my $unbacked = re_unback($op->precomp);
+ if ($extended) {
+ $re = re_uninterp_extended(escape_extended_re($unbacked));
+ }
+ else {
+ $re = re_uninterp(escape_str($unbacked));
+ }
} else {
- $re = $self->deparse($kid, 1);
+ ($re) = $self->regcomp($kid, 1, $extended);
}
$flags .= "e" if $op->pmflags & PMf_EVAL;
$flags .= "g" if $op->pmflags & PMf_GLOBAL;
$flags .= "m" if $op->pmflags & PMf_MULTILINE;
$flags .= "o" if $op->pmflags & PMf_KEEP;
$flags .= "s" if $op->pmflags & PMf_SINGLELINE;
- $flags .= "x" if $op->pmflags & PMf_EXTENDED;
+ $flags .= "x" if $extended;
$flags = $substwords{$flags} if $substwords{$flags};
if ($binop) {
return $self->maybe_parens("$var =~ s"
which probably isn't what you intended (the C<'???'> is a sign that
perl optimized away a constant value).
+=item B<-P>
+
+Disable prototype checking. With this option, all function calls are
+deparsed as if no prototype was defined for them. In other words,
+
+ perl -MO=Deparse,-P -e 'sub foo (\@) { 1 } foo @x'
+
+will print
+
+ sub foo (\@) {
+ 1;
+ }
+ &foo(\@x);
+
+making clear how the parameters are actually passed to C<foo>.
+
=item B<-q>
Expand double-quoted strings into the corresponding combinations of
=item *
-Lvalue method calls are not yet fully supported. (Ordinary lvalue
-subroutine calls ought to be okay though.)
+In fact, the above is a specific instance of a more general problem:
+we can't guarantee to produce BEGIN blocks or C<use> declarations in
+exactly the right place. So if you use a module which affects compilation
+(such as by over-riding keywords, overloading constants or whatever)
+then the output code might not work as intended.
-=item *
-
-If you have a regex which is anything other than a literal of some
-kind, B::Deparse will produce incorrect output.
-e.g. C<$foo =~ give_me_a_regex()> will come back as
-C<$foo =~ /give_me_a_regex()/>
-
-=item *
-
- m{ #foo
- bar }x
-
-comes out as
-
- m/#foo\n bar/x)
-
-which isn't right.
+This is the most serious outstanding problem, and will be very hard
+to fix.
=item *
If a keyword is over-ridden, and your program explicitly calls
the built-in version by using CORE::keyword, the output of B::Deparse
-will not reflect this.
+will not reflect this. If you run the resulting code, it will call
+the over-ridden version rather than the built-in one. (Maybe there
+should be an option to B<always> print keyword calls as C<CORE::name>.)
=item *
=item *
Constants (other than simple strings or numbers) don't work properly.
-Examples that fail include:
+Pathological examples that fail (and probably always will) include:
use constant E2BIG => ($!=7);
use constant x=>\$x; print x
+The following could (and should) be made to work:
+
+ use constant regex => qr/blah/;
+ print regex;
+
=item *
An input file that uses source filtering probably won't be deparsed into
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, Robin
-Houston, Hugo van der Sanden, Gurusamy Sarathy, and Nick Ing-Simmons.
+Houston, Hugo van der Sanden, Gurusamy Sarathy, Nick Ing-Simmons,
+and Rafael Garcia-Suarez.
=cut