1N/A# B::Deparse.pm
1N/A# Copyright (c) 1998-2000, 2002, 2003 Stephen McCamant. All rights reserved.
1N/A# This module is free software; you can redistribute and/or modify
1N/A# it under the same terms as Perl itself.
1N/A
1N/A# This is based on the module of the same name by Malcolm Beattie,
1N/A# but essentially none of his code remains.
1N/A
1N/Apackage B::Deparse;
1N/Ause Carp;
1N/Ause B qw(class main_root main_start main_cv svref_2object opnumber perlstring
1N/A OPf_WANT OPf_WANT_VOID OPf_WANT_SCALAR OPf_WANT_LIST
1N/A OPf_KIDS OPf_REF OPf_STACKED OPf_SPECIAL OPf_MOD
1N/A OPpLVAL_INTRO OPpOUR_INTRO OPpENTERSUB_AMPER OPpSLICE OPpCONST_BARE
1N/A OPpTRANS_SQUASH OPpTRANS_DELETE OPpTRANS_COMPLEMENT OPpTARGET_MY
1N/A OPpCONST_ARYBASE OPpEXISTS_SUB OPpSORT_NUMERIC OPpSORT_INTEGER
1N/A OPpSORT_REVERSE OPpSORT_INPLACE
1N/A SVf_IOK SVf_NOK SVf_ROK SVf_POK SVpad_OUR SVf_FAKE SVs_RMG SVs_SMG
1N/A CVf_METHOD CVf_LOCKED CVf_LVALUE
1N/A PMf_KEEP PMf_GLOBAL PMf_CONTINUE PMf_EVAL PMf_ONCE PMf_SKIPWHITE
1N/A PMf_MULTILINE PMf_SINGLELINE PMf_FOLD PMf_EXTENDED);
1N/A# Not sure if I really should have this as maint's version, given that Deparse
1N/A# differs from blead. (latter has // support)
1N/A$VERSION = 0.66;
1N/Ause strict;
1N/Ause vars qw/$AUTOLOAD/;
1N/Ause warnings ();
1N/A
1N/A# Changes between 0.50 and 0.51:
1N/A# - fixed nulled leave with live enter in sort { }
1N/A# - fixed reference constants (\"str")
1N/A# - handle empty programs gracefully
1N/A# - handle infinte loops (for (;;) {}, while (1) {})
1N/A# - differentiate between `for my $x ...' and `my $x; for $x ...'
1N/A# - various minor cleanups
1N/A# - moved globals into an object
1N/A# - added `-u', like B::C
1N/A# - package declarations using cop_stash
1N/A# - subs, formats and code sorted by cop_seq
1N/A# Changes between 0.51 and 0.52:
1N/A# - added pp_threadsv (special variables under USE_5005THREADS)
1N/A# - added documentation
1N/A# Changes between 0.52 and 0.53:
1N/A# - many changes adding precedence contexts and associativity
1N/A# - added `-p' and `-s' output style options
1N/A# - various other minor fixes
1N/A# Changes between 0.53 and 0.54:
1N/A# - added support for new `for (1..100)' optimization,
1N/A# thanks to Gisle Aas
1N/A# Changes between 0.54 and 0.55:
1N/A# - added support for new qr// construct
1N/A# - added support for new pp_regcreset OP
1N/A# Changes between 0.55 and 0.56:
1N/A# - tested on base/*.t, cmd/*.t, comp/*.t, io/*.t
1N/A# - fixed $# on non-lexicals broken in last big rewrite
1N/A# - added temporary fix for change in opcode of OP_STRINGIFY
1N/A# - fixed problem in 0.54's for() patch in `for (@ary)'
1N/A# - fixed precedence in conditional of ?:
1N/A# - tweaked list paren elimination in `my($x) = @_'
1N/A# - made continue-block detection trickier wrt. null ops
1N/A# - fixed various prototype problems in pp_entersub
1N/A# - added support for sub prototypes that never get GVs
1N/A# - added unquoting for special filehandle first arg in truncate
1N/A# - print doubled rv2gv (a bug) as `*{*GV}' instead of illegal `**GV'
1N/A# - added semicolons at the ends of blocks
1N/A# - added -l `#line' declaration option -- fixes cmd/subval.t 27,28
1N/A# Changes between 0.56 and 0.561:
1N/A# - fixed multiply-declared my var in pp_truncate (thanks to Sarathy)
1N/A# - used new B.pm symbolic constants (done by Nick Ing-Simmons)
1N/A# Changes between 0.561 and 0.57:
1N/A# - stylistic changes to symbolic constant stuff
1N/A# - handled scope in s///e replacement code
1N/A# - added unquote option for expanding "" into concats, etc.
1N/A# - split method and proto parts of pp_entersub into separate functions
1N/A# - various minor cleanups
1N/A# Changes after 0.57:
1N/A# - added parens in \&foo (patch by Albert Dvornik)
1N/A# Changes between 0.57 and 0.58:
1N/A# - fixed `0' statements that weren't being printed
1N/A# - added methods for use from other programs
1N/A# (based on patches from James Duncan and Hugo van der Sanden)
1N/A# - added -si and -sT to control indenting (also based on a patch from Hugo)
1N/A# - added -sv to print something else instead of '???'
1N/A# - preliminary version of utf8 tr/// handling
1N/A# Changes after 0.58:
1N/A# - uses of $op->ppaddr changed to new $op->name (done by Sarathy)
1N/A# - added support for Hugo's new OP_SETSTATE (like nextstate)
1N/A# Changes between 0.58 and 0.59
1N/A# - added support for Chip's OP_METHOD_NAMED
1N/A# - added support for Ilya's OPpTARGET_MY optimization
1N/A# - elided arrows before `()' subscripts when possible
1N/A# Changes between 0.59 and 0.60
1N/A# - support for method attribues was added
1N/A# - some warnings fixed
1N/A# - separate recognition of constant subs
1N/A# - rewrote continue block handling, now recoginizing for loops
1N/A# - added more control of expanding control structures
1N/A# Changes between 0.60 and 0.61 (mostly by Robin Houston)
1N/A# - many bug-fixes
1N/A# - support for pragmas and 'use'
1N/A# - support for the little-used $[ variable
1N/A# - support for __DATA__ sections
1N/A# - UTF8 support
1N/A# - BEGIN, CHECK, INIT and END blocks
1N/A# - scoping of subroutine declarations fixed
1N/A# - compile-time output from the input program can be suppressed, so that the
1N/A# output is just the deparsed code. (a change to O.pm in fact)
1N/A# - our() declarations
1N/A# - *all* the known bugs are now listed in the BUGS section
1N/A# - comprehensive test mechanism (TEST -deparse)
1N/A# Changes between 0.62 and 0.63 (mostly by Rafael Garcia-Suarez)
1N/A# - bug-fixes
1N/A# - new switch -P
1N/A# - support for command-line switches (-l, -0, etc.)
1N/A# Changes between 0.63 and 0.64
1N/A# - support for //, CHECK blocks, and assertions
1N/A# - improved handling of foreach loops and lexicals
1N/A# - option to use Data::Dumper for constants
1N/A# - more bug fixes
1N/A# - discovered lots more bugs not yet fixed
1N/A
1N/A# Todo:
1N/A# (See also BUGS section at the end of this file)
1N/A#
1N/A# - finish tr/// changes
1N/A# - add option for even more parens (generalize \&foo change)
1N/A# - left/right context
1N/A# - copy comments (look at real text with $^P?)
1N/A# - avoid semis in one-statement blocks
1N/A# - associativity of &&=, ||=, ?:
1N/A# - ',' => '=>' (auto-unquote?)
1N/A# - break long lines ("\r" as discretionary break?)
1N/A# - configurable syntax highlighting: ANSI color, HTML, TeX, etc.
1N/A# - more style options: brace style, hex vs. octal, quotes, ...
1N/A# - print big ints as hex/octal instead of decimal (heuristic?)
1N/A# - handle `my $x if 0'?
1N/A# - version using op_next instead of op_first/sibling?
1N/A# - avoid string copies (pass arrays, one big join?)
1N/A# - here-docs?
1N/A
1N/A# Current test.deparse failures
1N/A# comp/assertions 38 - disabled assertions should be like "my($x) if 0"
1N/A# 'sub f : assertion {}; no assertions; my $x=1; {f(my $x=2); print "$x\n"}'
1N/A# comp/hints 6 - location of BEGIN blocks wrt. block openings
1N/A# run/switchI 1 - missing -I switches entirely
1N/A# perl -Ifoo -e 'print @INC'
1N/A# op/caller 2 - warning mask propagates backwards before warnings::register
1N/A# 'use warnings; BEGIN {${^WARNING_BITS} eq "U"x12;} use warnings::register'
1N/A# op/getpid 2 - can't assign to shared my() declaration (threads only)
1N/A# 'my $x : shared = 5'
1N/A# op/override 7 - parens on overriden require change v-string interpretation
1N/A# 'BEGIN{*CORE::GLOBAL::require=sub {}} require v5.6'
1N/A# c.f. 'BEGIN { *f = sub {0} }; f 2'
1N/A# op/pat 774 - losing Unicode-ness of Latin1-only strings
1N/A# 'use charnames ":short"; $x="\N{latin:a with acute}"'
1N/A# op/recurse 12 - missing parens on recursive call makes it look like method
1N/A# 'sub f { f($x) }'
1N/A# op/subst 90 - inconsistent handling of utf8 under "use utf8"
1N/A# op/taint 29 - "use re 'taint'" deparsed in the wrong place wrt. block open
1N/A# op/tiehandle compile - "use strict" deparsed in the wrong place
1N/A# uni/tr_ several
1N/A# ext/B/t/xref 11 - line numbers when we add newlines to one-line subs
1N/A# ext/Data/Dumper/t/dumper compile
1N/A# ext/DB_file/several
1N/A# ext/Encode/several
1N/A# ext/Ernno/Errno warnings
1N/A# ext/IO/lib/IO/t/io_sel 23
1N/A# ext/PerlIO/t/encoding compile
1N/A# ext/POSIX/t/posix 6
1N/A# ext/Socket/Socket 8
1N/A# ext/Storable/t/croak compile
1N/A# lib/Attribute/Handlers/t/multi compile
1N/A# lib/bignum/ several
1N/A# lib/charnames 35
1N/A# lib/constant 32
1N/A# lib/English 40
1N/A# lib/ExtUtils/t/bytes 4
1N/A# lib/File/DosGlob compile
1N/A# lib/Filter/Simple/t/data 1
1N/A# lib/Math/BigInt/t/constant 1
1N/A# lib/Net/t/config Deparse-warning
1N/A# lib/overload compile
1N/A# lib/Switch/ several
1N/A# lib/Symbol 4
1N/A# lib/Test/Simple several
1N/A# lib/Term/Complete
1N/A# lib/Tie/File/t/29_downcopy 5
1N/A# lib/vars 22
1N/A
1N/A# Object fields (were globals):
1N/A#
1N/A# avoid_local:
1N/A# (local($a), local($b)) and local($a, $b) have the same internal
1N/A# representation but the short form looks better. We notice we can
1N/A# use a large-scale local when checking the list, but need to prevent
1N/A# individual locals too. This hash holds the addresses of OPs that
1N/A# have already had their local-ness accounted for. The same thing
1N/A# is done with my().
1N/A#
1N/A# curcv:
1N/A# CV for current sub (or main program) being deparsed
1N/A#
1N/A# curcvlex:
1N/A# Cached hash of lexical variables for curcv: keys are names,
1N/A# each value is an array of pairs, indicating the cop_seq of scopes
1N/A# in which a var of that name is valid.
1N/A#
1N/A# curcop:
1N/A# COP for statement being deparsed
1N/A#
1N/A# curstash:
1N/A# name of the current package for deparsed code
1N/A#
1N/A# subs_todo:
1N/A# array of [cop_seq, CV, is_format?] for subs and formats we still
1N/A# want to deparse
1N/A#
1N/A# protos_todo:
1N/A# as above, but [name, prototype] for subs that never got a GV
1N/A#
1N/A# subs_done, forms_done:
1N/A# keys are addresses of GVs for subs and formats we've already
1N/A# deparsed (or at least put into subs_todo)
1N/A#
1N/A# subs_declared
1N/A# keys are names of subs for which we've printed declarations.
1N/A# That means we can omit parentheses from the arguments.
1N/A#
1N/A# subs_deparsed
1N/A# Keeps track of fully qualified names of all deparsed subs.
1N/A#
1N/A# parens: -p
1N/A# linenums: -l
1N/A# unquote: -q
1N/A# cuddle: ` ' or `\n', depending on -sC
1N/A# indent_size: -si
1N/A# use_tabs: -sT
1N/A# ex_const: -sv
1N/A
1N/A# A little explanation of how precedence contexts and associativity
1N/A# work:
1N/A#
1N/A# deparse() calls each per-op subroutine with an argument $cx (short
1N/A# for context, but not the same as the cx* in the perl core), which is
1N/A# a number describing the op's parents in terms of precedence, whether
1N/A# they're inside an expression or at statement level, etc. (see
1N/A# chart below). When ops with children call deparse on them, they pass
1N/A# along their precedence. Fractional values are used to implement
1N/A# associativity (`($x + $y) + $z' => `$x + $y + $y') and related
1N/A# parentheses hacks. The major disadvantage of this scheme is that
1N/A# it doesn't know about right sides and left sides, so say if you
1N/A# assign a listop to a variable, it can't tell it's allowed to leave
1N/A# the parens off the listop.
1N/A
1N/A# Precedences:
1N/A# 26 [TODO] inside interpolation context ("")
1N/A# 25 left terms and list operators (leftward)
1N/A# 24 left ->
1N/A# 23 nonassoc ++ --
1N/A# 22 right **
1N/A# 21 right ! ~ \ and unary + and -
1N/A# 20 left =~ !~
1N/A# 19 left * / % x
1N/A# 18 left + - .
1N/A# 17 left << >>
1N/A# 16 nonassoc named unary operators
1N/A# 15 nonassoc < > <= >= lt gt le ge
1N/A# 14 nonassoc == != <=> eq ne cmp
1N/A# 13 left &
1N/A# 12 left | ^
1N/A# 11 left &&
1N/A# 10 left ||
1N/A# 9 nonassoc .. ...
1N/A# 8 right ?:
1N/A# 7 right = += -= *= etc.
1N/A# 6 left , =>
1N/A# 5 nonassoc list operators (rightward)
1N/A# 4 right not
1N/A# 3 left and
1N/A# 2 left or xor
1N/A# 1 statement modifiers
1N/A# 0.5 statements, but still print scopes as do { ... }
1N/A# 0 statement level
1N/A
1N/A# Nonprinting characters with special meaning:
1N/A# \cS - steal parens (see maybe_parens_unop)
1N/A# \n - newline and indent
1N/A# \t - increase indent
1N/A# \b - decrease indent (`outdent')
1N/A# \f - flush left (no indent)
1N/A# \cK - kill following semicolon, if any
1N/A
1N/Asub null {
1N/A my $op = shift;
1N/A return class($op) eq "NULL";
1N/A}
1N/A
1N/Asub todo {
1N/A my $self = shift;
1N/A my($cv, $is_form) = @_;
1N/A return unless ($cv->FILE eq $0 || exists $self->{files}{$cv->FILE});
1N/A my $seq;
1N/A if ($cv->OUTSIDE_SEQ) {
1N/A $seq = $cv->OUTSIDE_SEQ;
1N/A } elsif (!null($cv->START) and is_state($cv->START)) {
1N/A $seq = $cv->START->cop_seq;
1N/A } else {
1N/A $seq = 0;
1N/A }
1N/A push @{$self->{'subs_todo'}}, [$seq, $cv, $is_form];
1N/A unless ($is_form || class($cv->STASH) eq 'SPECIAL') {
1N/A $self->{'subs_deparsed'}{$cv->STASH->NAME."::".$cv->GV->NAME} = 1;
1N/A }
1N/A}
1N/A
1N/Asub next_todo {
1N/A my $self = shift;
1N/A my $ent = shift @{$self->{'subs_todo'}};
1N/A my $cv = $ent->[1];
1N/A my $gv = $cv->GV;
1N/A my $name = $self->gv_name($gv);
1N/A if ($ent->[2]) {
1N/A return "format $name =\n"
1N/A . $self->deparse_format($ent->[1]). "\n";
1N/A } else {
1N/A $self->{'subs_declared'}{$name} = 1;
1N/A if ($name eq "BEGIN") {
1N/A my $use_dec = $self->begin_is_use($cv);
1N/A if (defined ($use_dec) and $self->{'expand'} < 5) {
1N/A return () if 0 == length($use_dec);
1N/A return $use_dec;
1N/A }
1N/A }
1N/A my $l = '';
1N/A if ($self->{'linenums'}) {
1N/A my $line = $gv->LINE;
1N/A my $file = $gv->FILE;
1N/A $l = "\n\f#line $line \"$file\"\n";
1N/A }
1N/A my $p = '';
1N/A if (class($cv->STASH) ne "SPECIAL") {
1N/A my $stash = $cv->STASH->NAME;
1N/A if ($stash ne $self->{'curstash'}) {
1N/A $p = "package $stash;\n";
1N/A $name = "$self->{'curstash'}::$name" unless $name =~ /::/;
1N/A $self->{'curstash'} = $stash;
1N/A }
1N/A $name =~ s/^\Q$stash\E:://;
1N/A }
1N/A return "${p}${l}sub $name " . $self->deparse_sub($cv);
1N/A }
1N/A}
1N/A
1N/A# Return a "use" declaration for this BEGIN block, if appropriate
1N/Asub begin_is_use {
1N/A my ($self, $cv) = @_;
1N/A my $root = $cv->ROOT;
1N/A local @$self{qw'curcv curcvlex'} = ($cv);
1N/A#require B::Debug;
1N/A#B::walkoptree($cv->ROOT, "debug");
1N/A my $lineseq = $root->first;
1N/A return if $lineseq->name ne "lineseq";
1N/A
1N/A my $req_op = $lineseq->first->sibling;
1N/A return if $req_op->name ne "require";
1N/A
1N/A my $module;
1N/A if ($req_op->first->private & OPpCONST_BARE) {
1N/A # Actually it should always be a bareword
1N/A $module = $self->const_sv($req_op->first)->PV;
1N/A $module =~ s[/][::]g;
1N/A $module =~ s/.pm$//;
1N/A }
1N/A else {
1N/A $module = $self->const($self->const_sv($req_op->first), 6);
1N/A }
1N/A
1N/A my $version;
1N/A my $version_op = $req_op->sibling;
1N/A return if class($version_op) eq "NULL";
1N/A if ($version_op->name eq "lineseq") {
1N/A # We have a version parameter; skip nextstate & pushmark
1N/A my $constop = $version_op->first->next->next;
1N/A
1N/A return unless $self->const_sv($constop)->PV eq $module;
1N/A $constop = $constop->sibling;
1N/A $version = $self->const_sv($constop);
1N/A if (class($version) eq "IV") {
1N/A $version = $version->int_value;
1N/A } elsif (class($version) eq "NV") {
1N/A $version = $version->NV;
1N/A } elsif (class($version) ne "PVMG") {
1N/A # Includes PVIV and PVNV
1N/A $version = $version->PV;
1N/A } else {
1N/A # version specified as a v-string
1N/A $version = 'v'.join '.', map ord, split //, $version->PV;
1N/A }
1N/A $constop = $constop->sibling;
1N/A return if $constop->name ne "method_named";
1N/A return if $self->const_sv($constop)->PV ne "VERSION";
1N/A }
1N/A
1N/A $lineseq = $version_op->sibling;
1N/A return if $lineseq->name ne "lineseq";
1N/A my $entersub = $lineseq->first->sibling;
1N/A if ($entersub->name eq "stub") {
1N/A return "use $module $version ();\n" if defined $version;
1N/A return "use $module ();\n";
1N/A }
1N/A return if $entersub->name ne "entersub";
1N/A
1N/A # See if there are import arguments
1N/A my $args = '';
1N/A
1N/A my $svop = $entersub->first->sibling; # Skip over pushmark
1N/A return unless $self->const_sv($svop)->PV eq $module;
1N/A
1N/A # Pull out the arguments
1N/A for ($svop=$svop->sibling; $svop->name ne "method_named";
1N/A $svop = $svop->sibling) {
1N/A $args .= ", " if length($args);
1N/A $args .= $self->deparse($svop, 6);
1N/A }
1N/A
1N/A my $use = 'use';
1N/A my $method_named = $svop;
1N/A return if $method_named->name ne "method_named";
1N/A my $method_name = $self->const_sv($method_named)->PV;
1N/A
1N/A if ($method_name eq "unimport") {
1N/A $use = 'no';
1N/A }
1N/A
1N/A # Certain pragmas are dealt with using hint bits,
1N/A # so we ignore them here
1N/A if ($module eq 'strict' || $module eq 'integer'
1N/A || $module eq 'bytes' || $module eq 'warnings') {
1N/A return "";
1N/A }
1N/A
1N/A if (defined $version && length $args) {
1N/A return "$use $module $version ($args);\n";
1N/A } elsif (defined $version) {
1N/A return "$use $module $version;\n";
1N/A } elsif (length $args) {
1N/A return "$use $module ($args);\n";
1N/A } else {
1N/A return "$use $module;\n";
1N/A }
1N/A}
1N/A
1N/Asub stash_subs {
1N/A my ($self, $pack) = @_;
1N/A my (@ret, $stash);
1N/A if (!defined $pack) {
1N/A $pack = '';
1N/A $stash = \%::;
1N/A }
1N/A else {
1N/A $pack =~ s/(::)?$/::/;
1N/A no strict 'refs';
1N/A $stash = \%$pack;
1N/A }
1N/A my %stash = svref_2object($stash)->ARRAY;
1N/A while (my ($key, $val) = each %stash) {
1N/A next if $key eq 'main::'; # avoid infinite recursion
1N/A my $class = class($val);
1N/A if ($class eq "PV") {
1N/A # Just a prototype. As an ugly but fairly effective way
1N/A # to find out if it belongs here is to see if the AUTOLOAD
1N/A # (if any) for the stash was defined in one of our files.
1N/A my $A = $stash{"AUTOLOAD"};
1N/A if (defined ($A) && class($A) eq "GV" && defined($A->CV)
1N/A && class($A->CV) eq "CV") {
1N/A my $AF = $A->FILE;
1N/A next unless $AF eq $0 || exists $self->{'files'}{$AF};
1N/A }
1N/A push @{$self->{'protos_todo'}}, [$pack . $key, $val->PV];
1N/A } elsif ($class eq "IV") {
1N/A # Just a name. As above.
1N/A my $A = $stash{"AUTOLOAD"};
1N/A if (defined ($A) && class($A) eq "GV" && defined($A->CV)
1N/A && class($A->CV) eq "CV") {
1N/A my $AF = $A->FILE;
1N/A next unless $AF eq $0 || exists $self->{'files'}{$AF};
1N/A }
1N/A push @{$self->{'protos_todo'}}, [$pack . $key, undef];
1N/A } elsif ($class eq "GV") {
1N/A if (class(my $cv = $val->CV) ne "SPECIAL") {
1N/A next if $self->{'subs_done'}{$$val}++;
1N/A next if $$val != ${$cv->GV}; # Ignore imposters
1N/A $self->todo($cv, 0);
1N/A }
1N/A if (class(my $cv = $val->FORM) ne "SPECIAL") {
1N/A next if $self->{'forms_done'}{$$val}++;
1N/A next if $$val != ${$cv->GV}; # Ignore imposters
1N/A $self->todo($cv, 1);
1N/A }
1N/A if (class($val->HV) ne "SPECIAL" && $key =~ /::$/) {
1N/A $self->stash_subs($pack . $key);
1N/A }
1N/A }
1N/A }
1N/A}
1N/A
1N/Asub print_protos {
1N/A my $self = shift;
1N/A my $ar;
1N/A my @ret;
1N/A foreach $ar (@{$self->{'protos_todo'}}) {
1N/A my $proto = (defined $ar->[1] ? " (". $ar->[1] . ")" : "");
1N/A push @ret, "sub " . $ar->[0] . "$proto;\n";
1N/A }
1N/A delete $self->{'protos_todo'};
1N/A return @ret;
1N/A}
1N/A
1N/Asub style_opts {
1N/A my $self = shift;
1N/A my $opts = shift;
1N/A my $opt;
1N/A while (length($opt = substr($opts, 0, 1))) {
1N/A if ($opt eq "C") {
1N/A $self->{'cuddle'} = " ";
1N/A $opts = substr($opts, 1);
1N/A } elsif ($opt eq "i") {
1N/A $opts =~ s/^i(\d+)//;
1N/A $self->{'indent_size'} = $1;
1N/A } elsif ($opt eq "T") {
1N/A $self->{'use_tabs'} = 1;
1N/A $opts = substr($opts, 1);
1N/A } elsif ($opt eq "v") {
1N/A $opts =~ s/^v([^.]*)(.|$)//;
1N/A $self->{'ex_const'} = $1;
1N/A }
1N/A }
1N/A}
1N/A
1N/Asub new {
1N/A my $class = shift;
1N/A my $self = bless {}, $class;
1N/A $self->{'cuddle'} = "\n";
1N/A $self->{'curcop'} = undef;
1N/A $self->{'curstash'} = "main";
1N/A $self->{'ex_const'} = "'???'";
1N/A $self->{'expand'} = 0;
1N/A $self->{'files'} = {};
1N/A $self->{'indent_size'} = 4;
1N/A $self->{'linenums'} = 0;
1N/A $self->{'parens'} = 0;
1N/A $self->{'subs_todo'} = [];
1N/A $self->{'unquote'} = 0;
1N/A $self->{'use_dumper'} = 0;
1N/A $self->{'use_tabs'} = 0;
1N/A
1N/A $self->{'ambient_arybase'} = 0;
1N/A $self->{'ambient_warnings'} = undef; # Assume no lexical warnings
1N/A $self->{'ambient_hints'} = 0;
1N/A $self->init();
1N/A
1N/A while (my $arg = shift @_) {
1N/A if ($arg eq "-d") {
1N/A $self->{'use_dumper'} = 1;
1N/A require Data::Dumper;
1N/A } elsif ($arg =~ /^-f(.*)/) {
1N/A $self->{'files'}{$1} = 1;
1N/A } elsif ($arg eq "-l") {
1N/A $self->{'linenums'} = 1;
1N/A } elsif ($arg eq "-p") {
1N/A $self->{'parens'} = 1;
1N/A } elsif ($arg eq "-P") {
1N/A $self->{'noproto'} = 1;
1N/A } elsif ($arg eq "-q") {
1N/A $self->{'unquote'} = 1;
1N/A } elsif (substr($arg, 0, 2) eq "-s") {
1N/A $self->style_opts(substr $arg, 2);
1N/A } elsif ($arg =~ /^-x(\d)$/) {
1N/A $self->{'expand'} = $1;
1N/A }
1N/A }
1N/A return $self;
1N/A}
1N/A
1N/A{
1N/A # Mask out the bits that L<warnings::register> uses
1N/A my $WARN_MASK;
1N/A BEGIN {
1N/A $WARN_MASK = $warnings::Bits{all} | $warnings::DeadBits{all};
1N/A }
1N/A sub WARN_MASK () {
1N/A return $WARN_MASK;
1N/A }
1N/A}
1N/A
1N/A# Initialise the contextual information, either from
1N/A# defaults provided with the ambient_pragmas method,
1N/A# or from perl's own defaults otherwise.
1N/Asub init {
1N/A my $self = shift;
1N/A
1N/A $self->{'arybase'} = $self->{'ambient_arybase'};
1N/A $self->{'warnings'} = defined ($self->{'ambient_warnings'})
1N/A ? $self->{'ambient_warnings'} & WARN_MASK
1N/A : undef;
1N/A $self->{'hints'} = $self->{'ambient_hints'} & 0xFF;
1N/A
1N/A # also a convenient place to clear out subs_declared
1N/A delete $self->{'subs_declared'};
1N/A}
1N/A
1N/Asub compile {
1N/A my(@args) = @_;
1N/A return sub {
1N/A my $self = B::Deparse->new(@args);
1N/A # First deparse command-line args
1N/A if (defined $^I) { # deparse -i
1N/A print q(BEGIN { $^I = ).perlstring($^I).qq(; }\n);
1N/A }
1N/A if ($^W) { # deparse -w
1N/A print qq(BEGIN { \$^W = $^W; }\n);
1N/A }
1N/A if ($/ ne "\n" or defined $O::savebackslash) { # deparse -l and -0
1N/A my $fs = perlstring($/) || 'undef';
1N/A my $bs = perlstring($O::savebackslash) || 'undef';
1N/A print qq(BEGIN { \$/ = $fs; \$\\ = $bs; }\n);
1N/A }
1N/A my @BEGINs = B::begin_av->isa("B::AV") ? B::begin_av->ARRAY : ();
1N/A my @CHECKs = B::check_av->isa("B::AV") ? B::check_av->ARRAY : ();
1N/A my @INITs = B::init_av->isa("B::AV") ? B::init_av->ARRAY : ();
1N/A my @ENDs = B::end_av->isa("B::AV") ? B::end_av->ARRAY : ();
1N/A for my $block (@BEGINs, @CHECKs, @INITs, @ENDs) {
1N/A $self->todo($block, 0);
1N/A }
1N/A $self->stash_subs();
1N/A local($SIG{"__DIE__"}) =
1N/A sub {
1N/A if ($self->{'curcop'}) {
1N/A my $cop = $self->{'curcop'};
1N/A my($line, $file) = ($cop->line, $cop->file);
1N/A print STDERR "While deparsing $file near line $line,\n";
1N/A }
1N/A };
1N/A $self->{'curcv'} = main_cv;
1N/A $self->{'curcvlex'} = undef;
1N/A print $self->print_protos;
1N/A @{$self->{'subs_todo'}} =
1N/A sort {$a->[0] <=> $b->[0]} @{$self->{'subs_todo'}};
1N/A print $self->indent($self->deparse_root(main_root)), "\n"
1N/A unless null main_root;
1N/A my @text;
1N/A while (scalar(@{$self->{'subs_todo'}})) {
1N/A push @text, $self->next_todo;
1N/A }
1N/A print $self->indent(join("", @text)), "\n" if @text;
1N/A
1N/A # Print __DATA__ section, if necessary
1N/A no strict 'refs';
1N/A my $laststash = defined $self->{'curcop'}
1N/A ? $self->{'curcop'}->stash->NAME : $self->{'curstash'};
1N/A if (defined *{$laststash."::DATA"}{IO}) {
1N/A print "package $laststash;\n"
1N/A unless $laststash eq $self->{'curstash'};
1N/A print "__DATA__\n";
1N/A print readline(*{$laststash."::DATA"});
1N/A }
1N/A }
1N/A}
1N/A
1N/Asub coderef2text {
1N/A my $self = shift;
1N/A my $sub = shift;
1N/A croak "Usage: ->coderef2text(CODEREF)" unless UNIVERSAL::isa($sub, "CODE");
1N/A
1N/A $self->init();
1N/A return $self->indent($self->deparse_sub(svref_2object($sub)));
1N/A}
1N/A
1N/Asub ambient_pragmas {
1N/A my $self = shift;
1N/A my ($arybase, $hint_bits, $warning_bits) = (0, 0);
1N/A
1N/A while (@_ > 1) {
1N/A my $name = shift();
1N/A my $val = shift();
1N/A
1N/A if ($name eq 'strict') {
1N/A require strict;
1N/A
1N/A if ($val eq 'none') {
1N/A $hint_bits &= ~strict::bits(qw/refs subs vars/);
1N/A next();
1N/A }
1N/A
1N/A my @names;
1N/A if ($val eq "all") {
1N/A @names = qw/refs subs vars/;
1N/A }
1N/A elsif (ref $val) {
1N/A @names = @$val;
1N/A }
1N/A else {
1N/A @names = split' ', $val;
1N/A }
1N/A $hint_bits |= strict::bits(@names);
1N/A }
1N/A
1N/A elsif ($name eq '$[') {
1N/A $arybase = $val;
1N/A }
1N/A
1N/A elsif ($name eq 'integer'
1N/A || $name eq 'bytes'
1N/A || $name eq 'utf8') {
1N/A require "$name.pm";
1N/A if ($val) {
1N/A $hint_bits |= ${$::{"${name}::"}{"hint_bits"}};
1N/A }
1N/A else {
1N/A $hint_bits &= ~${$::{"${name}::"}{"hint_bits"}};
1N/A }
1N/A }
1N/A
1N/A elsif ($name eq 're') {
1N/A require re;
1N/A if ($val eq 'none') {
1N/A $hint_bits &= ~re::bits(qw/taint eval/);
1N/A next();
1N/A }
1N/A
1N/A my @names;
1N/A if ($val eq 'all') {
1N/A @names = qw/taint eval/;
1N/A }
1N/A elsif (ref $val) {
1N/A @names = @$val;
1N/A }
1N/A else {
1N/A @names = split' ',$val;
1N/A }
1N/A $hint_bits |= re::bits(@names);
1N/A }
1N/A
1N/A elsif ($name eq 'warnings') {
1N/A if ($val eq 'none') {
1N/A $warning_bits = $warnings::NONE;
1N/A next();
1N/A }
1N/A
1N/A my @names;
1N/A if (ref $val) {
1N/A @names = @$val;
1N/A }
1N/A else {
1N/A @names = split/\s+/, $val;
1N/A }
1N/A
1N/A $warning_bits = $warnings::NONE if !defined ($warning_bits);
1N/A $warning_bits |= warnings::bits(@names);
1N/A }
1N/A
1N/A elsif ($name eq 'warning_bits') {
1N/A $warning_bits = $val;
1N/A }
1N/A
1N/A elsif ($name eq 'hint_bits') {
1N/A $hint_bits = $val;
1N/A }
1N/A
1N/A else {
1N/A croak "Unknown pragma type: $name";
1N/A }
1N/A }
1N/A if (@_) {
1N/A croak "The ambient_pragmas method expects an even number of args";
1N/A }
1N/A
1N/A $self->{'ambient_arybase'} = $arybase;
1N/A $self->{'ambient_warnings'} = $warning_bits;
1N/A $self->{'ambient_hints'} = $hint_bits;
1N/A}
1N/A
1N/A# This method is the inner loop, so try to keep it simple
1N/Asub deparse {
1N/A my $self = shift;
1N/A my($op, $cx) = @_;
1N/A
1N/A Carp::confess("Null op in deparse") if !defined($op)
1N/A || class($op) eq "NULL";
1N/A my $meth = "pp_" . $op->name;
1N/A return $self->$meth($op, $cx);
1N/A}
1N/A
1N/Asub indent {
1N/A my $self = shift;
1N/A my $txt = shift;
1N/A my @lines = split(/\n/, $txt);
1N/A my $leader = "";
1N/A my $level = 0;
1N/A my $line;
1N/A for $line (@lines) {
1N/A my $cmd = substr($line, 0, 1);
1N/A if ($cmd eq "\t" or $cmd eq "\b") {
1N/A $level += ($cmd eq "\t" ? 1 : -1) * $self->{'indent_size'};
1N/A if ($self->{'use_tabs'}) {
1N/A $leader = "\t" x ($level / 8) . " " x ($level % 8);
1N/A } else {
1N/A $leader = " " x $level;
1N/A }
1N/A $line = substr($line, 1);
1N/A }
1N/A if (substr($line, 0, 1) eq "\f") {
1N/A $line = substr($line, 1); # no indent
1N/A } else {
1N/A $line = $leader . $line;
1N/A }
1N/A $line =~ s/\cK;?//g;
1N/A }
1N/A return join("\n", @lines);
1N/A}
1N/A
1N/Asub deparse_sub {
1N/A my $self = shift;
1N/A my $cv = shift;
1N/A my $proto = "";
1N/ACarp::confess("NULL in deparse_sub") if !defined($cv) || $cv->isa("B::NULL");
1N/ACarp::confess("SPECIAL in deparse_sub") if $cv->isa("B::SPECIAL");
1N/A local $self->{'curcop'} = $self->{'curcop'};
1N/A if ($cv->FLAGS & SVf_POK) {
1N/A $proto = "(". $cv->PV . ") ";
1N/A }
1N/A if ($cv->CvFLAGS & (CVf_METHOD|CVf_LOCKED|CVf_LVALUE)) {
1N/A $proto .= ": ";
1N/A $proto .= "lvalue " if $cv->CvFLAGS & CVf_LVALUE;
1N/A $proto .= "locked " if $cv->CvFLAGS & CVf_LOCKED;
1N/A $proto .= "method " if $cv->CvFLAGS & CVf_METHOD;
1N/A }
1N/A
1N/A local($self->{'curcv'}) = $cv;
1N/A local($self->{'curcvlex'});
1N/A local(@$self{qw'curstash warnings hints'})
1N/A = @$self{qw'curstash warnings hints'};
1N/A my $body;
1N/A if (not null $cv->ROOT) {
1N/A my $lineseq = $cv->ROOT->first;
1N/A if ($lineseq->name eq "lineseq") {
1N/A my @ops;
1N/A for(my$o=$lineseq->first; $$o; $o=$o->sibling) {
1N/A push @ops, $o;
1N/A }
1N/A $body = $self->lineseq(undef, @ops).";";
1N/A my $scope_en = $self->find_scope_en($lineseq);
1N/A if (defined $scope_en) {
1N/A my $subs = join"", $self->seq_subs($scope_en);
1N/A $body .= ";\n$subs" if length($subs);
1N/A }
1N/A }
1N/A else {
1N/A $body = $self->deparse($cv->ROOT->first, 0);
1N/A }
1N/A }
1N/A else {
1N/A my $sv = $cv->const_sv;
1N/A if ($$sv) {
1N/A # uh-oh. inlinable sub... format it differently
1N/A return $proto . "{ " . $self->const($sv, 0) . " }\n";
1N/A } else { # XSUB? (or just a declaration)
1N/A return "$proto;\n";
1N/A }
1N/A }
1N/A return $proto ."{\n\t$body\n\b}" ."\n";
1N/A}
1N/A
1N/Asub deparse_format {
1N/A my $self = shift;
1N/A my $form = shift;
1N/A my @text;
1N/A local($self->{'curcv'}) = $form;
1N/A local($self->{'curcvlex'});
1N/A local($self->{'in_format'}) = 1;
1N/A local(@$self{qw'curstash warnings hints'})
1N/A = @$self{qw'curstash warnings hints'};
1N/A my $op = $form->ROOT;
1N/A my $kid;
1N/A return "\f." if $op->first->name eq 'stub'
1N/A || $op->first->name eq 'nextstate';
1N/A $op = $op->first->first; # skip leavewrite, lineseq
1N/A while (not null $op) {
1N/A $op = $op->sibling; # skip nextstate
1N/A my @exprs;
1N/A $kid = $op->first->sibling; # skip pushmark
1N/A push @text, "\f".$self->const_sv($kid)->PV;
1N/A $kid = $kid->sibling;
1N/A for (; not null $kid; $kid = $kid->sibling) {
1N/A push @exprs, $self->deparse($kid, 0);
1N/A }
1N/A push @text, "\f".join(", ", @exprs)."\n" if @exprs;
1N/A $op = $op->sibling;
1N/A }
1N/A return join("", @text) . "\f.";
1N/A}
1N/A
1N/Asub is_scope {
1N/A my $op = shift;
1N/A return $op->name eq "leave" || $op->name eq "scope"
1N/A || $op->name eq "lineseq"
1N/A || ($op->name eq "null" && class($op) eq "UNOP"
1N/A && (is_scope($op->first) || $op->first->name eq "enter"));
1N/A}
1N/A
1N/Asub is_state {
1N/A my $name = $_[0]->name;
1N/A return $name eq "nextstate" || $name eq "dbstate" || $name eq "setstate";
1N/A}
1N/A
1N/Asub is_miniwhile { # check for one-line loop (`foo() while $y--')
1N/A my $op = shift;
1N/A return (!null($op) and null($op->sibling)
1N/A and $op->name eq "null" and class($op) eq "UNOP"
1N/A and (($op->first->name =~ /^(and|or)$/
1N/A and $op->first->first->sibling->name eq "lineseq")
1N/A or ($op->first->name eq "lineseq"
1N/A and not null $op->first->first->sibling
1N/A and $op->first->first->sibling->name eq "unstack")
1N/A ));
1N/A}
1N/A
1N/A# Check if the op and its sibling are the initialization and the rest of a
1N/A# for (..;..;..) { ... } loop
1N/Asub is_for_loop {
1N/A my $op = shift;
1N/A # This OP might be almost anything, though it won't be a
1N/A # nextstate. (It's the initialization, so in the canonical case it
1N/A # will be an sassign.) The sibling is a lineseq whose first child
1N/A # is a nextstate and whose second is a leaveloop.
1N/A my $lseq = $op->sibling;
1N/A if (!is_state $op and !null($lseq) and $lseq->name eq "lineseq") {
1N/A if ($lseq->first && !null($lseq->first) && is_state($lseq->first)
1N/A && (my $sib = $lseq->first->sibling)) {
1N/A return (!null($sib) && $sib->name eq "leaveloop");
1N/A }
1N/A }
1N/A return 0;
1N/A}
1N/A
1N/Asub is_scalar {
1N/A my $op = shift;
1N/A return ($op->name eq "rv2sv" or
1N/A $op->name eq "padsv" or
1N/A $op->name eq "gv" or # only in array/hash constructs
1N/A $op->flags & OPf_KIDS && !null($op->first)
1N/A && $op->first->name eq "gvsv");
1N/A}
1N/A
1N/Asub maybe_parens {
1N/A my $self = shift;
1N/A my($text, $cx, $prec) = @_;
1N/A if ($prec < $cx # unary ops nest just fine
1N/A or $prec == $cx and $cx != 4 and $cx != 16 and $cx != 21
1N/A or $self->{'parens'})
1N/A {
1N/A $text = "($text)";
1N/A # In a unop, let parent reuse our parens; see maybe_parens_unop
1N/A $text = "\cS" . $text if $cx == 16;
1N/A return $text;
1N/A } else {
1N/A return $text;
1N/A }
1N/A}
1N/A
1N/A# same as above, but get around the `if it looks like a function' rule
1N/Asub maybe_parens_unop {
1N/A my $self = shift;
1N/A my($name, $kid, $cx) = @_;
1N/A if ($cx > 16 or $self->{'parens'}) {
1N/A $kid = $self->deparse($kid, 1);
1N/A if ($name eq "umask" && $kid =~ /^\d+$/) {
1N/A $kid = sprintf("%#o", $kid);
1N/A }
1N/A return "$name($kid)";
1N/A } else {
1N/A $kid = $self->deparse($kid, 16);
1N/A if ($name eq "umask" && $kid =~ /^\d+$/) {
1N/A $kid = sprintf("%#o", $kid);
1N/A }
1N/A if (substr($kid, 0, 1) eq "\cS") {
1N/A # use kid's parens
1N/A return $name . substr($kid, 1);
1N/A } elsif (substr($kid, 0, 1) eq "(") {
1N/A # avoid looks-like-a-function trap with extra parens
1N/A # (`+' can lead to ambiguities)
1N/A return "$name(" . $kid . ")";
1N/A } else {
1N/A return "$name $kid";
1N/A }
1N/A }
1N/A}
1N/A
1N/Asub maybe_parens_func {
1N/A my $self = shift;
1N/A my($func, $text, $cx, $prec) = @_;
1N/A if ($prec <= $cx or substr($text, 0, 1) eq "(" or $self->{'parens'}) {
1N/A return "$func($text)";
1N/A } else {
1N/A return "$func $text";
1N/A }
1N/A}
1N/A
1N/Asub maybe_local {
1N/A my $self = shift;
1N/A my($op, $cx, $text) = @_;
1N/A my $our_intro = ($op->name =~ /^(gv|rv2)[ash]v$/) ? OPpOUR_INTRO : 0;
1N/A if ($op->private & (OPpLVAL_INTRO|$our_intro)
1N/A and not $self->{'avoid_local'}{$$op}) {
1N/A my $our_local = ($op->private & OPpLVAL_INTRO) ? "local" : "our";
1N/A if( $our_local eq 'our' ) {
1N/A die "Unexpected our($text)\n" unless $text =~ /^\W(\w+::)*\w+\z/;
1N/A $text =~ s/(\w+::)+//;
1N/A }
1N/A if (want_scalar($op)) {
1N/A return "$our_local $text";
1N/A } else {
1N/A return $self->maybe_parens_func("$our_local", $text, $cx, 16);
1N/A }
1N/A } else {
1N/A return $text;
1N/A }
1N/A}
1N/A
1N/Asub maybe_targmy {
1N/A my $self = shift;
1N/A my($op, $cx, $func, @args) = @_;
1N/A if ($op->private & OPpTARGET_MY) {
1N/A my $var = $self->padname($op->targ);
1N/A my $val = $func->($self, $op, 7, @args);
1N/A return $self->maybe_parens("$var = $val", $cx, 7);
1N/A } else {
1N/A return $func->($self, $op, $cx, @args);
1N/A }
1N/A}
1N/A
1N/Asub padname_sv {
1N/A my $self = shift;
1N/A my $targ = shift;
1N/A return $self->{'curcv'}->PADLIST->ARRAYelt(0)->ARRAYelt($targ);
1N/A}
1N/A
1N/Asub maybe_my {
1N/A my $self = shift;
1N/A my($op, $cx, $text) = @_;
1N/A if ($op->private & OPpLVAL_INTRO and not $self->{'avoid_local'}{$$op}) {
1N/A if (want_scalar($op)) {
1N/A return "my $text";
1N/A } else {
1N/A return $self->maybe_parens_func("my", $text, $cx, 16);
1N/A }
1N/A } else {
1N/A return $text;
1N/A }
1N/A}
1N/A
1N/A# The following OPs don't have functions:
1N/A
1N/A# pp_padany -- does not exist after parsing
1N/A
1N/Asub AUTOLOAD {
1N/A if ($AUTOLOAD =~ s/^.*::pp_//) {
1N/A warn "unexpected OP_".uc $AUTOLOAD;
1N/A return "XXX";
1N/A } else {
1N/A die "Undefined subroutine $AUTOLOAD called";
1N/A }
1N/A}
1N/A
1N/Asub DESTROY {} # Do not AUTOLOAD
1N/A
1N/A# $root should be the op which represents the root of whatever
1N/A# we're sequencing here. If it's undefined, then we don't append
1N/A# any subroutine declarations to the deparsed ops, otherwise we
1N/A# append appropriate declarations.
1N/Asub lineseq {
1N/A my($self, $root, @ops) = @_;
1N/A my($expr, @exprs);
1N/A
1N/A my $out_cop = $self->{'curcop'};
1N/A my $out_seq = defined($out_cop) ? $out_cop->cop_seq : undef;
1N/A my $limit_seq;
1N/A if (defined $root) {
1N/A $limit_seq = $out_seq;
1N/A my $nseq;
1N/A $nseq = $self->find_scope_st($root->sibling) if ${$root->sibling};
1N/A $limit_seq = $nseq if !defined($limit_seq)
1N/A or defined($nseq) && $nseq < $limit_seq;
1N/A }
1N/A $limit_seq = $self->{'limit_seq'}
1N/A if defined($self->{'limit_seq'})
1N/A && (!defined($limit_seq) || $self->{'limit_seq'} < $limit_seq);
1N/A local $self->{'limit_seq'} = $limit_seq;
1N/A for (my $i = 0; $i < @ops; $i++) {
1N/A $expr = "";
1N/A if (is_state $ops[$i]) {
1N/A $expr = $self->deparse($ops[$i], 0);
1N/A $i++;
1N/A if ($i > $#ops) {
1N/A push @exprs, $expr;
1N/A last;
1N/A }
1N/A }
1N/A if (!is_state $ops[$i] and (my $ls = $ops[$i+1]) and
1N/A !null($ops[$i+1]) and $ops[$i+1]->name eq "lineseq")
1N/A {
1N/A if ($ls->first && !null($ls->first) && is_state($ls->first)
1N/A && (my $sib = $ls->first->sibling)) {
1N/A if (!null($sib) && $sib->name eq "leaveloop") {
1N/A push @exprs, $expr . $self->for_loop($ops[$i], 0);
1N/A $i++;
1N/A next;
1N/A }
1N/A }
1N/A }
1N/A $expr .= $self->deparse($ops[$i], (@ops != 1)/2);
1N/A $expr =~ s/;\n?\z//;
1N/A push @exprs, $expr;
1N/A }
1N/A my $body = join(";\n", grep {length} @exprs);
1N/A my $subs = "";
1N/A if (defined $root && defined $limit_seq && !$self->{'in_format'}) {
1N/A $subs = join "\n", $self->seq_subs($limit_seq);
1N/A }
1N/A return join(";\n", grep {length} $body, $subs);
1N/A}
1N/A
1N/Asub scopeop {
1N/A my($real_block, $self, $op, $cx) = @_;
1N/A my $kid;
1N/A my @kids;
1N/A
1N/A local(@$self{qw'curstash warnings hints'})
1N/A = @$self{qw'curstash warnings hints'} if $real_block;
1N/A if ($real_block) {
1N/A $kid = $op->first->sibling; # skip enter
1N/A if (is_miniwhile($kid)) {
1N/A my $top = $kid->first;
1N/A my $name = $top->name;
1N/A if ($name eq "and") {
1N/A $name = "while";
1N/A } elsif ($name eq "or") {
1N/A $name = "until";
1N/A } else { # no conditional -> while 1 or until 0
1N/A return $self->deparse($top->first, 1) . " while 1";
1N/A }
1N/A my $cond = $top->first;
1N/A my $body = $cond->sibling->first; # skip lineseq
1N/A $cond = $self->deparse($cond, 1);
1N/A $body = $self->deparse($body, 1);
1N/A return "$body $name $cond";
1N/A }
1N/A } else {
1N/A $kid = $op->first;
1N/A }
1N/A for (; !null($kid); $kid = $kid->sibling) {
1N/A push @kids, $kid;
1N/A }
1N/A if ($cx > 0) { # inside an expression, (a do {} while for lineseq)
1N/A return "do {\n\t" . $self->lineseq($op, @kids) . "\n\b}";
1N/A } else {
1N/A my $lineseq = $self->lineseq($op, @kids);
1N/A return (length ($lineseq) ? "$lineseq;" : "");
1N/A }
1N/A}
1N/A
1N/Asub pp_scope { scopeop(0, @_); }
1N/Asub pp_lineseq { scopeop(0, @_); }
1N/Asub pp_leave { scopeop(1, @_); }
1N/A
1N/A# This is a special case of scopeop and lineseq, for the case of the
1N/A# main_root. The difference is that we print the output statements as
1N/A# soon as we get them, for the sake of impatient users.
1N/Asub deparse_root {
1N/A my $self = shift;
1N/A my($op) = @_;
1N/A local(@$self{qw'curstash warnings hints'})
1N/A = @$self{qw'curstash warnings hints'};
1N/A my @kids;
1N/A for (my $kid = $op->first->sibling; !null($kid); $kid = $kid->sibling) {
1N/A push @kids, $kid;
1N/A }
1N/A for (my $i = 0; $i < @kids; $i++) {
1N/A my $expr = "";
1N/A if (is_state $kids[$i]) {
1N/A $expr = $self->deparse($kids[$i], 0);
1N/A $i++;
1N/A if ($i > $#kids) {
1N/A print $self->indent($expr);
1N/A last;
1N/A }
1N/A }
1N/A if (is_for_loop($kids[$i])) {
1N/A $expr .= $self->for_loop($kids[$i], 0);
1N/A $expr .= ";\n" unless $i == $#kids;
1N/A print $self->indent($expr);
1N/A $i++;
1N/A next;
1N/A }
1N/A $expr .= $self->deparse($kids[$i], (@kids != 1)/2);
1N/A $expr =~ s/;\n?\z//;
1N/A $expr .= ";";
1N/A print $self->indent($expr);
1N/A print "\n" unless $i == $#kids;
1N/A }
1N/A}
1N/A
1N/A# The BEGIN {} is used here because otherwise this code isn't executed
1N/A# when you run B::Deparse on itself.
1N/Amy %globalnames;
1N/ABEGIN { map($globalnames{$_}++, "SIG", "STDIN", "STDOUT", "STDERR", "INC",
1N/A "ENV", "ARGV", "ARGVOUT", "_"); }
1N/A
1N/Asub gv_name {
1N/A my $self = shift;
1N/A my $gv = shift;
1N/ACarp::confess() unless ref($gv) eq "B::GV";
1N/A my $stash = $gv->STASH->NAME;
1N/A my $name = $gv->SAFENAME;
1N/A if (($stash eq 'main' && $globalnames{$name})
1N/A or ($stash eq $self->{'curstash'} && !$globalnames{$name})
1N/A or $name =~ /^[^A-Za-z_]/)
1N/A {
1N/A $stash = "";
1N/A } else {
1N/A $stash = $stash . "::";
1N/A }
1N/A if ($name =~ /^(\^..|{)/) {
1N/A $name = "{$name}"; # ${^WARNING_BITS}, etc and ${
1N/A }
1N/A return $stash . $name;
1N/A}
1N/A
1N/A# Return the name to use for a stash variable.
1N/A# If a lexical with the same name is in scope, it may need to be
1N/A# fully-qualified.
1N/Asub stash_variable {
1N/A my ($self, $prefix, $name) = @_;
1N/A
1N/A return "$prefix$name" if $name =~ /::/;
1N/A
1N/A unless ($prefix eq '$' || $prefix eq '@' || #'
1N/A $prefix eq '%' || $prefix eq '$#') {
1N/A return "$prefix$name";
1N/A }
1N/A
1N/A my $v = ($prefix eq '$#' ? '@' : $prefix) . $name;
1N/A return $prefix .$self->{'curstash'}.'::'. $name if $self->lex_in_scope($v);
1N/A return "$prefix$name";
1N/A}
1N/A
1N/Asub lex_in_scope {
1N/A my ($self, $name) = @_;
1N/A $self->populate_curcvlex() if !defined $self->{'curcvlex'};
1N/A
1N/A return 0 if !defined($self->{'curcop'});
1N/A my $seq = $self->{'curcop'}->cop_seq;
1N/A return 0 if !exists $self->{'curcvlex'}{$name};
1N/A for my $a (@{$self->{'curcvlex'}{$name}}) {
1N/A my ($st, $en) = @$a;
1N/A return 1 if $seq > $st && $seq <= $en;
1N/A }
1N/A return 0;
1N/A}
1N/A
1N/Asub populate_curcvlex {
1N/A my $self = shift;
1N/A for (my $cv = $self->{'curcv'}; class($cv) eq "CV"; $cv = $cv->OUTSIDE) {
1N/A my $padlist = $cv->PADLIST;
1N/A # an undef CV still in lexical chain
1N/A next if class($padlist) eq "SPECIAL";
1N/A my @padlist = $padlist->ARRAY;
1N/A my @ns = $padlist[0]->ARRAY;
1N/A
1N/A for (my $i=0; $i<@ns; ++$i) {
1N/A next if class($ns[$i]) eq "SPECIAL";
1N/A next if $ns[$i]->FLAGS & SVpad_OUR; # Skip "our" vars
1N/A if (class($ns[$i]) eq "PV") {
1N/A # Probably that pesky lexical @_
1N/A next;
1N/A }
1N/A my $name = $ns[$i]->PVX;
1N/A my ($seq_st, $seq_en) =
1N/A ($ns[$i]->FLAGS & SVf_FAKE)
1N/A ? (0, 999999)
1N/A : ($ns[$i]->NVX, $ns[$i]->IVX);
1N/A
1N/A push @{$self->{'curcvlex'}{$name}}, [$seq_st, $seq_en];
1N/A }
1N/A }
1N/A}
1N/A
1N/Asub find_scope_st { ((find_scope(@_))[0]); }
1N/Asub find_scope_en { ((find_scope(@_))[1]); }
1N/A
1N/A# Recurses down the tree, looking for pad variable introductions and COPs
1N/Asub find_scope {
1N/A my ($self, $op, $scope_st, $scope_en) = @_;
1N/A carp("Undefined op in find_scope") if !defined $op;
1N/A return ($scope_st, $scope_en) unless $op->flags & OPf_KIDS;
1N/A
1N/A for (my $o=$op->first; $$o; $o=$o->sibling) {
1N/A if ($o->name =~ /^pad.v$/ && $o->private & OPpLVAL_INTRO) {
1N/A my $s = int($self->padname_sv($o->targ)->NVX);
1N/A my $e = $self->padname_sv($o->targ)->IVX;
1N/A $scope_st = $s if !defined($scope_st) || $s < $scope_st;
1N/A $scope_en = $e if !defined($scope_en) || $e > $scope_en;
1N/A }
1N/A elsif (is_state($o)) {
1N/A my $c = $o->cop_seq;
1N/A $scope_st = $c if !defined($scope_st) || $c < $scope_st;
1N/A $scope_en = $c if !defined($scope_en) || $c > $scope_en;
1N/A }
1N/A elsif ($o->flags & OPf_KIDS) {
1N/A ($scope_st, $scope_en) =
1N/A $self->find_scope($o, $scope_st, $scope_en)
1N/A }
1N/A }
1N/A
1N/A return ($scope_st, $scope_en);
1N/A}
1N/A
1N/A# Returns a list of subs which should be inserted before the COP
1N/Asub cop_subs {
1N/A my ($self, $op, $out_seq) = @_;
1N/A my $seq = $op->cop_seq;
1N/A # If we have nephews, then our sequence number indicates
1N/A # the cop_seq of the end of some sort of scope.
1N/A if (class($op->sibling) ne "NULL" && $op->sibling->flags & OPf_KIDS
1N/A and my $nseq = $self->find_scope_st($op->sibling) ) {
1N/A $seq = $nseq;
1N/A }
1N/A $seq = $out_seq if defined($out_seq) && $out_seq < $seq;
1N/A return $self->seq_subs($seq);
1N/A}
1N/A
1N/Asub seq_subs {
1N/A my ($self, $seq) = @_;
1N/A my @text;
1N/A#push @text, "# ($seq)\n";
1N/A
1N/A return "" if !defined $seq;
1N/A while (scalar(@{$self->{'subs_todo'}})
1N/A and $seq > $self->{'subs_todo'}[0][0]) {
1N/A push @text, $self->next_todo;
1N/A }
1N/A return @text;
1N/A}
1N/A
1N/A# Notice how subs and formats are inserted between statements here;
1N/A# also $[ assignments and pragmas.
1N/Asub pp_nextstate {
1N/A my $self = shift;
1N/A my($op, $cx) = @_;
1N/A $self->{'curcop'} = $op;
1N/A my @text;
1N/A push @text, $self->cop_subs($op);
1N/A push @text, $op->label . ": " if $op->label;
1N/A my $stash = $op->stashpv;
1N/A if ($stash ne $self->{'curstash'}) {
1N/A push @text, "package $stash;\n";
1N/A $self->{'curstash'} = $stash;
1N/A }
1N/A
1N/A if ($self->{'arybase'} != $op->arybase) {
1N/A push @text, '$[ = '. $op->arybase .";\n";
1N/A $self->{'arybase'} = $op->arybase;
1N/A }
1N/A
1N/A my $warnings = $op->warnings;
1N/A my $warning_bits;
1N/A if ($warnings->isa("B::SPECIAL") && $$warnings == 4) {
1N/A $warning_bits = $warnings::Bits{"all"} & WARN_MASK;
1N/A }
1N/A elsif ($warnings->isa("B::SPECIAL") && $$warnings == 5) {
1N/A $warning_bits = $warnings::NONE;
1N/A }
1N/A elsif ($warnings->isa("B::SPECIAL")) {
1N/A $warning_bits = undef;
1N/A }
1N/A else {
1N/A $warning_bits = $warnings->PV & WARN_MASK;
1N/A }
1N/A
1N/A if (defined ($warning_bits) and
1N/A !defined($self->{warnings}) || $self->{'warnings'} ne $warning_bits) {
1N/A push @text, declare_warnings($self->{'warnings'}, $warning_bits);
1N/A $self->{'warnings'} = $warning_bits;
1N/A }
1N/A
1N/A if ($self->{'hints'} != $op->private) {
1N/A push @text, declare_hints($self->{'hints'}, $op->private);
1N/A $self->{'hints'} = $op->private;
1N/A }
1N/A
1N/A # This should go after of any branches that add statements, to
1N/A # increase the chances that it refers to the same line it did in
1N/A # the original program.
1N/A if ($self->{'linenums'}) {
1N/A push @text, "\f#line " . $op->line .
1N/A ' "' . $op->file, qq'"\n';
1N/A }
1N/A
1N/A return join("", @text);
1N/A}
1N/A
1N/Asub declare_warnings {
1N/A my ($from, $to) = @_;
1N/A if (($to & WARN_MASK) eq warnings::bits("all")) {
1N/A return "use warnings;\n";
1N/A }
1N/A elsif (($to & WARN_MASK) eq "\0"x length($to)) {
1N/A return "no warnings;\n";
1N/A }
1N/A return "BEGIN {\${^WARNING_BITS} = ".perlstring($to)."}\n";
1N/A}
1N/A
1N/Asub declare_hints {
1N/A my ($from, $to) = @_;
1N/A my $use = $to & ~$from;
1N/A my $no = $from & ~$to;
1N/A my $decls = "";
1N/A for my $pragma (hint_pragmas($use)) {
1N/A $decls .= "use $pragma;\n";
1N/A }
1N/A for my $pragma (hint_pragmas($no)) {
1N/A $decls .= "no $pragma;\n";
1N/A }
1N/A return $decls;
1N/A}
1N/A
1N/Asub hint_pragmas {
1N/A my ($bits) = @_;
1N/A my @pragmas;
1N/A push @pragmas, "integer" if $bits & 0x1;
1N/A push @pragmas, "strict 'refs'" if $bits & 0x2;
1N/A push @pragmas, "bytes" if $bits & 0x8;
1N/A return @pragmas;
1N/A}
1N/A
1N/Asub pp_dbstate { pp_nextstate(@_) }
1N/Asub pp_setstate { pp_nextstate(@_) }
1N/A
1N/Asub pp_unstack { return "" } # see also leaveloop
1N/A
1N/Asub baseop {
1N/A my $self = shift;
1N/A my($op, $cx, $name) = @_;
1N/A return $name;
1N/A}
1N/A
1N/Asub pp_stub {
1N/A my $self = shift;
1N/A my($op, $cx, $name) = @_;
1N/A if ($cx >= 1) {
1N/A return "()";
1N/A }
1N/A else {
1N/A return "();";
1N/A }
1N/A}
1N/Asub pp_wantarray { baseop(@_, "wantarray") }
1N/Asub pp_fork { baseop(@_, "fork") }
1N/Asub pp_wait { maybe_targmy(@_, \&baseop, "wait") }
1N/Asub pp_getppid { maybe_targmy(@_, \&baseop, "getppid") }
1N/Asub pp_time { maybe_targmy(@_, \&baseop, "time") }
1N/Asub pp_tms { baseop(@_, "times") }
1N/Asub pp_ghostent { baseop(@_, "gethostent") }
1N/Asub pp_gnetent { baseop(@_, "getnetent") }
1N/Asub pp_gprotoent { baseop(@_, "getprotoent") }
1N/Asub pp_gservent { baseop(@_, "getservent") }
1N/Asub pp_ehostent { baseop(@_, "endhostent") }
1N/Asub pp_enetent { baseop(@_, "endnetent") }
1N/Asub pp_eprotoent { baseop(@_, "endprotoent") }
1N/Asub pp_eservent { baseop(@_, "endservent") }
1N/Asub pp_gpwent { baseop(@_, "getpwent") }
1N/Asub pp_spwent { baseop(@_, "setpwent") }
1N/Asub pp_epwent { baseop(@_, "endpwent") }
1N/Asub pp_ggrent { baseop(@_, "getgrent") }
1N/Asub pp_sgrent { baseop(@_, "setgrent") }
1N/Asub pp_egrent { baseop(@_, "endgrent") }
1N/Asub pp_getlogin { baseop(@_, "getlogin") }
1N/A
1N/Asub POSTFIX () { 1 }
1N/A
1N/A# I couldn't think of a good short name, but this is the category of
1N/A# symbolic unary operators with interesting precedence
1N/A
1N/Asub pfixop {
1N/A my $self = shift;
1N/A my($op, $cx, $name, $prec, $flags) = (@_, 0);
1N/A my $kid = $op->first;
1N/A $kid = $self->deparse($kid, $prec);
1N/A return $self->maybe_parens(($flags & POSTFIX) ? "$kid$name" : "$name$kid",
1N/A $cx, $prec);
1N/A}
1N/A
1N/Asub pp_preinc { pfixop(@_, "++", 23) }
1N/Asub pp_predec { pfixop(@_, "--", 23) }
1N/Asub pp_postinc { maybe_targmy(@_, \&pfixop, "++", 23, POSTFIX) }
1N/Asub pp_postdec { maybe_targmy(@_, \&pfixop, "--", 23, POSTFIX) }
1N/Asub pp_i_preinc { pfixop(@_, "++", 23) }
1N/Asub pp_i_predec { pfixop(@_, "--", 23) }
1N/Asub pp_i_postinc { maybe_targmy(@_, \&pfixop, "++", 23, POSTFIX) }
1N/Asub pp_i_postdec { maybe_targmy(@_, \&pfixop, "--", 23, POSTFIX) }
1N/Asub pp_complement { maybe_targmy(@_, \&pfixop, "~", 21) }
1N/A
1N/Asub pp_negate { maybe_targmy(@_, \&real_negate) }
1N/Asub real_negate {
1N/A my $self = shift;
1N/A my($op, $cx) = @_;
1N/A if ($op->first->name =~ /^(i_)?negate$/) {
1N/A # avoid --$x
1N/A $self->pfixop($op, $cx, "-", 21.5);
1N/A } else {
1N/A $self->pfixop($op, $cx, "-", 21);
1N/A }
1N/A}
1N/Asub pp_i_negate { pp_negate(@_) }
1N/A
1N/Asub pp_not {
1N/A my $self = shift;
1N/A my($op, $cx) = @_;
1N/A if ($cx <= 4) {
1N/A $self->pfixop($op, $cx, "not ", 4);
1N/A } else {
1N/A $self->pfixop($op, $cx, "!", 21);
1N/A }
1N/A}
1N/A
1N/Asub unop {
1N/A my $self = shift;
1N/A my($op, $cx, $name) = @_;
1N/A my $kid;
1N/A if ($op->flags & OPf_KIDS) {
1N/A $kid = $op->first;
1N/A if (defined prototype("CORE::$name")
1N/A && prototype("CORE::$name") =~ /^;?\*/
1N/A && $kid->name eq "rv2gv") {
1N/A $kid = $kid->first;
1N/A }
1N/A
1N/A return $self->maybe_parens_unop($name, $kid, $cx);
1N/A } else {
1N/A return $name . ($op->flags & OPf_SPECIAL ? "()" : "");
1N/A }
1N/A}
1N/A
1N/Asub pp_chop { maybe_targmy(@_, \&unop, "chop") }
1N/Asub pp_chomp { maybe_targmy(@_, \&unop, "chomp") }
1N/Asub pp_schop { maybe_targmy(@_, \&unop, "chop") }
1N/Asub pp_schomp { maybe_targmy(@_, \&unop, "chomp") }
1N/Asub pp_defined { unop(@_, "defined") }
1N/Asub pp_undef { unop(@_, "undef") }
1N/Asub pp_study { unop(@_, "study") }
1N/Asub pp_ref { unop(@_, "ref") }
1N/Asub pp_pos { maybe_local(@_, unop(@_, "pos")) }
1N/A
1N/Asub pp_sin { maybe_targmy(@_, \&unop, "sin") }
1N/Asub pp_cos { maybe_targmy(@_, \&unop, "cos") }
1N/Asub pp_rand { maybe_targmy(@_, \&unop, "rand") }
1N/Asub pp_srand { unop(@_, "srand") }
1N/Asub pp_exp { maybe_targmy(@_, \&unop, "exp") }
1N/Asub pp_log { maybe_targmy(@_, \&unop, "log") }
1N/Asub pp_sqrt { maybe_targmy(@_, \&unop, "sqrt") }
1N/Asub pp_int { maybe_targmy(@_, \&unop, "int") }
1N/Asub pp_hex { maybe_targmy(@_, \&unop, "hex") }
1N/Asub pp_oct { maybe_targmy(@_, \&unop, "oct") }
1N/Asub pp_abs { maybe_targmy(@_, \&unop, "abs") }
1N/A
1N/Asub pp_length { maybe_targmy(@_, \&unop, "length") }
1N/Asub pp_ord { maybe_targmy(@_, \&unop, "ord") }
1N/Asub pp_chr { maybe_targmy(@_, \&unop, "chr") }
1N/A
1N/Asub pp_each { unop(@_, "each") }
1N/Asub pp_values { unop(@_, "values") }
1N/Asub pp_keys { unop(@_, "keys") }
1N/Asub pp_pop { unop(@_, "pop") }
1N/Asub pp_shift { unop(@_, "shift") }
1N/A
1N/Asub pp_caller { unop(@_, "caller") }
1N/Asub pp_reset { unop(@_, "reset") }
1N/Asub pp_exit { unop(@_, "exit") }
1N/Asub pp_prototype { unop(@_, "prototype") }
1N/A
1N/Asub pp_close { unop(@_, "close") }
1N/Asub pp_fileno { unop(@_, "fileno") }
1N/Asub pp_umask { unop(@_, "umask") }
1N/Asub pp_untie { unop(@_, "untie") }
1N/Asub pp_tied { unop(@_, "tied") }
1N/Asub pp_dbmclose { unop(@_, "dbmclose") }
1N/Asub pp_getc { unop(@_, "getc") }
1N/Asub pp_eof { unop(@_, "eof") }
1N/Asub pp_tell { unop(@_, "tell") }
1N/Asub pp_getsockname { unop(@_, "getsockname") }
1N/Asub pp_getpeername { unop(@_, "getpeername") }
1N/A
1N/Asub pp_chdir { maybe_targmy(@_, \&unop, "chdir") }
1N/Asub pp_chroot { maybe_targmy(@_, \&unop, "chroot") }
1N/Asub pp_readlink { unop(@_, "readlink") }
1N/Asub pp_rmdir { maybe_targmy(@_, \&unop, "rmdir") }
1N/Asub pp_readdir { unop(@_, "readdir") }
1N/Asub pp_telldir { unop(@_, "telldir") }
1N/Asub pp_rewinddir { unop(@_, "rewinddir") }
1N/Asub pp_closedir { unop(@_, "closedir") }
1N/Asub pp_getpgrp { maybe_targmy(@_, \&unop, "getpgrp") }
1N/Asub pp_localtime { unop(@_, "localtime") }
1N/Asub pp_gmtime { unop(@_, "gmtime") }
1N/Asub pp_alarm { unop(@_, "alarm") }
1N/Asub pp_sleep { maybe_targmy(@_, \&unop, "sleep") }
1N/A
1N/Asub pp_dofile { unop(@_, "do") }
1N/Asub pp_entereval { unop(@_, "eval") }
1N/A
1N/Asub pp_ghbyname { unop(@_, "gethostbyname") }
1N/Asub pp_gnbyname { unop(@_, "getnetbyname") }
1N/Asub pp_gpbyname { unop(@_, "getprotobyname") }
1N/Asub pp_shostent { unop(@_, "sethostent") }
1N/Asub pp_snetent { unop(@_, "setnetent") }
1N/Asub pp_sprotoent { unop(@_, "setprotoent") }
1N/Asub pp_sservent { unop(@_, "setservent") }
1N/Asub pp_gpwnam { unop(@_, "getpwnam") }
1N/Asub pp_gpwuid { unop(@_, "getpwuid") }
1N/Asub pp_ggrnam { unop(@_, "getgrnam") }
1N/Asub pp_ggrgid { unop(@_, "getgrgid") }
1N/A
1N/Asub pp_lock { unop(@_, "lock") }
1N/A
1N/Asub pp_exists {
1N/A my $self = shift;
1N/A my($op, $cx) = @_;
1N/A my $arg;
1N/A if ($op->private & OPpEXISTS_SUB) {
1N/A # Checking for the existence of a subroutine
1N/A return $self->maybe_parens_func("exists",
1N/A $self->pp_rv2cv($op->first, 16), $cx, 16);
1N/A }
1N/A if ($op->flags & OPf_SPECIAL) {
1N/A # Array element, not hash element
1N/A return $self->maybe_parens_func("exists",
1N/A $self->pp_aelem($op->first, 16), $cx, 16);
1N/A }
1N/A return $self->maybe_parens_func("exists", $self->pp_helem($op->first, 16),
1N/A $cx, 16);
1N/A}
1N/A
1N/Asub pp_delete {
1N/A my $self = shift;
1N/A my($op, $cx) = @_;
1N/A my $arg;
1N/A if ($op->private & OPpSLICE) {
1N/A if ($op->flags & OPf_SPECIAL) {
1N/A # Deleting from an array, not a hash
1N/A return $self->maybe_parens_func("delete",
1N/A $self->pp_aslice($op->first, 16),
1N/A $cx, 16);
1N/A }
1N/A return $self->maybe_parens_func("delete",
1N/A $self->pp_hslice($op->first, 16),
1N/A $cx, 16);
1N/A } else {
1N/A if ($op->flags & OPf_SPECIAL) {
1N/A # Deleting from an array, not a hash
1N/A return $self->maybe_parens_func("delete",
1N/A $self->pp_aelem($op->first, 16),
1N/A $cx, 16);
1N/A }
1N/A return $self->maybe_parens_func("delete",
1N/A $self->pp_helem($op->first, 16),
1N/A $cx, 16);
1N/A }
1N/A}
1N/A
1N/Asub pp_require {
1N/A my $self = shift;
1N/A my($op, $cx) = @_;
1N/A if (class($op) eq "UNOP" and $op->first->name eq "const"
1N/A and $op->first->private & OPpCONST_BARE)
1N/A {
1N/A my $name = $self->const_sv($op->first)->PV;
1N/A $name =~ s[/][::]g;
1N/A $name =~ s/\.pm//g;
1N/A return "require $name";
1N/A } else {
1N/A $self->unop($op, $cx, "require");
1N/A }
1N/A}
1N/A
1N/Asub pp_scalar {
1N/A my $self = shift;
1N/A my($op, $cv) = @_;
1N/A my $kid = $op->first;
1N/A if (not null $kid->sibling) {
1N/A # XXX Was a here-doc
1N/A return $self->dquote($op);
1N/A }
1N/A $self->unop(@_, "scalar");
1N/A}
1N/A
1N/A
1N/Asub padval {
1N/A my $self = shift;
1N/A my $targ = shift;
1N/A return $self->{'curcv'}->PADLIST->ARRAYelt(1)->ARRAYelt($targ);
1N/A}
1N/A
1N/Asub pp_refgen {
1N/A my $self = shift;
1N/A my($op, $cx) = @_;
1N/A my $kid = $op->first;
1N/A if ($kid->name eq "null") {
1N/A $kid = $kid->first;
1N/A if ($kid->name eq "anonlist" || $kid->name eq "anonhash") {
1N/A my($pre, $post) = @{{"anonlist" => ["[","]"],
1N/A "anonhash" => ["{","}"]}->{$kid->name}};
1N/A my($expr, @exprs);
1N/A $kid = $kid->first->sibling; # skip pushmark
1N/A for (; !null($kid); $kid = $kid->sibling) {
1N/A $expr = $self->deparse($kid, 6);
1N/A push @exprs, $expr;
1N/A }
1N/A return $pre . join(", ", @exprs) . $post;
1N/A } elsif (!null($kid->sibling) and
1N/A $kid->sibling->name eq "anoncode") {
1N/A return "sub " .
1N/A $self->deparse_sub($self->padval($kid->sibling->targ));
1N/A } elsif ($kid->name eq "pushmark") {
1N/A my $sib_name = $kid->sibling->name;
1N/A if ($sib_name =~ /^(pad|rv2)[ah]v$/
1N/A and not $kid->sibling->flags & OPf_REF)
1N/A {
1N/A # The @a in \(@a) isn't in ref context, but only when the
1N/A # parens are there.
1N/A return "\\(" . $self->pp_list($op->first) . ")";
1N/A } elsif ($sib_name eq 'entersub') {
1N/A my $text = $self->deparse($kid->sibling, 1);
1N/A # Always show parens for \(&func()), but only with -p otherwise
1N/A $text = "($text)" if $self->{'parens'}
1N/A or $kid->sibling->private & OPpENTERSUB_AMPER;
1N/A return "\\$text";
1N/A }
1N/A }
1N/A }
1N/A $self->pfixop($op, $cx, "\\", 20);
1N/A}
1N/A
1N/Asub pp_srefgen { pp_refgen(@_) }
1N/A
1N/Asub pp_readline {
1N/A my $self = shift;
1N/A my($op, $cx) = @_;
1N/A my $kid = $op->first;
1N/A $kid = $kid->first if $kid->name eq "rv2gv"; # <$fh>
1N/A return "<" . $self->deparse($kid, 1) . ">" if is_scalar($kid);
1N/A return $self->unop($op, $cx, "readline");
1N/A}
1N/A
1N/Asub pp_rcatline {
1N/A my $self = shift;
1N/A my($op) = @_;
1N/A return "<" . $self->gv_name($self->gv_or_padgv($op)) . ">";
1N/A}
1N/A
1N/A# Unary operators that can occur as pseudo-listops inside double quotes
1N/Asub dq_unop {
1N/A my $self = shift;
1N/A my($op, $cx, $name, $prec, $flags) = (@_, 0, 0);
1N/A my $kid;
1N/A if ($op->flags & OPf_KIDS) {
1N/A $kid = $op->first;
1N/A # If there's more than one kid, the first is an ex-pushmark.
1N/A $kid = $kid->sibling if not null $kid->sibling;
1N/A return $self->maybe_parens_unop($name, $kid, $cx);
1N/A } else {
1N/A return $name . ($op->flags & OPf_SPECIAL ? "()" : "");
1N/A }
1N/A}
1N/A
1N/Asub pp_ucfirst { dq_unop(@_, "ucfirst") }
1N/Asub pp_lcfirst { dq_unop(@_, "lcfirst") }
1N/Asub pp_uc { dq_unop(@_, "uc") }
1N/Asub pp_lc { dq_unop(@_, "lc") }
1N/Asub pp_quotemeta { maybe_targmy(@_, \&dq_unop, "quotemeta") }
1N/A
1N/Asub loopex {
1N/A my $self = shift;
1N/A my ($op, $cx, $name) = @_;
1N/A if (class($op) eq "PVOP") {
1N/A return "$name " . $op->pv;
1N/A } elsif (class($op) eq "OP") {
1N/A return $name;
1N/A } elsif (class($op) eq "UNOP") {
1N/A # Note -- loop exits are actually exempt from the
1N/A # looks-like-a-func rule, but a few extra parens won't hurt
1N/A return $self->maybe_parens_unop($name, $op->first, $cx);
1N/A }
1N/A}
1N/A
1N/Asub pp_last { loopex(@_, "last") }
1N/Asub pp_next { loopex(@_, "next") }
1N/Asub pp_redo { loopex(@_, "redo") }
1N/Asub pp_goto { loopex(@_, "goto") }
1N/Asub pp_dump { loopex(@_, "dump") }
1N/A
1N/Asub ftst {
1N/A my $self = shift;
1N/A my($op, $cx, $name) = @_;
1N/A if (class($op) eq "UNOP") {
1N/A # Genuine `-X' filetests are exempt from the LLAFR, but not
1N/A # l?stat(); for the sake of clarity, give'em all parens
1N/A return $self->maybe_parens_unop($name, $op->first, $cx);
1N/A } elsif (class($op) =~ /^(SV|PAD)OP$/) {
1N/A return $self->maybe_parens_func($name, $self->pp_gv($op, 1), $cx, 16);
1N/A } else { # I don't think baseop filetests ever survive ck_ftst, but...
1N/A return $name;
1N/A }
1N/A}
1N/A
1N/Asub pp_lstat { ftst(@_, "lstat") }
1N/Asub pp_stat { ftst(@_, "stat") }
1N/Asub pp_ftrread { ftst(@_, "-R") }
1N/Asub pp_ftrwrite { ftst(@_, "-W") }
1N/Asub pp_ftrexec { ftst(@_, "-X") }
1N/Asub pp_fteread { ftst(@_, "-r") }
1N/Asub pp_ftewrite { ftst(@_, "-w") }
1N/Asub pp_fteexec { ftst(@_, "-x") }
1N/Asub pp_ftis { ftst(@_, "-e") }
1N/Asub pp_fteowned { ftst(@_, "-O") }
1N/Asub pp_ftrowned { ftst(@_, "-o") }
1N/Asub pp_ftzero { ftst(@_, "-z") }
1N/Asub pp_ftsize { ftst(@_, "-s") }
1N/Asub pp_ftmtime { ftst(@_, "-M") }
1N/Asub pp_ftatime { ftst(@_, "-A") }
1N/Asub pp_ftctime { ftst(@_, "-C") }
1N/Asub pp_ftsock { ftst(@_, "-S") }
1N/Asub pp_ftchr { ftst(@_, "-c") }
1N/Asub pp_ftblk { ftst(@_, "-b") }
1N/Asub pp_ftfile { ftst(@_, "-f") }
1N/Asub pp_ftdir { ftst(@_, "-d") }
1N/Asub pp_ftpipe { ftst(@_, "-p") }
1N/Asub pp_ftlink { ftst(@_, "-l") }
1N/Asub pp_ftsuid { ftst(@_, "-u") }
1N/Asub pp_ftsgid { ftst(@_, "-g") }
1N/Asub pp_ftsvtx { ftst(@_, "-k") }
1N/Asub pp_fttty { ftst(@_, "-t") }
1N/Asub pp_fttext { ftst(@_, "-T") }
1N/Asub pp_ftbinary { ftst(@_, "-B") }
1N/A
1N/Asub SWAP_CHILDREN () { 1 }
1N/Asub ASSIGN () { 2 } # has OP= variant
1N/Asub LIST_CONTEXT () { 4 } # Assignment is in list context
1N/A
1N/Amy(%left, %right);
1N/A
1N/Asub assoc_class {
1N/A my $op = shift;
1N/A my $name = $op->name;
1N/A if ($name eq "concat" and $op->first->name eq "concat") {
1N/A # avoid spurious `=' -- see comment in pp_concat
1N/A return "concat";
1N/A }
1N/A if ($name eq "null" and class($op) eq "UNOP"
1N/A and $op->first->name =~ /^(and|x?or)$/
1N/A and null $op->first->sibling)
1N/A {
1N/A # Like all conditional constructs, OP_ANDs and OP_ORs are topped
1N/A # with a null that's used as the common end point of the two
1N/A # flows of control. For precedence purposes, ignore it.
1N/A # (COND_EXPRs have these too, but we don't bother with
1N/A # their associativity).
1N/A return assoc_class($op->first);
1N/A }
1N/A return $name . ($op->flags & OPf_STACKED ? "=" : "");
1N/A}
1N/A
1N/A# Left associative operators, like `+', for which
1N/A# $a + $b + $c is equivalent to ($a + $b) + $c
1N/A
1N/ABEGIN {
1N/A %left = ('multiply' => 19, 'i_multiply' => 19,
1N/A 'divide' => 19, 'i_divide' => 19,
1N/A 'modulo' => 19, 'i_modulo' => 19,
1N/A 'repeat' => 19,
1N/A 'add' => 18, 'i_add' => 18,
1N/A 'subtract' => 18, 'i_subtract' => 18,
1N/A 'concat' => 18,
1N/A 'left_shift' => 17, 'right_shift' => 17,
1N/A 'bit_and' => 13,
1N/A 'bit_or' => 12, 'bit_xor' => 12,
1N/A 'and' => 3,
1N/A 'or' => 2, 'xor' => 2,
1N/A );
1N/A}
1N/A
1N/Asub deparse_binop_left {
1N/A my $self = shift;
1N/A my($op, $left, $prec) = @_;
1N/A if ($left{assoc_class($op)} && $left{assoc_class($left)}
1N/A and $left{assoc_class($op)} == $left{assoc_class($left)})
1N/A {
1N/A return $self->deparse($left, $prec - .00001);
1N/A } else {
1N/A return $self->deparse($left, $prec);
1N/A }
1N/A}
1N/A
1N/A# Right associative operators, like `=', for which
1N/A# $a = $b = $c is equivalent to $a = ($b = $c)
1N/A
1N/ABEGIN {
1N/A %right = ('pow' => 22,
1N/A 'sassign=' => 7, 'aassign=' => 7,
1N/A 'multiply=' => 7, 'i_multiply=' => 7,
1N/A 'divide=' => 7, 'i_divide=' => 7,
1N/A 'modulo=' => 7, 'i_modulo=' => 7,
1N/A 'repeat=' => 7,
1N/A 'add=' => 7, 'i_add=' => 7,
1N/A 'subtract=' => 7, 'i_subtract=' => 7,
1N/A 'concat=' => 7,
1N/A 'left_shift=' => 7, 'right_shift=' => 7,
1N/A 'bit_and=' => 7,
1N/A 'bit_or=' => 7, 'bit_xor=' => 7,
1N/A 'andassign' => 7,
1N/A 'orassign' => 7,
1N/A );
1N/A}
1N/A
1N/Asub deparse_binop_right {
1N/A my $self = shift;
1N/A my($op, $right, $prec) = @_;
1N/A if ($right{assoc_class($op)} && $right{assoc_class($right)}
1N/A and $right{assoc_class($op)} == $right{assoc_class($right)})
1N/A {
1N/A return $self->deparse($right, $prec - .00001);
1N/A } else {
1N/A return $self->deparse($right, $prec);
1N/A }
1N/A}
1N/A
1N/Asub binop {
1N/A my $self = shift;
1N/A my ($op, $cx, $opname, $prec, $flags) = (@_, 0);
1N/A my $left = $op->first;
1N/A my $right = $op->last;
1N/A my $eq = "";
1N/A if ($op->flags & OPf_STACKED && $flags & ASSIGN) {
1N/A $eq = "=";
1N/A $prec = 7;
1N/A }
1N/A if ($flags & SWAP_CHILDREN) {
1N/A ($left, $right) = ($right, $left);
1N/A }
1N/A $left = $self->deparse_binop_left($op, $left, $prec);
1N/A $left = "($left)" if $flags & LIST_CONTEXT
1N/A && $left !~ /^(my|our|local|)[\@\(]/;
1N/A $right = $self->deparse_binop_right($op, $right, $prec);
1N/A return $self->maybe_parens("$left $opname$eq $right", $cx, $prec);
1N/A}
1N/A
1N/Asub pp_add { maybe_targmy(@_, \&binop, "+", 18, ASSIGN) }
1N/Asub pp_multiply { maybe_targmy(@_, \&binop, "*", 19, ASSIGN) }
1N/Asub pp_subtract { maybe_targmy(@_, \&binop, "-",18, ASSIGN) }
1N/Asub pp_divide { maybe_targmy(@_, \&binop, "/", 19, ASSIGN) }
1N/Asub pp_modulo { maybe_targmy(@_, \&binop, "%", 19, ASSIGN) }
1N/Asub pp_i_add { maybe_targmy(@_, \&binop, "+", 18, ASSIGN) }
1N/Asub pp_i_multiply { maybe_targmy(@_, \&binop, "*", 19, ASSIGN) }
1N/Asub pp_i_subtract { maybe_targmy(@_, \&binop, "-", 18, ASSIGN) }
1N/Asub pp_i_divide { maybe_targmy(@_, \&binop, "/", 19, ASSIGN) }
1N/Asub pp_i_modulo { maybe_targmy(@_, \&binop, "%", 19, ASSIGN) }
1N/Asub pp_pow { maybe_targmy(@_, \&binop, "**", 22, ASSIGN) }
1N/A
1N/Asub pp_left_shift { maybe_targmy(@_, \&binop, "<<", 17, ASSIGN) }
1N/Asub pp_right_shift { maybe_targmy(@_, \&binop, ">>", 17, ASSIGN) }
1N/Asub pp_bit_and { maybe_targmy(@_, \&binop, "&", 13, ASSIGN) }
1N/Asub pp_bit_or { maybe_targmy(@_, \&binop, "|", 12, ASSIGN) }
1N/Asub pp_bit_xor { maybe_targmy(@_, \&binop, "^", 12, ASSIGN) }
1N/A
1N/Asub pp_eq { binop(@_, "==", 14) }
1N/Asub pp_ne { binop(@_, "!=", 14) }
1N/Asub pp_lt { binop(@_, "<", 15) }
1N/Asub pp_gt { binop(@_, ">", 15) }
1N/Asub pp_ge { binop(@_, ">=", 15) }
1N/Asub pp_le { binop(@_, "<=", 15) }
1N/Asub pp_ncmp { binop(@_, "<=>", 14) }
1N/Asub pp_i_eq { binop(@_, "==", 14) }
1N/Asub pp_i_ne { binop(@_, "!=", 14) }
1N/Asub pp_i_lt { binop(@_, "<", 15) }
1N/Asub pp_i_gt { binop(@_, ">", 15) }
1N/Asub pp_i_ge { binop(@_, ">=", 15) }
1N/Asub pp_i_le { binop(@_, "<=", 15) }
1N/Asub pp_i_ncmp { binop(@_, "<=>", 14) }
1N/A
1N/Asub pp_seq { binop(@_, "eq", 14) }
1N/Asub pp_sne { binop(@_, "ne", 14) }
1N/Asub pp_slt { binop(@_, "lt", 15) }
1N/Asub pp_sgt { binop(@_, "gt", 15) }
1N/Asub pp_sge { binop(@_, "ge", 15) }
1N/Asub pp_sle { binop(@_, "le", 15) }
1N/Asub pp_scmp { binop(@_, "cmp", 14) }
1N/A
1N/Asub pp_sassign { binop(@_, "=", 7, SWAP_CHILDREN) }
1N/Asub pp_aassign { binop(@_, "=", 7, SWAP_CHILDREN | LIST_CONTEXT) }
1N/A
1N/A# `.' is special because concats-of-concats are optimized to save copying
1N/A# by making all but the first concat stacked. The effect is as if the
1N/A# programmer had written `($a . $b) .= $c', except legal.
1N/Asub pp_concat { maybe_targmy(@_, \&real_concat) }
1N/Asub real_concat {
1N/A my $self = shift;
1N/A my($op, $cx) = @_;
1N/A my $left = $op->first;
1N/A my $right = $op->last;
1N/A my $eq = "";
1N/A my $prec = 18;
1N/A if ($op->flags & OPf_STACKED and $op->first->name ne "concat") {
1N/A $eq = "=";
1N/A $prec = 7;
1N/A }
1N/A $left = $self->deparse_binop_left($op, $left, $prec);
1N/A $right = $self->deparse_binop_right($op, $right, $prec);
1N/A return $self->maybe_parens("$left .$eq $right", $cx, $prec);
1N/A}
1N/A
1N/A# `x' is weird when the left arg is a list
1N/Asub pp_repeat {
1N/A my $self = shift;
1N/A my($op, $cx) = @_;
1N/A my $left = $op->first;
1N/A my $right = $op->last;
1N/A my $eq = "";
1N/A my $prec = 19;
1N/A if ($op->flags & OPf_STACKED) {
1N/A $eq = "=";
1N/A $prec = 7;
1N/A }
1N/A if (null($right)) { # list repeat; count is inside left-side ex-list
1N/A my $kid = $left->first->sibling; # skip pushmark
1N/A my @exprs;
1N/A for (; !null($kid->sibling); $kid = $kid->sibling) {
1N/A push @exprs, $self->deparse($kid, 6);
1N/A }
1N/A $right = $kid;
1N/A $left = "(" . join(", ", @exprs). ")";
1N/A } else {
1N/A $left = $self->deparse_binop_left($op, $left, $prec);
1N/A }
1N/A $right = $self->deparse_binop_right($op, $right, $prec);
1N/A return $self->maybe_parens("$left x$eq $right", $cx, $prec);
1N/A}
1N/A
1N/Asub range {
1N/A my $self = shift;
1N/A my ($op, $cx, $type) = @_;
1N/A my $left = $op->first;
1N/A my $right = $left->sibling;
1N/A $left = $self->deparse($left, 9);
1N/A $right = $self->deparse($right, 9);
1N/A return $self->maybe_parens("$left $type $right", $cx, 9);
1N/A}
1N/A
1N/Asub pp_flop {
1N/A my $self = shift;
1N/A my($op, $cx) = @_;
1N/A my $flip = $op->first;
1N/A my $type = ($flip->flags & OPf_SPECIAL) ? "..." : "..";
1N/A return $self->range($flip->first, $cx, $type);
1N/A}
1N/A
1N/A# one-line while/until is handled in pp_leave
1N/A
1N/Asub logop {
1N/A my $self = shift;
1N/A my ($op, $cx, $lowop, $lowprec, $highop, $highprec, $blockname) = @_;
1N/A my $left = $op->first;
1N/A my $right = $op->first->sibling;
1N/A if ($cx < 1 and is_scope($right) and $blockname
1N/A and $self->{'expand'} < 7)
1N/A { # if ($a) {$b}
1N/A $left = $self->deparse($left, 1);
1N/A $right = $self->deparse($right, 0);
1N/A return "$blockname ($left) {\n\t$right\n\b}\cK";
1N/A } elsif ($cx < 1 and $blockname and not $self->{'parens'}
1N/A and $self->{'expand'} < 7) { # $b if $a
1N/A $right = $self->deparse($right, 1);
1N/A $left = $self->deparse($left, 1);
1N/A return "$right $blockname $left";
1N/A } elsif ($cx > $lowprec and $highop) { # $a && $b
1N/A $left = $self->deparse_binop_left($op, $left, $highprec);
1N/A $right = $self->deparse_binop_right($op, $right, $highprec);
1N/A return $self->maybe_parens("$left $highop $right", $cx, $highprec);
1N/A } else { # $a and $b
1N/A $left = $self->deparse_binop_left($op, $left, $lowprec);
1N/A $right = $self->deparse_binop_right($op, $right, $lowprec);
1N/A return $self->maybe_parens("$left $lowop $right", $cx, $lowprec);
1N/A }
1N/A}
1N/A
1N/Asub pp_and { logop(@_, "and", 3, "&&", 11, "if") }
1N/Asub pp_or { logop(@_, "or", 2, "||", 10, "unless") }
1N/A
1N/A# xor is syntactically a logop, but it's really a binop (contrary to
1N/A# old versions of opcode.pl). Syntax is what matters here.
1N/Asub pp_xor { logop(@_, "xor", 2, "", 0, "") }
1N/A
1N/Asub logassignop {
1N/A my $self = shift;
1N/A my ($op, $cx, $opname) = @_;
1N/A my $left = $op->first;
1N/A my $right = $op->first->sibling->first; # skip sassign
1N/A $left = $self->deparse($left, 7);
1N/A $right = $self->deparse($right, 7);
1N/A return $self->maybe_parens("$left $opname $right", $cx, 7);
1N/A}
1N/A
1N/Asub pp_andassign { logassignop(@_, "&&=") }
1N/Asub pp_orassign { logassignop(@_, "||=") }
1N/A
1N/Asub listop {
1N/A my $self = shift;
1N/A my($op, $cx, $name) = @_;
1N/A my(@exprs);
1N/A my $parens = ($cx >= 5) || $self->{'parens'};
1N/A my $kid = $op->first->sibling;
1N/A return $name if null $kid;
1N/A my $first;
1N/A $name = "socketpair" if $name eq "sockpair";
1N/A my $proto = prototype("CORE::$name");
1N/A if (defined $proto
1N/A && $proto =~ /^;?\*/
1N/A && $kid->name eq "rv2gv") {
1N/A $first = $self->deparse($kid->first, 6);
1N/A }
1N/A else {
1N/A $first = $self->deparse($kid, 6);
1N/A }
1N/A if ($name eq "chmod" && $first =~ /^\d+$/) {
1N/A $first = sprintf("%#o", $first);
1N/A }
1N/A $first = "+$first" if not $parens and substr($first, 0, 1) eq "(";
1N/A push @exprs, $first;
1N/A $kid = $kid->sibling;
1N/A if (defined $proto && $proto =~ /^\*\*/ && $kid->name eq "rv2gv") {
1N/A push @exprs, $self->deparse($kid->first, 6);
1N/A $kid = $kid->sibling;
1N/A }
1N/A for (; !null($kid); $kid = $kid->sibling) {
1N/A push @exprs, $self->deparse($kid, 6);
1N/A }
1N/A if ($parens) {
1N/A return "$name(" . join(", ", @exprs) . ")";
1N/A } else {
1N/A return "$name " . join(", ", @exprs);
1N/A }
1N/A}
1N/A
1N/Asub pp_bless { listop(@_, "bless") }
1N/Asub pp_atan2 { maybe_targmy(@_, \&listop, "atan2") }
1N/Asub pp_substr { maybe_local(@_, listop(@_, "substr")) }
1N/Asub pp_vec { maybe_local(@_, listop(@_, "vec")) }
1N/Asub pp_index { maybe_targmy(@_, \&listop, "index") }
1N/Asub pp_rindex { maybe_targmy(@_, \&listop, "rindex") }
1N/Asub pp_sprintf { maybe_targmy(@_, \&listop, "sprintf") }
1N/Asub pp_formline { listop(@_, "formline") } # see also deparse_format
1N/Asub pp_crypt { maybe_targmy(@_, \&listop, "crypt") }
1N/Asub pp_unpack { listop(@_, "unpack") }
1N/Asub pp_pack { listop(@_, "pack") }
1N/Asub pp_join { maybe_targmy(@_, \&listop, "join") }
1N/Asub pp_splice { listop(@_, "splice") }
1N/Asub pp_push { maybe_targmy(@_, \&listop, "push") }
1N/Asub pp_unshift { maybe_targmy(@_, \&listop, "unshift") }
1N/Asub pp_reverse { listop(@_, "reverse") }
1N/Asub pp_warn { listop(@_, "warn") }
1N/Asub pp_die { listop(@_, "die") }
1N/A# Actually, return is exempt from the LLAFR (see examples in this very
1N/A# module!), but for consistency's sake, ignore that fact
1N/Asub pp_return { listop(@_, "return") }
1N/Asub pp_open { listop(@_, "open") }
1N/Asub pp_pipe_op { listop(@_, "pipe") }
1N/Asub pp_tie { listop(@_, "tie") }
1N/Asub pp_binmode { listop(@_, "binmode") }
1N/Asub pp_dbmopen { listop(@_, "dbmopen") }
1N/Asub pp_sselect { listop(@_, "select") }
1N/Asub pp_select { listop(@_, "select") }
1N/Asub pp_read { listop(@_, "read") }
1N/Asub pp_sysopen { listop(@_, "sysopen") }
1N/Asub pp_sysseek { listop(@_, "sysseek") }
1N/Asub pp_sysread { listop(@_, "sysread") }
1N/Asub pp_syswrite { listop(@_, "syswrite") }
1N/Asub pp_send { listop(@_, "send") }
1N/Asub pp_recv { listop(@_, "recv") }
1N/Asub pp_seek { listop(@_, "seek") }
1N/Asub pp_fcntl { listop(@_, "fcntl") }
1N/Asub pp_ioctl { listop(@_, "ioctl") }
1N/Asub pp_flock { maybe_targmy(@_, \&listop, "flock") }
1N/Asub pp_socket { listop(@_, "socket") }
1N/Asub pp_sockpair { listop(@_, "sockpair") }
1N/Asub pp_bind { listop(@_, "bind") }
1N/Asub pp_connect { listop(@_, "connect") }
1N/Asub pp_listen { listop(@_, "listen") }
1N/Asub pp_accept { listop(@_, "accept") }
1N/Asub pp_shutdown { listop(@_, "shutdown") }
1N/Asub pp_gsockopt { listop(@_, "getsockopt") }
1N/Asub pp_ssockopt { listop(@_, "setsockopt") }
1N/Asub pp_chown { maybe_targmy(@_, \&listop, "chown") }
1N/Asub pp_unlink { maybe_targmy(@_, \&listop, "unlink") }
1N/Asub pp_chmod { maybe_targmy(@_, \&listop, "chmod") }
1N/Asub pp_utime { maybe_targmy(@_, \&listop, "utime") }
1N/Asub pp_rename { maybe_targmy(@_, \&listop, "rename") }
1N/Asub pp_link { maybe_targmy(@_, \&listop, "link") }
1N/Asub pp_symlink { maybe_targmy(@_, \&listop, "symlink") }
1N/Asub pp_mkdir { maybe_targmy(@_, \&listop, "mkdir") }
1N/Asub pp_open_dir { listop(@_, "opendir") }
1N/Asub pp_seekdir { listop(@_, "seekdir") }
1N/Asub pp_waitpid { maybe_targmy(@_, \&listop, "waitpid") }
1N/Asub pp_system { maybe_targmy(@_, \&listop, "system") }
1N/Asub pp_exec { maybe_targmy(@_, \&listop, "exec") }
1N/Asub pp_kill { maybe_targmy(@_, \&listop, "kill") }
1N/Asub pp_setpgrp { maybe_targmy(@_, \&listop, "setpgrp") }
1N/Asub pp_getpriority { maybe_targmy(@_, \&listop, "getpriority") }
1N/Asub pp_setpriority { maybe_targmy(@_, \&listop, "setpriority") }
1N/Asub pp_shmget { listop(@_, "shmget") }
1N/Asub pp_shmctl { listop(@_, "shmctl") }
1N/Asub pp_shmread { listop(@_, "shmread") }
1N/Asub pp_shmwrite { listop(@_, "shmwrite") }
1N/Asub pp_msgget { listop(@_, "msgget") }
1N/Asub pp_msgctl { listop(@_, "msgctl") }
1N/Asub pp_msgsnd { listop(@_, "msgsnd") }
1N/Asub pp_msgrcv { listop(@_, "msgrcv") }
1N/Asub pp_semget { listop(@_, "semget") }
1N/Asub pp_semctl { listop(@_, "semctl") }
1N/Asub pp_semop { listop(@_, "semop") }
1N/Asub pp_ghbyaddr { listop(@_, "gethostbyaddr") }
1N/Asub pp_gnbyaddr { listop(@_, "getnetbyaddr") }
1N/Asub pp_gpbynumber { listop(@_, "getprotobynumber") }
1N/Asub pp_gsbyname { listop(@_, "getservbyname") }
1N/Asub pp_gsbyport { listop(@_, "getservbyport") }
1N/Asub pp_syscall { listop(@_, "syscall") }
1N/A
1N/Asub pp_glob {
1N/A my $self = shift;
1N/A my($op, $cx) = @_;
1N/A my $text = $self->dq($op->first->sibling); # skip pushmark
1N/A if ($text =~ /^\$?(\w|::|\`)+$/ # could look like a readline
1N/A or $text =~ /[<>]/) {
1N/A return 'glob(' . single_delim('qq', '"', $text) . ')';
1N/A } else {
1N/A return '<' . $text . '>';
1N/A }
1N/A}
1N/A
1N/A# Truncate is special because OPf_SPECIAL makes a bareword first arg
1N/A# be a filehandle. This could probably be better fixed in the core
1N/A# by moving the GV lookup into ck_truc.
1N/A
1N/Asub pp_truncate {
1N/A my $self = shift;
1N/A my($op, $cx) = @_;
1N/A my(@exprs);
1N/A my $parens = ($cx >= 5) || $self->{'parens'};
1N/A my $kid = $op->first->sibling;
1N/A my $fh;
1N/A if ($op->flags & OPf_SPECIAL) {
1N/A # $kid is an OP_CONST
1N/A $fh = $self->const_sv($kid)->PV;
1N/A } else {
1N/A $fh = $self->deparse($kid, 6);
1N/A $fh = "+$fh" if not $parens and substr($fh, 0, 1) eq "(";
1N/A }
1N/A my $len = $self->deparse($kid->sibling, 6);
1N/A if ($parens) {
1N/A return "truncate($fh, $len)";
1N/A } else {
1N/A return "truncate $fh, $len";
1N/A }
1N/A}
1N/A
1N/Asub indirop {
1N/A my $self = shift;
1N/A my($op, $cx, $name) = @_;
1N/A my($expr, @exprs);
1N/A my $kid = $op->first->sibling;
1N/A my $indir = "";
1N/A if ($op->flags & OPf_STACKED) {
1N/A $indir = $kid;
1N/A $indir = $indir->first; # skip rv2gv
1N/A if (is_scope($indir)) {
1N/A $indir = "{" . $self->deparse($indir, 0) . "}";
1N/A $indir = "{;}" if $indir eq "{}";
1N/A } elsif ($indir->name eq "const" && $indir->private & OPpCONST_BARE) {
1N/A $indir = $self->const_sv($indir)->PV;
1N/A } else {
1N/A $indir = $self->deparse($indir, 24);
1N/A }
1N/A $indir = $indir . " ";
1N/A $kid = $kid->sibling;
1N/A }
1N/A if ($name eq "sort" && $op->private & (OPpSORT_NUMERIC | OPpSORT_INTEGER)) {
1N/A $indir = ($op->private & OPpSORT_REVERSE) ? '{$b <=> $a} '
1N/A : '{$a <=> $b} ';
1N/A }
1N/A elsif ($name eq "sort" && $op->private & OPpSORT_REVERSE) {
1N/A $indir = '{$b cmp $a} ';
1N/A }
1N/A for (; !null($kid); $kid = $kid->sibling) {
1N/A $expr = $self->deparse($kid, 6);
1N/A push @exprs, $expr;
1N/A }
1N/A if ($name eq "sort" && ($op->private & OPpSORT_INPLACE)) {
1N/A return "$exprs[0] = sort $indir $exprs[0]";
1N/A }
1N/A
1N/A my $args = $indir . join(", ", @exprs);
1N/A if ($indir ne "" and $name eq "sort") {
1N/A # We don't want to say "sort(f 1, 2, 3)", since perl -w will
1N/A # give bareword warnings in that case. Therefore if context
1N/A # requires, we'll put parens around the outside "(sort f 1, 2,
1N/A # 3)". Unfortunately, we'll currently think the parens are
1N/A # neccessary more often that they really are, because we don't
1N/A # distinguish which side of an assignment we're on.
1N/A if ($cx >= 5) {
1N/A return "($name $args)";
1N/A } else {
1N/A return "$name $args";
1N/A }
1N/A } else {
1N/A return $self->maybe_parens_func($name, $args, $cx, 5);
1N/A }
1N/A
1N/A}
1N/A
1N/Asub pp_prtf { indirop(@_, "printf") }
1N/Asub pp_print { indirop(@_, "print") }
1N/Asub pp_sort { indirop(@_, "sort") }
1N/A
1N/Asub mapop {
1N/A my $self = shift;
1N/A my($op, $cx, $name) = @_;
1N/A my($expr, @exprs);
1N/A my $kid = $op->first; # this is the (map|grep)start
1N/A $kid = $kid->first->sibling; # skip a pushmark
1N/A my $code = $kid->first; # skip a null
1N/A if (is_scope $code) {
1N/A $code = "{" . $self->deparse($code, 0) . "} ";
1N/A } else {
1N/A $code = $self->deparse($code, 24) . ", ";
1N/A }
1N/A $kid = $kid->sibling;
1N/A for (; !null($kid); $kid = $kid->sibling) {
1N/A $expr = $self->deparse($kid, 6);
1N/A push @exprs, $expr if defined $expr;
1N/A }
1N/A return $self->maybe_parens_func($name, $code . join(", ", @exprs), $cx, 5);
1N/A}
1N/A
1N/Asub pp_mapwhile { mapop(@_, "map") }
1N/Asub pp_grepwhile { mapop(@_, "grep") }
1N/A
1N/Asub pp_list {
1N/A my $self = shift;
1N/A my($op, $cx) = @_;
1N/A my($expr, @exprs);
1N/A my $kid = $op->first->sibling; # skip pushmark
1N/A my $lop;
1N/A my $local = "either"; # could be local(...), my(...) or our(...)
1N/A for ($lop = $kid; !null($lop); $lop = $lop->sibling) {
1N/A # This assumes that no other private flags equal 128, and that
1N/A # OPs that store things other than flags in their op_private,
1N/A # like OP_AELEMFAST, won't be immediate children of a list.
1N/A #
1N/A # OP_ENTERSUB can break this logic, so check for it.
1N/A # I suspect that open and exit can too.
1N/A
1N/A if (!($lop->private & (OPpLVAL_INTRO|OPpOUR_INTRO)
1N/A or $lop->name eq "undef")
1N/A or $lop->name eq "entersub"
1N/A or $lop->name eq "exit"
1N/A or $lop->name eq "open")
1N/A {
1N/A $local = ""; # or not
1N/A last;
1N/A }
1N/A if ($lop->name =~ /^pad[ash]v$/) { # my()
1N/A ($local = "", last) if $local eq "local" || $local eq "our";
1N/A $local = "my";
1N/A } elsif ($lop->name =~ /^(gv|rv2)[ash]v$/
1N/A && $lop->private & OPpOUR_INTRO
1N/A or $lop->name eq "null" && $lop->first->name eq "gvsv"
1N/A && $lop->first->private & OPpOUR_INTRO) { # our()
1N/A ($local = "", last) if $local eq "my" || $local eq "local";
1N/A $local = "our";
1N/A } elsif ($lop->name ne "undef") { # local()
1N/A ($local = "", last) if $local eq "my" || $local eq "our";
1N/A $local = "local";
1N/A }
1N/A }
1N/A $local = "" if $local eq "either"; # no point if it's all undefs
1N/A return $self->deparse($kid, $cx) if null $kid->sibling and not $local;
1N/A for (; !null($kid); $kid = $kid->sibling) {
1N/A if ($local) {
1N/A if (class($kid) eq "UNOP" and $kid->first->name eq "gvsv") {
1N/A $lop = $kid->first;
1N/A } else {
1N/A $lop = $kid;
1N/A }
1N/A $self->{'avoid_local'}{$$lop}++;
1N/A $expr = $self->deparse($kid, 6);
1N/A delete $self->{'avoid_local'}{$$lop};
1N/A } else {
1N/A $expr = $self->deparse($kid, 6);
1N/A }
1N/A push @exprs, $expr;
1N/A }
1N/A if ($local) {
1N/A return "$local(" . join(", ", @exprs) . ")";
1N/A } else {
1N/A return $self->maybe_parens( join(", ", @exprs), $cx, 6);
1N/A }
1N/A}
1N/A
1N/Asub is_ifelse_cont {
1N/A my $op = shift;
1N/A return ($op->name eq "null" and class($op) eq "UNOP"
1N/A and $op->first->name =~ /^(and|cond_expr)$/
1N/A and is_scope($op->first->first->sibling));
1N/A}
1N/A
1N/Asub pp_cond_expr {
1N/A my $self = shift;
1N/A my($op, $cx) = @_;
1N/A my $cond = $op->first;
1N/A my $true = $cond->sibling;
1N/A my $false = $true->sibling;
1N/A my $cuddle = $self->{'cuddle'};
1N/A unless ($cx < 1 and (is_scope($true) and $true->name ne "null") and
1N/A (is_scope($false) || is_ifelse_cont($false))
1N/A and $self->{'expand'} < 7) {
1N/A $cond = $self->deparse($cond, 8);
1N/A $true = $self->deparse($true, 8);
1N/A $false = $self->deparse($false, 8);
1N/A return $self->maybe_parens("$cond ? $true : $false", $cx, 8);
1N/A }
1N/A
1N/A $cond = $self->deparse($cond, 1);
1N/A $true = $self->deparse($true, 0);
1N/A my $head = "if ($cond) {\n\t$true\n\b}";
1N/A my @elsifs;
1N/A while (!null($false) and is_ifelse_cont($false)) {
1N/A my $newop = $false->first;
1N/A my $newcond = $newop->first;
1N/A my $newtrue = $newcond->sibling;
1N/A $false = $newtrue->sibling; # last in chain is OP_AND => no else
1N/A $newcond = $self->deparse($newcond, 1);
1N/A $newtrue = $self->deparse($newtrue, 0);
1N/A push @elsifs, "elsif ($newcond) {\n\t$newtrue\n\b}";
1N/A }
1N/A if (!null($false)) {
1N/A $false = $cuddle . "else {\n\t" .
1N/A $self->deparse($false, 0) . "\n\b}\cK";
1N/A } else {
1N/A $false = "\cK";
1N/A }
1N/A return $head . join($cuddle, "", @elsifs) . $false;
1N/A}
1N/A
1N/Asub loop_common {
1N/A my $self = shift;
1N/A my($op, $cx, $init) = @_;
1N/A my $enter = $op->first;
1N/A my $kid = $enter->sibling;
1N/A local(@$self{qw'curstash warnings hints'})
1N/A = @$self{qw'curstash warnings hints'};
1N/A my $head = "";
1N/A my $bare = 0;
1N/A my $body;
1N/A my $cond = undef;
1N/A if ($kid->name eq "lineseq") { # bare or infinite loop
1N/A if ($kid->last->name eq "unstack") { # infinite
1N/A $head = "while (1) "; # Can't use for(;;) if there's a continue
1N/A $cond = "";
1N/A } else {
1N/A $bare = 1;
1N/A }
1N/A $body = $kid;
1N/A } elsif ($enter->name eq "enteriter") { # foreach
1N/A my $ary = $enter->first->sibling; # first was pushmark
1N/A my $var = $ary->sibling;
1N/A if ($enter->flags & OPf_STACKED
1N/A and not null $ary->first->sibling->sibling)
1N/A {
1N/A $ary = $self->deparse($ary->first->sibling, 9) . " .. " .
1N/A $self->deparse($ary->first->sibling->sibling, 9);
1N/A } else {
1N/A $ary = $self->deparse($ary, 1);
1N/A }
1N/A if (null $var) {
1N/A if ($enter->flags & OPf_SPECIAL) { # thread special var
1N/A $var = $self->pp_threadsv($enter, 1);
1N/A } else { # regular my() variable
1N/A $var = $self->pp_padsv($enter, 1);
1N/A }
1N/A } elsif ($var->name eq "rv2gv") {
1N/A $var = $self->pp_rv2sv($var, 1);
1N/A if ($enter->private & OPpOUR_INTRO) {
1N/A # our declarations don't have package names
1N/A $var =~ s/^(.).*::/$1/;
1N/A $var = "our $var";
1N/A }
1N/A } elsif ($var->name eq "gv") {
1N/A $var = "\$" . $self->deparse($var, 1);
1N/A }
1N/A $head = "foreach $var ($ary) ";
1N/A $body = $kid->first->first->sibling; # skip OP_AND and OP_ITER
1N/A } elsif ($kid->name eq "null") { # while/until
1N/A $kid = $kid->first;
1N/A my $name = {"and" => "while", "or" => "until"}->{$kid->name};
1N/A $cond = $self->deparse($kid->first, 1);
1N/A $head = "$name ($cond) ";
1N/A $body = $kid->first->sibling;
1N/A } elsif ($kid->name eq "stub") { # bare and empty
1N/A return "{;}"; # {} could be a hashref
1N/A }
1N/A # If there isn't a continue block, then the next pointer for the loop
1N/A # will point to the unstack, which is kid's last child, except
1N/A # in a bare loop, when it will point to the leaveloop. When neither of
1N/A # these conditions hold, then the second-to-last child is the continue
1N/A # block (or the last in a bare loop).
1N/A my $cont_start = $enter->nextop;
1N/A my $cont;
1N/A if ($$cont_start != $$op && ${$cont_start} != ${$body->last}) {
1N/A if ($bare) {
1N/A $cont = $body->last;
1N/A } else {
1N/A $cont = $body->first;
1N/A while (!null($cont->sibling->sibling)) {
1N/A $cont = $cont->sibling;
1N/A }
1N/A }
1N/A my $state = $body->first;
1N/A my $cuddle = $self->{'cuddle'};
1N/A my @states;
1N/A for (; $$state != $$cont; $state = $state->sibling) {
1N/A push @states, $state;
1N/A }
1N/A $body = $self->lineseq(undef, @states);
1N/A if (defined $cond and not is_scope $cont and $self->{'expand'} < 3) {
1N/A $head = "for ($init; $cond; " . $self->deparse($cont, 1) .") ";
1N/A $cont = "\cK";
1N/A } else {
1N/A $cont = $cuddle . "continue {\n\t" .
1N/A $self->deparse($cont, 0) . "\n\b}\cK";
1N/A }
1N/A } else {
1N/A return "" if !defined $body;
1N/A if (length $init) {
1N/A $head = "for ($init; $cond;) ";
1N/A }
1N/A $cont = "\cK";
1N/A $body = $self->deparse($body, 0);
1N/A }
1N/A $body =~ s/;?$/;\n/;
1N/A
1N/A return $head . "{\n\t" . $body . "\b}" . $cont;
1N/A}
1N/A
1N/Asub pp_leaveloop { loop_common(@_, "") }
1N/A
1N/Asub for_loop {
1N/A my $self = shift;
1N/A my($op, $cx) = @_;
1N/A my $init = $self->deparse($op, 1);
1N/A return $self->loop_common($op->sibling->first->sibling, $cx, $init);
1N/A}
1N/A
1N/Asub pp_leavetry {
1N/A my $self = shift;
1N/A return "eval {\n\t" . $self->pp_leave(@_) . "\n\b}";
1N/A}
1N/A
1N/ABEGIN { eval "sub OP_CONST () {" . opnumber("const") . "}" }
1N/ABEGIN { eval "sub OP_STRINGIFY () {" . opnumber("stringify") . "}" }
1N/ABEGIN { eval "sub OP_RV2SV () {" . opnumber("rv2sv") . "}" }
1N/ABEGIN { eval "sub OP_LIST () {" . opnumber("list") . "}" }
1N/A
1N/Asub pp_null {
1N/A my $self = shift;
1N/A my($op, $cx) = @_;
1N/A if (class($op) eq "OP") {
1N/A # old value is lost
1N/A return $self->{'ex_const'} if $op->targ == OP_CONST;
1N/A } elsif ($op->first->name eq "pushmark") {
1N/A return $self->pp_list($op, $cx);
1N/A } elsif ($op->first->name eq "enter") {
1N/A return $self->pp_leave($op, $cx);
1N/A } elsif ($op->targ == OP_STRINGIFY) {
1N/A return $self->dquote($op, $cx);
1N/A } elsif (!null($op->first->sibling) and
1N/A $op->first->sibling->name eq "readline" and
1N/A $op->first->sibling->flags & OPf_STACKED) {
1N/A return $self->maybe_parens($self->deparse($op->first, 7) . " = "
1N/A . $self->deparse($op->first->sibling, 7),
1N/A $cx, 7);
1N/A } elsif (!null($op->first->sibling) and
1N/A $op->first->sibling->name eq "trans" and
1N/A $op->first->sibling->flags & OPf_STACKED) {
1N/A return $self->maybe_parens($self->deparse($op->first, 20) . " =~ "
1N/A . $self->deparse($op->first->sibling, 20),
1N/A $cx, 20);
1N/A } elsif ($op->flags & OPf_SPECIAL && $cx < 1 && !$op->targ) {
1N/A return "do {\n\t". $self->deparse($op->first, $cx) ."\n\b};";
1N/A } elsif (!null($op->first->sibling) and
1N/A $op->first->sibling->name eq "null" and
1N/A class($op->first->sibling) eq "UNOP" and
1N/A $op->first->sibling->first->flags & OPf_STACKED and
1N/A $op->first->sibling->first->name eq "rcatline") {
1N/A return $self->maybe_parens($self->deparse($op->first, 18) . " .= "
1N/A . $self->deparse($op->first->sibling, 18),
1N/A $cx, 18);
1N/A } else {
1N/A return $self->deparse($op->first, $cx);
1N/A }
1N/A}
1N/A
1N/Asub padname {
1N/A my $self = shift;
1N/A my $targ = shift;
1N/A return $self->padname_sv($targ)->PVX;
1N/A}
1N/A
1N/Asub padany {
1N/A my $self = shift;
1N/A my $op = shift;
1N/A return substr($self->padname($op->targ), 1); # skip $/@/%
1N/A}
1N/A
1N/Asub pp_padsv {
1N/A my $self = shift;
1N/A my($op, $cx) = @_;
1N/A return $self->maybe_my($op, $cx, $self->padname($op->targ));
1N/A}
1N/A
1N/Asub pp_padav { pp_padsv(@_) }
1N/Asub pp_padhv { pp_padsv(@_) }
1N/A
1N/Amy @threadsv_names;
1N/A
1N/ABEGIN {
1N/A @threadsv_names = ("_", "1", "2", "3", "4", "5", "6", "7", "8", "9",
1N/A "&", "`", "'", "+", "/", ".", ",", "\\", '"', ";",
1N/A "^", "-", "%", "=", "|", "~", ":", "^A", "^E",
1N/A "!", "@");
1N/A}
1N/A
1N/Asub pp_threadsv {
1N/A my $self = shift;
1N/A my($op, $cx) = @_;
1N/A return $self->maybe_local($op, $cx, "\$" . $threadsv_names[$op->targ]);
1N/A}
1N/A
1N/Asub gv_or_padgv {
1N/A my $self = shift;
1N/A my $op = shift;
1N/A if (class($op) eq "PADOP") {
1N/A return $self->padval($op->padix);
1N/A } else { # class($op) eq "SVOP"
1N/A return $op->gv;
1N/A }
1N/A}
1N/A
1N/Asub pp_gvsv {
1N/A my $self = shift;
1N/A my($op, $cx) = @_;
1N/A my $gv = $self->gv_or_padgv($op);
1N/A return $self->maybe_local($op, $cx, $self->stash_variable("\$",
1N/A $self->gv_name($gv)));
1N/A}
1N/A
1N/Asub pp_gv {
1N/A my $self = shift;
1N/A my($op, $cx) = @_;
1N/A my $gv = $self->gv_or_padgv($op);
1N/A return $self->gv_name($gv);
1N/A}
1N/A
1N/Asub pp_aelemfast {
1N/A my $self = shift;
1N/A my($op, $cx) = @_;
1N/A my $name;
1N/A if ($op->flags & OPf_SPECIAL) { # optimised PADAV
1N/A $name = $self->padname($op->targ);
1N/A $name =~ s/^@/\$/;
1N/A }
1N/A else {
1N/A my $gv = $self->gv_or_padgv($op);
1N/A $name = $self->gv_name($gv);
1N/A $name = $self->{'curstash'}."::$name"
1N/A if $name !~ /::/ && $self->lex_in_scope('@'.$name);
1N/A $name = '$' . $name;
1N/A }
1N/A
1N/A return $name . "[" . ($op->private + $self->{'arybase'}) . "]";
1N/A}
1N/A
1N/Asub rv2x {
1N/A my $self = shift;
1N/A my($op, $cx, $type) = @_;
1N/A
1N/A if (class($op) eq 'NULL' || !$op->can("first")) {
1N/A carp("Unexpected op in pp_rv2x");
1N/A return 'XXX';
1N/A }
1N/A my $kid = $op->first;
1N/A if ($kid->name eq "gv") {
1N/A return $self->stash_variable($type, $self->deparse($kid, 0));
1N/A } elsif (is_scalar $kid) {
1N/A my $str = $self->deparse($kid, 0);
1N/A if ($str =~ /^\$([^\w\d])\z/) {
1N/A # "$$+" isn't a legal way to write the scalar dereference
1N/A # of $+, since the lexer can't tell you aren't trying to
1N/A # do something like "$$ + 1" to get one more than your
1N/A # PID. Either "${$+}" or "$${+}" are workable
1N/A # disambiguations, but if the programmer did the former,
1N/A # they'd be in the "else" clause below rather than here.
1N/A # It's not clear if this should somehow be unified with
1N/A # the code in dq and re_dq that also adds lexer
1N/A # disambiguation braces.
1N/A $str = '$' . "{$1}"; #'
1N/A }
1N/A return $type . $str;
1N/A } else {
1N/A return $type . "{" . $self->deparse($kid, 0) . "}";
1N/A }
1N/A}
1N/A
1N/Asub pp_rv2sv { maybe_local(@_, rv2x(@_, "\$")) }
1N/Asub pp_rv2hv { maybe_local(@_, rv2x(@_, "%")) }
1N/Asub pp_rv2gv { maybe_local(@_, rv2x(@_, "*")) }
1N/A
1N/A# skip rv2av
1N/Asub pp_av2arylen {
1N/A my $self = shift;
1N/A my($op, $cx) = @_;
1N/A if ($op->first->name eq "padav") {
1N/A return $self->maybe_local($op, $cx, '$#' . $self->padany($op->first));
1N/A } else {
1N/A return $self->maybe_local($op, $cx,
1N/A $self->rv2x($op->first, $cx, '$#'));
1N/A }
1N/A}
1N/A
1N/A# skip down to the old, ex-rv2cv
1N/Asub pp_rv2cv {
1N/A my ($self, $op, $cx) = @_;
1N/A if (!null($op->first) && $op->first->name eq 'null' &&
1N/A $op->first->targ eq OP_LIST)
1N/A {
1N/A return $self->rv2x($op->first->first->sibling, $cx, "&")
1N/A }
1N/A else {
1N/A return $self->rv2x($op, $cx, "")
1N/A }
1N/A}
1N/A
1N/Asub list_const {
1N/A my $self = shift;
1N/A my($cx, @list) = @_;
1N/A my @a = map $self->const($_, 6), @list;
1N/A if (@a == 0) {
1N/A return "()";
1N/A } elsif (@a == 1) {
1N/A return $a[0];
1N/A } elsif ( @a > 2 and !grep(!/^-?\d+$/, @a)) {
1N/A # collapse (-1,0,1,2) into (-1..2)
1N/A my ($s, $e) = @a[0,-1];
1N/A my $i = $s;
1N/A return $self->maybe_parens("$s..$e", $cx, 9)
1N/A unless grep $i++ != $_, @a;
1N/A }
1N/A return $self->maybe_parens(join(", ", @a), $cx, 6);
1N/A}
1N/A
1N/Asub pp_rv2av {
1N/A my $self = shift;
1N/A my($op, $cx) = @_;
1N/A my $kid = $op->first;
1N/A if ($kid->name eq "const") { # constant list
1N/A my $av = $self->const_sv($kid);
1N/A return $self->list_const($cx, $av->ARRAY);
1N/A } else {
1N/A return $self->maybe_local($op, $cx, $self->rv2x($op, $cx, "\@"));
1N/A }
1N/A }
1N/A
1N/Asub is_subscriptable {
1N/A my $op = shift;
1N/A if ($op->name =~ /^[ahg]elem/) {
1N/A return 1;
1N/A } elsif ($op->name eq "entersub") {
1N/A my $kid = $op->first;
1N/A return 0 unless null $kid->sibling;
1N/A $kid = $kid->first;
1N/A $kid = $kid->sibling until null $kid->sibling;
1N/A return 0 if is_scope($kid);
1N/A $kid = $kid->first;
1N/A return 0 if $kid->name eq "gv";
1N/A return 0 if is_scalar($kid);
1N/A return is_subscriptable($kid);
1N/A } else {
1N/A return 0;
1N/A }
1N/A}
1N/A
1N/Asub elem {
1N/A my $self = shift;
1N/A my ($op, $cx, $left, $right, $padname) = @_;
1N/A my($array, $idx) = ($op->first, $op->first->sibling);
1N/A unless ($array->name eq $padname) { # Maybe this has been fixed
1N/A $array = $array->first; # skip rv2av (or ex-rv2av in _53+)
1N/A }
1N/A if ($array->name eq $padname) {
1N/A $array = $self->padany($array);
1N/A } elsif (is_scope($array)) { # ${expr}[0]
1N/A $array = "{" . $self->deparse($array, 0) . "}";
1N/A } elsif ($array->name eq "gv") {
1N/A $array = $self->gv_name($self->gv_or_padgv($array));
1N/A if ($array !~ /::/) {
1N/A my $prefix = ($left eq '[' ? '@' : '%');
1N/A $array = $self->{curstash}.'::'.$array
1N/A if $self->lex_in_scope($prefix . $array);
1N/A }
1N/A } elsif (is_scalar $array) { # $x[0], $$x[0], ...
1N/A $array = $self->deparse($array, 24);
1N/A } else {
1N/A # $x[20][3]{hi} or expr->[20]
1N/A my $arrow = is_subscriptable($array) ? "" : "->";
1N/A return $self->deparse($array, 24) . $arrow .
1N/A $left . $self->deparse($idx, 1) . $right;
1N/A }
1N/A $idx = $self->deparse($idx, 1);
1N/A
1N/A # Outer parens in an array index will confuse perl
1N/A # if we're interpolating in a regular expression, i.e.
1N/A # /$x$foo[(-1)]/ is *not* the same as /$x$foo[-1]/
1N/A #
1N/A # If $self->{parens}, then an initial '(' will
1N/A # definitely be paired with a final ')'. If
1N/A # !$self->{parens}, the misleading parens won't
1N/A # have been added in the first place.
1N/A #
1N/A # [You might think that we could get "(...)...(...)"
1N/A # where the initial and final parens do not match
1N/A # each other. But we can't, because the above would
1N/A # only happen if there's an infix binop between the
1N/A # two pairs of parens, and *that* means that the whole
1N/A # expression would be parenthesized as well.]
1N/A #
1N/A $idx =~ s/^\((.*)\)$/$1/ if $self->{'parens'};
1N/A
1N/A # Hash-element braces will autoquote a bareword inside themselves.
1N/A # We need to make sure that C<$hash{warn()}> doesn't come out as
1N/A # C<$hash{warn}>, which has a quite different meaning. Currently
1N/A # B::Deparse will always quote strings, even if the string was a
1N/A # bareword in the original (i.e. the OPpCONST_BARE flag is ignored
1N/A # for constant strings.) So we can cheat slightly here - if we see
1N/A # a bareword, we know that it is supposed to be a function call.
1N/A #
1N/A $idx =~ s/^([A-Za-z_]\w*)$/$1()/;
1N/A
1N/A return "\$" . $array . $left . $idx . $right;
1N/A}
1N/A
1N/Asub pp_aelem { maybe_local(@_, elem(@_, "[", "]", "padav")) }
1N/Asub pp_helem { maybe_local(@_, elem(@_, "{", "}", "padhv")) }
1N/A
1N/Asub pp_gelem {
1N/A my $self = shift;
1N/A my($op, $cx) = @_;
1N/A my($glob, $part) = ($op->first, $op->last);
1N/A $glob = $glob->first; # skip rv2gv
1N/A $glob = $glob->first if $glob->name eq "rv2gv"; # this one's a bug
1N/A my $scope = is_scope($glob);
1N/A $glob = $self->deparse($glob, 0);
1N/A $part = $self->deparse($part, 1);
1N/A return "*" . ($scope ? "{$glob}" : $glob) . "{$part}";
1N/A}
1N/A
1N/Asub slice {
1N/A my $self = shift;
1N/A my ($op, $cx, $left, $right, $regname, $padname) = @_;
1N/A my $last;
1N/A my(@elems, $kid, $array, $list);
1N/A if (class($op) eq "LISTOP") {
1N/A $last = $op->last;
1N/A } else { # ex-hslice inside delete()
1N/A for ($kid = $op->first; !null $kid->sibling; $kid = $kid->sibling) {}
1N/A $last = $kid;
1N/A }
1N/A $array = $last;
1N/A $array = $array->first
1N/A if $array->name eq $regname or $array->name eq "null";
1N/A if (is_scope($array)) {
1N/A $array = "{" . $self->deparse($array, 0) . "}";
1N/A } elsif ($array->name eq $padname) {
1N/A $array = $self->padany($array);
1N/A } else {
1N/A $array = $self->deparse($array, 24);
1N/A }
1N/A $kid = $op->first->sibling; # skip pushmark
1N/A if ($kid->name eq "list") {
1N/A $kid = $kid->first->sibling; # skip list, pushmark
1N/A for (; !null $kid; $kid = $kid->sibling) {
1N/A push @elems, $self->deparse($kid, 6);
1N/A }
1N/A $list = join(", ", @elems);
1N/A } else {
1N/A $list = $self->deparse($kid, 1);
1N/A }
1N/A return "\@" . $array . $left . $list . $right;
1N/A}
1N/A
1N/Asub pp_aslice { maybe_local(@_, slice(@_, "[", "]", "rv2av", "padav")) }
1N/Asub pp_hslice { maybe_local(@_, slice(@_, "{", "}", "rv2hv", "padhv")) }
1N/A
1N/Asub pp_lslice {
1N/A my $self = shift;
1N/A my($op, $cx) = @_;
1N/A my $idx = $op->first;
1N/A my $list = $op->last;
1N/A my(@elems, $kid);
1N/A $list = $self->deparse($list, 1);
1N/A $idx = $self->deparse($idx, 1);
1N/A return "($list)" . "[$idx]";
1N/A}
1N/A
1N/Asub want_scalar {
1N/A my $op = shift;
1N/A return ($op->flags & OPf_WANT) == OPf_WANT_SCALAR;
1N/A}
1N/A
1N/Asub want_list {
1N/A my $op = shift;
1N/A return ($op->flags & OPf_WANT) == OPf_WANT_LIST;
1N/A}
1N/A
1N/Asub method {
1N/A my $self = shift;
1N/A my($op, $cx) = @_;
1N/A my $kid = $op->first->sibling; # skip pushmark
1N/A my($meth, $obj, @exprs);
1N/A if ($kid->name eq "list" and want_list $kid) {
1N/A # When an indirect object isn't a bareword but the args are in
1N/A # parens, the parens aren't part of the method syntax (the LLAFR
1N/A # doesn't apply), but they make a list with OPf_PARENS set that
1N/A # doesn't get flattened by the append_elem that adds the method,
1N/A # making a (object, arg1, arg2, ...) list where the object
1N/A # usually is. This can be distinguished from
1N/A # `($obj, $arg1, $arg2)->meth()' (which is legal if $arg2 is an
1N/A # object) because in the later the list is in scalar context
1N/A # as the left side of -> always is, while in the former
1N/A # the list is in list context as method arguments always are.
1N/A # (Good thing there aren't method prototypes!)
1N/A $meth = $kid->sibling;
1N/A $kid = $kid->first->sibling; # skip pushmark
1N/A $obj = $kid;
1N/A $kid = $kid->sibling;
1N/A for (; not null $kid; $kid = $kid->sibling) {
1N/A push @exprs, $self->deparse($kid, 6);
1N/A }
1N/A } else {
1N/A $obj = $kid;
1N/A $kid = $kid->sibling;
1N/A for (; !null ($kid->sibling) && $kid->name ne "method_named";
1N/A $kid = $kid->sibling) {
1N/A push @exprs, $self->deparse($kid, 6);
1N/A }
1N/A $meth = $kid;
1N/A }
1N/A $obj = $self->deparse($obj, 24);
1N/A if ($meth->name eq "method_named") {
1N/A $meth = $self->const_sv($meth)->PV;
1N/A } else {
1N/A $meth = $meth->first;
1N/A if ($meth->name eq "const") {
1N/A # As of 5.005_58, this case is probably obsoleted by the
1N/A # method_named case above
1N/A $meth = $self->const_sv($meth)->PV; # needs to be bare
1N/A } else {
1N/A $meth = $self->deparse($meth, 1);
1N/A }
1N/A }
1N/A my $args = join(", ", @exprs);
1N/A $kid = $obj . "->" . $meth;
1N/A if (length $args) {
1N/A return $kid . "(" . $args . ")"; # parens mandatory
1N/A } else {
1N/A return $kid;
1N/A }
1N/A}
1N/A
1N/A# returns "&" if the prototype doesn't match the args,
1N/A# or ("", $args_after_prototype_demunging) if it does.
1N/Asub check_proto {
1N/A my $self = shift;
1N/A return "&" if $self->{'noproto'};
1N/A my($proto, @args) = @_;
1N/A my($arg, $real);
1N/A my $doneok = 0;
1N/A my @reals;
1N/A # An unbackslashed @ or % gobbles up the rest of the args
1N/A 1 while $proto =~ s/(?<!\\)([@%])[^\]]+$/$1/;
1N/A while ($proto) {
1N/A $proto =~ s/^(\\?[\$\@&%*]|\\\[[\$\@&%*]+\]|;)//;
1N/A my $chr = $1;
1N/A if ($chr eq "") {
1N/A return "&" if @args;
1N/A } elsif ($chr eq ";") {
1N/A $doneok = 1;
1N/A } elsif ($chr eq "@" or $chr eq "%") {
1N/A push @reals, map($self->deparse($_, 6), @args);
1N/A @args = ();
1N/A } else {
1N/A $arg = shift @args;
1N/A last unless $arg;
1N/A if ($chr eq "\$") {
1N/A if (want_scalar $arg) {
1N/A push @reals, $self->deparse($arg, 6);
1N/A } else {
1N/A return "&";
1N/A }
1N/A } elsif ($chr eq "&") {
1N/A if ($arg->name =~ /^(s?refgen|undef)$/) {
1N/A push @reals, $self->deparse($arg, 6);
1N/A } else {
1N/A return "&";
1N/A }
1N/A } elsif ($chr eq "*") {
1N/A if ($arg->name =~ /^s?refgen$/
1N/A and $arg->first->first->name eq "rv2gv")
1N/A {
1N/A $real = $arg->first->first; # skip refgen, null
1N/A if ($real->first->name eq "gv") {
1N/A push @reals, $self->deparse($real, 6);
1N/A } else {
1N/A push @reals, $self->deparse($real->first, 6);
1N/A }
1N/A } else {
1N/A return "&";
1N/A }
1N/A } elsif (substr($chr, 0, 1) eq "\\") {
1N/A $chr =~ tr/\\[]//d;
1N/A if ($arg->name =~ /^s?refgen$/ and
1N/A !null($real = $arg->first) and
1N/A ($chr =~ /\$/ && is_scalar($real->first)
1N/A or ($chr =~ /@/
1N/A && class($real->first->sibling) ne 'NULL'
1N/A && $real->first->sibling->name
1N/A =~ /^(rv2|pad)av$/)
1N/A or ($chr =~ /%/
1N/A && class($real->first->sibling) ne 'NULL'
1N/A && $real->first->sibling->name
1N/A =~ /^(rv2|pad)hv$/)
1N/A #or ($chr =~ /&/ # This doesn't work
1N/A # && $real->first->name eq "rv2cv")
1N/A or ($chr =~ /\*/
1N/A && $real->first->name eq "rv2gv")))
1N/A {
1N/A push @reals, $self->deparse($real, 6);
1N/A } else {
1N/A return "&";
1N/A }
1N/A }
1N/A }
1N/A }
1N/A return "&" if $proto and !$doneok; # too few args and no `;'
1N/A return "&" if @args; # too many args
1N/A return ("", join ", ", @reals);
1N/A}
1N/A
1N/Asub pp_entersub {
1N/A my $self = shift;
1N/A my($op, $cx) = @_;
1N/A return $self->method($op, $cx) unless null $op->first->sibling;
1N/A my $prefix = "";
1N/A my $amper = "";
1N/A my($kid, @exprs);
1N/A if ($op->flags & OPf_SPECIAL && !($op->flags & OPf_MOD)) {
1N/A $prefix = "do ";
1N/A } elsif ($op->private & OPpENTERSUB_AMPER) {
1N/A $amper = "&";
1N/A }
1N/A $kid = $op->first;
1N/A $kid = $kid->first->sibling; # skip ex-list, pushmark
1N/A for (; not null $kid->sibling; $kid = $kid->sibling) {
1N/A push @exprs, $kid;
1N/A }
1N/A my $simple = 0;
1N/A my $proto = undef;
1N/A if (is_scope($kid)) {
1N/A $amper = "&";
1N/A $kid = "{" . $self->deparse($kid, 0) . "}";
1N/A } elsif ($kid->first->name eq "gv") {
1N/A my $gv = $self->gv_or_padgv($kid->first);
1N/A if (class($gv->CV) ne "SPECIAL") {
1N/A $proto = $gv->CV->PV if $gv->CV->FLAGS & SVf_POK;
1N/A }
1N/A $simple = 1; # only calls of named functions can be prototyped
1N/A $kid = $self->deparse($kid, 24);
1N/A } elsif (is_scalar ($kid->first) && $kid->first->name ne 'rv2cv') {
1N/A $amper = "&";
1N/A $kid = $self->deparse($kid, 24);
1N/A } else {
1N/A $prefix = "";
1N/A my $arrow = is_subscriptable($kid->first) ? "" : "->";
1N/A $kid = $self->deparse($kid, 24) . $arrow;
1N/A }
1N/A
1N/A # Doesn't matter how many prototypes there are, if
1N/A # they haven't happened yet!
1N/A my $declared;
1N/A {
1N/A no strict 'refs';
1N/A no warnings 'uninitialized';
1N/A $declared = exists $self->{'subs_declared'}{$kid}
1N/A || (
1N/A defined &{ %{$self->{'curstash'}."::"}->{$kid} }
1N/A && !exists
1N/A $self->{'subs_deparsed'}{$self->{'curstash'}."::".$kid}
1N/A && defined prototype $self->{'curstash'}."::".$kid
1N/A );
1N/A if (!$declared && defined($proto)) {
1N/A # Avoid "too early to check prototype" warning
1N/A ($amper, $proto) = ('&');
1N/A }
1N/A }
1N/A
1N/A my $args;
1N/A if ($declared and defined $proto and not $amper) {
1N/A ($amper, $args) = $self->check_proto($proto, @exprs);
1N/A if ($amper eq "&") {
1N/A $args = join(", ", map($self->deparse($_, 6), @exprs));
1N/A }
1N/A } else {
1N/A $args = join(", ", map($self->deparse($_, 6), @exprs));
1N/A }
1N/A if ($prefix or $amper) {
1N/A if ($op->flags & OPf_STACKED) {
1N/A return $prefix . $amper . $kid . "(" . $args . ")";
1N/A } else {
1N/A return $prefix . $amper. $kid;
1N/A }
1N/A } else {
1N/A # glob() invocations can be translated into calls of
1N/A # CORE::GLOBAL::glob with a second parameter, a number.
1N/A # Reverse this.
1N/A if ($kid eq "CORE::GLOBAL::glob") {
1N/A $kid = "glob";
1N/A $args =~ s/\s*,[^,]+$//;
1N/A }
1N/A
1N/A # It's a syntax error to call CORE::GLOBAL::foo without a prefix,
1N/A # so it must have been translated from a keyword call. Translate
1N/A # it back.
1N/A $kid =~ s/^CORE::GLOBAL:://;
1N/A
1N/A my $dproto = defined($proto) ? $proto : "undefined";
1N/A if (!$declared) {
1N/A return "$kid(" . $args . ")";
1N/A } elsif ($dproto eq "") {
1N/A return $kid;
1N/A } elsif ($dproto eq "\$" and is_scalar($exprs[0])) {
1N/A # is_scalar is an excessively conservative test here:
1N/A # really, we should be comparing to the precedence of the
1N/A # top operator of $exprs[0] (ala unop()), but that would
1N/A # take some major code restructuring to do right.
1N/A return $self->maybe_parens_func($kid, $args, $cx, 16);
1N/A } elsif ($dproto ne '$' and defined($proto) || $simple) { #'
1N/A return $self->maybe_parens_func($kid, $args, $cx, 5);
1N/A } else {
1N/A return "$kid(" . $args . ")";
1N/A }
1N/A }
1N/A}
1N/A
1N/Asub pp_enterwrite { unop(@_, "write") }
1N/A
1N/A# escape things that cause interpolation in double quotes,
1N/A# but not character escapes
1N/Asub uninterp {
1N/A my($str) = @_;
1N/A $str =~ s/(^|\G|[^\\])((?:\\\\)*)([\$\@]|\\[uUlLQE])/$1$2\\$3/g;
1N/A return $str;
1N/A}
1N/A
1N/A{
1N/Amy $bal;
1N/ABEGIN {
1N/A use re "eval";
1N/A # Matches any string which is balanced with respect to {braces}
1N/A $bal = qr(
1N/A (?:
1N/A [^\\{}]
1N/A | \\\\
1N/A | \\[{}]
1N/A | \{(??{$bal})\}
1N/A )*
1N/A )x;
1N/A}
1N/A
1N/A# the same, but treat $|, $), $( and $ at the end of the string differently
1N/Asub re_uninterp {
1N/A my($str) = @_;
1N/A
1N/A $str =~ s/
1N/A ( ^|\G # $1
1N/A | [^\\]
1N/A )
1N/A
1N/A ( # $2
1N/A (?:\\\\)*
1N/A )
1N/A
1N/A ( # $3
1N/A (\(\?\??\{$bal\}\)) # $4
1N/A | [\$\@]
1N/A (?!\||\)|\(|$)
1N/A | \\[uUlLQE]
1N/A )
1N/A
1N/A /defined($4) && length($4) ? "$1$2$4" : "$1$2\\$3"/xeg;
1N/A
1N/A return $str;
1N/A}
1N/A
1N/A# This is for regular expressions with the /x modifier
1N/A# We have to leave comments unmangled.
1N/Asub re_uninterp_extended {
1N/A my($str) = @_;
1N/A
1N/A $str =~ s/
1N/A ( ^|\G # $1
1N/A | [^\\]
1N/A )
1N/A
1N/A ( # $2
1N/A (?:\\\\)*
1N/A )
1N/A
1N/A ( # $3
1N/A ( \(\?\??\{$bal\}\) # $4 (skip over (?{}) and (??{}) blocks)
1N/A | \#[^\n]* # (skip over comments)
1N/A )
1N/A | [\$\@]
1N/A (?!\||\)|\(|$|\s)
1N/A | \\[uUlLQE]
1N/A )
1N/A
1N/A /defined($4) && length($4) ? "$1$2$4" : "$1$2\\$3"/xeg;
1N/A
1N/A return $str;
1N/A}
1N/A}
1N/A
1N/Amy %unctrl = # portable to to EBCDIC
1N/A (
1N/A "\c@" => '\c@', # unused
1N/A "\cA" => '\cA',
1N/A "\cB" => '\cB',
1N/A "\cC" => '\cC',
1N/A "\cD" => '\cD',
1N/A "\cE" => '\cE',
1N/A "\cF" => '\cF',
1N/A "\cG" => '\cG',
1N/A "\cH" => '\cH',
1N/A "\cI" => '\cI',
1N/A "\cJ" => '\cJ',
1N/A "\cK" => '\cK',
1N/A "\cL" => '\cL',
1N/A "\cM" => '\cM',
1N/A "\cN" => '\cN',
1N/A "\cO" => '\cO',
1N/A "\cP" => '\cP',
1N/A "\cQ" => '\cQ',
1N/A "\cR" => '\cR',
1N/A "\cS" => '\cS',
1N/A "\cT" => '\cT',
1N/A "\cU" => '\cU',
1N/A "\cV" => '\cV',
1N/A "\cW" => '\cW',
1N/A "\cX" => '\cX',
1N/A "\cY" => '\cY',
1N/A "\cZ" => '\cZ',
1N/A "\c[" => '\c[', # unused
1N/A "\c\\" => '\c\\', # unused
1N/A "\c]" => '\c]', # unused
1N/A "\c_" => '\c_', # unused
1N/A );
1N/A
1N/A# character escapes, but not delimiters that might need to be escaped
1N/Asub escape_str { # ASCII, UTF8
1N/A my($str) = @_;
1N/A $str =~ s/(.)/ord($1) > 255 ? sprintf("\\x{%x}", ord($1)) : $1/eg;
1N/A $str =~ s/\a/\\a/g;
1N/A# $str =~ s/\cH/\\b/g; # \b means something different in a regex
1N/A $str =~ s/\t/\\t/g;
1N/A $str =~ s/\n/\\n/g;
1N/A $str =~ s/\e/\\e/g;
1N/A $str =~ s/\f/\\f/g;
1N/A $str =~ s/\r/\\r/g;
1N/A $str =~ s/([\cA-\cZ])/$unctrl{$1}/ge;
1N/A $str =~ s/([[:^print:]])/sprintf("\\%03o", ord($1))/ge;
1N/A return $str;
1N/A}
1N/A
1N/A# For regexes with the /x modifier.
1N/A# Leave whitespace unmangled.
1N/Asub escape_extended_re {
1N/A my($str) = @_;
1N/A $str =~ s/(.)/ord($1) > 255 ? sprintf("\\x{%x}", ord($1)) : $1/eg;
1N/A $str =~ s/([[:^print:]])/
1N/A ($1 =~ y! \t\n!!) ? $1 : sprintf("\\%03o", ord($1))/ge;
1N/A $str =~ s/\n/\n\f/g;
1N/A return $str;
1N/A}
1N/A
1N/A# Don't do this for regexen
1N/Asub unback {
1N/A my($str) = @_;
1N/A $str =~ s/\\/\\\\/g;
1N/A return $str;
1N/A}
1N/A
1N/A# Remove backslashes which precede literal control characters,
1N/A# to avoid creating ambiguity when we escape the latter.
1N/Asub re_unback {
1N/A my($str) = @_;
1N/A
1N/A # the insane complexity here is due to the behaviour of "\c\"
1N/A $str =~ s/(^|[^\\]|\\c\\)(?<!\\c)\\(\\\\)*(?=[[:^print:]])/$1$2/g;
1N/A return $str;
1N/A}
1N/A
1N/Asub balanced_delim {
1N/A my($str) = @_;
1N/A my @str = split //, $str;
1N/A my($ar, $open, $close, $fail, $c, $cnt);
1N/A for $ar (['[',']'], ['(',')'], ['<','>'], ['{','}']) {
1N/A ($open, $close) = @$ar;
1N/A $fail = 0; $cnt = 0;
1N/A for $c (@str) {
1N/A if ($c eq $open) {
1N/A $cnt++;
1N/A } elsif ($c eq $close) {
1N/A $cnt--;
1N/A if ($cnt < 0) {
1N/A # qq()() isn't ")("
1N/A $fail = 1;
1N/A last;
1N/A }
1N/A }
1N/A }
1N/A $fail = 1 if $cnt != 0;
1N/A return ($open, "$open$str$close") if not $fail;
1N/A }
1N/A return ("", $str);
1N/A}
1N/A
1N/Asub single_delim {
1N/A my($q, $default, $str) = @_;
1N/A return "$default$str$default" if $default and index($str, $default) == -1;
1N/A if ($q ne 'qr') {
1N/A (my $succeed, $str) = balanced_delim($str);
1N/A return "$q$str" if $succeed;
1N/A }
1N/A for my $delim ('/', '"', '#') {
1N/A return "$q$delim" . $str . $delim if index($str, $delim) == -1;
1N/A }
1N/A if ($default) {
1N/A $str =~ s/$default/\\$default/g;
1N/A return "$default$str$default";
1N/A } else {
1N/A $str =~ s[/][\\/]g;
1N/A return "$q/$str/";
1N/A }
1N/A}
1N/A
1N/Amy $max_prec;
1N/ABEGIN { $max_prec = int(0.999 + 8*length(pack("F", 42))*log(2)/log(10)); }
1N/A
1N/A# Split a floating point number into an integer mantissa and a binary
1N/A# exponent. Assumes you've already made sure the number isn't zero or
1N/A# some weird infinity or NaN.
1N/Asub split_float {
1N/A my($f) = @_;
1N/A my $exponent = 0;
1N/A if ($f == int($f)) {
1N/A while ($f % 2 == 0) {
1N/A $f /= 2;
1N/A $exponent++;
1N/A }
1N/A } else {
1N/A while ($f != int($f)) {
1N/A $f *= 2;
1N/A $exponent--;
1N/A }
1N/A }
1N/A my $mantissa = sprintf("%.0f", $f);
1N/A return ($mantissa, $exponent);
1N/A}
1N/A
1N/Asub const {
1N/A my $self = shift;
1N/A my($sv, $cx) = @_;
1N/A if ($self->{'use_dumper'}) {
1N/A return $self->const_dumper($sv, $cx);
1N/A }
1N/A if (class($sv) eq "SPECIAL") {
1N/A # sv_undef, sv_yes, sv_no
1N/A return ('undef', '1', $self->maybe_parens("!1", $cx, 21))[$$sv-1];
1N/A } elsif (class($sv) eq "NULL") {
1N/A return 'undef';
1N/A }
1N/A # convert a version object into the "v1.2.3" string in its V magic
1N/A if ($sv->FLAGS & SVs_RMG) {
1N/A for (my $mg = $sv->MAGIC; $mg; $mg = $mg->MOREMAGIC) {
1N/A return $mg->PTR if $mg->TYPE eq 'V';
1N/A }
1N/A }
1N/A
1N/A if ($sv->FLAGS & SVf_IOK) {
1N/A my $str = $sv->int_value;
1N/A $str = $self->maybe_parens($str, $cx, 21) if $str < 0;
1N/A return $str;
1N/A } elsif ($sv->FLAGS & SVf_NOK) {
1N/A my $nv = $sv->NV;
1N/A if ($nv == 0) {
1N/A if (pack("F", $nv) eq pack("F", 0)) {
1N/A # positive zero
1N/A return "0";
1N/A } else {
1N/A # negative zero
1N/A return $self->maybe_parens("-.0", $cx, 21);
1N/A }
1N/A } elsif (1/$nv == 0) {
1N/A if ($nv > 0) {
1N/A # positive infinity
1N/A return $self->maybe_parens("9**9**9", $cx, 22);
1N/A } else {
1N/A # negative infinity
1N/A return $self->maybe_parens("-9**9**9", $cx, 21);
1N/A }
1N/A } elsif ($nv != $nv) {
1N/A # NaN
1N/A if (pack("F", $nv) eq pack("F", sin(9**9**9))) {
1N/A # the normal kind
1N/A return "sin(9**9**9)";
1N/A } elsif (pack("F", $nv) eq pack("F", -sin(9**9**9))) {
1N/A # the inverted kind
1N/A return $self->maybe_parens("-sin(9**9**9)", $cx, 21);
1N/A } else {
1N/A # some other kind
1N/A my $hex = unpack("h*", pack("F", $nv));
1N/A return qq'unpack("F", pack("h*", "$hex"))';
1N/A }
1N/A }
1N/A # first, try the default stringification
1N/A my $str = "$nv";
1N/A if ($str != $nv) {
1N/A # failing that, try using more precision
1N/A $str = sprintf("%.${max_prec}g", $nv);
1N/A# if (pack("F", $str) ne pack("F", $nv)) {
1N/A if ($str != $nv) {
1N/A # not representable in decimal with whatever sprintf()
1N/A # and atof() Perl is using here.
1N/A my($mant, $exp) = split_float($nv);
1N/A return $self->maybe_parens("$mant * 2**$exp", $cx, 19);
1N/A }
1N/A }
1N/A $str = $self->maybe_parens($str, $cx, 21) if $nv < 0;
1N/A return $str;
1N/A } elsif ($sv->FLAGS & SVf_ROK && $sv->can("RV")) {
1N/A my $ref = $sv->RV;
1N/A if (class($ref) eq "AV") {
1N/A return "[" . $self->list_const(2, $ref->ARRAY) . "]";
1N/A } elsif (class($ref) eq "HV") {
1N/A my %hash = $ref->ARRAY;
1N/A my @elts;
1N/A for my $k (sort keys %hash) {
1N/A push @elts, "$k => " . $self->const($hash{$k}, 6);
1N/A }
1N/A return "{" . join(", ", @elts) . "}";
1N/A } elsif (class($ref) eq "CV") {
1N/A return "sub " . $self->deparse_sub($ref);
1N/A }
1N/A if ($ref->FLAGS & SVs_SMG) {
1N/A for (my $mg = $ref->MAGIC; $mg; $mg = $mg->MOREMAGIC) {
1N/A if ($mg->TYPE eq 'r') {
1N/A my $re = re_uninterp(escape_str(re_unback($mg->precomp)));
1N/A return single_delim("qr", "", $re);
1N/A }
1N/A }
1N/A }
1N/A
1N/A return $self->maybe_parens("\\" . $self->const($ref, 20), $cx, 20);
1N/A } elsif ($sv->FLAGS & SVf_POK) {
1N/A my $str = $sv->PV;
1N/A if ($str =~ /[^ -~]/) { # ASCII for non-printing
1N/A return single_delim("qq", '"', uninterp escape_str unback $str);
1N/A } else {
1N/A return single_delim("q", "'", unback $str);
1N/A }
1N/A } else {
1N/A return "undef";
1N/A }
1N/A}
1N/A
1N/Asub const_dumper {
1N/A my $self = shift;
1N/A my($sv, $cx) = @_;
1N/A my $ref = $sv->object_2svref();
1N/A my $dumper = Data::Dumper->new([$$ref], ['$v']);
1N/A $dumper->Purity(1)->Terse(1)->Deparse(1)->Indent(0)->Useqq(1)->Sortkeys(1);
1N/A my $str = $dumper->Dump();
1N/A if ($str =~ /^\$v/) {
1N/A return '${my ' . $str . ' \$v}';
1N/A } else {
1N/A return $str;
1N/A }
1N/A}
1N/A
1N/Asub const_sv {
1N/A my $self = shift;
1N/A my $op = shift;
1N/A my $sv = $op->sv;
1N/A # the constant could be in the pad (under useithreads)
1N/A $sv = $self->padval($op->targ) unless $$sv;
1N/A return $sv;
1N/A}
1N/A
1N/Asub pp_const {
1N/A my $self = shift;
1N/A my($op, $cx) = @_;
1N/A if ($op->private & OPpCONST_ARYBASE) {
1N/A return '$[';
1N/A }
1N/A# if ($op->private & OPpCONST_BARE) { # trouble with `=>' autoquoting
1N/A# return $self->const_sv($op)->PV;
1N/A# }
1N/A my $sv = $self->const_sv($op);
1N/A return $self->const($sv, $cx);
1N/A}
1N/A
1N/Asub dq {
1N/A my $self = shift;
1N/A my $op = shift;
1N/A my $type = $op->name;
1N/A if ($type eq "const") {
1N/A return '$[' if $op->private & OPpCONST_ARYBASE;
1N/A return uninterp(escape_str(unback($self->const_sv($op)->as_string)));
1N/A } elsif ($type eq "concat") {
1N/A my $first = $self->dq($op->first);
1N/A my $last = $self->dq($op->last);
1N/A
1N/A # Disambiguate "${foo}bar", "${foo}{bar}", "${foo}[1]", "$foo\::bar"
1N/A ($last =~ /^[A-Z\\\^\[\]_?]/ &&
1N/A $first =~ s/([\$@])\^$/${1}{^}/) # "${^}W" etc
1N/A || ($last =~ /^[:'{\[\w_]/ && #'
1N/A $first =~ s/([\$@])([A-Za-z_]\w*)$/${1}{$2}/);
1N/A
1N/A return $first . $last;
1N/A } elsif ($type eq "uc") {
1N/A return '\U' . $self->dq($op->first->sibling) . '\E';
1N/A } elsif ($type eq "lc") {
1N/A return '\L' . $self->dq($op->first->sibling) . '\E';
1N/A } elsif ($type eq "ucfirst") {
1N/A return '\u' . $self->dq($op->first->sibling);
1N/A } elsif ($type eq "lcfirst") {
1N/A return '\l' . $self->dq($op->first->sibling);
1N/A } elsif ($type eq "quotemeta") {
1N/A return '\Q' . $self->dq($op->first->sibling) . '\E';
1N/A } elsif ($type eq "join") {
1N/A return $self->deparse($op->last, 26); # was join($", @ary)
1N/A } else {
1N/A return $self->deparse($op, 26);
1N/A }
1N/A}
1N/A
1N/Asub pp_backtick {
1N/A my $self = shift;
1N/A my($op, $cx) = @_;
1N/A # skip pushmark
1N/A return single_delim("qx", '`', $self->dq($op->first->sibling));
1N/A}
1N/A
1N/Asub dquote {
1N/A my $self = shift;
1N/A my($op, $cx) = @_;
1N/A my $kid = $op->first->sibling; # skip ex-stringify, pushmark
1N/A return $self->deparse($kid, $cx) if $self->{'unquote'};
1N/A $self->maybe_targmy($kid, $cx,
1N/A sub {single_delim("qq", '"', $self->dq($_[1]))});
1N/A}
1N/A
1N/A# OP_STRINGIFY is a listop, but it only ever has one arg
1N/Asub pp_stringify { maybe_targmy(@_, \&dquote) }
1N/A
1N/A# tr/// and s/// (and tr[][], tr[]//, tr###, etc)
1N/A# note that tr(from)/to/ is OK, but not tr/from/(to)
1N/Asub double_delim {
1N/A my($from, $to) = @_;
1N/A my($succeed, $delim);
1N/A if ($from !~ m[/] and $to !~ m[/]) {
1N/A return "/$from/$to/";
1N/A } elsif (($succeed, $from) = balanced_delim($from) and $succeed) {
1N/A if (($succeed, $to) = balanced_delim($to) and $succeed) {
1N/A return "$from$to";
1N/A } else {
1N/A for $delim ('/', '"', '#') { # note no `'' -- s''' is special
1N/A return "$from$delim$to$delim" if index($to, $delim) == -1;
1N/A }
1N/A $to =~ s[/][\\/]g;
1N/A return "$from/$to/";
1N/A }
1N/A } else {
1N/A for $delim ('/', '"', '#') { # note no '
1N/A return "$delim$from$delim$to$delim"
1N/A if index($to . $from, $delim) == -1;
1N/A }
1N/A $from =~ s[/][\\/]g;
1N/A $to =~ s[/][\\/]g;
1N/A return "/$from/$to/";
1N/A }
1N/A}
1N/A
1N/A# Only used by tr///, so backslashes hyphens
1N/Asub pchr { # ASCII
1N/A my($n) = @_;
1N/A if ($n == ord '\\') {
1N/A return '\\\\';
1N/A } elsif ($n == ord "-") {
1N/A return "\\-";
1N/A } elsif ($n >= ord(' ') and $n <= ord('~')) {
1N/A return chr($n);
1N/A } elsif ($n == ord "\a") {
1N/A return '\\a';
1N/A } elsif ($n == ord "\b") {
1N/A return '\\b';
1N/A } elsif ($n == ord "\t") {
1N/A return '\\t';
1N/A } elsif ($n == ord "\n") {
1N/A return '\\n';
1N/A } elsif ($n == ord "\e") {
1N/A return '\\e';
1N/A } elsif ($n == ord "\f") {
1N/A return '\\f';
1N/A } elsif ($n == ord "\r") {
1N/A return '\\r';
1N/A } elsif ($n >= ord("\cA") and $n <= ord("\cZ")) {
1N/A return '\\c' . chr(ord("@") + $n);
1N/A } else {
1N/A# return '\x' . sprintf("%02x", $n);
1N/A return '\\' . sprintf("%03o", $n);
1N/A }
1N/A}
1N/A
1N/Asub collapse {
1N/A my(@chars) = @_;
1N/A my($str, $c, $tr) = ("");
1N/A for ($c = 0; $c < @chars; $c++) {
1N/A $tr = $chars[$c];
1N/A $str .= pchr($tr);
1N/A if ($c <= $#chars - 2 and $chars[$c + 1] == $tr + 1 and
1N/A $chars[$c + 2] == $tr + 2)
1N/A {
1N/A for (; $c <= $#chars-1 and $chars[$c + 1] == $chars[$c] + 1; $c++)
1N/A {}
1N/A $str .= "-";
1N/A $str .= pchr($chars[$c]);
1N/A }
1N/A }
1N/A return $str;
1N/A}
1N/A
1N/Asub tr_decode_byte {
1N/A my($table, $flags) = @_;
1N/A my(@table) = unpack("s*", $table);
1N/A splice @table, 0x100, 1; # Number of subsequent elements
1N/A my($c, $tr, @from, @to, @delfrom, $delhyphen);
1N/A if ($table[ord "-"] != -1 and
1N/A $table[ord("-") - 1] == -1 || $table[ord("-") + 1] == -1)
1N/A {
1N/A $tr = $table[ord "-"];
1N/A $table[ord "-"] = -1;
1N/A if ($tr >= 0) {
1N/A @from = ord("-");
1N/A @to = $tr;
1N/A } else { # -2 ==> delete
1N/A $delhyphen = 1;
1N/A }
1N/A }
1N/A for ($c = 0; $c < @table; $c++) {
1N/A $tr = $table[$c];
1N/A if ($tr >= 0) {
1N/A push @from, $c; push @to, $tr;
1N/A } elsif ($tr == -2) {
1N/A push @delfrom, $c;
1N/A }
1N/A }
1N/A @from = (@from, @delfrom);
1N/A if ($flags & OPpTRANS_COMPLEMENT) {
1N/A my @newfrom = ();
1N/A my %from;
1N/A @from{@from} = (1) x @from;
1N/A for ($c = 0; $c < 256; $c++) {
1N/A push @newfrom, $c unless $from{$c};
1N/A }
1N/A @from = @newfrom;
1N/A }
1N/A unless ($flags & OPpTRANS_DELETE || !@to) {
1N/A pop @to while $#to and $to[$#to] == $to[$#to -1];
1N/A }
1N/A my($from, $to);
1N/A $from = collapse(@from);
1N/A $to = collapse(@to);
1N/A $from .= "-" if $delhyphen;
1N/A return ($from, $to);
1N/A}
1N/A
1N/Asub tr_chr {
1N/A my $x = shift;
1N/A if ($x == ord "-") {
1N/A return "\\-";
1N/A } elsif ($x == ord "\\") {
1N/A return "\\\\";
1N/A } else {
1N/A return chr $x;
1N/A }
1N/A}
1N/A
1N/A# XXX This doesn't yet handle all cases correctly either
1N/A
1N/Asub tr_decode_utf8 {
1N/A my($swash_hv, $flags) = @_;
1N/A my %swash = $swash_hv->ARRAY;
1N/A my $final = undef;
1N/A $final = $swash{'FINAL'}->IV if exists $swash{'FINAL'};
1N/A my $none = $swash{"NONE"}->IV;
1N/A my $extra = $none + 1;
1N/A my(@from, @delfrom, @to);
1N/A my $line;
1N/A foreach $line (split /\n/, $swash{'LIST'}->PV) {
1N/A my($min, $max, $result) = split(/\t/, $line);
1N/A $min = hex $min;
1N/A if (length $max) {
1N/A $max = hex $max;
1N/A } else {
1N/A $max = $min;
1N/A }
1N/A $result = hex $result;
1N/A if ($result == $extra) {
1N/A push @delfrom, [$min, $max];
1N/A } else {
1N/A push @from, [$min, $max];
1N/A push @to, [$result, $result + $max - $min];
1N/A }
1N/A }
1N/A for my $i (0 .. $#from) {
1N/A if ($from[$i][0] == ord '-') {
1N/A unshift @from, splice(@from, $i, 1);
1N/A unshift @to, splice(@to, $i, 1);
1N/A last;
1N/A } elsif ($from[$i][1] == ord '-') {
1N/A $from[$i][1]--;
1N/A $to[$i][1]--;
1N/A unshift @from, ord '-';
1N/A unshift @to, ord '-';
1N/A last;
1N/A }
1N/A }
1N/A for my $i (0 .. $#delfrom) {
1N/A if ($delfrom[$i][0] == ord '-') {
1N/A push @delfrom, splice(@delfrom, $i, 1);
1N/A last;
1N/A } elsif ($delfrom[$i][1] == ord '-') {
1N/A $delfrom[$i][1]--;
1N/A push @delfrom, ord '-';
1N/A last;
1N/A }
1N/A }
1N/A if (defined $final and $to[$#to][1] != $final) {
1N/A push @to, [$final, $final];
1N/A }
1N/A push @from, @delfrom;
1N/A if ($flags & OPpTRANS_COMPLEMENT) {
1N/A my @newfrom;
1N/A my $next = 0;
1N/A for my $i (0 .. $#from) {
1N/A push @newfrom, [$next, $from[$i][0] - 1];
1N/A $next = $from[$i][1] + 1;
1N/A }
1N/A @from = ();
1N/A for my $range (@newfrom) {
1N/A if ($range->[0] <= $range->[1]) {
1N/A push @from, $range;
1N/A }
1N/A }
1N/A }
1N/A my($from, $to, $diff);
1N/A for my $chunk (@from) {
1N/A $diff = $chunk->[1] - $chunk->[0];
1N/A if ($diff > 1) {
1N/A $from .= tr_chr($chunk->[0]) . "-" . tr_chr($chunk->[1]);
1N/A } elsif ($diff == 1) {
1N/A $from .= tr_chr($chunk->[0]) . tr_chr($chunk->[1]);
1N/A } else {
1N/A $from .= tr_chr($chunk->[0]);
1N/A }
1N/A }
1N/A for my $chunk (@to) {
1N/A $diff = $chunk->[1] - $chunk->[0];
1N/A if ($diff > 1) {
1N/A $to .= tr_chr($chunk->[0]) . "-" . tr_chr($chunk->[1]);
1N/A } elsif ($diff == 1) {
1N/A $to .= tr_chr($chunk->[0]) . tr_chr($chunk->[1]);
1N/A } else {
1N/A $to .= tr_chr($chunk->[0]);
1N/A }
1N/A }
1N/A #$final = sprintf("%04x", $final) if defined $final;
1N/A #$none = sprintf("%04x", $none) if defined $none;
1N/A #$extra = sprintf("%04x", $extra) if defined $extra;
1N/A #print STDERR "final: $final\n none: $none\nextra: $extra\n";
1N/A #print STDERR $swash{'LIST'}->PV;
1N/A return (escape_str($from), escape_str($to));
1N/A}
1N/A
1N/Asub pp_trans {
1N/A my $self = shift;
1N/A my($op, $cx) = @_;
1N/A my($from, $to);
1N/A if (class($op) eq "PVOP") {
1N/A ($from, $to) = tr_decode_byte($op->pv, $op->private);
1N/A } else { # class($op) eq "SVOP"
1N/A ($from, $to) = tr_decode_utf8($op->sv->RV, $op->private);
1N/A }
1N/A my $flags = "";
1N/A $flags .= "c" if $op->private & OPpTRANS_COMPLEMENT;
1N/A $flags .= "d" if $op->private & OPpTRANS_DELETE;
1N/A $to = "" if $from eq $to and $flags eq "";
1N/A $flags .= "s" if $op->private & OPpTRANS_SQUASH;
1N/A return "tr" . double_delim($from, $to) . $flags;
1N/A}
1N/A
1N/A# Like dq(), but different
1N/Asub re_dq {
1N/A my $self = shift;
1N/A my ($op, $extended) = @_;
1N/A
1N/A my $type = $op->name;
1N/A if ($type eq "const") {
1N/A return '$[' if $op->private & OPpCONST_ARYBASE;
1N/A my $unbacked = re_unback($self->const_sv($op)->as_string);
1N/A return re_uninterp_extended(escape_extended_re($unbacked))
1N/A if $extended;
1N/A return re_uninterp(escape_str($unbacked));
1N/A } elsif ($type eq "concat") {
1N/A my $first = $self->re_dq($op->first, $extended);
1N/A my $last = $self->re_dq($op->last, $extended);
1N/A
1N/A # Disambiguate "${foo}bar", "${foo}{bar}", "${foo}[1]"
1N/A ($last =~ /^[A-Z\\\^\[\]_?]/ &&
1N/A $first =~ s/([\$@])\^$/${1}{^}/) # "${^}W" etc
1N/A || ($last =~ /^[{\[\w_]/ &&
1N/A $first =~ s/([\$@])([A-Za-z_]\w*)$/${1}{$2}/);
1N/A
1N/A return $first . $last;
1N/A } elsif ($type eq "uc") {
1N/A return '\U' . $self->re_dq($op->first->sibling, $extended) . '\E';
1N/A } elsif ($type eq "lc") {
1N/A return '\L' . $self->re_dq($op->first->sibling, $extended) . '\E';
1N/A } elsif ($type eq "ucfirst") {
1N/A return '\u' . $self->re_dq($op->first->sibling, $extended);
1N/A } elsif ($type eq "lcfirst") {
1N/A return '\l' . $self->re_dq($op->first->sibling, $extended);
1N/A } elsif ($type eq "quotemeta") {
1N/A return '\Q' . $self->re_dq($op->first->sibling, $extended) . '\E';
1N/A } elsif ($type eq "join") {
1N/A return $self->deparse($op->last, 26); # was join($", @ary)
1N/A } else {
1N/A return $self->deparse($op, 26);
1N/A }
1N/A}
1N/A
1N/Asub pure_string {
1N/A my ($self, $op) = @_;
1N/A return 0 if null $op;
1N/A my $type = $op->name;
1N/A
1N/A if ($type eq 'const') {
1N/A return 1;
1N/A }
1N/A elsif ($type =~ /^[ul]c(first)?$/ || $type eq 'quotemeta') {
1N/A return $self->pure_string($op->first->sibling);
1N/A }
1N/A elsif ($type eq 'join') {
1N/A my $join_op = $op->first->sibling; # Skip pushmark
1N/A return 0 unless $join_op->name eq 'null' && $join_op->targ eq OP_RV2SV;
1N/A
1N/A my $gvop = $join_op->first;
1N/A return 0 unless $gvop->name eq 'gvsv';
1N/A return 0 unless '"' eq $self->gv_name($self->gv_or_padgv($gvop));
1N/A
1N/A return 0 unless ${$join_op->sibling} eq ${$op->last};
1N/A return 0 unless $op->last->name =~ /^(rv2|pad)av$/;
1N/A }
1N/A elsif ($type eq 'concat') {
1N/A return $self->pure_string($op->first)
1N/A && $self->pure_string($op->last);
1N/A }
1N/A elsif (is_scalar($op) || $type =~ /^[ah]elem$/) {
1N/A return 1;
1N/A }
1N/A elsif ($type eq "null" and $op->can('first') and not null $op->first and
1N/A $op->first->name eq "null" and $op->first->can('first')
1N/A and not null $op->first->first and
1N/A $op->first->first->name eq "aelemfast") {
1N/A return 1;
1N/A }
1N/A else {
1N/A return 0;
1N/A }
1N/A
1N/A return 1;
1N/A}
1N/A
1N/Asub regcomp {
1N/A my $self = shift;
1N/A my($op, $cx, $extended) = @_;
1N/A my $kid = $op->first;
1N/A $kid = $kid->first if $kid->name eq "regcmaybe";
1N/A $kid = $kid->first if $kid->name eq "regcreset";
1N/A return ($self->re_dq($kid, $extended), 1) if $self->pure_string($kid);
1N/A return ($self->deparse($kid, $cx), 0);
1N/A}
1N/A
1N/Asub pp_regcomp {
1N/A my ($self, $op, $cx) = @_;
1N/A return (($self->regcomp($op, $cx, 0))[0]);
1N/A}
1N/A
1N/A# osmic acid -- see osmium tetroxide
1N/A
1N/Amy %matchwords;
1N/Amap($matchwords{join "", sort split //, $_} = $_, 'cig', 'cog', 'cos', 'cogs',
1N/A 'cox', 'go', 'is', 'ism', 'iso', 'mig', 'mix', 'osmic', 'ox', 'sic',
1N/A 'sig', 'six', 'smog', 'so', 'soc', 'sog', 'xi');
1N/A
1N/Asub matchop {
1N/A my $self = shift;
1N/A my($op, $cx, $name, $delim) = @_;
1N/A my $kid = $op->first;
1N/A my ($binop, $var, $re) = ("", "", "");
1N/A if ($op->flags & OPf_STACKED) {
1N/A $binop = 1;
1N/A $var = $self->deparse($kid, 20);
1N/A $kid = $kid->sibling;
1N/A }
1N/A my $quote = 1;
1N/A my $extended = ($op->pmflags & PMf_EXTENDED);
1N/A if (null $kid) {
1N/A my $unbacked = re_unback($op->precomp);
1N/A if ($extended) {
1N/A $re = re_uninterp_extended(escape_extended_re($unbacked));
1N/A } else {
1N/A $re = re_uninterp(escape_str(re_unback($op->precomp)));
1N/A }
1N/A } elsif ($kid->name ne 'regcomp') {
1N/A carp("found ".$kid->name." where regcomp expected");
1N/A } else {
1N/A ($re, $quote) = $self->regcomp($kid, 21, $extended);
1N/A }
1N/A my $flags = "";
1N/A $flags .= "c" if $op->pmflags & PMf_CONTINUE;
1N/A $flags .= "g" if $op->pmflags & PMf_GLOBAL;
1N/A $flags .= "i" if $op->pmflags & PMf_FOLD;
1N/A $flags .= "m" if $op->pmflags & PMf_MULTILINE;
1N/A $flags .= "o" if $op->pmflags & PMf_KEEP;
1N/A $flags .= "s" if $op->pmflags & PMf_SINGLELINE;
1N/A $flags .= "x" if $op->pmflags & PMf_EXTENDED;
1N/A $flags = $matchwords{$flags} if $matchwords{$flags};
1N/A if ($op->pmflags & PMf_ONCE) { # only one kind of delimiter works here
1N/A $re =~ s/\?/\\?/g;
1N/A $re = "?$re?";
1N/A } elsif ($quote) {
1N/A $re = single_delim($name, $delim, $re);
1N/A }
1N/A $re = $re . $flags if $quote;
1N/A if ($binop) {
1N/A return $self->maybe_parens("$var =~ $re", $cx, 20);
1N/A } else {
1N/A return $re;
1N/A }
1N/A}
1N/A
1N/Asub pp_match { matchop(@_, "m", "/") }
1N/Asub pp_pushre { matchop(@_, "m", "/") }
1N/Asub pp_qr { matchop(@_, "qr", "") }
1N/A
1N/Asub pp_split {
1N/A my $self = shift;
1N/A my($op, $cx) = @_;
1N/A my($kid, @exprs, $ary, $expr);
1N/A $kid = $op->first;
1N/A
1N/A # For our kid (an OP_PUSHRE), pmreplroot is never actually the
1N/A # root of a replacement; it's either empty, or abused to point to
1N/A # the GV for an array we split into (an optimization to save
1N/A # assignment overhead). Depending on whether we're using ithreads,
1N/A # this OP* holds either a GV* or a PADOFFSET. Luckily, B.xs
1N/A # figures out for us which it is.
1N/A my $replroot = $kid->pmreplroot;
1N/A my $gv = 0;
1N/A if (ref($replroot) eq "B::GV") {
1N/A $gv = $replroot;
1N/A } elsif (!ref($replroot) and $replroot > 0) {
1N/A $gv = $self->padval($replroot);
1N/A }
1N/A $ary = $self->stash_variable('@', $self->gv_name($gv)) if $gv;
1N/A
1N/A for (; !null($kid); $kid = $kid->sibling) {
1N/A push @exprs, $self->deparse($kid, 6);
1N/A }
1N/A
1N/A # handle special case of split(), and split(" ") that compiles to /\s+/
1N/A $kid = $op->first;
1N/A if ($kid->flags & OPf_SPECIAL
1N/A && $exprs[0] eq '/\\s+/'
1N/A && $kid->pmflags & PMf_SKIPWHITE ) {
1N/A $exprs[0] = '" "';
1N/A }
1N/A
1N/A $expr = "split(" . join(", ", @exprs) . ")";
1N/A if ($ary) {
1N/A return $self->maybe_parens("$ary = $expr", $cx, 7);
1N/A } else {
1N/A return $expr;
1N/A }
1N/A}
1N/A
1N/A# oxime -- any of various compounds obtained chiefly by the action of
1N/A# hydroxylamine on aldehydes and ketones and characterized by the
1N/A# bivalent grouping C=NOH [Webster's Tenth]
1N/A
1N/Amy %substwords;
1N/Amap($substwords{join "", sort split //, $_} = $_, 'ego', 'egoism', 'em',
1N/A 'es', 'ex', 'exes', 'gee', 'go', 'goes', 'ie', 'ism', 'iso', 'me',
1N/A 'meese', 'meso', 'mig', 'mix', 'os', 'ox', 'oxime', 'see', 'seem',
1N/A 'seg', 'sex', 'sig', 'six', 'smog', 'sog', 'some', 'xi');
1N/A
1N/Asub pp_subst {
1N/A my $self = shift;
1N/A my($op, $cx) = @_;
1N/A my $kid = $op->first;
1N/A my($binop, $var, $re, $repl) = ("", "", "", "");
1N/A if ($op->flags & OPf_STACKED) {
1N/A $binop = 1;
1N/A $var = $self->deparse($kid, 20);
1N/A $kid = $kid->sibling;
1N/A }
1N/A my $flags = "";
1N/A if (null($op->pmreplroot)) {
1N/A $repl = $self->dq($kid);
1N/A $kid = $kid->sibling;
1N/A } else {
1N/A $repl = $op->pmreplroot->first; # skip substcont
1N/A while ($repl->name eq "entereval") {
1N/A $repl = $repl->first;
1N/A $flags .= "e";
1N/A }
1N/A if ($op->pmflags & PMf_EVAL) {
1N/A $repl = $self->deparse($repl->first, 0);
1N/A } else {
1N/A $repl = $self->dq($repl);
1N/A }
1N/A }
1N/A my $extended = ($op->pmflags & PMf_EXTENDED);
1N/A if (null $kid) {
1N/A my $unbacked = re_unback($op->precomp);
1N/A if ($extended) {
1N/A $re = re_uninterp_extended(escape_extended_re($unbacked));
1N/A }
1N/A else {
1N/A $re = re_uninterp(escape_str($unbacked));
1N/A }
1N/A } else {
1N/A ($re) = $self->regcomp($kid, 1, $extended);
1N/A }
1N/A $flags .= "e" if $op->pmflags & PMf_EVAL;
1N/A $flags .= "g" if $op->pmflags & PMf_GLOBAL;
1N/A $flags .= "i" if $op->pmflags & PMf_FOLD;
1N/A $flags .= "m" if $op->pmflags & PMf_MULTILINE;
1N/A $flags .= "o" if $op->pmflags & PMf_KEEP;
1N/A $flags .= "s" if $op->pmflags & PMf_SINGLELINE;
1N/A $flags .= "x" if $extended;
1N/A $flags = $substwords{$flags} if $substwords{$flags};
1N/A if ($binop) {
1N/A return $self->maybe_parens("$var =~ s"
1N/A . double_delim($re, $repl) . $flags,
1N/A $cx, 20);
1N/A } else {
1N/A return "s". double_delim($re, $repl) . $flags;
1N/A }
1N/A}
1N/A
1N/A1;
1N/A__END__
1N/A
1N/A=head1 NAME
1N/A
1N/AB::Deparse - Perl compiler backend to produce perl code
1N/A
1N/A=head1 SYNOPSIS
1N/A
1N/AB<perl> B<-MO=Deparse>[B<,-d>][B<,-f>I<FILE>][B<,-p>][B<,-q>][B<,-l>]
1N/A [B<,-s>I<LETTERS>][B<,-x>I<LEVEL>] I<prog.pl>
1N/A
1N/A=head1 DESCRIPTION
1N/A
1N/AB::Deparse is a backend module for the Perl compiler that generates
1N/Aperl source code, based on the internal compiled structure that perl
1N/Aitself creates after parsing a program. The output of B::Deparse won't
1N/Abe exactly the same as the original source, since perl doesn't keep
1N/Atrack of comments or whitespace, and there isn't a one-to-one
1N/Acorrespondence between perl's syntactical constructions and their
1N/Acompiled form, but it will often be close. When you use the B<-p>
1N/Aoption, the output also includes parentheses even when they are not
1N/Arequired by precedence, which can make it easy to see if perl is
1N/Aparsing your expressions the way you intended.
1N/A
1N/AWhile B::Deparse goes to some lengths to try to figure out what your
1N/Aoriginal program was doing, some parts of the language can still trip
1N/Ait up; it still fails even on some parts of Perl's own test suite. If
1N/Ayou encounter a failure other than the most common ones described in
1N/Athe BUGS section below, you can help contribute to B::Deparse's
1N/Aongoing development by submitting a bug report with a small
1N/Aexample.
1N/A
1N/A=head1 OPTIONS
1N/A
1N/AAs with all compiler backend options, these must follow directly after
1N/Athe '-MO=Deparse', separated by a comma but not any white space.
1N/A
1N/A=over 4
1N/A
1N/A=item B<-d>
1N/A
1N/AOutput data values (when they appear as constants) using Data::Dumper.
1N/AWithout this option, B::Deparse will use some simple routines of its
1N/Aown for the same purpose. Currently, Data::Dumper is better for some
1N/Akinds of data (such as complex structures with sharing and
1N/Aself-reference) while the built-in routines are better for others
1N/A(such as odd floating-point values).
1N/A
1N/A=item B<-f>I<FILE>
1N/A
1N/ANormally, B::Deparse deparses the main code of a program, and all the subs
1N/Adefined in the same file. To include subs defined in other files, pass the
1N/AB<-f> option with the filename. You can pass the B<-f> option several times, to
1N/Ainclude more than one secondary file. (Most of the time you don't want to
1N/Ause it at all.) You can also use this option to include subs which are
1N/Adefined in the scope of a B<#line> directive with two parameters.
1N/A
1N/A=item B<-l>
1N/A
1N/AAdd '#line' declarations to the output based on the line and file
1N/Alocations of the original code.
1N/A
1N/A=item B<-p>
1N/A
1N/APrint extra parentheses. Without this option, B::Deparse includes
1N/Aparentheses in its output only when they are needed, based on the
1N/Astructure of your program. With B<-p>, it uses parentheses (almost)
1N/Awhenever they would be legal. This can be useful if you are used to
1N/ALISP, or if you want to see how perl parses your input. If you say
1N/A
1N/A if ($var & 0x7f == 65) {print "Gimme an A!"}
1N/A print ($which ? $a : $b), "\n";
1N/A $name = $ENV{USER} or "Bob";
1N/A
1N/AC<B::Deparse,-p> will print
1N/A
1N/A if (($var & 0)) {
1N/A print('Gimme an A!')
1N/A };
1N/A (print(($which ? $a : $b)), '???');
1N/A (($name = $ENV{'USER'}) or '???')
1N/A
1N/Awhich probably isn't what you intended (the C<'???'> is a sign that
1N/Aperl optimized away a constant value).
1N/A
1N/A=item B<-P>
1N/A
1N/ADisable prototype checking. With this option, all function calls are
1N/Adeparsed as if no prototype was defined for them. In other words,
1N/A
1N/A perl -MO=Deparse,-P -e 'sub foo (\@) { 1 } foo @x'
1N/A
1N/Awill print
1N/A
1N/A sub foo (\@) {
1N/A 1;
1N/A }
1N/A &foo(\@x);
1N/A
1N/Amaking clear how the parameters are actually passed to C<foo>.
1N/A
1N/A=item B<-q>
1N/A
1N/AExpand double-quoted strings into the corresponding combinations of
1N/Aconcatenation, uc, ucfirst, lc, lcfirst, quotemeta, and join. For
1N/Ainstance, print
1N/A
1N/A print "Hello, $world, @ladies, \u$gentlemen\E, \u\L$me!";
1N/A
1N/Aas
1N/A
1N/A print 'Hello, ' . $world . ', ' . join($", @ladies) . ', '
1N/A . ucfirst($gentlemen) . ', ' . ucfirst(lc $me . '!');
1N/A
1N/ANote that the expanded form represents the way perl handles such
1N/Aconstructions internally -- this option actually turns off the reverse
1N/Atranslation that B::Deparse usually does. On the other hand, note that
1N/AC<$x = "$y"> is not the same as C<$x = $y>: the former makes the value
1N/Aof $y into a string before doing the assignment.
1N/A
1N/A=item B<-s>I<LETTERS>
1N/A
1N/ATweak the style of B::Deparse's output. The letters should follow
1N/Adirectly after the 's', with no space or punctuation. The following
1N/Aoptions are available:
1N/A
1N/A=over 4
1N/A
1N/A=item B<C>
1N/A
1N/ACuddle C<elsif>, C<else>, and C<continue> blocks. For example, print
1N/A
1N/A if (...) {
1N/A ...
1N/A } else {
1N/A ...
1N/A }
1N/A
1N/Ainstead of
1N/A
1N/A if (...) {
1N/A ...
1N/A }
1N/A else {
1N/A ...
1N/A }
1N/A
1N/AThe default is not to cuddle.
1N/A
1N/A=item B<i>I<NUMBER>
1N/A
1N/AIndent lines by multiples of I<NUMBER> columns. The default is 4 columns.
1N/A
1N/A=item B<T>
1N/A
1N/AUse tabs for each 8 columns of indent. The default is to use only spaces.
1N/AFor instance, if the style options are B<-si4T>, a line that's indented
1N/A3 times will be preceded by one tab and four spaces; if the options were
1N/AB<-si8T>, the same line would be preceded by three tabs.
1N/A
1N/A=item B<v>I<STRING>B<.>
1N/A
1N/APrint I<STRING> for the value of a constant that can't be determined
1N/Abecause it was optimized away (mnemonic: this happens when a constant
1N/Ais used in B<v>oid context). The end of the string is marked by a period.
1N/AThe string should be a valid perl expression, generally a constant.
1N/ANote that unless it's a number, it probably needs to be quoted, and on
1N/Aa command line quotes need to be protected from the shell. Some
1N/Aconventional values include 0, 1, 42, '', 'foo', and
1N/A'Useless use of constant omitted' (which may need to be
1N/AB<-sv"'Useless use of constant omitted'.">
1N/Aor something similar depending on your shell). The default is '???'.
1N/AIf you're using B::Deparse on a module or other file that's require'd,
1N/Ayou shouldn't use a value that evaluates to false, since the customary
1N/Atrue constant at the end of a module will be in void context when the
1N/Afile is compiled as a main program.
1N/A
1N/A=back
1N/A
1N/A=item B<-x>I<LEVEL>
1N/A
1N/AExpand conventional syntax constructions into equivalent ones that expose
1N/Atheir internal operation. I<LEVEL> should be a digit, with higher values
1N/Ameaning more expansion. As with B<-q>, this actually involves turning off
1N/Aspecial cases in B::Deparse's normal operations.
1N/A
1N/AIf I<LEVEL> is at least 3, C<for> loops will be translated into equivalent
1N/Awhile loops with continue blocks; for instance
1N/A
1N/A for ($i = 0; $i < 10; ++$i) {
1N/A print $i;
1N/A }
1N/A
1N/Aturns into
1N/A
1N/A $i = 0;
1N/A while ($i < 10) {
1N/A print $i;
1N/A } continue {
1N/A ++$i
1N/A }
1N/A
1N/ANote that in a few cases this translation can't be perfectly carried back
1N/Ainto the source code -- if the loop's initializer declares a my variable,
1N/Afor instance, it won't have the correct scope outside of the loop.
1N/A
1N/AIf I<LEVEL> is at least 5, C<use> declarations will be translated into
1N/AC<BEGIN> blocks containing calls to C<require> and C<import>; for
1N/Ainstance,
1N/A
1N/A use strict 'refs';
1N/A
1N/Aturns into
1N/A
1N/A sub BEGIN {
1N/A require strict;
1N/A do {
1N/A 'strict'->import('refs')
1N/A };
1N/A }
1N/A
1N/AIf I<LEVEL> is at least 7, C<if> statements will be translated into
1N/Aequivalent expressions using C<&&>, C<?:> and C<do {}>; for instance
1N/A
1N/A print 'hi' if $nice;
1N/A if ($nice) {
1N/A print 'hi';
1N/A }
1N/A if ($nice) {
1N/A print 'hi';
1N/A } else {
1N/A print 'bye';
1N/A }
1N/A
1N/Aturns into
1N/A
1N/A $nice and print 'hi';
1N/A $nice and do { print 'hi' };
1N/A $nice ? do { print 'hi' } : do { print 'bye' };
1N/A
1N/ALong sequences of elsifs will turn into nested ternary operators, which
1N/AB::Deparse doesn't know how to indent nicely.
1N/A
1N/A=back
1N/A
1N/A=head1 USING B::Deparse AS A MODULE
1N/A
1N/A=head2 Synopsis
1N/A
1N/A use B::Deparse;
1N/A $deparse = B::Deparse->new("-p", "-sC");
1N/A $body = $deparse->coderef2text(\&func);
1N/A eval "sub func $body"; # the inverse operation
1N/A
1N/A=head2 Description
1N/A
1N/AB::Deparse can also be used on a sub-by-sub basis from other perl
1N/Aprograms.
1N/A
1N/A=head2 new
1N/A
1N/A $deparse = B::Deparse->new(OPTIONS)
1N/A
1N/ACreate an object to store the state of a deparsing operation and any
1N/Aoptions. The options are the same as those that can be given on the
1N/Acommand line (see L</OPTIONS>); options that are separated by commas
1N/Aafter B<-MO=Deparse> should be given as separate strings. Some
1N/Aoptions, like B<-u>, don't make sense for a single subroutine, so
1N/Adon't pass them.
1N/A
1N/A=head2 ambient_pragmas
1N/A
1N/A $deparse->ambient_pragmas(strict => 'all', '$[' => $[);
1N/A
1N/AThe compilation of a subroutine can be affected by a few compiler
1N/Adirectives, B<pragmas>. These are:
1N/A
1N/A=over 4
1N/A
1N/A=item *
1N/A
1N/Ause strict;
1N/A
1N/A=item *
1N/A
1N/Ause warnings;
1N/A
1N/A=item *
1N/A
1N/AAssigning to the special variable $[
1N/A
1N/A=item *
1N/A
1N/Ause integer;
1N/A
1N/A=item *
1N/A
1N/Ause bytes;
1N/A
1N/A=item *
1N/A
1N/Ause utf8;
1N/A
1N/A=item *
1N/A
1N/Ause re;
1N/A
1N/A=back
1N/A
1N/AOrdinarily, if you use B::Deparse on a subroutine which has
1N/Abeen compiled in the presence of one or more of these pragmas,
1N/Athe output will include statements to turn on the appropriate
1N/Adirectives. So if you then compile the code returned by coderef2text,
1N/Ait will behave the same way as the subroutine which you deparsed.
1N/A
1N/AHowever, you may know that you intend to use the results in a
1N/Aparticular context, where some pragmas are already in scope. In
1N/Athis case, you use the B<ambient_pragmas> method to describe the
1N/Aassumptions you wish to make.
1N/A
1N/ANot all of the options currently have any useful effect. See
1N/AL</BUGS> for more details.
1N/A
1N/AThe parameters it accepts are:
1N/A
1N/A=over 4
1N/A
1N/A=item strict
1N/A
1N/ATakes a string, possibly containing several values separated
1N/Aby whitespace. The special values "all" and "none" mean what you'd
1N/Aexpect.
1N/A
1N/A $deparse->ambient_pragmas(strict => 'subs refs');
1N/A
1N/A=item $[
1N/A
1N/ATakes a number, the value of the array base $[.
1N/A
1N/A=item bytes
1N/A
1N/A=item utf8
1N/A
1N/A=item integer
1N/A
1N/AIf the value is true, then the appropriate pragma is assumed to
1N/Abe in the ambient scope, otherwise not.
1N/A
1N/A=item re
1N/A
1N/ATakes a string, possibly containing a whitespace-separated list of
1N/Avalues. The values "all" and "none" are special. It's also permissible
1N/Ato pass an array reference here.
1N/A
1N/A $deparser->ambient_pragmas(re => 'eval');
1N/A
1N/A
1N/A=item warnings
1N/A
1N/ATakes a string, possibly containing a whitespace-separated list of
1N/Avalues. The values "all" and "none" are special, again. It's also
1N/Apermissible to pass an array reference here.
1N/A
1N/A $deparser->ambient_pragmas(warnings => [qw[void io]]);
1N/A
1N/AIf one of the values is the string "FATAL", then all the warnings
1N/Ain that list will be considered fatal, just as with the B<warnings>
1N/Apragma itself. Should you need to specify that some warnings are
1N/Afatal, and others are merely enabled, you can pass the B<warnings>
1N/Aparameter twice:
1N/A
1N/A $deparser->ambient_pragmas(
1N/A warnings => 'all',
1N/A warnings => [FATAL => qw/void io/],
1N/A );
1N/A
1N/ASee L<perllexwarn> for more information about lexical warnings.
1N/A
1N/A=item hint_bits
1N/A
1N/A=item warning_bits
1N/A
1N/AThese two parameters are used to specify the ambient pragmas in
1N/Athe format used by the special variables $^H and ${^WARNING_BITS}.
1N/A
1N/AThey exist principally so that you can write code like:
1N/A
1N/A { my ($hint_bits, $warning_bits);
1N/A BEGIN {($hint_bits, $warning_bits) = ($^H, ${^WARNING_BITS})}
1N/A $deparser->ambient_pragmas (
1N/A hint_bits => $hint_bits,
1N/A warning_bits => $warning_bits,
1N/A '$[' => 0 + $[
1N/A ); }
1N/A
1N/Awhich specifies that the ambient pragmas are exactly those which
1N/Aare in scope at the point of calling.
1N/A
1N/A=back
1N/A
1N/A=head2 coderef2text
1N/A
1N/A $body = $deparse->coderef2text(\&func)
1N/A $body = $deparse->coderef2text(sub ($$) { ... })
1N/A
1N/AReturn source code for the body of a subroutine (a block, optionally
1N/Apreceded by a prototype in parens), given a reference to the
1N/Asub. Because a subroutine can have no names, or more than one name,
1N/Athis method doesn't return a complete subroutine definition -- if you
1N/Awant to eval the result, you should prepend "sub subname ", or "sub "
1N/Afor an anonymous function constructor. Unless the sub was defined in
1N/Athe main:: package, the code will include a package declaration.
1N/A
1N/A=head1 BUGS
1N/A
1N/A=over 4
1N/A
1N/A=item *
1N/A
1N/AThe only pragmas to be completely supported are: C<use warnings>,
1N/AC<use strict 'refs'>, C<use bytes>, and C<use integer>. (C<$[>, which
1N/Abehaves like a pragma, is also supported.)
1N/A
1N/AExcepting those listed above, we're currently unable to guarantee that
1N/AB::Deparse will produce a pragma at the correct point in the program.
1N/A(Specifically, pragmas at the beginning of a block often appear right
1N/Abefore the start of the block instead.)
1N/ASince the effects of pragmas are often lexically scoped, this can mean
1N/Athat the pragma holds sway over a different portion of the program
1N/Athan in the input file.
1N/A
1N/A=item *
1N/A
1N/AIn fact, the above is a specific instance of a more general problem:
1N/Awe can't guarantee to produce BEGIN blocks or C<use> declarations in
1N/Aexactly the right place. So if you use a module which affects compilation
1N/A(such as by over-riding keywords, overloading constants or whatever)
1N/Athen the output code might not work as intended.
1N/A
1N/AThis is the most serious outstanding problem, and will require some help
1N/Afrom the Perl core to fix.
1N/A
1N/A=item *
1N/A
1N/AIf a keyword is over-ridden, and your program explicitly calls
1N/Athe built-in version by using CORE::keyword, the output of B::Deparse
1N/Awill not reflect this. If you run the resulting code, it will call
1N/Athe over-ridden version rather than the built-in one. (Maybe there
1N/Ashould be an option to B<always> print keyword calls as C<CORE::name>.)
1N/A
1N/A=item *
1N/A
1N/ASome constants don't print correctly either with or without B<-d>.
1N/AFor instance, neither B::Deparse nor Data::Dumper know how to print
1N/Adual-valued scalars correctly, as in:
1N/A
1N/A use constant E2BIG => ($!=7); $y = E2BIG; print $y, 0+$y;
1N/A
1N/A=item *
1N/A
1N/AAn input file that uses source filtering probably won't be deparsed into
1N/Arunnable code, because it will still include the B<use> declaration
1N/Afor the source filtering module, even though the code that is
1N/Aproduced is already ordinary Perl which shouldn't be filtered again.
1N/A
1N/A=item *
1N/A
1N/AThere are probably many more bugs on non-ASCII platforms (EBCDIC).
1N/A
1N/A=back
1N/A
1N/A=head1 AUTHOR
1N/A
1N/AStephen McCamant <smcc@CSUA.Berkeley.EDU>, based on an earlier version
1N/Aby Malcolm Beattie <mbeattie@sable.ox.ac.uk>, with contributions from
1N/AGisle Aas, James Duncan, Albert Dvornik, Robin Houston, Dave Mitchell,
1N/AHugo van der Sanden, Gurusamy Sarathy, Nick Ing-Simmons, and Rafael
1N/AGarcia-Suarez.
1N/A
1N/A=cut