2 # Copyright (c) 1998-2000, 2002, 2003, 2004, 2005, 2006 Stephen McCamant.
4 # This module is free software; you can redistribute and/or modify
5 # it under the same terms as Perl itself.
7 # This is based on the module of the same name by Malcolm Beattie,
8 # but essentially none of his code remains.
12 use B qw(class main_root main_start main_cv svref_2object opnumber perlstring
13 OPf_WANT OPf_WANT_VOID OPf_WANT_SCALAR OPf_WANT_LIST
14 OPf_KIDS OPf_REF OPf_STACKED OPf_SPECIAL OPf_MOD OPf_PARENS
15 OPpLVAL_INTRO OPpOUR_INTRO OPpENTERSUB_AMPER OPpSLICE OPpKVSLICE
17 OPpTRANS_SQUASH OPpTRANS_DELETE OPpTRANS_COMPLEMENT OPpTARGET_MY
18 OPpEXISTS_SUB OPpSORT_NUMERIC OPpSORT_INTEGER OPpREPEAT_DOLIST
19 OPpSORT_REVERSE OPpMULTIDEREF_EXISTS OPpMULTIDEREF_DELETE
20 OPpSPLIT_ASSIGN OPpSPLIT_LEX
21 OPpPADHV_ISKEYS OPpRV2HV_ISKEYS
22 OPpMULTICONCAT_APPEND OPpMULTICONCAT_STRINGIFY OPpMULTICONCAT_FAKE
23 OPpTRUEBOOL OPpINDEX_BOOLNEG
24 SVf_IOK SVf_NOK SVf_ROK SVf_POK SVpad_OUR SVf_FAKE SVs_RMG SVs_SMG
25 SVs_PADTMP SVpad_TYPED
27 PMf_KEEP PMf_GLOBAL PMf_CONTINUE PMf_EVAL PMf_ONCE
28 PMf_MULTILINE PMf_SINGLELINE PMf_FOLD PMf_EXTENDED PMf_EXTENDED_MORE
31 MDEREF_AV_pop_rv2av_aelem
32 MDEREF_AV_gvsv_vivify_rv2av_aelem
33 MDEREF_AV_padsv_vivify_rv2av_aelem
34 MDEREF_AV_vivify_rv2av_aelem
37 MDEREF_HV_pop_rv2hv_helem
38 MDEREF_HV_gvsv_vivify_rv2hv_helem
39 MDEREF_HV_padsv_vivify_rv2hv_helem
40 MDEREF_HV_vivify_rv2hv_helem
56 use vars qw/$AUTOLOAD/;
61 # List version-specific constants here.
62 # Easiest way to keep this code portable between version looks to
63 # be to fake up a dummy constant that will never actually be true.
64 foreach (qw(OPpSORT_INPLACE OPpSORT_DESCEND OPpITER_REVERSED OPpCONST_NOVER
65 OPpPAD_STATE PMf_SKIPWHITE RXf_SKIPWHITE
66 PMf_CHARSET PMf_KEEPCOPY PMf_NOCAPTURE CVf_ANONCONST
67 CVf_LOCKED OPpREVERSE_INPLACE OPpSUBSTR_REPL_FIRST
68 PMf_NONDESTRUCT OPpCONST_ARYBASE OPpEVAL_BYTES
69 OPpLVREF_TYPE OPpLVREF_SV OPpLVREF_AV OPpLVREF_HV
70 OPpLVREF_CV OPpLVREF_ELEM SVpad_STATE)) {
71 eval { B->import($_) };
73 *{$_} = sub () {0} unless *{$_}{CODE};
77 # Changes between 0.50 and 0.51:
78 # - fixed nulled leave with live enter in sort { }
79 # - fixed reference constants (\"str")
80 # - handle empty programs gracefully
81 # - handle infinite loops (for (;;) {}, while (1) {})
82 # - differentiate between 'for my $x ...' and 'my $x; for $x ...'
83 # - various minor cleanups
84 # - moved globals into an object
85 # - added '-u', like B::C
86 # - package declarations using cop_stash
87 # - subs, formats and code sorted by cop_seq
88 # Changes between 0.51 and 0.52:
89 # - added pp_threadsv (special variables under USE_5005THREADS)
90 # - added documentation
91 # Changes between 0.52 and 0.53:
92 # - many changes adding precedence contexts and associativity
93 # - added '-p' and '-s' output style options
94 # - various other minor fixes
95 # Changes between 0.53 and 0.54:
96 # - added support for new 'for (1..100)' optimization,
98 # Changes between 0.54 and 0.55:
99 # - added support for new qr// construct
100 # - added support for new pp_regcreset OP
101 # Changes between 0.55 and 0.56:
102 # - tested on base/*.t, cmd/*.t, comp/*.t, io/*.t
103 # - fixed $# on non-lexicals broken in last big rewrite
104 # - added temporary fix for change in opcode of OP_STRINGIFY
105 # - fixed problem in 0.54's for() patch in 'for (@ary)'
106 # - fixed precedence in conditional of ?:
107 # - tweaked list paren elimination in 'my($x) = @_'
108 # - made continue-block detection trickier wrt. null ops
109 # - fixed various prototype problems in pp_entersub
110 # - added support for sub prototypes that never get GVs
111 # - added unquoting for special filehandle first arg in truncate
112 # - print doubled rv2gv (a bug) as '*{*GV}' instead of illegal '**GV'
113 # - added semicolons at the ends of blocks
114 # - added -l '#line' declaration option -- fixes cmd/subval.t 27,28
115 # Changes between 0.56 and 0.561:
116 # - fixed multiply-declared my var in pp_truncate (thanks to Sarathy)
117 # - used new B.pm symbolic constants (done by Nick Ing-Simmons)
118 # Changes between 0.561 and 0.57:
119 # - stylistic changes to symbolic constant stuff
120 # - handled scope in s///e replacement code
121 # - added unquote option for expanding "" into concats, etc.
122 # - split method and proto parts of pp_entersub into separate functions
123 # - various minor cleanups
124 # Changes after 0.57:
125 # - added parens in \&foo (patch by Albert Dvornik)
126 # Changes between 0.57 and 0.58:
127 # - fixed '0' statements that weren't being printed
128 # - added methods for use from other programs
129 # (based on patches from James Duncan and Hugo van der Sanden)
130 # - added -si and -sT to control indenting (also based on a patch from Hugo)
131 # - added -sv to print something else instead of '???'
132 # - preliminary version of utf8 tr/// handling
133 # Changes after 0.58:
134 # - uses of $op->ppaddr changed to new $op->name (done by Sarathy)
135 # - added support for Hugo's new OP_SETSTATE (like nextstate)
136 # Changes between 0.58 and 0.59
137 # - added support for Chip's OP_METHOD_NAMED
138 # - added support for Ilya's OPpTARGET_MY optimization
139 # - elided arrows before '()' subscripts when possible
140 # Changes between 0.59 and 0.60
141 # - support for method attributes was added
142 # - some warnings fixed
143 # - separate recognition of constant subs
144 # - rewrote continue block handling, now recognizing for loops
145 # - added more control of expanding control structures
146 # Changes between 0.60 and 0.61 (mostly by Robin Houston)
148 # - support for pragmas and 'use'
149 # - support for the little-used $[ variable
150 # - support for __DATA__ sections
152 # - BEGIN, CHECK, INIT and END blocks
153 # - scoping of subroutine declarations fixed
154 # - compile-time output from the input program can be suppressed, so that the
155 # output is just the deparsed code. (a change to O.pm in fact)
156 # - our() declarations
157 # - *all* the known bugs are now listed in the BUGS section
158 # - comprehensive test mechanism (TEST -deparse)
159 # Changes between 0.62 and 0.63 (mostly by Rafael Garcia-Suarez)
162 # - support for command-line switches (-l, -0, etc.)
163 # Changes between 0.63 and 0.64
164 # - support for //, CHECK blocks, and assertions
165 # - improved handling of foreach loops and lexicals
166 # - option to use Data::Dumper for constants
168 # - discovered lots more bugs not yet fixed
172 # Changes between 0.72 and 0.73
173 # - support new switch constructs
176 # (See also BUGS section at the end of this file)
178 # - finish tr/// changes
179 # - add option for even more parens (generalize \&foo change)
180 # - left/right context
181 # - copy comments (look at real text with $^P?)
182 # - avoid semis in one-statement blocks
183 # - associativity of &&=, ||=, ?:
184 # - ',' => '=>' (auto-unquote?)
185 # - break long lines ("\r" as discretionary break?)
186 # - configurable syntax highlighting: ANSI color, HTML, TeX, etc.
187 # - more style options: brace style, hex vs. octal, quotes, ...
188 # - print big ints as hex/octal instead of decimal (heuristic?)
189 # - handle 'my $x if 0'?
190 # - version using op_next instead of op_first/sibling?
191 # - avoid string copies (pass arrays, one big join?)
194 # Current test.deparse failures
195 # comp/hints 6 - location of BEGIN blocks wrt. block openings
196 # run/switchI 1 - missing -I switches entirely
197 # perl -Ifoo -e 'print @INC'
198 # op/caller 2 - warning mask propagates backwards before warnings::register
199 # 'use warnings; BEGIN {${^WARNING_BITS} eq "U"x12;} use warnings::register'
200 # op/getpid 2 - can't assign to shared my() declaration (threads only)
201 # 'my $x : shared = 5'
202 # op/override 7 - parens on overridden require change v-string interpretation
203 # 'BEGIN{*CORE::GLOBAL::require=sub {}} require v5.6'
204 # c.f. 'BEGIN { *f = sub {0} }; f 2'
205 # op/pat 774 - losing Unicode-ness of Latin1-only strings
206 # 'use charnames ":short"; $x="\N{latin:a with acute}"'
207 # op/recurse 12 - missing parens on recursive call makes it look like method
209 # op/subst 90 - inconsistent handling of utf8 under "use utf8"
210 # op/taint 29 - "use re 'taint'" deparsed in the wrong place wrt. block open
211 # op/tiehandle compile - "use strict" deparsed in the wrong place
213 # ext/B/t/xref 11 - line numbers when we add newlines to one-line subs
214 # ext/Data/Dumper/t/dumper compile
215 # ext/DB_file/several
217 # ext/Ernno/Errno warnings
218 # ext/IO/lib/IO/t/io_sel 23
219 # ext/PerlIO/t/encoding compile
220 # ext/POSIX/t/posix 6
221 # ext/Socket/Socket 8
222 # ext/Storable/t/croak compile
223 # lib/Attribute/Handlers/t/multi compile
224 # lib/bignum/ several
228 # lib/ExtUtils/t/bytes 4
229 # lib/File/DosGlob compile
230 # lib/Filter/Simple/t/data 1
231 # lib/Math/BigInt/t/constant 1
232 # lib/Net/t/config Deparse-warning
233 # lib/overload compile
234 # lib/Switch/ several
236 # lib/Test/Simple several
238 # lib/Tie/File/t/29_downcopy 5
244 # True when deparsing via $deparse->coderef2text; false when deparsing the
248 # (local($a), local($b)) and local($a, $b) have the same internal
249 # representation but the short form looks better. We notice we can
250 # use a large-scale local when checking the list, but need to prevent
251 # individual locals too. This hash holds the addresses of OPs that
252 # have already had their local-ness accounted for. The same thing
256 # CV for current sub (or main program) being deparsed
259 # Cached hash of lexical variables for curcv: keys are
260 # names prefixed with "m" or "o" (representing my/our), and
261 # each value is an array with two elements indicating the cop_seq
262 # of scopes in which a var of that name is valid and a third ele-
263 # ment referencing the pad name.
266 # COP for statement being deparsed
269 # name of the current package for deparsed code
272 # array of [cop_seq, CV, is_format?, name] for subs and formats we still
273 # want to deparse. The fourth element is a pad name thingy for lexical
274 # subs or a string for special blocks. For other subs, it is undef. For
275 # lexical subs, CV may be undef, indicating a stub declaration.
278 # as above, but [name, prototype] for subs that never got a GV
280 # subs_done, forms_done:
281 # keys are addresses of GVs for subs and formats we've already
282 # deparsed (or at least put into subs_todo)
285 # keys are names of subs for which we've printed declarations.
286 # That means we can omit parentheses from the arguments. It also means we
287 # need to put CORE:: on core functions of the same name.
290 # True when deparsing the replacement part of a substitution.
293 # True when deparsing the argument to \.
298 # cuddle: ' ' or '\n', depending on -sC
303 # A little explanation of how precedence contexts and associativity
306 # deparse() calls each per-op subroutine with an argument $cx (short
307 # for context, but not the same as the cx* in the perl core), which is
308 # a number describing the op's parents in terms of precedence, whether
309 # they're inside an expression or at statement level, etc. (see
310 # chart below). When ops with children call deparse on them, they pass
311 # along their precedence. Fractional values are used to implement
312 # associativity ('($x + $y) + $z' => '$x + $y + $y') and related
313 # parentheses hacks. The major disadvantage of this scheme is that
314 # it doesn't know about right sides and left sides, so say if you
315 # assign a listop to a variable, it can't tell it's allowed to leave
316 # the parens off the listop.
319 # 26 [TODO] inside interpolation context ("")
320 # 25 left terms and list operators (leftward)
324 # 21 right ! ~ \ and unary + and -
329 # 16 nonassoc named unary operators
330 # 15 nonassoc < > <= >= lt gt le ge
331 # 14 nonassoc == != <=> eq ne cmp
338 # 7 right = += -= *= etc.
340 # 5 nonassoc list operators (rightward)
344 # 1 statement modifiers
345 # 0.5 statements, but still print scopes as do { ... }
349 # Nonprinting characters with special meaning:
350 # \cS - steal parens (see maybe_parens_unop)
351 # \n - newline and indent
352 # \t - increase indent
353 # \b - decrease indent ('outdent')
354 # \f - flush left (no indent)
355 # \cK - kill following semicolon, if any
357 # Semicolon handling:
358 # - Individual statements are not deparsed with trailing semicolons.
359 # (If necessary, \cK is tacked on to the end.)
360 # - Whatever code joins statements together or emits them (lineseq,
361 # scopeop, deparse_root) is responsible for adding semicolons where
363 # - use statements are deparsed with trailing semicolons because they are
364 # immediately concatenated with the following statement.
365 # - indent() removes semicolons wherever it sees \cK.
368 BEGIN { for (qw[ const stringify rv2sv list glob pushmark null aelem
369 kvaslice kvhslice padsv
370 nextstate dbstate rv2av rv2hv helem custom ]) {
371 eval "sub OP_\U$_ () { " . opnumber($_) . "}"
374 # _pessimise_walk(): recursively walk the optree of a sub,
375 # possibly undoing optimisations along the way.
377 sub _pessimise_walk {
378 my ($self, $startop) = @_;
380 return unless $$startop;
382 for ($op = $startop; $$op; $prevop = $op, $op = $op->sibling) {
383 my $ppname = $op->name;
385 # pessimisations start here
387 if ($ppname eq "padrange") {
389 # the original optimisation either (1) changed this:
390 # pushmark -> (various pad and list and null ops) -> the_rest
391 # or (2), for the = @_ case, changed this:
392 # pushmark -> gv[_] -> rv2av -> (pad stuff) -> the_rest
394 # padrange ----------------------------------------> the_rest
395 # so we just need to convert the padrange back into a
396 # pushmark, and in case (1), set its op_next to op_sibling,
397 # which is the head of the original chain of optimised-away
398 # pad ops, or for (2), set it to sibling->first, which is
399 # the original gv[_].
401 $B::overlay->{$$op} = {
404 private => ($op->private & OPpLVAL_INTRO),
408 # pessimisations end here
410 if (class($op) eq 'PMOP') {
411 if (ref($op->pmreplroot)
412 && ${$op->pmreplroot}
413 && $op->pmreplroot->isa( 'B::OP' ))
415 $self-> _pessimise_walk($op->pmreplroot);
418 # pessimise any /(?{...})/ code blocks
420 my $code_list = $op->code_list;
422 $self->_pessimise_walk($code_list);
424 elsif (${$re = $op->pmregexp} && ${$cv = $re->qr_anoncv}) {
425 $code_list = $cv->ROOT # leavesub
428 $self->_pessimise_walk($code_list);
432 if ($op->flags & OPf_KIDS) {
433 $self-> _pessimise_walk($op->first);
440 # _pessimise_walk_exe(): recursively walk the op_next chain of a sub,
441 # possibly undoing optimisations along the way.
443 sub _pessimise_walk_exe {
444 my ($self, $startop, $visited) = @_;
446 no warnings 'recursion';
448 return unless $$startop;
449 return if $visited->{$$startop};
451 for ($op = $startop; $$op; $prevop = $op, $op = $op->next) {
452 last if $visited->{$$op};
453 $visited->{$$op} = 1;
454 my $ppname = $op->name;
456 /^((and|d?or)(assign)?|(map|grep)while|range|cond_expr|once)$/
457 # entertry is also a logop, but its op_other invariably points
458 # into the same chain as the main execution path, so we skip it
460 $self->_pessimise_walk_exe($op->other, $visited);
462 elsif ($ppname eq "subst") {
463 $self->_pessimise_walk_exe($op->pmreplstart, $visited);
465 elsif ($ppname =~ /^(enter(loop|iter))$/) {
466 # redoop and nextop will already be covered by the main block
468 $self->_pessimise_walk_exe($op->lastop, $visited);
471 # pessimisations start here
475 # Go through an optree and "remove" some optimisations by using an
476 # overlay to selectively modify or un-null some ops. Deparsing in the
477 # absence of those optimisations is then easier.
479 # Note that older optimisations are not removed, as Deparse was already
480 # written to recognise them before the pessimise/overlay system was added.
483 my ($self, $root, $start) = @_;
485 no warnings 'recursion';
486 # walk tree in root-to-branch order
487 $self->_pessimise_walk($root);
490 # walk tree in execution order
491 $self->_pessimise_walk_exe($start, \%visited);
497 return class($op) eq "NULL";
501 # Add a CV to the list of subs that still need deparsing.
505 my($cv, $is_form, $name) = @_;
506 my $cvfile = $cv->FILE//'';
507 return unless ($cvfile eq $0 || exists $self->{files}{$cvfile});
509 if ($cv->OUTSIDE_SEQ) {
510 $seq = $cv->OUTSIDE_SEQ;
511 } elsif (!null($cv->START) and is_state($cv->START)) {
512 $seq = $cv->START->cop_seq;
516 my $stash = $cv->STASH;
517 if (class($stash) eq 'HV') {
518 $self->{packs}{$stash->NAME}++;
520 push @{$self->{'subs_todo'}}, [$seq, $cv, $is_form, $name];
524 # Pop the next sub from the todo list and deparse it
528 my $ent = shift @{$self->{'subs_todo'}};
529 my ($seq, $cv, $is_form, $name) = @$ent;
531 # any 'use strict; package foo' that should come before the sub
532 # declaration to sync with the first COP of the sub
534 if ($cv and !null($cv->START) and is_state($cv->START)) {
535 $pragmata = $self->pragmata($cv->START);
538 if (ref $name) { # lexical sub
541 my $flags = $name->FLAGS;
543 !$cv || $seq <= $name->COP_SEQ_RANGE_LOW
544 ? $self->keyword($flags & SVpad_OUR
546 : $flags & SVpad_STATE
550 # XXX We would do $self->keyword("sub"), but ‘my CORE::sub’
551 # doesn’t work and ‘my sub’ ignores a &sub in scope. I.e.,
552 # we have a core bug here.
553 push @text, "sub " . substr $name->PVX, 1;
556 push @text, " " . $self->deparse_sub($cv);
557 $text[-1] =~ s/ ;$/;/;
563 return $pragmata . join "", @text;
567 $name //= $self->gv_name($gv);
569 return $pragmata . $self->keyword("format") . " $name =\n"
570 . $self->deparse_format($cv). "\n";
573 if ($name eq "BEGIN") {
574 $use_dec = $self->begin_is_use($cv);
575 if (defined ($use_dec) and $self->{'expand'} < 5) {
576 return $pragmata if 0 == length($use_dec);
578 # XXX bit of a hack: Test::More's use_ok() method
579 # builds a fake use statement which deparses as, e.g.
580 # use Net::Ping (@{$args[0];});
581 # As well as being superfluous (the use_ok() is deparsed
582 # too) and ugly, it fails under use strict and otherwise
583 # makes use of a lexical var that's not in scope.
591 \s*\#line\ \d+\ \".*"\s*
598 $use_dec =~ s/^(use|no)\b/$self->keyword($1)/e;
602 if ($self->{'linenums'}) {
603 my $line = $gv->LINE;
604 my $file = $gv->FILE;
605 $l = "\n\f#line $line \"$file\"\n";
609 if (class($cv->STASH) ne "SPECIAL") {
610 $stash = $cv->STASH->NAME;
611 if ($stash ne $self->{'curstash'}) {
612 $p = $self->keyword("package") . " $stash;\n";
613 $name = "$self->{'curstash'}::$name" unless $name =~ /::/;
614 $self->{'curstash'} = $stash;
618 return "$pragmata$p$l$use_dec";
620 if ( $name !~ /::/ and $self->lex_in_scope("&$name")
621 || $self->lex_in_scope("&$name", 1) )
623 $name = "$self->{'curstash'}::$name";
624 } elsif (defined $stash) {
625 $name =~ s/^\Q$stash\E::(?!\z|.*::)//;
627 my $ret = "$pragmata${p}${l}" . $self->keyword("sub") . " $name "
628 . $self->deparse_sub($cv);
629 $self->{'subs_declared'}{$name} = 1;
635 # Return a "use" declaration for this BEGIN block, if appropriate
637 my ($self, $cv) = @_;
638 my $root = $cv->ROOT;
639 local @$self{qw'curcv curcvlex'} = ($cv);
640 local $B::overlay = {};
641 $self->pessimise($root, $cv->START);
643 #B::walkoptree($cv->ROOT, "debug");
644 my $lineseq = $root->first;
645 return if $lineseq->name ne "lineseq";
647 my $req_op = $lineseq->first->sibling;
648 return if $req_op->name ne "require";
650 # maybe it's C<require expr> rather than C<require 'foo'>
651 return if ($req_op->first->name ne 'const');
654 if ($req_op->first->private & OPpCONST_BARE) {
655 # Actually it should always be a bareword
656 $module = $self->const_sv($req_op->first)->PV;
657 $module =~ s[/][::]g;
661 $module = $self->const($self->const_sv($req_op->first), 6);
665 my $version_op = $req_op->sibling;
666 return if class($version_op) eq "NULL";
667 if ($version_op->name eq "lineseq") {
668 # We have a version parameter; skip nextstate & pushmark
669 my $constop = $version_op->first->next->next;
671 return unless $self->const_sv($constop)->PV eq $module;
672 $constop = $constop->sibling;
673 $version = $self->const_sv($constop);
674 if (class($version) eq "IV") {
675 $version = $version->int_value;
676 } elsif (class($version) eq "NV") {
677 $version = $version->NV;
678 } elsif (class($version) ne "PVMG") {
679 # Includes PVIV and PVNV
680 $version = $version->PV;
682 # version specified as a v-string
683 $version = 'v'.join '.', map ord, split //, $version->PV;
685 $constop = $constop->sibling;
686 return if $constop->name ne "method_named";
687 return if $self->meth_sv($constop)->PV ne "VERSION";
690 $lineseq = $version_op->sibling;
691 return if $lineseq->name ne "lineseq";
692 my $entersub = $lineseq->first->sibling;
693 if ($entersub->name eq "stub") {
694 return "use $module $version ();\n" if defined $version;
695 return "use $module ();\n";
697 return if $entersub->name ne "entersub";
699 # See if there are import arguments
702 my $svop = $entersub->first->sibling; # Skip over pushmark
703 return unless $self->const_sv($svop)->PV eq $module;
705 # Pull out the arguments
706 for ($svop=$svop->sibling; index($svop->name, "method_") != 0;
707 $svop = $svop->sibling) {
708 $args .= ", " if length($args);
709 $args .= $self->deparse($svop, 6);
713 my $method_named = $svop;
714 return if $method_named->name ne "method_named";
715 my $method_name = $self->meth_sv($method_named)->PV;
717 if ($method_name eq "unimport") {
721 # Certain pragmas are dealt with using hint bits,
722 # so we ignore them here
723 if ($module eq 'strict' || $module eq 'integer'
724 || $module eq 'bytes' || $module eq 'warnings'
725 || $module eq 'feature') {
729 if (defined $version && length $args) {
730 return "$use $module $version ($args);\n";
731 } elsif (defined $version) {
732 return "$use $module $version;\n";
733 } elsif (length $args) {
734 return "$use $module ($args);\n";
736 return "$use $module;\n";
741 my ($self, $pack, $seen) = @_;
743 if (!defined $pack) {
748 $pack =~ s/(::)?$/::/;
750 $stash = \%{"main::$pack"};
754 $INC{"overload.pm"} ? overload::StrVal($stash) : $stash
756 my %stash = svref_2object($stash)->ARRAY;
757 while (my ($key, $val) = each %stash) {
758 my $flags = $val->FLAGS;
759 if ($flags & SVf_ROK) {
760 # A reference. Dump this if it is a reference to a CV. If it
761 # is a constant acting as a proxy for a full subroutine, then
762 # we may or may not have to dump it. If some form of perl-
763 # space visible code must have created it, be it a use
764 # statement, or some direct symbol-table manipulation code that
765 # we will deparse, then we don’t want to dump it. If it is the
766 # result of a declaration like sub f () { 42 } then we *do*
767 # want to dump it. The only way to distinguish these seems
768 # to be the SVs_PADTMP flag on the constant, which is admit-
770 my $class = class(my $referent = $val->RV);
771 if ($class eq "CV") {
772 $self->todo($referent, 0);
774 $class !~ /^(AV|HV|CV|FM|IO|SPECIAL)\z/
775 # A more robust way to write that would be this, but B does
776 # not provide the SVt_ constants:
777 # ($referent->FLAGS & B::SVTYPEMASK) < B::SVt_PVAV
778 and $referent->FLAGS & SVs_PADTMP
780 push @{$self->{'protos_todo'}}, [$pack . $key, $val];
782 } elsif ($flags & (SVf_POK|SVf_IOK)) {
783 # Just a prototype. As an ugly but fairly effective way
784 # to find out if it belongs here is to see if the AUTOLOAD
785 # (if any) for the stash was defined in one of our files.
786 my $A = $stash{"AUTOLOAD"};
787 if (defined ($A) && class($A) eq "GV" && defined($A->CV)
788 && class($A->CV) eq "CV") {
790 next unless $AF eq $0 || exists $self->{'files'}{$AF};
792 push @{$self->{'protos_todo'}},
793 [$pack . $key, $flags & SVf_POK ? $val->PV: undef];
794 } elsif (class($val) eq "GV") {
795 if (class(my $cv = $val->CV) ne "SPECIAL") {
796 next if $self->{'subs_done'}{$$val}++;
797 next if $$val != ${$cv->GV}; # Ignore imposters
800 if (class(my $cv = $val->FORM) ne "SPECIAL") {
801 next if $self->{'forms_done'}{$$val}++;
802 next if $$val != ${$cv->GV}; # Ignore imposters
805 if (class($val->HV) ne "SPECIAL" && $key =~ /::$/) {
806 $self->stash_subs($pack . $key, $seen);
816 foreach $ar (@{$self->{'protos_todo'}}) {
818 # Only print a constant if it occurs in the same package as a
819 # dumped sub. This is not perfect, but a heuristic that will
820 # hopefully work most of the time. Ideally we would use
821 # CvFILE, but a constant stub has no CvFILE.
822 my $pack = ($ar->[0] =~ /(.*)::/)[0];
823 next if $pack and !$self->{packs}{$pack}
825 my $body = defined $ar->[1]
827 ? " () {\n " . $self->const($ar->[1]->RV,0) . ";\n}"
828 : " (". $ar->[1] . ");"
830 push @ret, "sub " . $ar->[0] . "$body\n";
832 delete $self->{'protos_todo'};
840 while (length($opt = substr($opts, 0, 1))) {
842 $self->{'cuddle'} = " ";
843 $opts = substr($opts, 1);
844 } elsif ($opt eq "i") {
845 $opts =~ s/^i(\d+)//;
846 $self->{'indent_size'} = $1;
847 } elsif ($opt eq "T") {
848 $self->{'use_tabs'} = 1;
849 $opts = substr($opts, 1);
850 } elsif ($opt eq "v") {
851 $opts =~ s/^v([^.]*)(.|$)//;
852 $self->{'ex_const'} = $1;
859 my $self = bless {}, $class;
860 $self->{'cuddle'} = "\n";
861 $self->{'curcop'} = undef;
862 $self->{'curstash'} = "main";
863 $self->{'ex_const'} = "'???'";
864 $self->{'expand'} = 0;
865 $self->{'files'} = {};
866 $self->{'packs'} = {};
867 $self->{'indent_size'} = 4;
868 $self->{'linenums'} = 0;
869 $self->{'parens'} = 0;
870 $self->{'subs_todo'} = [];
871 $self->{'unquote'} = 0;
872 $self->{'use_dumper'} = 0;
873 $self->{'use_tabs'} = 0;
875 $self->{'ambient_arybase'} = 0;
876 $self->{'ambient_warnings'} = undef; # Assume no lexical warnings
877 $self->{'ambient_hints'} = 0;
878 $self->{'ambient_hinthash'} = undef;
881 while (my $arg = shift @_) {
883 $self->{'use_dumper'} = 1;
884 require Data::Dumper;
885 } elsif ($arg =~ /^-f(.*)/) {
886 $self->{'files'}{$1} = 1;
887 } elsif ($arg eq "-l") {
888 $self->{'linenums'} = 1;
889 } elsif ($arg eq "-p") {
890 $self->{'parens'} = 1;
891 } elsif ($arg eq "-P") {
892 $self->{'noproto'} = 1;
893 } elsif ($arg eq "-q") {
894 $self->{'unquote'} = 1;
895 } elsif (substr($arg, 0, 2) eq "-s") {
896 $self->style_opts(substr $arg, 2);
897 } elsif ($arg =~ /^-x(\d)$/) {
898 $self->{'expand'} = $1;
905 # Mask out the bits that L<warnings::register> uses
908 $WARN_MASK = $warnings::Bits{all} | $warnings::DeadBits{all};
915 # Initialise the contextual information, either from
916 # defaults provided with the ambient_pragmas method,
917 # or from perl's own defaults otherwise.
921 $self->{'arybase'} = $self->{'ambient_arybase'};
922 $self->{'warnings'} = defined ($self->{'ambient_warnings'})
923 ? $self->{'ambient_warnings'} & WARN_MASK
925 $self->{'hints'} = $self->{'ambient_hints'};
926 $self->{'hints'} &= 0xFF if $] < 5.009;
927 $self->{'hinthash'} = $self->{'ambient_hinthash'};
929 # also a convenient place to clear out subs_declared
930 delete $self->{'subs_declared'};
936 my $self = B::Deparse->new(@args);
937 # First deparse command-line args
938 if (defined $^I) { # deparse -i
939 print q(BEGIN { $^I = ).perlstring($^I).qq(; }\n);
941 if ($^W) { # deparse -w
942 print qq(BEGIN { \$^W = $^W; }\n);
944 if ($/ ne "\n" or defined $O::savebackslash) { # deparse -l and -0
945 my $fs = perlstring($/) || 'undef';
946 my $bs = perlstring($O::savebackslash) || 'undef';
947 print qq(BEGIN { \$/ = $fs; \$\\ = $bs; }\n);
949 my @BEGINs = B::begin_av->isa("B::AV") ? B::begin_av->ARRAY : ();
950 my @UNITCHECKs = B::unitcheck_av->isa("B::AV")
951 ? B::unitcheck_av->ARRAY
953 my @CHECKs = B::check_av->isa("B::AV") ? B::check_av->ARRAY : ();
954 my @INITs = B::init_av->isa("B::AV") ? B::init_av->ARRAY : ();
955 my @ENDs = B::end_av->isa("B::AV") ? B::end_av->ARRAY : ();
956 my @names = qw(BEGIN UNITCHECK CHECK INIT END);
957 my @blocks = \(@BEGINs, @UNITCHECKs, @CHECKs, @INITs, @ENDs);
959 my ($name, $blocks) = (shift @names, shift @blocks);
960 for my $block (@$blocks) {
961 $self->todo($block, 0, $name);
965 local($SIG{"__DIE__"}) =
967 if ($self->{'curcop'}) {
968 my $cop = $self->{'curcop'};
969 my($line, $file) = ($cop->line, $cop->file);
970 print STDERR "While deparsing $file near line $line,\n";
973 $self->{'curcv'} = main_cv;
974 $self->{'curcvlex'} = undef;
975 print $self->print_protos;
976 @{$self->{'subs_todo'}} =
977 sort {$a->[0] <=> $b->[0]} @{$self->{'subs_todo'}};
978 my $root = main_root;
979 local $B::overlay = {};
980 unless (null $root) {
981 $self->pad_subs($self->{'curcv'});
982 # Check for a stub-followed-by-ex-cop, resulting from a program
983 # consisting solely of sub declarations. For backward-compati-
984 # bility (and sane output) we don’t want to emit the stub.
988 # ex-nextstate (or ex-dbstate)
990 if ( $root->name eq 'leave'
991 and ($kid = $root->first)->name eq 'enter'
992 and !null($kid = $kid->sibling) and $kid->name eq 'stub'
993 and !null($kid = $kid->sibling) and $kid->name eq 'null'
994 and class($kid) eq 'COP' and null $kid->sibling )
998 $self->pessimise($root, main_start);
999 print $self->indent($self->deparse_root($root)), "\n";
1003 while (scalar(@{$self->{'subs_todo'}})) {
1004 push @text, $self->next_todo;
1006 print $self->indent(join("", @text)), "\n" if @text;
1008 # Print __DATA__ section, if necessary
1010 my $laststash = defined $self->{'curcop'}
1011 ? $self->{'curcop'}->stash->NAME : $self->{'curstash'};
1012 if (defined *{$laststash."::DATA"}{IO}) {
1013 print $self->keyword("package") . " $laststash;\n"
1014 unless $laststash eq $self->{'curstash'};
1015 print $self->keyword("__DATA__") . "\n";
1016 print readline(*{$laststash."::DATA"});
1024 croak "Usage: ->coderef2text(CODEREF)" unless UNIVERSAL::isa($sub, "CODE");
1027 local $self->{in_coderef2text} = 1;
1028 return $self->indent($self->deparse_sub(svref_2object($sub)));
1031 my %strict_bits = do {
1033 map +($_ => strict::bits($_)), qw/refs subs vars/
1036 sub ambient_pragmas {
1038 my ($arybase, $hint_bits, $warning_bits, $hinthash) = (0, 0);
1044 if ($name eq 'strict') {
1047 if ($val eq 'none') {
1048 $hint_bits &= $strict_bits{$_} for qw/refs subs vars/;
1053 if ($val eq "all") {
1054 @names = qw/refs subs vars/;
1060 @names = split' ', $val;
1062 $hint_bits |= $strict_bits{$_} for @names;
1065 elsif ($name eq '$[') {
1066 if (OPpCONST_ARYBASE) {
1069 croak "\$[ can't be non-zero on this perl" unless $val == 0;
1073 elsif ($name eq 'integer'
1075 || $name eq 'utf8') {
1078 $hint_bits |= ${$::{"${name}::"}{"hint_bits"}};
1081 $hint_bits &= ~${$::{"${name}::"}{"hint_bits"}};
1085 elsif ($name eq 're') {
1087 if ($val eq 'none') {
1088 $hint_bits &= ~re::bits(qw/taint eval/);
1093 if ($val eq 'all') {
1094 @names = qw/taint eval/;
1100 @names = split' ',$val;
1102 $hint_bits |= re::bits(@names);
1105 elsif ($name eq 'warnings') {
1106 if ($val eq 'none') {
1107 $warning_bits = $warnings::NONE;
1116 @names = split/\s+/, $val;
1119 $warning_bits = $warnings::NONE if !defined ($warning_bits);
1120 $warning_bits |= warnings::bits(@names);
1123 elsif ($name eq 'warning_bits') {
1124 $warning_bits = $val;
1127 elsif ($name eq 'hint_bits') {
1131 elsif ($name eq '%^H') {
1136 croak "Unknown pragma type: $name";
1140 croak "The ambient_pragmas method expects an even number of args";
1143 $self->{'ambient_arybase'} = $arybase;
1144 $self->{'ambient_warnings'} = $warning_bits;
1145 $self->{'ambient_hints'} = $hint_bits;
1146 $self->{'ambient_hinthash'} = $hinthash;
1149 # This method is the inner loop, so try to keep it simple
1154 Carp::confess("Null op in deparse") if !defined($op)
1155 || class($op) eq "NULL";
1156 my $meth = "pp_" . $op->name;
1157 return $self->$meth($op, $cx);
1163 # \cK also swallows a preceding line break when followed by a
1165 $txt =~ s/\n\cK;//g;
1166 my @lines = split(/\n/, $txt);
1170 for $line (@lines) {
1171 my $cmd = substr($line, 0, 1);
1172 if ($cmd eq "\t" or $cmd eq "\b") {
1173 $level += ($cmd eq "\t" ? 1 : -1) * $self->{'indent_size'};
1174 if ($self->{'use_tabs'}) {
1175 $leader = "\t" x ($level / 8) . " " x ($level % 8);
1177 $leader = " " x $level;
1179 $line = substr($line, 1);
1181 if (index($line, "\f") > 0) {
1184 if (substr($line, 0, 1) eq "\f") {
1185 $line = substr($line, 1); # no indent
1187 $line = $leader . $line;
1189 $line =~ s/\cK;?//g;
1191 return join("\n", @lines);
1195 my ($self, $cv) = @_;
1196 my $padlist = $cv->PADLIST;
1197 my @names = $padlist->ARRAYelt(0)->ARRAY;
1198 my @values = $padlist->ARRAYelt(1)->ARRAY;
1201 for my $ix (0.. $#names) { for $_ ($names[$ix]) {
1202 next if class($_) eq "SPECIAL";
1204 if (defined $name && $name =~ /^&./) {
1205 my $low = $_->COP_SEQ_RANGE_LOW;
1206 my $flags = $_->FLAGS;
1207 my $outer = $flags & PADNAMEt_OUTER;
1208 if ($flags & SVpad_OUR) {
1209 push @todo, [$low, undef, 0, $_]
1210 # [seq, no cv, not format, padname]
1214 my $protocv = $flags & SVpad_STATE
1217 if (class ($protocv) ne 'CV') {
1221 while ($flags & PADNAMEt_OUTER && class ($protocv) ne 'CV')
1224 next PADENTRY if class($cv) eq 'SPECIAL'; # XXX freed?
1225 my $padlist = $cv->PADLIST;
1226 my $ix = $name->PARENT_PAD_INDEX;
1227 $name = $padlist->NAMES->ARRAYelt($ix);
1228 $flags = $name->FLAGS;
1229 $protocv = $flags & SVpad_STATE
1230 ? $padlist->ARRAYelt(1)->ARRAYelt($ix)
1234 my $defined_in_this_sub = ${$protocv->OUTSIDE} == $$cv || do {
1235 my $other = $protocv->PADLIST;
1236 $$other && $other->outid == $padlist->id;
1238 if ($flags & PADNAMEt_OUTER) {
1239 next unless $defined_in_this_sub;
1240 push @todo, [$protocv->OUTSIDE_SEQ, $protocv, 0, $_];
1243 my $outseq = $protocv->OUTSIDE_SEQ;
1244 if ($outseq <= $low) {
1245 # defined before its name is visible, so it’s gotta be
1246 # declared and defined at once: my sub foo { ... }
1247 push @todo, [$low, $protocv, 0, $_];
1250 # declared and defined separately: my sub f; sub f { ... }
1251 push @todo, [$low, undef, 0, $_];
1252 push @todo, [$outseq, $protocv, 0, $_]
1253 if $defined_in_this_sub;
1257 @{$self->{'subs_todo'}} =
1258 sort {$a->[0] <=> $b->[0]} @{$self->{'subs_todo'}}, @todo
1262 # deparse_argops(): deparse, if possible, a sequence of argcheck + argelem
1263 # ops into a subroutine signature. If successful, return the first op
1264 # following the signature ops plus the signature string; else return the
1267 # Normally a bunch of argelem ops will have been generated by the
1268 # signature parsing, but it's possible that ops have been added manually
1269 # or altered. In this case we "return ()" and fall back to general
1270 # deparsing of the individual sigelems as 'my $x = $_[N]' etc.
1272 # We're only called if the first two ops are nextstate and argcheck.
1274 sub deparse_argops {
1275 my ($self, $firstop, $cv) = @_;
1279 return if $o->label; #first nextstate;
1284 my ($params, $opt_params, $slurpy) = $o->aux_list($cv);
1285 my $mandatory = $params - $opt_params;
1286 my $seen_slurpy = 0;
1289 # keep looking for valid nextstate + argelem pairs
1295 last unless $o->name =~ /^(next|db)state$/;
1299 my $o2 = $o->sibling;
1302 if ($o2->name eq 'argelem') {
1303 my $ix = $o2->string($cv);
1304 while (++$last_ix < $ix) {
1305 push @sig, $last_ix < $mandatory ? '$' : '$=';
1307 my $var = $self->padname($o2->targ);
1308 if ($var =~ /^[@%]/) {
1309 return if $seen_slurpy;
1311 return if $ix != $params or !$slurpy
1312 or substr($var,0,1) ne $slurpy;
1315 return if $ix >= $params;
1317 if ($o2->flags & OPf_KIDS) {
1318 my $kid = $o2->first;
1319 return unless $$kid and $kid->name eq 'argdefelem';
1320 my $def = $self->deparse($kid->first, 7);
1321 $def = "($def)" if $kid->first->flags & OPf_PARENS;
1326 elsif ($o2->name eq 'null'
1327 and ($o2->flags & OPf_KIDS)
1328 and $o2->first->name eq 'argdefelem')
1330 # special case - a void context default expression: $ = expr
1332 my $defop = $o2->first;
1333 my $ix = $defop->targ;
1334 while (++$last_ix < $ix) {
1335 push @sig, $last_ix < $mandatory ? '$' : '$=';
1337 return if $last_ix >= $params
1338 or $last_ix < $mandatory;
1339 my $def = $self->deparse($defop->first, 7);
1340 $def = "($def)" if $defop->first->flags & OPf_PARENS;
1341 push @sig, '$ = ' . $def;
1350 while (++$last_ix < $params) {
1351 push @sig, $last_ix < $mandatory ? '$' : '$=';
1353 push @sig, $slurpy if $slurpy and !$seen_slurpy;
1355 return ($o, join(', ', @sig));
1358 # Deparse a sub. Returns everything except the 'sub foo',
1359 # e.g. ($$) : method { ...; }
1360 # or ($a, $b) : prototype($$) lvalue;
1366 my $protosig; # prototype or signature (what goes in the (....))
1368 Carp::confess("NULL in deparse_sub") if !defined($cv) || $cv->isa("B::NULL");
1369 Carp::confess("SPECIAL in deparse_sub") if $cv->isa("B::SPECIAL");
1370 local $self->{'curcop'} = $self->{'curcop'};
1372 my $has_sig = $self->{hinthash}{feature_signatures};
1373 if ($cv->FLAGS & SVf_POK) {
1374 my $proto = $cv->PV;
1376 push @attrs, "prototype($proto)";
1382 if ($cv->CvFLAGS & (CVf_METHOD|CVf_LOCKED|CVf_LVALUE|CVf_ANONCONST)) {
1383 push @attrs, "lvalue" if $cv->CvFLAGS & CVf_LVALUE;
1384 push @attrs, "method" if $cv->CvFLAGS & CVf_METHOD;
1385 push @attrs, "const" if $cv->CvFLAGS & CVf_ANONCONST;
1388 local($self->{'curcv'}) = $cv;
1389 local($self->{'curcvlex'});
1390 local(@$self{qw'curstash warnings hints hinthash'})
1391 = @$self{qw'curstash warnings hints hinthash'};
1393 my $root = $cv->ROOT;
1394 local $B::overlay = {};
1395 if (not null $root) {
1396 $self->pad_subs($cv);
1397 $self->pessimise($root, $cv->START);
1398 my $lineseq = $root->first;
1399 if ($lineseq->name eq "lineseq") {
1400 my $firstop = $lineseq->first;
1404 # try to deparse first few ops as a signature if possible
1406 and $firstop->name =~ /^(next|db)state$/
1407 and (($o2 = $firstop->sibling))
1410 if ($o2->name eq 'argcheck') {
1411 my ($nexto, $sig) = $self->deparse_argops($firstop, $cv);
1412 if (defined $nexto) {
1421 for (my $o = $firstop; $$o; $o=$o->sibling) {
1424 $body = $self->lineseq(undef, 0, @ops).";";
1425 if (!$has_sig and $ops[-1]->name =~ /^(next|db)state$/) {
1426 # this handles void context in
1427 # use feature signatures; sub ($=1) {}
1430 my $scope_en = $self->find_scope_en($lineseq);
1431 if (defined $scope_en) {
1432 my $subs = join"", $self->seq_subs($scope_en);
1433 $body .= ";\n$subs" if length($subs);
1437 $body = $self->deparse($root->first, 0);
1439 $body = "{\n\t$body\n\b}";
1442 my $sv = $cv->const_sv;
1444 # uh-oh. inlinable sub... format it differently
1445 $body = "{ " . $self->const($sv, 0) . " }\n";
1446 } else { # XSUB? (or just a declaration)
1450 $protosig = defined $protosig ? "($protosig) " : "";
1452 $attrs = ': ' . join('', map "$_ ", @attrs) if @attrs;
1453 return "$protosig$attrs$body\n";
1456 sub deparse_format {
1460 local($self->{'curcv'}) = $form;
1461 local($self->{'curcvlex'});
1462 local($self->{'in_format'}) = 1;
1463 local(@$self{qw'curstash warnings hints hinthash'})
1464 = @$self{qw'curstash warnings hints hinthash'};
1465 my $op = $form->ROOT;
1466 local $B::overlay = {};
1467 $self->pessimise($op, $form->START);
1469 return "\f." if $op->first->name eq 'stub'
1470 || $op->first->name eq 'nextstate';
1471 $op = $op->first->first; # skip leavewrite, lineseq
1472 while (not null $op) {
1473 $op = $op->sibling; # skip nextstate
1475 $kid = $op->first->sibling; # skip pushmark
1476 push @text, "\f".$self->const_sv($kid)->PV;
1477 $kid = $kid->sibling;
1478 for (; not null $kid; $kid = $kid->sibling) {
1479 push @exprs, $self->deparse($kid, -1);
1480 $exprs[-1] =~ s/;\z//;
1482 push @text, "\f".join(", ", @exprs)."\n" if @exprs;
1485 return join("", @text) . "\f.";
1490 return $op->name eq "leave" || $op->name eq "scope"
1491 || $op->name eq "lineseq"
1492 || ($op->name eq "null" && class($op) eq "UNOP"
1493 && (is_scope($op->first) || $op->first->name eq "enter"));
1497 my $name = $_[0]->name;
1498 return $name eq "nextstate" || $name eq "dbstate" || $name eq "setstate";
1501 sub is_miniwhile { # check for one-line loop ('foo() while $y--')
1503 return (!null($op) and null($op->sibling)
1504 and $op->name eq "null" and class($op) eq "UNOP"
1505 and (($op->first->name =~ /^(and|or)$/
1506 and $op->first->first->sibling->name eq "lineseq")
1507 or ($op->first->name eq "lineseq"
1508 and not null $op->first->first->sibling
1509 and $op->first->first->sibling->name eq "unstack")
1513 # Check if the op and its sibling are the initialization and the rest of a
1514 # for (..;..;..) { ... } loop
1517 # This OP might be almost anything, though it won't be a
1518 # nextstate. (It's the initialization, so in the canonical case it
1519 # will be an sassign.) The sibling is (old style) a lineseq whose
1520 # first child is a nextstate and whose second is a leaveloop, or
1521 # (new style) an unstack whose sibling is a leaveloop.
1522 my $lseq = $op->sibling;
1523 return 0 unless !is_state($op) and !null($lseq);
1524 if ($lseq->name eq "lineseq") {
1525 if ($lseq->first && !null($lseq->first) && is_state($lseq->first)
1526 && (my $sib = $lseq->first->sibling)) {
1527 return (!null($sib) && $sib->name eq "leaveloop");
1529 } elsif ($lseq->name eq "unstack" && ($lseq->flags & OPf_SPECIAL)) {
1530 my $sib = $lseq->sibling;
1531 return $sib && !null($sib) && $sib->name eq "leaveloop";
1538 return ($op->name eq "rv2sv" or
1539 $op->name eq "padsv" or
1540 $op->name eq "gv" or # only in array/hash constructs
1541 $op->flags & OPf_KIDS && !null($op->first)
1542 && $op->first->name eq "gvsv");
1547 my($text, $cx, $prec) = @_;
1548 if ($prec < $cx # unary ops nest just fine
1549 or $prec == $cx and $cx != 4 and $cx != 16 and $cx != 21
1550 or $self->{'parens'})
1553 # In a unop, let parent reuse our parens; see maybe_parens_unop
1554 $text = "\cS" . $text if $cx == 16;
1561 # same as above, but get around the 'if it looks like a function' rule
1562 sub maybe_parens_unop {
1564 my($name, $kid, $cx) = @_;
1565 if ($cx > 16 or $self->{'parens'}) {
1566 $kid = $self->deparse($kid, 1);
1567 if ($name eq "umask" && $kid =~ /^\d+$/) {
1568 $kid = sprintf("%#o", $kid);
1570 return $self->keyword($name) . "($kid)";
1572 $kid = $self->deparse($kid, 16);
1573 if ($name eq "umask" && $kid =~ /^\d+$/) {
1574 $kid = sprintf("%#o", $kid);
1576 $name = $self->keyword($name);
1577 if (substr($kid, 0, 1) eq "\cS") {
1579 return $name . substr($kid, 1);
1580 } elsif (substr($kid, 0, 1) eq "(") {
1581 # avoid looks-like-a-function trap with extra parens
1582 # ('+' can lead to ambiguities)
1583 return "$name(" . $kid . ")";
1585 return "$name $kid";
1590 sub maybe_parens_func {
1592 my($func, $text, $cx, $prec) = @_;
1593 if ($prec <= $cx or substr($text, 0, 1) eq "(" or $self->{'parens'}) {
1594 return "$func($text)";
1596 return "$func $text";
1601 my ($self, $name) = @_;
1602 $self->populate_curcvlex() if !defined $self->{'curcvlex'};
1603 my $seq = $self->{'curcop'} ? $self->{'curcop'}->cop_seq : 0;
1604 for my $a (@{$self->{'curcvlex'}{"o$name"}}) {
1605 my ($st, undef, $padname) = @$a;
1606 if ($st >= $seq && $padname->FLAGS & SVpad_TYPED) {
1607 return $padname->SvSTASH->NAME;
1615 my($op, $cx, $text) = @_;
1616 my $name = $op->name;
1617 my $our_intro = ($name =~ /^(?:(?:gv|rv2)[ash]v|split|refassign
1621 my $lval_intro = $name eq 'split' ? 0 : OPpLVAL_INTRO;
1622 # The @a in \(@a) isn't in ref context, but only when the
1624 my $need_parens = $self->{'in_refgen'} && $name =~ /[ah]v\z/
1625 && ($op->flags & (OPf_PARENS|OPf_REF)) == OPf_PARENS;
1626 if ((my $priv = $op->private) & ($lval_intro|$our_intro)) {
1628 push @our_local, "local" if $priv & $lval_intro;
1629 push @our_local, "our" if $priv & $our_intro;
1630 my $our_local = join " ", map $self->keyword($_), @our_local;
1631 if( $our_local[-1] eq 'our' ) {
1632 if ( $text !~ /^\W(\w+::)*\w+\z/
1633 and !utf8::decode($text) || $text !~ /^\W(\w+::)*\w+\z/
1635 die "Unexpected our($text)\n";
1637 $text =~ s/(\w+::)+//;
1639 if (my $type = $self->find_our_type($text)) {
1640 $our_local .= ' ' . $type;
1643 return $need_parens ? "($text)" : $text
1644 if $self->{'avoid_local'}{$$op};
1646 return "$our_local($text)";
1647 } elsif (want_scalar($op)) {
1648 return "$our_local $text";
1650 return $self->maybe_parens_func("$our_local", $text, $cx, 16);
1653 return $need_parens ? "($text)" : $text;
1659 my($op, $cx, $func, @args) = @_;
1660 if ($op->private & OPpTARGET_MY) {
1661 my $var = $self->padname($op->targ);
1662 my $val = $func->($self, $op, 7, @args);
1663 return $self->maybe_parens("$var = $val", $cx, 7);
1665 return $func->($self, $op, $cx, @args);
1672 return $self->{'curcv'}->PADLIST->ARRAYelt(0)->ARRAYelt($targ);
1677 my($op, $cx, $text, $padname, $forbid_parens) = @_;
1678 # The @a in \(@a) isn't in ref context, but only when the
1680 my $need_parens = !$forbid_parens && $self->{'in_refgen'}
1681 && $op->name =~ /[ah]v\z/
1682 && ($op->flags & (OPf_PARENS|OPf_REF)) == OPf_PARENS;
1683 # The @a in \my @a must not have parens.
1684 if (!$need_parens && $self->{'in_refgen'}) {
1687 if ($op->private & OPpLVAL_INTRO and not $self->{'avoid_local'}{$$op}) {
1688 # Check $padname->FLAGS for statehood, rather than $op->private,
1689 # because enteriter ops do not carry the flag.
1691 $self->keyword($padname->FLAGS & SVpad_STATE ? "state" : "my");
1692 if ($padname->FLAGS & SVpad_TYPED) {
1693 $my .= ' ' . $padname->SvSTASH->NAME;
1696 return "$my($text)";
1697 } elsif ($forbid_parens || want_scalar($op)) {
1700 return $self->maybe_parens_func($my, $text, $cx, 16);
1703 return $need_parens ? "($text)" : $text;
1707 # The following OPs don't have functions:
1709 # pp_padany -- does not exist after parsing
1712 if ($AUTOLOAD =~ s/^.*::pp_//) {
1713 warn "unexpected OP_".
1714 ($_[1]->type == OP_CUSTOM ? "CUSTOM ($AUTOLOAD)" : uc $AUTOLOAD);
1717 die "Undefined subroutine $AUTOLOAD called";
1721 sub DESTROY {} # Do not AUTOLOAD
1723 # $root should be the op which represents the root of whatever
1724 # we're sequencing here. If it's undefined, then we don't append
1725 # any subroutine declarations to the deparsed ops, otherwise we
1726 # append appropriate declarations.
1728 my($self, $root, $cx, @ops) = @_;
1731 my $out_cop = $self->{'curcop'};
1732 my $out_seq = defined($out_cop) ? $out_cop->cop_seq : undef;
1734 if (defined $root) {
1735 $limit_seq = $out_seq;
1737 $nseq = $self->find_scope_st($root->sibling) if ${$root->sibling};
1738 $limit_seq = $nseq if !defined($limit_seq)
1739 or defined($nseq) && $nseq < $limit_seq;
1741 $limit_seq = $self->{'limit_seq'}
1742 if defined($self->{'limit_seq'})
1743 && (!defined($limit_seq) || $self->{'limit_seq'} < $limit_seq);
1744 local $self->{'limit_seq'} = $limit_seq;
1746 $self->walk_lineseq($root, \@ops,
1747 sub { push @exprs, $_[0]} );
1749 my $sep = $cx ? '; ' : ";\n";
1750 my $body = join($sep, grep {length} @exprs);
1752 if (defined $root && defined $limit_seq && !$self->{'in_format'}) {
1753 $subs = join "\n", $self->seq_subs($limit_seq);
1755 return join($sep, grep {length} $body, $subs);
1759 my($real_block, $self, $op, $cx) = @_;
1763 local(@$self{qw'curstash warnings hints hinthash'})
1764 = @$self{qw'curstash warnings hints hinthash'} if $real_block;
1766 $kid = $op->first->sibling; # skip enter
1767 if (is_miniwhile($kid)) {
1768 my $top = $kid->first;
1769 my $name = $top->name;
1770 if ($name eq "and") {
1771 $name = $self->keyword("while");
1772 } elsif ($name eq "or") {
1773 $name = $self->keyword("until");
1774 } else { # no conditional -> while 1 or until 0
1775 return $self->deparse($top->first, 1) . " "
1776 . $self->keyword("while") . " 1";
1778 my $cond = $top->first;
1779 my $body = $cond->sibling->first; # skip lineseq
1780 $cond = $self->deparse($cond, 1);
1781 $body = $self->deparse($body, 1);
1782 return "$body $name $cond";
1787 for (; !null($kid); $kid = $kid->sibling) {
1790 if ($cx > 0) { # inside an expression, (a do {} while for lineseq)
1791 my $body = $self->lineseq($op, 0, @kids);
1792 return is_lexical_subs(@kids)
1794 : ($self->lex_in_scope("&do") ? "CORE::do" : "do")
1795 . " {\n\t$body\n\b}";
1797 my $lineseq = $self->lineseq($op, $cx, @kids);
1798 return (length ($lineseq) ? "$lineseq;" : "");
1802 sub pp_scope { scopeop(0, @_); }
1803 sub pp_lineseq { scopeop(0, @_); }
1804 sub pp_leave { scopeop(1, @_); }
1806 # This is a special case of scopeop and lineseq, for the case of the
1807 # main_root. The difference is that we print the output statements as
1808 # soon as we get them, for the sake of impatient users.
1812 local(@$self{qw'curstash warnings hints hinthash'})
1813 = @$self{qw'curstash warnings hints hinthash'};
1815 return if null $op->first; # Can happen, e.g., for Bytecode without -k
1816 for (my $kid = $op->first->sibling; !null($kid); $kid = $kid->sibling) {
1819 $self->walk_lineseq($op, \@kids,
1820 sub { return unless length $_[0];
1821 print $self->indent($_[0].';');
1823 unless $_[1] == $#kids;
1828 my ($self, $op, $kids, $callback) = @_;
1830 for (my $i = 0; $i < @kids; $i++) {
1832 if (is_state $kids[$i]) {
1833 $expr = $self->deparse($kids[$i++], 0);
1835 $callback->($expr, $i);
1839 if (is_for_loop($kids[$i])) {
1840 $callback->($expr . $self->for_loop($kids[$i], 0),
1841 $i += $kids[$i]->sibling->name eq "unstack" ? 2 : 1);
1844 my $expr2 = $self->deparse($kids[$i], (@kids != 1)/2);
1845 $expr2 =~ s/^sub :(?!:)/+sub :/; # statement label otherwise
1847 $callback->($expr, $i);
1851 # The BEGIN {} is used here because otherwise this code isn't executed
1852 # when you run B::Deparse on itself.
1854 BEGIN { map($globalnames{$_}++, "SIG", "STDIN", "STDOUT", "STDERR", "INC",
1855 "ENV", "ARGV", "ARGVOUT", "_"); }
1861 #Carp::confess() unless ref($gv) eq "B::GV";
1862 my $cv = $gv->FLAGS & SVf_ROK ? $gv->RV : 0;
1863 my $stash = ($cv || $gv)->STASH->NAME;
1865 ? $cv ? $cv->NAME_HEK || $cv->GV->NAME : $gv->NAME
1867 ? B::safename($cv->NAME_HEK || $cv->GV->NAME)
1869 if ($stash eq 'main' && $name =~ /^::/) {
1872 elsif (($stash eq 'main'
1873 && ($globalnames{$name} || $name =~ /^[^A-Za-z_:]/))
1874 or ($stash eq $self->{'curstash'} && !$globalnames{$name}
1875 && ($stash eq 'main' || $name !~ /::/))
1880 $stash = $stash . "::";
1882 if (!$raw and $name =~ /^(\^..|{)/) {
1883 $name = "{$name}"; # ${^WARNING_BITS}, etc and ${
1885 return $stash . $name;
1888 # Return the name to use for a stash variable.
1889 # If a lexical with the same name is in scope, or
1890 # if strictures are enabled, it may need to be
1892 sub stash_variable {
1893 my ($self, $prefix, $name, $cx) = @_;
1895 return "$prefix$name" if $name =~ /::/;
1897 unless ($prefix eq '$' || $prefix eq '@' || $prefix eq '&' || #'
1898 $prefix eq '%' || $prefix eq '$#') {
1899 return "$prefix$name";
1902 if ($name =~ /^[^[:alpha:]_+-]$/) {
1903 if (defined $cx && $cx == 26) {
1904 if ($prefix eq '@') {
1905 return "$prefix\{$name}";
1907 elsif ($name eq '#') { return '${#}' } # "${#}a" vs "$#a"
1909 if ($prefix eq '$#') {
1910 return "\$#{$name}";
1914 return $prefix . $self->maybe_qualify($prefix, $name);
1917 my %unctrl = # portable to EBCDIC
1919 "\c@" => '@', # unused
1946 "\c[" => '[', # unused
1947 "\c\\" => '\\', # unused
1948 "\c]" => ']', # unused
1949 "\c_" => '_', # unused
1952 # Return just the name, without the prefix. It may be returned as a quoted
1953 # string. The second return value is a boolean indicating that.
1954 sub stash_variable_name {
1955 my($self, $prefix, $gv) = @_;
1956 my $name = $self->gv_name($gv, 1);
1957 $name = $self->maybe_qualify($prefix,$name);
1958 if ($name =~ /^(?:\S|(?!\d)[\ca-\cz]?(?:\w|::)*|\d+)\z/) {
1959 $name =~ s/^([\ca-\cz])/'^' . $unctrl{$1}/e;
1960 $name =~ /^(\^..|{)/ and $name = "{$name}";
1961 return $name, 0; # not quoted
1964 single_delim("q", "'", $name, $self), 1;
1969 my ($self,$prefix,$name) = @_;
1970 my $v = ($prefix eq '$#' ? '@' : $prefix) . $name;
1971 return $name if !$prefix || $name =~ /::/;
1972 return $self->{'curstash'}.'::'. $name
1974 $name =~ /^(?!\d)\w/ # alphabetic
1975 && $v !~ /^\$[ab]\z/ # not $a or $b
1976 && !$globalnames{$name} # not a global name
1977 && $self->{hints} & $strict_bits{vars} # strict vars
1978 && !$self->lex_in_scope($v,1) # no "our"
1979 or $self->lex_in_scope($v); # conflicts with "my" variable
1984 my ($self, $name, $our) = @_;
1985 substr $name, 0, 0, = $our ? 'o' : 'm'; # our/my
1986 $self->populate_curcvlex() if !defined $self->{'curcvlex'};
1988 return 0 if !defined($self->{'curcop'});
1989 my $seq = $self->{'curcop'}->cop_seq;
1990 return 0 if !exists $self->{'curcvlex'}{$name};
1991 for my $a (@{$self->{'curcvlex'}{$name}}) {
1992 my ($st, $en) = @$a;
1993 return 1 if $seq > $st && $seq <= $en;
1998 sub populate_curcvlex {
2000 for (my $cv = $self->{'curcv'}; class($cv) eq "CV"; $cv = $cv->OUTSIDE) {
2001 my $padlist = $cv->PADLIST;
2002 # an undef CV still in lexical chain
2003 next if class($padlist) eq "SPECIAL";
2004 my @padlist = $padlist->ARRAY;
2005 my @ns = $padlist[0]->ARRAY;
2007 for (my $i=0; $i<@ns; ++$i) {
2008 next if class($ns[$i]) eq "SPECIAL";
2009 if (class($ns[$i]) eq "PV") {
2010 # Probably that pesky lexical @_
2013 my $name = $ns[$i]->PVX;
2014 next unless defined $name;
2015 my ($seq_st, $seq_en) =
2016 ($ns[$i]->FLAGS & SVf_FAKE)
2018 : ($ns[$i]->COP_SEQ_RANGE_LOW, $ns[$i]->COP_SEQ_RANGE_HIGH);
2020 push @{$self->{'curcvlex'}{
2021 ($ns[$i]->FLAGS & SVpad_OUR ? 'o' : 'm') . $name
2022 }}, [$seq_st, $seq_en, $ns[$i]];
2027 sub find_scope_st { ((find_scope(@_))[0]); }
2028 sub find_scope_en { ((find_scope(@_))[1]); }
2030 # Recurses down the tree, looking for pad variable introductions and COPs
2032 my ($self, $op, $scope_st, $scope_en) = @_;
2033 carp("Undefined op in find_scope") if !defined $op;
2034 return ($scope_st, $scope_en) unless $op->flags & OPf_KIDS;
2037 while(my $op = shift @queue ) {
2038 for (my $o=$op->first; $$o; $o=$o->sibling) {
2039 if ($o->name =~ /^pad.v$/ && $o->private & OPpLVAL_INTRO) {
2040 my $s = int($self->padname_sv($o->targ)->COP_SEQ_RANGE_LOW);
2041 my $e = $self->padname_sv($o->targ)->COP_SEQ_RANGE_HIGH;
2042 $scope_st = $s if !defined($scope_st) || $s < $scope_st;
2043 $scope_en = $e if !defined($scope_en) || $e > $scope_en;
2044 return ($scope_st, $scope_en);
2046 elsif (is_state($o)) {
2047 my $c = $o->cop_seq;
2048 $scope_st = $c if !defined($scope_st) || $c < $scope_st;
2049 $scope_en = $c if !defined($scope_en) || $c > $scope_en;
2050 return ($scope_st, $scope_en);
2052 elsif ($o->flags & OPf_KIDS) {
2053 unshift (@queue, $o);
2058 return ($scope_st, $scope_en);
2061 # Returns a list of subs which should be inserted before the COP
2063 my ($self, $op, $out_seq) = @_;
2064 my $seq = $op->cop_seq;
2065 if ($] < 5.021006) {
2066 # If we have nephews, then our sequence number indicates
2067 # the cop_seq of the end of some sort of scope.
2068 if (class($op->sibling) ne "NULL" && $op->sibling->flags & OPf_KIDS
2069 and my $nseq = $self->find_scope_st($op->sibling) ) {
2073 $seq = $out_seq if defined($out_seq) && $out_seq < $seq;
2074 return $self->seq_subs($seq);
2078 my ($self, $seq) = @_;
2080 #push @text, "# ($seq)\n";
2082 return "" if !defined $seq;
2084 while (scalar(@{$self->{'subs_todo'}})
2085 and $seq > $self->{'subs_todo'}[0][0]) {
2086 my $cv = $self->{'subs_todo'}[0][1];
2087 # Skip the OUTSIDE check for lexical subs. We may be deparsing a
2088 # cloned anon sub with lexical subs declared in it, in which case
2089 # the OUTSIDE pointer points to the anon protosub.
2090 my $lexical = ref $self->{'subs_todo'}[0][3];
2091 my $outside = !$lexical && $cv && $cv->OUTSIDE;
2092 if (!$lexical and $cv
2093 and ${$cv->OUTSIDE || \0} != ${$self->{'curcv'}})
2095 push @pending, shift @{$self->{'subs_todo'}};
2098 push @text, $self->next_todo;
2100 unshift @{$self->{'subs_todo'}}, @pending;
2104 sub _features_from_bundle {
2105 my ($hints, $hh) = @_;
2106 foreach (@{$feature::feature_bundle{@feature::hint_bundles[$hints >> $feature::hint_shift]}}) {
2107 $hh->{$feature::feature{$_}} = 1;
2112 # generate any pragmas, 'package foo' etc needed to synchronise
2113 # with the given cop
2121 my $stash = $op->stashpv;
2122 if ($stash ne $self->{'curstash'}) {
2123 push @text, $self->keyword("package") . " $stash;\n";
2124 $self->{'curstash'} = $stash;
2127 if (OPpCONST_ARYBASE && $self->{'arybase'} != $op->arybase) {
2128 push @text, '$[ = '. $op->arybase .";\n";
2129 $self->{'arybase'} = $op->arybase;
2132 my $warnings = $op->warnings;
2134 if ($warnings->isa("B::SPECIAL") && $$warnings == 4) {
2135 $warning_bits = $warnings::Bits{"all"} & WARN_MASK;
2137 elsif ($warnings->isa("B::SPECIAL") && $$warnings == 5) {
2138 $warning_bits = $warnings::NONE;
2140 elsif ($warnings->isa("B::SPECIAL")) {
2141 $warning_bits = undef;
2144 $warning_bits = $warnings->PV & WARN_MASK;
2147 if (defined ($warning_bits) and
2148 !defined($self->{warnings}) || $self->{'warnings'} ne $warning_bits) {
2150 $self->declare_warnings($self->{'warnings'}, $warning_bits);
2151 $self->{'warnings'} = $warning_bits;
2154 my $hints = $] < 5.008009 ? $op->private : $op->hints;
2155 my $old_hints = $self->{'hints'};
2156 if ($self->{'hints'} != $hints) {
2157 push @text, $self->declare_hints($self->{'hints'}, $hints);
2158 $self->{'hints'} = $hints;
2163 $newhh = $op->hints_hash->HASH;
2166 if ($] >= 5.015006) {
2167 # feature bundle hints
2168 my $from = $old_hints & $feature::hint_mask;
2169 my $to = $ hints & $feature::hint_mask;
2171 if ($to == $feature::hint_mask) {
2172 if ($self->{'hinthash'}) {
2173 delete $self->{'hinthash'}{$_}
2174 for grep /^feature_/, keys %{$self->{'hinthash'}};
2176 else { $self->{'hinthash'} = {} }
2178 = _features_from_bundle($from, $self->{'hinthash'});
2182 $feature::hint_bundles[$to >> $feature::hint_shift];
2183 $bundle =~ s/(\d[13579])\z/$1+1/e; # 5.11 => 5.12
2185 $self->keyword("no") . " feature ':all';\n",
2186 $self->keyword("use") . " feature ':$bundle';\n";
2192 push @text, $self->declare_hinthash(
2193 $self->{'hinthash'}, $newhh,
2194 $self->{indent_size}, $self->{hints},
2196 $self->{'hinthash'} = $newhh;
2199 return join("", @text);
2203 # Notice how subs and formats are inserted between statements here;
2204 # also $[ assignments and pragmas.
2208 $self->{'curcop'} = $op;
2212 my @subs = $self->cop_subs($op);
2214 # Special marker to swallow up the semicolon
2219 push @text, $self->pragmata($op);
2222 # This should go after of any branches that add statements, to
2223 # increase the chances that it refers to the same line it did in
2224 # the original program.
2225 if ($self->{'linenums'} && $cx != .5) { # $cx == .5 means in a format
2226 push @text, "\f#line " . $op->line .
2227 ' "' . $op->file, qq'"\n';
2230 push @text, $op->label . ": " if $op->label;
2232 return join("", @text);
2235 sub declare_warnings {
2236 my ($self, $from, $to) = @_;
2237 if (($to & WARN_MASK) eq (warnings::bits("all") & WARN_MASK)) {
2238 return $self->keyword("use") . " warnings;\n";
2240 elsif (($to & WARN_MASK) eq ("\0"x length($to) & WARN_MASK)) {
2241 return $self->keyword("no") . " warnings;\n";
2243 return "BEGIN {\${^WARNING_BITS} = \""
2244 . join("", map { sprintf("\\x%02x", ord $_) } split "", $to)
2249 my ($self, $from, $to) = @_;
2250 my $use = $to & ~$from;
2251 my $no = $from & ~$to;
2253 for my $pragma (hint_pragmas($use)) {
2254 $decls .= $self->keyword("use") . " $pragma;\n";
2256 for my $pragma (hint_pragmas($no)) {
2257 $decls .= $self->keyword("no") . " $pragma;\n";
2262 # Internal implementation hints that the core sets automatically, so don't need
2263 # (or want) to be passed back to the user
2264 my %ignored_hints = (
2275 sub declare_hinthash {
2276 my ($self, $from, $to, $indent, $hints) = @_;
2277 my $doing_features =
2278 ($hints & $feature::hint_mask) == $feature::hint_mask;
2281 my @unfeatures; # bugs?
2282 for my $key (sort keys %$to) {
2283 next if $ignored_hints{$key};
2284 my $is_feature = $key =~ /^feature_/ && $^V ge 5.15.6;
2285 next if $is_feature and not $doing_features;
2286 if (!exists $from->{$key} or $from->{$key} ne $to->{$key}) {
2287 push(@features, $key), next if $is_feature;
2289 qq(\$^H{) . single_delim("q", "'", $key, $self) . qq(} = )
2292 ? single_delim("q", "'", $to->{$key}, $self)
2298 for my $key (sort keys %$from) {
2299 next if $ignored_hints{$key};
2300 my $is_feature = $key =~ /^feature_/ && $^V ge 5.15.6;
2301 next if $is_feature and not $doing_features;
2302 if (!exists $to->{$key}) {
2303 push(@unfeatures, $key), next if $is_feature;
2304 push @decls, qq(delete \$^H{'$key'};);
2308 if (@features || @unfeatures) {
2309 if (!%rev_feature) { %rev_feature = reverse %feature::feature }
2312 push @ret, $self->keyword("use") . " feature "
2313 . join(", ", map "'$rev_feature{$_}'", @features) . ";\n";
2316 push @ret, $self->keyword("no") . " feature "
2317 . join(", ", map "'$rev_feature{$_}'", @unfeatures)
2322 join("\n" . (" " x $indent), "BEGIN {", @decls) . "\n}\n\cK";
2328 my (@pragmas, @strict);
2329 push @pragmas, "integer" if $bits & 0x1;
2330 for (sort keys %strict_bits) {
2331 push @strict, "'$_'" if $bits & $strict_bits{$_};
2333 if (@strict == keys %strict_bits) {
2334 push @pragmas, "strict";
2337 push @pragmas, "strict " . join ', ', @strict;
2339 push @pragmas, "bytes" if $bits & 0x8;
2343 sub pp_dbstate { pp_nextstate(@_) }
2344 sub pp_setstate { pp_nextstate(@_) }
2346 sub pp_unstack { return "" } # see also leaveloop
2348 my %feature_keywords = (
2349 # keyword => 'feature',
2354 default => 'switch',
2356 evalbytes=>'evalbytes',
2357 __SUB__ => '__SUB__',
2361 # keywords that are strong and also have a prototype
2363 my %strong_proto_keywords = map { $_ => 1 } qw(
2371 sub feature_enabled {
2372 my($self,$name) = @_;
2374 my $hints = $self->{hints} & $feature::hint_mask;
2375 if ($hints && $hints != $feature::hint_mask) {
2376 $hh = _features_from_bundle($hints);
2378 elsif ($hints) { $hh = $self->{'hinthash'} }
2379 return $hh && $hh->{"feature_$feature_keywords{$name}"}
2385 return $name if $name =~ /^CORE::/; # just in case
2386 if (exists $feature_keywords{$name}) {
2387 return "CORE::$name" if not $self->feature_enabled($name);
2389 # This sub may be called for a program that has no nextstate ops. In
2390 # that case we may have a lexical sub named no/use/sub in scope but
2391 # but $self->lex_in_scope will return false because it depends on the
2392 # current nextstate op. So we need this alternate method if there is
2394 if (!$self->{'curcop'}) {
2395 $self->populate_curcvlex() if !defined $self->{'curcvlex'};
2396 return "CORE::$name" if exists $self->{'curcvlex'}{"m&$name"}
2397 || exists $self->{'curcvlex'}{"o&$name"};
2398 } elsif ($self->lex_in_scope("&$name")
2399 || $self->lex_in_scope("&$name", 1)) {
2400 return "CORE::$name";
2402 if ($strong_proto_keywords{$name}
2403 || ($name !~ /^(?:chom?p|do|exec|glob|s(?:elect|ystem))\z/
2404 && !defined eval{prototype "CORE::$name"})
2407 exists $self->{subs_declared}{$name}
2409 exists &{"$self->{curstash}::$name"}
2411 return "CORE::$name"
2418 my($op, $cx, $name) = @_;
2419 return $self->keyword($name);
2422 sub pp_stub { "()" }
2423 sub pp_wantarray { baseop(@_, "wantarray") }
2424 sub pp_fork { baseop(@_, "fork") }
2425 sub pp_wait { maybe_targmy(@_, \&baseop, "wait") }
2426 sub pp_getppid { maybe_targmy(@_, \&baseop, "getppid") }
2427 sub pp_time { maybe_targmy(@_, \&baseop, "time") }
2428 sub pp_tms { baseop(@_, "times") }
2429 sub pp_ghostent { baseop(@_, "gethostent") }
2430 sub pp_gnetent { baseop(@_, "getnetent") }
2431 sub pp_gprotoent { baseop(@_, "getprotoent") }
2432 sub pp_gservent { baseop(@_, "getservent") }
2433 sub pp_ehostent { baseop(@_, "endhostent") }
2434 sub pp_enetent { baseop(@_, "endnetent") }
2435 sub pp_eprotoent { baseop(@_, "endprotoent") }
2436 sub pp_eservent { baseop(@_, "endservent") }
2437 sub pp_gpwent { baseop(@_, "getpwent") }
2438 sub pp_spwent { baseop(@_, "setpwent") }
2439 sub pp_epwent { baseop(@_, "endpwent") }
2440 sub pp_ggrent { baseop(@_, "getgrent") }
2441 sub pp_sgrent { baseop(@_, "setgrent") }
2442 sub pp_egrent { baseop(@_, "endgrent") }
2443 sub pp_getlogin { baseop(@_, "getlogin") }
2445 sub POSTFIX () { 1 }
2447 # I couldn't think of a good short name, but this is the category of
2448 # symbolic unary operators with interesting precedence
2452 my($op, $cx, $name, $prec, $flags) = (@_, 0);
2453 my $kid = $op->first;
2454 $kid = $self->deparse($kid, $prec);
2455 return $self->maybe_parens(($flags & POSTFIX)
2457 # avoid confusion with filetests
2459 && $kid =~ /^[a-zA-Z](?!\w)/
2465 sub pp_preinc { pfixop(@_, "++", 23) }
2466 sub pp_predec { pfixop(@_, "--", 23) }
2467 sub pp_postinc { maybe_targmy(@_, \&pfixop, "++", 23, POSTFIX) }
2468 sub pp_postdec { maybe_targmy(@_, \&pfixop, "--", 23, POSTFIX) }
2469 sub pp_i_preinc { pfixop(@_, "++", 23) }
2470 sub pp_i_predec { pfixop(@_, "--", 23) }
2471 sub pp_i_postinc { maybe_targmy(@_, \&pfixop, "++", 23, POSTFIX) }
2472 sub pp_i_postdec { maybe_targmy(@_, \&pfixop, "--", 23, POSTFIX) }
2473 sub pp_complement { maybe_targmy(@_, \&pfixop, "~", 21) }
2474 *pp_ncomplement = *pp_complement;
2475 sub pp_scomplement { maybe_targmy(@_, \&pfixop, "~.", 21) }
2477 sub pp_negate { maybe_targmy(@_, \&real_negate) }
2481 if ($op->first->name =~ /^(i_)?negate$/) {
2483 $self->pfixop($op, $cx, "-", 21.5);
2485 $self->pfixop($op, $cx, "-", 21);
2488 sub pp_i_negate { pp_negate(@_) }
2494 $self->listop($op, $cx, "not", $op->first);
2496 $self->pfixop($op, $cx, "!", 21);
2502 my($op, $cx, $name, $nollafr) = @_;
2504 if ($op->flags & OPf_KIDS) {
2507 # this deals with 'boolkeys' right now
2508 return $self->deparse($kid,$cx);
2510 my $builtinname = $name;
2511 $builtinname =~ /^CORE::/ or $builtinname = "CORE::$name";
2512 if (defined prototype($builtinname)
2513 && $builtinname ne 'CORE::readline'
2514 && prototype($builtinname) =~ /^;?\*/
2515 && $kid->name eq "rv2gv") {
2520 if (($kid = $self->deparse($kid, 16)) !~ s/^\cS//) {
2521 # require foo() is a syntax error.
2522 $kid =~ /^(?!\d)\w/ and $kid = "($kid)";
2524 return $self->maybe_parens(
2525 $self->keyword($name) . " $kid", $cx, 16
2528 return $self->maybe_parens_unop($name, $kid, $cx);
2530 return $self->maybe_parens(
2531 $self->keyword($name) . ($op->flags & OPf_SPECIAL ? "()" : ""),
2537 sub pp_chop { maybe_targmy(@_, \&unop, "chop") }
2538 sub pp_chomp { maybe_targmy(@_, \&unop, "chomp") }
2539 sub pp_schop { maybe_targmy(@_, \&unop, "chop") }
2540 sub pp_schomp { maybe_targmy(@_, \&unop, "chomp") }
2541 sub pp_defined { unop(@_, "defined") }
2542 sub pp_undef { unop(@_, "undef") }
2543 sub pp_study { unop(@_, "study") }
2544 sub pp_ref { unop(@_, "ref") }
2545 sub pp_pos { maybe_local(@_, unop(@_, "pos")) }
2547 sub pp_sin { maybe_targmy(@_, \&unop, "sin") }
2548 sub pp_cos { maybe_targmy(@_, \&unop, "cos") }
2549 sub pp_rand { maybe_targmy(@_, \&unop, "rand") }
2550 sub pp_srand { unop(@_, "srand") }
2551 sub pp_exp { maybe_targmy(@_, \&unop, "exp") }
2552 sub pp_log { maybe_targmy(@_, \&unop, "log") }
2553 sub pp_sqrt { maybe_targmy(@_, \&unop, "sqrt") }
2554 sub pp_int { maybe_targmy(@_, \&unop, "int") }
2555 sub pp_hex { maybe_targmy(@_, \&unop, "hex") }
2556 sub pp_oct { maybe_targmy(@_, \&unop, "oct") }
2557 sub pp_abs { maybe_targmy(@_, \&unop, "abs") }
2559 sub pp_length { maybe_targmy(@_, \&unop, "length") }
2560 sub pp_ord { maybe_targmy(@_, \&unop, "ord") }
2561 sub pp_chr { maybe_targmy(@_, \&unop, "chr") }
2563 sub pp_each { unop(@_, "each") }
2564 sub pp_values { unop(@_, "values") }
2565 sub pp_keys { unop(@_, "keys") }
2566 { no strict 'refs'; *{"pp_r$_"} = *{"pp_$_"} for qw< keys each values >; }
2568 # no name because its an optimisation op that has no keyword
2571 sub pp_aeach { unop(@_, "each") }
2572 sub pp_avalues { unop(@_, "values") }
2573 sub pp_akeys { unop(@_, "keys") }
2574 sub pp_pop { unop(@_, "pop") }
2575 sub pp_shift { unop(@_, "shift") }
2577 sub pp_caller { unop(@_, "caller") }
2578 sub pp_reset { unop(@_, "reset") }
2579 sub pp_exit { unop(@_, "exit") }
2580 sub pp_prototype { unop(@_, "prototype") }
2582 sub pp_close { unop(@_, "close") }
2583 sub pp_fileno { unop(@_, "fileno") }
2584 sub pp_umask { unop(@_, "umask") }
2585 sub pp_untie { unop(@_, "untie") }
2586 sub pp_tied { unop(@_, "tied") }
2587 sub pp_dbmclose { unop(@_, "dbmclose") }
2588 sub pp_getc { unop(@_, "getc") }
2589 sub pp_eof { unop(@_, "eof") }
2590 sub pp_tell { unop(@_, "tell") }
2591 sub pp_getsockname { unop(@_, "getsockname") }
2592 sub pp_getpeername { unop(@_, "getpeername") }
2595 my ($self, $op, $cx) = @_;
2596 if (($op->flags & (OPf_SPECIAL|OPf_KIDS)) == (OPf_SPECIAL|OPf_KIDS)) {
2597 my $kw = $self->keyword("chdir");
2598 my $kid = $self->const_sv($op->first)->PV;
2600 . ($cx >= 16 || $self->{'parens'} ? "($kid)" : " $kid");
2601 maybe_targmy(@_, sub { $_[3] }, $code);
2603 maybe_targmy(@_, \&unop, "chdir")
2607 sub pp_chroot { maybe_targmy(@_, \&unop, "chroot") }
2608 sub pp_readlink { unop(@_, "readlink") }
2609 sub pp_rmdir { maybe_targmy(@_, \&unop, "rmdir") }
2610 sub pp_readdir { unop(@_, "readdir") }
2611 sub pp_telldir { unop(@_, "telldir") }
2612 sub pp_rewinddir { unop(@_, "rewinddir") }
2613 sub pp_closedir { unop(@_, "closedir") }
2614 sub pp_getpgrp { maybe_targmy(@_, \&unop, "getpgrp") }
2615 sub pp_localtime { unop(@_, "localtime") }
2616 sub pp_gmtime { unop(@_, "gmtime") }
2617 sub pp_alarm { unop(@_, "alarm") }
2618 sub pp_sleep { maybe_targmy(@_, \&unop, "sleep") }
2621 my $code = unop(@_, "do", 1); # llafr does not apply
2622 if ($code =~ s/^((?:CORE::)?do) \{/$1({/) { $code .= ')' }
2628 $_[1]->private & OPpEVAL_BYTES ? 'evalbytes' : "eval"
2632 sub pp_ghbyname { unop(@_, "gethostbyname") }
2633 sub pp_gnbyname { unop(@_, "getnetbyname") }
2634 sub pp_gpbyname { unop(@_, "getprotobyname") }
2635 sub pp_shostent { unop(@_, "sethostent") }
2636 sub pp_snetent { unop(@_, "setnetent") }
2637 sub pp_sprotoent { unop(@_, "setprotoent") }
2638 sub pp_sservent { unop(@_, "setservent") }
2639 sub pp_gpwnam { unop(@_, "getpwnam") }
2640 sub pp_gpwuid { unop(@_, "getpwuid") }
2641 sub pp_ggrnam { unop(@_, "getgrnam") }
2642 sub pp_ggrgid { unop(@_, "getgrgid") }
2644 sub pp_lock { unop(@_, "lock") }
2646 sub pp_continue { unop(@_, "continue"); }
2647 sub pp_break { unop(@_, "break"); }
2651 my($op, $cx, $givwhen) = @_;
2653 my $enterop = $op->first;
2655 if ($enterop->flags & OPf_SPECIAL) {
2656 $head = $self->keyword("default");
2657 $block = $self->deparse($enterop->first, 0);
2660 my $cond = $enterop->first;
2661 my $cond_str = $self->deparse($cond, 1);
2662 $head = "$givwhen ($cond_str)";
2663 $block = $self->deparse($cond->sibling, 0);
2671 sub pp_leavegiven { givwhen(@_, $_[0]->keyword("given")); }
2672 sub pp_leavewhen { givwhen(@_, $_[0]->keyword("when")); }
2678 my $name = $self->keyword("exists");
2679 if ($op->private & OPpEXISTS_SUB) {
2680 # Checking for the existence of a subroutine
2681 return $self->maybe_parens_func($name,
2682 $self->pp_rv2cv($op->first, 16), $cx, 16);
2684 if ($op->flags & OPf_SPECIAL) {
2685 # Array element, not hash element
2686 return $self->maybe_parens_func($name,
2687 $self->pp_aelem($op->first, 16), $cx, 16);
2689 return $self->maybe_parens_func($name, $self->pp_helem($op->first, 16),
2697 my $name = $self->keyword("delete");
2698 if ($op->private & (OPpSLICE|OPpKVSLICE)) {
2699 if ($op->flags & OPf_SPECIAL) {
2700 # Deleting from an array, not a hash
2701 return $self->maybe_parens_func($name,
2702 $self->pp_aslice($op->first, 16),
2705 return $self->maybe_parens_func($name,
2706 $self->pp_hslice($op->first, 16),
2709 if ($op->flags & OPf_SPECIAL) {
2710 # Deleting from an array, not a hash
2711 return $self->maybe_parens_func($name,
2712 $self->pp_aelem($op->first, 16),
2715 return $self->maybe_parens_func($name,
2716 $self->pp_helem($op->first, 16),
2724 my $opname = $op->flags & OPf_SPECIAL ? 'CORE::require' : 'require';
2725 my $kid = $op->first;
2726 if ($kid->name eq 'const') {
2727 my $priv = $kid->private;
2728 my $sv = $self->const_sv($kid);
2730 if ($priv & OPpCONST_BARE) {
2734 } elsif ($priv & OPpCONST_NOVER) {
2735 $opname = $self->keyword('no');
2736 $arg = $self->const($sv, 16);
2737 } elsif ((my $tmp = $self->const($sv, 16)) =~ /^v/) {
2741 return $self->maybe_parens("$opname $arg", $cx, 16);
2747 1, # llafr does not apply
2754 my $kid = $op->first;
2755 if (not null $kid->sibling) {
2756 # XXX Was a here-doc
2757 return $self->dquote($op);
2759 $self->unop(@_, "scalar");
2766 return $self->{'curcv'}->PADLIST->ARRAYelt(1)->ARRAYelt($targ);
2769 sub anon_hash_or_list {
2773 my($pre, $post) = @{{"anonlist" => ["[","]"],
2774 "anonhash" => ["{","}"]}->{$op->name}};
2776 $op = $op->first->sibling; # skip pushmark
2777 for (; !null($op); $op = $op->sibling) {
2778 $expr = $self->deparse($op, 6);
2781 if ($pre eq "{" and $cx < 1) {
2782 # Disambiguate that it's not a block
2785 return $pre . join(", ", @exprs) . $post;
2791 if ($op->flags & OPf_SPECIAL) {
2792 return $self->anon_hash_or_list($op, $cx);
2794 warn "Unexpected op pp_" . $op->name() . " without OPf_SPECIAL";
2798 *pp_anonhash = \&pp_anonlist;
2803 my $kid = $op->first;
2804 if ($kid->name eq "null") {
2805 my $anoncode = $kid = $kid->first;
2806 if ($anoncode->name eq "anonconst") {
2807 $anoncode = $anoncode->first->first->sibling;
2809 if ($anoncode->name eq "anoncode"
2810 or !null($anoncode = $kid->sibling) and
2811 $anoncode->name eq "anoncode") {
2812 return $self->e_anoncode({ code => $self->padval($anoncode->targ) });
2813 } elsif ($kid->name eq "pushmark") {
2814 my $sib_name = $kid->sibling->name;
2815 if ($sib_name eq 'entersub') {
2816 my $text = $self->deparse($kid->sibling, 1);
2817 # Always show parens for \(&func()), but only with -p otherwise
2818 $text = "($text)" if $self->{'parens'}
2819 or $kid->sibling->private & OPpENTERSUB_AMPER;
2824 local $self->{'in_refgen'} = 1;
2825 $self->pfixop($op, $cx, "\\", 20);
2829 my ($self, $info) = @_;
2830 my $text = $self->deparse_sub($info->{code});
2831 return $self->keyword("sub") . " $text";
2834 sub pp_srefgen { pp_refgen(@_) }
2839 my $kid = $op->first;
2841 and $op->flags & OPf_SPECIAL
2842 and $self->deparse($kid, 1) eq 'ARGV')
2846 return $self->unop($op, $cx, "readline");
2852 return "<" . $self->gv_name($self->gv_or_padgv($op)) . ">";
2855 # Unary operators that can occur as pseudo-listops inside double quotes
2858 my($op, $cx, $name, $prec, $flags) = (@_, 0, 0);
2860 if ($op->flags & OPf_KIDS) {
2862 # If there's more than one kid, the first is an ex-pushmark.
2863 $kid = $kid->sibling if not null $kid->sibling;
2864 return $self->maybe_parens_unop($name, $kid, $cx);
2866 return $name . ($op->flags & OPf_SPECIAL ? "()" : "");
2870 sub pp_ucfirst { dq_unop(@_, "ucfirst") }
2871 sub pp_lcfirst { dq_unop(@_, "lcfirst") }
2872 sub pp_uc { dq_unop(@_, "uc") }
2873 sub pp_lc { dq_unop(@_, "lc") }
2874 sub pp_quotemeta { maybe_targmy(@_, \&dq_unop, "quotemeta") }
2875 sub pp_fc { dq_unop(@_, "fc") }
2879 my ($op, $cx, $name) = @_;
2880 if (class($op) eq "PVOP") {
2881 $name .= " " . $op->pv;
2882 } elsif (class($op) eq "OP") {
2884 } elsif (class($op) eq "UNOP") {
2885 (my $kid = $self->deparse($op->first, 7)) =~ s/^\cS//;
2886 # last foo() is a syntax error.
2887 $kid =~ /^(?!\d)\w/ and $kid = "($kid)";
2890 return $self->maybe_parens($name, $cx, 7);
2893 sub pp_last { loopex(@_, "last") }
2894 sub pp_next { loopex(@_, "next") }
2895 sub pp_redo { loopex(@_, "redo") }
2896 sub pp_goto { loopex(@_, "goto") }
2897 sub pp_dump { loopex(@_, "CORE::dump") }
2901 my($op, $cx, $name) = @_;
2902 if (class($op) eq "UNOP") {
2903 # Genuine '-X' filetests are exempt from the LLAFR, but not
2905 if ($name =~ /^-/) {
2906 (my $kid = $self->deparse($op->first, 16)) =~ s/^\cS//;
2907 return $self->maybe_parens("$name $kid", $cx, 16);
2909 return $self->maybe_parens_unop($name, $op->first, $cx);
2910 } elsif (class($op) =~ /^(SV|PAD)OP$/) {
2911 return $self->maybe_parens_func($name, $self->pp_gv($op, 1), $cx, 16);
2912 } else { # I don't think baseop filetests ever survive ck_ftst, but...
2917 sub pp_lstat { ftst(@_, "lstat") }
2918 sub pp_stat { ftst(@_, "stat") }
2919 sub pp_ftrread { ftst(@_, "-R") }
2920 sub pp_ftrwrite { ftst(@_, "-W") }
2921 sub pp_ftrexec { ftst(@_, "-X") }
2922 sub pp_fteread { ftst(@_, "-r") }
2923 sub pp_ftewrite { ftst(@_, "-w") }
2924 sub pp_fteexec { ftst(@_, "-x") }
2925 sub pp_ftis { ftst(@_, "-e") }
2926 sub pp_fteowned { ftst(@_, "-O") }
2927 sub pp_ftrowned { ftst(@_, "-o") }
2928 sub pp_ftzero { ftst(@_, "-z") }
2929 sub pp_ftsize { ftst(@_, "-s") }
2930 sub pp_ftmtime { ftst(@_, "-M") }
2931 sub pp_ftatime { ftst(@_, "-A") }
2932 sub pp_ftctime { ftst(@_, "-C") }
2933 sub pp_ftsock { ftst(@_, "-S") }
2934 sub pp_ftchr { ftst(@_, "-c") }
2935 sub pp_ftblk { ftst(@_, "-b") }
2936 sub pp_ftfile { ftst(@_, "-f") }
2937 sub pp_ftdir { ftst(@_, "-d") }
2938 sub pp_ftpipe { ftst(@_, "-p") }
2939 sub pp_ftlink { ftst(@_, "-l") }
2940 sub pp_ftsuid { ftst(@_, "-u") }
2941 sub pp_ftsgid { ftst(@_, "-g") }
2942 sub pp_ftsvtx { ftst(@_, "-k") }
2943 sub pp_fttty { ftst(@_, "-t") }
2944 sub pp_fttext { ftst(@_, "-T") }
2945 sub pp_ftbinary { ftst(@_, "-B") }
2947 sub SWAP_CHILDREN () { 1 }
2948 sub ASSIGN () { 2 } # has OP= variant
2949 sub LIST_CONTEXT () { 4 } # Assignment is in list context
2955 my $name = $op->name;
2956 if ($name eq "concat" and $op->first->name eq "concat") {
2957 # avoid spurious '=' -- see comment in pp_concat
2960 if ($name eq "null" and class($op) eq "UNOP"
2961 and $op->first->name =~ /^(and|x?or)$/
2962 and null $op->first->sibling)
2964 # Like all conditional constructs, OP_ANDs and OP_ORs are topped
2965 # with a null that's used as the common end point of the two
2966 # flows of control. For precedence purposes, ignore it.
2967 # (COND_EXPRs have these too, but we don't bother with
2968 # their associativity).
2969 return assoc_class($op->first);
2971 return $name . ($op->flags & OPf_STACKED ? "=" : "");
2974 # Left associative operators, like '+', for which
2975 # $a + $b + $c is equivalent to ($a + $b) + $c
2978 %left = ('multiply' => 19, 'i_multiply' => 19,
2979 'divide' => 19, 'i_divide' => 19,
2980 'modulo' => 19, 'i_modulo' => 19,
2982 'add' => 18, 'i_add' => 18,
2983 'subtract' => 18, 'i_subtract' => 18,
2985 'left_shift' => 17, 'right_shift' => 17,
2986 'bit_and' => 13, 'nbit_and' => 13, 'sbit_and' => 13,
2987 'bit_or' => 12, 'bit_xor' => 12,
2988 'sbit_or' => 12, 'sbit_xor' => 12,
2989 'nbit_or' => 12, 'nbit_xor' => 12,
2991 'or' => 2, 'xor' => 2,
2995 sub deparse_binop_left {
2997 my($op, $left, $prec) = @_;
2998 if ($left{assoc_class($op)} && $left{assoc_class($left)}
2999 and $left{assoc_class($op)} == $left{assoc_class($left)})
3001 return $self->deparse($left, $prec - .00001);
3003 return $self->deparse($left, $prec);
3007 # Right associative operators, like '=', for which
3008 # $a = $b = $c is equivalent to $a = ($b = $c)
3011 %right = ('pow' => 22,
3012 'sassign=' => 7, 'aassign=' => 7,
3013 'multiply=' => 7, 'i_multiply=' => 7,
3014 'divide=' => 7, 'i_divide=' => 7,
3015 'modulo=' => 7, 'i_modulo=' => 7,
3016 'repeat=' => 7, 'refassign' => 7, 'refassign=' => 7,
3017 'add=' => 7, 'i_add=' => 7,
3018 'subtract=' => 7, 'i_subtract=' => 7,
3020 'left_shift=' => 7, 'right_shift=' => 7,
3021 'bit_and=' => 7, 'sbit_and=' => 7, 'nbit_and=' => 7,
3022 'nbit_or=' => 7, 'nbit_xor=' => 7,
3023 'sbit_or=' => 7, 'sbit_xor=' => 7,
3029 sub deparse_binop_right {
3031 my($op, $right, $prec) = @_;
3032 if ($right{assoc_class($op)} && $right{assoc_class($right)}
3033 and $right{assoc_class($op)} == $right{assoc_class($right)})
3035 return $self->deparse($right, $prec - .00001);
3037 return $self->deparse($right, $prec);
3043 my ($op, $cx, $opname, $prec, $flags) = (@_, 0);
3044 my $left = $op->first;
3045 my $right = $op->last;
3047 if ($op->flags & OPf_STACKED && $flags & ASSIGN) {
3051 if ($flags & SWAP_CHILDREN) {
3052 ($left, $right) = ($right, $left);
3055 $left = $self->deparse_binop_left($op, $left, $prec);
3056 $left = "($left)" if $flags & LIST_CONTEXT
3057 and $left !~ /^(my|our|local|)[\@\(]/
3059 # Parenthesize if the left argument is a
3061 my $left = $leftop->first->sibling;
3062 $left->name eq 'repeat'
3063 && null($left->sibling);
3065 $right = $self->deparse_binop_right($op, $right, $prec);
3066 return $self->maybe_parens("$left $opname$eq $right", $cx, $prec);
3069 sub pp_add { maybe_targmy(@_, \&binop, "+", 18, ASSIGN) }
3070 sub pp_multiply { maybe_targmy(@_, \&binop, "*", 19, ASSIGN) }
3071 sub pp_subtract { maybe_targmy(@_, \&binop, "-",18, ASSIGN) }
3072 sub pp_divide { maybe_targmy(@_, \&binop, "/", 19, ASSIGN) }
3073 sub pp_modulo { maybe_targmy(@_, \&binop, "%", 19, ASSIGN) }
3074 sub pp_i_add { maybe_targmy(@_, \&binop, "+", 18, ASSIGN) }
3075 sub pp_i_multiply { maybe_targmy(@_, \&binop, "*", 19, ASSIGN) }
3076 sub pp_i_subtract { maybe_targmy(@_, \&binop, "-", 18, ASSIGN) }
3077 sub pp_i_divide { maybe_targmy(@_, \&binop, "/", 19, ASSIGN) }
3078 sub pp_i_modulo { maybe_targmy(@_, \&binop, "%", 19, ASSIGN) }
3079 sub pp_pow { maybe_targmy(@_, \&binop, "**", 22, ASSIGN) }
3081 sub pp_left_shift { maybe_targmy(@_, \&binop, "<<", 17, ASSIGN) }
3082 sub pp_right_shift { maybe_targmy(@_, \&binop, ">>", 17, ASSIGN) }
3083 sub pp_bit_and { maybe_targmy(@_, \&binop, "&", 13, ASSIGN) }
3084 sub pp_bit_or { maybe_targmy(@_, \&binop, "|", 12, ASSIGN) }
3085 sub pp_bit_xor { maybe_targmy(@_, \&binop, "^", 12, ASSIGN) }
3086 *pp_nbit_and = *pp_bit_and;
3087 *pp_nbit_or = *pp_bit_or;
3088 *pp_nbit_xor = *pp_bit_xor;
3089 sub pp_sbit_and { maybe_targmy(@_, \&binop, "&.", 13, ASSIGN) }
3090 sub pp_sbit_or { maybe_targmy(@_, \&binop, "|.", 12, ASSIGN) }
3091 sub pp_sbit_xor { maybe_targmy(@_, \&binop, "^.", 12, ASSIGN) }
3093 sub pp_eq { binop(@_, "==", 14) }
3094 sub pp_ne { binop(@_, "!=", 14) }
3095 sub pp_lt { binop(@_, "<", 15) }
3096 sub pp_gt { binop(@_, ">", 15) }
3097 sub pp_ge { binop(@_, ">=", 15) }
3098 sub pp_le { binop(@_, "<=", 15) }
3099 sub pp_ncmp { binop(@_, "<=>", 14) }
3100 sub pp_i_eq { binop(@_, "==", 14) }
3101 sub pp_i_ne { binop(@_, "!=", 14) }
3102 sub pp_i_lt { binop(@_, "<", 15) }
3103 sub pp_i_gt { binop(@_, ">", 15) }
3104 sub pp_i_ge { binop(@_, ">=", 15) }
3105 sub pp_i_le { binop(@_, "<=", 15) }
3106 sub pp_i_ncmp { maybe_targmy(@_, \&binop, "<=>", 14) }
3108 sub pp_seq { binop(@_, "eq", 14) }
3109 sub pp_sne { binop(@_, "ne", 14) }
3110 sub pp_slt { binop(@_, "lt", 15) }
3111 sub pp_sgt { binop(@_, "gt", 15) }
3112 sub pp_sge { binop(@_, "ge", 15) }
3113 sub pp_sle { binop(@_, "le", 15) }
3114 sub pp_scmp { maybe_targmy(@_, \&binop, "cmp", 14) }
3116 sub pp_sassign { binop(@_, "=", 7, SWAP_CHILDREN) }
3117 sub pp_aassign { binop(@_, "=", 7, SWAP_CHILDREN | LIST_CONTEXT) }
3120 my ($self, $op, $cx) = @_;
3121 if (($op->flags & OPf_SPECIAL) && $self->{expand} < 2) {
3122 return $self->deparse($op->last, $cx);
3125 binop(@_, "~~", 14);
3129 # '.' is special because concats-of-concats are optimized to save copying
3130 # by making all but the first concat stacked. The effect is as if the
3131 # programmer had written '($a . $b) .= $c', except legal.
3132 sub pp_concat { maybe_targmy(@_, \&real_concat) }
3136 my $left = $op->first;
3137 my $right = $op->last;
3140 if ($op->flags & OPf_STACKED and $op->first->name ne "concat") {
3144 $left = $self->deparse_binop_left($op, $left, $prec);
3145 $right = $self->deparse_binop_right($op, $right, $prec);
3146 return $self->maybe_parens("$left .$eq $right", $cx, $prec);
3149 sub pp_repeat { maybe_targmy(@_, \&repeat) }
3151 # 'x' is weird when the left arg is a list
3155 my $left = $op->first;
3156 my $right = $op->last;
3159 if ($op->flags & OPf_STACKED) {
3163 if (null($right)) { # list repeat; count is inside left-side ex-list
3164 # in 5.21.5 and earlier
3165 my $kid = $left->first->sibling; # skip pushmark
3167 for (; !null($kid->sibling); $kid = $kid->sibling) {
3168 push @exprs, $self->deparse($kid, 6);
3171 $left = "(" . join(", ", @exprs). ")";
3173 my $dolist = $op->private & OPpREPEAT_DOLIST;
3174 $left = $self->deparse_binop_left($op, $left, $dolist ? 1 : $prec);
3179 $right = $self->deparse_binop_right($op, $right, $prec);
3180 return $self->maybe_parens("$left x$eq $right", $cx, $prec);
3185 my ($op, $cx, $type) = @_;
3186 my $left = $op->first;
3187 my $right = $left->sibling;
3188 $left = $self->deparse($left, 9);
3189 $right = $self->deparse($right, 9);
3190 return $self->maybe_parens("$left $type $right", $cx, 9);
3196 my $flip = $op->first;
3197 my $type = ($flip->flags & OPf_SPECIAL) ? "..." : "..";
3198 return $self->range($flip->first, $cx, $type);
3201 # one-line while/until is handled in pp_leave
3205 my ($op, $cx, $lowop, $lowprec, $highop, $highprec, $blockname) = @_;
3206 my $left = $op->first;
3207 my $right = $op->first->sibling;
3208 $blockname &&= $self->keyword($blockname);
3209 if ($cx < 1 and is_scope($right) and $blockname
3210 and $self->{'expand'} < 7)
3212 $left = $self->deparse($left, 1);
3213 $right = $self->deparse($right, 0);
3214 return "$blockname ($left) {\n\t$right\n\b}\cK";
3215 } elsif ($cx < 1 and $blockname and not $self->{'parens'}
3216 and $self->{'expand'} < 7) { # $b if $a
3217 $right = $self->deparse($right, 1);
3218 $left = $self->deparse($left, 1);
3219 return "$right $blockname $left";
3220 } elsif ($cx > $lowprec and $highop) { # $a && $b
3221 $left = $self->deparse_binop_left($op, $left, $highprec);
3222 $right = $self->deparse_binop_right($op, $right, $highprec);
3223 return $self->maybe_parens("$left $highop $right", $cx, $highprec);
3224 } else { # $a and $b
3225 $left = $self->deparse_binop_left($op, $left, $lowprec);
3226 $right = $self->deparse_binop_right($op, $right, $lowprec);
3227 return $self->maybe_parens("$left $lowop $right", $cx, $lowprec);
3231 sub pp_and { logop(@_, "and", 3, "&&", 11, "if") }
3232 sub pp_or { logop(@_, "or", 2, "||", 10, "unless") }
3233 sub pp_dor { logop(@_, "//", 10) }
3235 # xor is syntactically a logop, but it's really a binop (contrary to
3236 # old versions of opcode.pl). Syntax is what matters here.
3237 sub pp_xor { logop(@_, "xor", 2, "", 0, "") }
3241 my ($op, $cx, $opname) = @_;
3242 my $left = $op->first;
3243 my $right = $op->first->sibling->first; # skip sassign
3244 $left = $self->deparse($left, 7);
3245 $right = $self->deparse($right, 7);
3246 return $self->maybe_parens("$left $opname $right", $cx, 7);
3249 sub pp_andassign { logassignop(@_, "&&=") }
3250 sub pp_orassign { logassignop(@_, "||=") }
3251 sub pp_dorassign { logassignop(@_, "//=") }
3253 sub rv2gv_or_string {
3255 if ($op->name eq "gv") { # could be open("open") or open("###")
3257 $self->stash_variable_name("", $self->gv_or_padgv($op));
3258 $quoted ? $name : "*$name";
3261 $self->deparse($op, 6);
3267 my($op, $cx, $name, $kid, $nollafr) = @_;
3269 my $parens = ($cx >= 5) || $self->{'parens'};
3270 $kid ||= $op->first->sibling;
3271 # If there are no arguments, add final parentheses (or parenthesize the
3272 # whole thing if the llafr does not apply) to account for cases like
3273 # (return)+1 or setpgrp()+1. When the llafr does not apply, we use a
3274 # precedence of 6 (< comma), as "return, 1" does not need parentheses.
3277 ? $self->maybe_parens($self->keyword($name), $cx, 7)
3278 : $self->keyword($name) . '()' x (7 < $cx);
3281 my $fullname = $self->keyword($name);
3282 my $proto = prototype("CORE::$name");
3284 ( (defined $proto && $proto =~ /^;?\*/)
3285 || $name eq 'select' # select(F) doesn't have a proto
3287 && $kid->name eq "rv2gv"
3288 && !($kid->private & OPpLVAL_INTRO)
3290 $first = $self->rv2gv_or_string($kid->first);
3293 $first = $self->deparse($kid, 6);
3295 if ($name eq "chmod" && $first =~ /^\d+$/) {
3296 $first = sprintf("%#o", $first);
3299 if not $parens and not $nollafr and substr($first, 0, 1) eq "(";
3300 push @exprs, $first;
3301 $kid = $kid->sibling;
3302 if (defined $proto && $proto =~ /^\*\*/ && $kid->name eq "rv2gv"
3303 && !($kid->private & OPpLVAL_INTRO)) {
3304 push @exprs, $first = $self->rv2gv_or_string($kid->first);
3305 $kid = $kid->sibling;
3307 for (; !null($kid); $kid = $kid->sibling) {
3308 push @exprs, $self->deparse($kid, 6);
3310 if ($name eq "reverse" && ($op->private & OPpREVERSE_INPLACE)) {
3311 return "$exprs[0] = $fullname"
3312 . ($parens ? "($exprs[0])" : " $exprs[0]");
3315 if ($parens && $nollafr) {
3316 return "($fullname " . join(", ", @exprs) . ")";
3318 return "$fullname(" . join(", ", @exprs) . ")";
3320 return "$fullname " . join(", ", @exprs);
3324 sub pp_bless { listop(@_, "bless") }
3325 sub pp_atan2 { maybe_targmy(@_, \&listop, "atan2") }
3327 my ($self,$op,$cx) = @_;
3328 if ($op->private & OPpSUBSTR_REPL_FIRST) {
3330 listop($self, $op, 7, "substr", $op->first->sibling->sibling)
3332 . $self->deparse($op->first->sibling, 7);
3334 maybe_local(@_, listop(@_, "substr"))
3338 # Also handles pp_rindex.
3340 # The body of this function includes an unrolled maybe_targmy(),
3341 # since the two parts of that sub's actions need to have have the
3342 # '== -1' bit in between
3344 my($self, $op, $cx) = @_;
3346 my $lex = ($op->private & OPpTARGET_MY);
3347 my $bool = ($op->private & OPpTRUEBOOL);
3349 my $val = $self->listop($op, ($bool ? 14 : $lex ? 7 : $cx), $op->name);
3351 # (index() == -1) has op_eq and op_const optimised away
3353 $val .= ($op->private & OPpINDEX_BOOLNEG) ? " == -1" : " != -1";
3354 $val = "($val)" if ($op->flags & OPf_PARENS);
3357 my $var = $self->padname($op->targ);
3358 $val = $self->maybe_parens("$var = $val", $cx, 7);
3363 sub pp_rindex { pp_index(@_); }
3364 sub pp_vec { maybe_targmy(@_, \&maybe_local, listop(@_, "vec")) }
3365 sub pp_sprintf { maybe_targmy(@_, \&listop, "sprintf") }
3366 sub pp_formline { listop(@_, "formline") } # see also deparse_format
3367 sub pp_crypt { maybe_targmy(@_, \&listop, "crypt") }
3368 sub pp_unpack { listop(@_, "unpack") }
3369 sub pp_pack { listop(@_, "pack") }
3370 sub pp_join { maybe_targmy(@_, \&listop, "join") }
3371 sub pp_splice { listop(@_, "splice") }
3372 sub pp_push { maybe_targmy(@_, \&listop, "push") }
3373 sub pp_unshift { maybe_targmy(@_, \&listop, "unshift") }
3374 sub pp_reverse { listop(@_, "reverse") }
3375 sub pp_warn { listop(@_, "warn") }
3376 sub pp_die { listop(@_, "die") }
3377 sub pp_return { listop(@_, "return", undef, 1) } # llafr does not apply
3378 sub pp_open { listop(@_, "open") }
3379 sub pp_pipe_op { listop(@_, "pipe") }
3380 sub pp_tie { listop(@_, "tie") }
3381 sub pp_binmode { listop(@_, "binmode") }
3382 sub pp_dbmopen { listop(@_, "dbmopen") }
3383 sub pp_sselect { listop(@_, "select") }
3384 sub pp_select { listop(@_, "select") }
3385 sub pp_read { listop(@_, "read") }
3386 sub pp_sysopen { listop(@_, "sysopen") }
3387 sub pp_sysseek { listop(@_, "sysseek") }
3388 sub pp_sysread { listop(@_, "sysread") }
3389 sub pp_syswrite { listop(@_, "syswrite") }
3390 sub pp_send { listop(@_, "send") }
3391 sub pp_recv { listop(@_, "recv") }
3392 sub pp_seek { listop(@_, "seek") }
3393 sub pp_fcntl { listop(@_, "fcntl") }
3394 sub pp_ioctl { listop(@_, "ioctl") }
3395 sub pp_flock { maybe_targmy(@_, \&listop, "flock") }
3396 sub pp_socket { listop(@_, "socket") }
3397 sub pp_sockpair { listop(@_, "socketpair") }
3398 sub pp_bind { listop(@_, "bind") }
3399 sub pp_connect { listop(@_, "connect") }
3400 sub pp_listen { listop(@_, "listen") }
3401 sub pp_accept { listop(@_, "accept") }
3402 sub pp_shutdown { listop(@_, "shutdown") }
3403 sub pp_gsockopt { listop(@_, "getsockopt") }
3404 sub pp_ssockopt { listop(@_, "setsockopt") }
3405 sub pp_chown { maybe_targmy(@_, \&listop, "chown") }
3406 sub pp_unlink { maybe_targmy(@_, \&listop, "unlink") }
3407 sub pp_chmod { maybe_targmy(@_, \&listop, "chmod") }
3408 sub pp_utime { maybe_targmy(@_, \&listop, "utime") }
3409 sub pp_rename { maybe_targmy(@_, \&listop, "rename") }
3410 sub pp_link { maybe_targmy(@_, \&listop, "link") }
3411 sub pp_symlink { maybe_targmy(@_, \&listop, "symlink") }
3412 sub pp_mkdir { maybe_targmy(@_, \&listop, "mkdir") }
3413 sub pp_open_dir { listop(@_, "opendir") }
3414 sub pp_seekdir { listop(@_, "seekdir") }
3415 sub pp_waitpid { maybe_targmy(@_, \&listop, "waitpid") }
3416 sub pp_system { maybe_targmy(@_, \&indirop, "system") }
3417 sub pp_exec { maybe_targmy(@_, \&indirop, "exec") }
3418 sub pp_kill { maybe_targmy(@_, \&listop, "kill") }
3419 sub pp_setpgrp { maybe_targmy(@_, \&listop, "setpgrp") }
3420 sub pp_getpriority { maybe_targmy(@_, \&listop, "getpriority") }
3421 sub pp_setpriority { maybe_targmy(@_, \&listop, "setpriority") }
3422 sub pp_shmget { listop(@_, "shmget") }
3423 sub pp_shmctl { listop(@_, "shmctl") }
3424 sub pp_shmread { listop(@_, "shmread") }
3425 sub pp_shmwrite { listop(@_, "shmwrite") }
3426 sub pp_msgget { listop(@_, "msgget") }
3427 sub pp_msgctl { listop(@_, "msgctl") }
3428 sub pp_msgsnd { listop(@_, "msgsnd") }
3429 sub pp_msgrcv { listop(@_, "msgrcv") }
3430 sub pp_semget { listop(@_, "semget") }
3431 sub pp_semctl { listop(@_, "semctl") }
3432 sub pp_semop { listop(@_, "semop") }
3433 sub pp_ghbyaddr { listop(@_, "gethostbyaddr") }
3434 sub pp_gnbyaddr { listop(@_, "getnetbyaddr") }
3435 sub pp_gpbynumber { listop(@_, "getprotobynumber") }
3436 sub pp_gsbyname { listop(@_, "getservbyname") }
3437 sub pp_gsbyport { listop(@_, "getservbyport") }
3438 sub pp_syscall { listop(@_, "syscall") }
3443 my $kid = $op->first->sibling; # skip pushmark
3445 $op->flags & OPf_SPECIAL ? 'glob' : $self->keyword('glob');
3446 my $text = $self->deparse($kid);
3447 return $cx >= 5 || $self->{'parens'}
3452 # Truncate is special because OPf_SPECIAL makes a bareword first arg
3453 # be a filehandle. This could probably be better fixed in the core
3454 # by moving the GV lookup into ck_truc.
3460 my $parens = ($cx >= 5) || $self->{'parens'};
3461 my $kid = $op->first->sibling;
3463 if ($op->flags & OPf_SPECIAL) {
3464 # $kid is an OP_CONST
3465 $fh = $self->const_sv($kid)->PV;
3467 $fh = $self->deparse($kid, 6);
3468 $fh = "+$fh" if not $parens and substr($fh, 0, 1) eq "(";
3470 my $len = $self->deparse($kid->sibling, 6);
3471 my $name = $self->keyword('truncate');
3473 return "$name($fh, $len)";
3475 return "$name $fh, $len";
3481 my($op, $cx, $name) = @_;
3483 my $firstkid = my $kid = $op->first->sibling;
3485 if ($op->flags & OPf_STACKED) {
3487 $indir = $indir->first; # skip rv2gv
3488 if (is_scope($indir)) {
3489 $indir = "{" . $self->deparse($indir, 0) . "}";
3490 $indir = "{;}" if $indir eq "{}";
3491 } elsif ($indir->name eq "const" && $indir->private & OPpCONST_BARE) {
3492 $indir = $self->const_sv($indir)->PV;
3494 $indir = $self->deparse($indir, 24);
3496 $indir = $indir . " ";
3497 $kid = $kid->sibling;
3499 if ($name eq "sort" && $op->private & (OPpSORT_NUMERIC | OPpSORT_INTEGER)) {
3500 $indir = ($op->private & OPpSORT_DESCEND) ? '{$b <=> $a} '
3503 elsif ($name eq "sort" && $op->private & OPpSORT_DESCEND) {
3504 $indir = '{$b cmp $a} ';
3506 for (; !null($kid); $kid = $kid->sibling) {
3507 $expr = $self->deparse($kid, !$indir && $kid == $firstkid && $name eq "sort" && $firstkid->name eq "entersub" ? 16 : 6);
3511 if ($name eq "sort" && $op->private & OPpSORT_REVERSE) {
3512 $name2 = $self->keyword('reverse') . ' ' . $self->keyword('sort');
3514 else { $name2 = $self->keyword($name) }
3515 if ($name eq "sort" && ($op->private & OPpSORT_INPLACE)) {
3516 return "$exprs[0] = $name2 $indir $exprs[0]";
3519 my $args = $indir . join(", ", @exprs);
3520 if ($indir ne "" && $name eq "sort") {
3521 # We don't want to say "sort(f 1, 2, 3)", since perl -w will
3522 # give bareword warnings in that case. Therefore if context
3523 # requires, we'll put parens around the outside "(sort f 1, 2,
3524 # 3)". Unfortunately, we'll currently think the parens are
3525 # necessary more often that they really are, because we don't
3526 # distinguish which side of an assignment we're on.
3528 return "($name2 $args)";
3530 return "$name2 $args";
3533 !$indir && $name eq "sort"
3534 && !null($op->first->sibling)
3535 && $op->first->sibling->name eq 'entersub'
3537 # We cannot say sort foo(bar), as foo will be interpreted as a
3538 # comparison routine. We have to say sort(...) in that case.
3539 return "$name2($args)";
3542 ? $self->maybe_parens_func($name2, $args, $cx, 5)
3543 : $name2 . '()' x (7 < $cx);
3548 sub pp_prtf { indirop(@_, "printf") }
3549 sub pp_print { indirop(@_, "print") }
3550 sub pp_say { indirop(@_, "say") }
3551 sub pp_sort { indirop(@_, "sort") }
3555 my($op, $cx, $name) = @_;
3557 my $kid = $op->first; # this is the (map|grep)start
3558 $kid = $kid->first->sibling; # skip a pushmark
3559 my $code = $kid->first; # skip a null
3560 if (is_scope $code) {
3561 $code = "{" . $self->deparse($code, 0) . "} ";
3563 $code = $self->deparse($code, 24);
3564 $code .= ", " if !null($kid->sibling);
3566 $kid = $kid->sibling;
3567 for (; !null($kid); $kid = $kid->sibling) {
3568 $expr = $self->deparse($kid, 6);
3569 push @exprs, $expr if defined $expr;
3571 return $self->maybe_parens_func($self->keyword($name),
3572 $code . join(", ", @exprs), $cx, 5);
3575 sub pp_mapwhile { mapop(@_, "map") }
3576 sub pp_grepwhile { mapop(@_, "grep") }
3577 sub pp_mapstart { baseop(@_, "map") }
3578 sub pp_grepstart { baseop(@_, "grep") }
3583 eval { require B::Op_private }
3584 ? @{$B::Op_private::ops_using{OPpLVAL_INTRO}}
3585 : qw(gvsv rv2sv rv2hv rv2gv rv2av aelem helem aslice
3586 hslice delete padsv padav padhv enteriter entersub padrange
3587 pushmark cond_expr refassign list)
3589 delete @uses_intro{qw( lvref lvrefslice lvavref entersub )};
3593 # Look for a my attribute declaration in a list or ex-list. Returns undef
3594 # if not found, 'my($x, @a) :Foo(bar)' etc otherwise.
3596 # There are three basic tree structs that are expected:
3599 # <1> ex-list vK/LVINTRO ->c
3600 # <0> ex-pushmark v ->3
3601 # <1> entersub[t2] vKRS*/TARG ->b
3603 # <0> padsv[$x:64,65] vM/LVINTRO ->c
3608 # <1> ex-list vK ->c
3609 # <0> ex-pushmark v ->3
3610 # <0> padav[@a:64,65] vM/LVINTRO ->4
3611 # <1> entersub[t2] vKRS*/TARG ->c
3614 # my ($x,@a,%h) :foo;
3616 # <;> nextstate(main 64 -e:1) v:{ ->3
3618 # <0> pushmark vM/LVINTRO ->4
3619 # <0> padsv[$x:64,65] vM/LVINTRO ->5
3620 # <0> padav[@a:64,65] vM/LVINTRO ->6
3621 # <0> padhv[%h:64,65] vM/LVINTRO ->7
3622 # <1> entersub[t4] vKRS*/TARG ->f
3624 # <1> entersub[t5] vKRS*/TARG ->n
3626 # <1> entersub[t6] vKRS*/TARG ->v
3628 # where the entersub in all cases looks like
3629 # <1> entersub[t2] vKRS*/TARG ->c
3630 # <0> pushmark s ->5
3631 # <$> const[PV "attributes"] sM ->6
3632 # <$> const[PV "main"] sM ->7
3633 # <1> srefgen sKM/1 ->9
3634 # <1> ex-list lKRM ->8
3635 # <0> padsv[@a:64,65] sRM ->8
3636 # <$> const[PV "foo"] sM ->a
3637 # <.> method_named[PV "import"] ->b
3640 my ($self, $op, $cx) = @_;
3642 my $kid = $op->first->sibling; # skip pushmark
3643 return if class($kid) eq 'NULL';
3648 # Extract out all the pad ops and entersub ops into
3649 # @padops and @entersubops. Return if anything else seen.
3650 # Also determine what class (if any) all the pad vars belong to
3652 my (@padops, @entersubops);
3653 for ($lop = $kid; !null($lop); $lop = $lop->sibling) {
3654 my $lopname = $lop->name;
3655 my $loppriv = $lop->private;
3656 if ($lopname =~ /^pad[sah]v$/) {
3657 return unless $loppriv & OPpLVAL_INTRO;
3658 return if $loppriv & OPpPAD_STATE;
3660 my $padname = $self->padname_sv($lop->targ);
3661 my $thisclass = ($padname->FLAGS & SVpad_TYPED)
3662 ? $padname->SvSTASH->NAME : 'main';
3664 # all pad vars must be in the same class
3665 $class //= $thisclass;
3666 return unless $thisclass eq $class;
3670 elsif ($lopname eq 'entersub') {
3671 push @entersubops, $lop;
3678 return unless @padops && @padops == @entersubops;
3680 # there should be a balance: each padop has a corresponding
3681 # 'attributes'->import() method call, in the same order.
3686 for my $i (0..$#padops) {
3687 my $padop = $padops[$i];
3688 my $esop = $entersubops[$i];
3690 push @varnames, $self->padname($padop->targ);
3692 return unless ($esop->flags & OPf_KIDS);
3694 my $kid = $esop->first;
3695 return unless $kid->type == OP_PUSHMARK;
3697 $kid = $kid->sibling;
3698 return unless $$kid && $kid->type == OP_CONST;
3699 return unless $self->const_sv($kid)->PV eq 'attributes';
3701 $kid = $kid->sibling;
3702 return unless $$kid && $kid->type == OP_CONST; # __PACKAGE__
3704 $kid = $kid->sibling;
3706 && $kid->name eq "srefgen"
3707 && ($kid->flags & OPf_KIDS)
3708 && ($kid->first->flags & OPf_KIDS)
3709 && $kid->first->first->name =~ /^pad[sah]v$/
3710 && $kid->first->first->targ == $padop->targ;
3712 $kid = $kid->sibling;
3715 last if ($kid->type != OP_CONST);
3716 push @attr, $self->const_sv($kid)->PV;
3717 $kid = $kid->sibling;
3719 return unless @attr;
3720 my $thisattr = ":" . join(' ', @attr);
3721 $attr_text //= $thisattr;
3722 # all import calls must have the same list of attributes
3723 return unless $attr_text eq $thisattr;
3725 return unless $kid->name eq 'method_named';
3726 return unless $self->meth_sv($kid)->PV eq 'import';
3728 $kid = $kid->sibling;
3733 $res .= " $class " if $class ne 'main';
3736 ? "(" . join(', ', @varnames) . ')'
3739 return "$res $attr_text";
3748 # might be my ($s,@a,%h) :Foo(bar);
3749 my $my_attr = maybe_my_attr($self, $op, $cx);
3750 return $my_attr if defined $my_attr;
3754 my $kid = $op->first->sibling; # skip pushmark
3755 return '' if class($kid) eq 'NULL';
3757 my $local = "either"; # could be local(...), my(...), state(...) or our(...)
3759 for ($lop = $kid; !null($lop); $lop = $lop->sibling) {
3760 my $lopname = $lop->name;
3761 my $loppriv = $lop->private;
3763 if ($lopname =~ /^pad[ash]v$/ && $loppriv & OPpLVAL_INTRO) {
3764 if ($loppriv & OPpPAD_STATE) { # state()
3765 ($local = "", last) if $local !~ /^(?:either|state)$/;
3768 ($local = "", last) if $local !~ /^(?:either|my)$/;
3771 my $padname = $self->padname_sv($lop->targ);
3772 if ($padname->FLAGS & SVpad_TYPED) {
3773 $newtype = $padname->SvSTASH->NAME;
3775 } elsif ($lopname =~ /^(?:gv|rv2)([ash])v$/
3776 && $loppriv & OPpOUR_INTRO
3777 or $lopname eq "null" && class($lop) eq 'UNOP'
3778 && $lop->first->name eq "gvsv"
3779 && $lop->first->private & OPpOUR_INTRO) { # our()
3780 my $newlocal = "local " x !!($loppriv & OPpLVAL_INTRO) . "our";
3782 if $local ne 'either' && $local ne $newlocal;
3784 my $funny = !$1 || $1 eq 's' ? '$' : $1 eq 'a' ? '@' : '%';
3785 if (my $t = $self->find_our_type(
3786 $funny . $self->gv_or_padgv($lop->first)->NAME
3790 } elsif ($lopname ne 'undef'
3791 and !($loppriv & OPpLVAL_INTRO)
3792 || !exists $uses_intro{$lopname eq 'null'
3793 ? substr B::ppname($lop->targ), 3
3796 $local = ""; # or not
3798 } elsif ($lopname ne "undef")
3801 ($local = "", last) if $local !~ /^(?:either|local)$/;
3804 if (defined $type && defined $newtype && $newtype ne $type) {
3810 $local = "" if $local eq "either"; # no point if it's all undefs
3811 $local &&= join ' ', map $self->keyword($_), split / /, $local;
3812 $local .= " $type " if $local && length $type;
3813 return $self->deparse($kid, $cx) if null $kid->sibling and not $local;
3814 for (; !null($kid); $kid = $kid->sibling) {
3816 if (class($kid) eq "UNOP" and $kid->first->name eq "gvsv") {
3821 $self->{'avoid_local'}{$$lop}++;
3822 $expr = $self->deparse($kid, 6);
3823 delete $self->{'avoid_local'}{$$lop};
3825 $expr = $self->deparse($kid, 6);
3830 return "$local(" . join(", ", @exprs) . ")";
3832 return $self->maybe_parens( join(", ", @exprs), $cx, 6);
3836 sub is_ifelse_cont {
3838 return ($op->name eq "null" and class($op) eq "UNOP"
3839 and $op->first->name =~ /^(and|cond_expr)$/
3840 and is_scope($op->first->first->sibling));
3846 my $cond = $op->first;
3847 my $true = $cond->sibling;
3848 my $false = $true->sibling;
3849 my $cuddle = $self->{'cuddle'};
3850 unless ($cx < 1 and (is_scope($true) and $true->name ne "null") and
3851 (is_scope($false) || is_ifelse_cont($false))
3852 and $self->{'expand'} < 7) {
3853 $cond = $self->deparse($cond, 8);
3854 $true = $self->deparse($true, 6);
3855 $false = $self->deparse($false, 8);
3856 return $self->maybe_parens("$cond ? $true : $false", $cx, 8);
3859 $cond = $self->deparse($cond, 1);
3860 $true = $self->deparse($true, 0);
3861 my $head = $self->keyword("if") . " ($cond) {\n\t$true\n\b}";
3864 while (!null($false) and is_ifelse_cont($false)) {
3865 my $newop = $false->first;
3866 my $newcond = $newop->first;
3867 my $newtrue = $newcond->sibling;
3868 $false = $newtrue->sibling; # last in chain is OP_AND => no else
3869 if ($newcond->name eq "lineseq")
3871 # lineseq to ensure correct line numbers in elsif()
3872 # Bug #37302 fixed by change #33710.
3873 $newcond = $newcond->first->sibling;
3875 $newcond = $self->deparse($newcond, 1);
3876 $newtrue = $self->deparse($newtrue, 0);
3877 $elsif ||= $self->keyword("elsif");
3878 push @elsifs, "$elsif ($newcond) {\n\t$newtrue\n\b}";
3880 if (!null($false)) {
3881 $false = $cuddle . $self->keyword("else") . " {\n\t" .
3882 $self->deparse($false, 0) . "\n\b}\cK";
3886 return $head . join($cuddle, "", @elsifs) . $false;
3890 my ($self, $op, $cx) = @_;
3891 my $cond = $op->first;
3892 my $true = $cond->sibling;
3894 my $ret = $self->deparse($true, $cx);
3895 $ret =~ s/^(\(?)\$/$1 . $self->keyword("state") . ' $'/e;
3901 my($op, $cx, $init) = @_;
3902 my $enter = $op->first;
3903 my $kid = $enter->sibling;
3904 local(@$self{qw'curstash warnings hints hinthash'})
3905 = @$self{qw'curstash warnings hints hinthash'};
3911 if ($kid->name eq "lineseq") { # bare or infinite loop
3912 if ($kid->last->name eq "unstack") { # infinite
3913 $head = "while (1) "; # Can't use for(;;) if there's a continue
3919 } elsif ($enter->name eq "enteriter") { # foreach
3920 my $ary = $enter->first->sibling; # first was pushmark
3921 my $var = $ary->sibling;
3922 if ($ary->name eq 'null' and $enter->private & OPpITER_REVERSED) {
3923 # "reverse" was optimised away
3924 $ary = listop($self, $ary->first->sibling, 1, 'reverse');
3925 } elsif ($enter->flags & OPf_STACKED
3926 and not null $ary->first->sibling->sibling)
3928 $ary = $self->deparse($ary->first->sibling, 9) . " .. " .
3929 $self->deparse($ary->first->sibling->sibling, 9);
3931 $ary = $self->deparse($ary, 1);
3934 $var = $self->pp_padsv($enter, 1, 1);
3935 } elsif ($var->name eq "rv2gv") {
3936 $var = $self->pp_rv2sv($var, 1);
3937 if ($enter->private & OPpOUR_INTRO) {
3938 # our declarations don't have package names
3939 $var =~ s/^(.).*::/$1/;
3942 } elsif ($var->name eq "gv") {
3943 $var = "\$" . $self->deparse($var, 1);
3945 $var = $self->deparse($var, 1);
3947 $body = $kid->first->first->sibling; # skip OP_AND and OP_ITER
3948 if (!is_state $body->first and $body->first->name !~ /^(?:stub|leave|scope)$/) {
3949 confess unless $var eq '$_';
3950 $body = $body->first;
3951 return $self->deparse($body, 2) . " "
3952 . $self->keyword("foreach") . " ($ary)";
3954 $head = "foreach $var ($ary) ";
3955 } elsif ($kid->name eq "null") { # while/until
3957 $name = {"and" => "while", "or" => "until"}->{$kid->name};
3958 $cond = $kid->first;
3959 $body = $kid->first->sibling;
3960 } elsif ($kid->name eq "stub") { # bare and empty
3961 return "{;}"; # {} could be a hashref
3963 # If there isn't a continue block, then the next pointer for the loop
3964 # will point to the unstack, which is kid's last child, except
3965 # in a bare loop, when it will point to the leaveloop. When neither of
3966 # these conditions hold, then the second-to-last child is the continue
3967 # block (or the last in a bare loop).
3968 my $cont_start = $enter->nextop;
3972 if ($$cont_start != $$op && ${$cont_start} != ${$body->last}) {
3974 $cont = $body->last;
3976 $cont = $body->first;
3977 while (!null($cont->sibling->sibling)) {
3978 $cont = $cont->sibling;
3981 my $state = $body->first;
3982 my $cuddle = $self->{'cuddle'};
3984 for (; $$state != $$cont; $state = $state->sibling) {
3985 push @states, $state;
3987 $body = $self->lineseq(undef, 0, @states);
3988 if (defined $cond and not is_scope $cont and $self->{'expand'} < 3) {
3989 $precond = "for ($init; ";
3990 $postcond = "; " . $self->deparse($cont, 1) .") ";
3993 $cont = $cuddle . "continue {\n\t" .
3994 $self->deparse($cont, 0) . "\n\b}\cK";
3997 return "" if !defined $body;
3999 $precond = "for ($init; ";
4003 $body = $self->deparse($body, 0);
4005 if ($precond) { # for(;;)
4006 $cond &&= $name eq 'until'
4007 ? listop($self, undef, 1, "not", $cond->first)
4008 : $self->deparse($cond, 1);
4009 $head = "$precond$cond$postcond";
4011 if ($name && !$head) {
4012 ref $cond and $cond = $self->deparse($cond, 1);
4013 $head = "$name ($cond) ";
4015 $head =~ s/^(for(?:each)?|while|until)/$self->keyword($1)/e;
4016 $body =~ s/;?$/;\n/;
4018 return $head . "{\n\t" . $body . "\b}" . $cont;
4021 sub pp_leaveloop { shift->loop_common(@_, "") }
4026 my $init = $self->deparse($op, 1);
4027 my $s = $op->sibling;
4028 my $ll = $s->name eq "unstack" ? $s->sibling : $s->first->sibling;
4029 return $self->loop_common($ll, $cx, $init);
4034 return "eval {\n\t" . $self->pp_leave(@_) . "\n\b}";
4038 my ($op, $expect_type) = @_;
4039 my $type = $op->type;
4040 return($type == $expect_type
4041 || ($type == OP_NULL && $op->targ == $expect_type));
4045 my($self, $op, $cx) = @_;
4047 # might be 'my $s :Foo(bar);'
4048 if ($op->targ == OP_LIST) {
4049 my $my_attr = maybe_my_attr($self, $op, $cx);
4050 return $my_attr if defined $my_attr;
4053 if (class($op) eq "OP") {
4055 return $self->{'ex_const'} if $op->targ == OP_CONST;
4056 } elsif (class ($op) eq "COP") {
4057 return &pp_nextstate;
4058 } elsif ($op->first->name eq 'pushmark'
4059 or $op->first->name eq 'null'
4060 && $op->first->targ == OP_PUSHMARK
4061 && _op_is_or_was($op, OP_LIST)) {
4062 return $self->pp_list($op, $cx);
4063 } elsif ($op->first->name eq "enter") {
4064 return $self->pp_leave($op, $cx);
4065 } elsif ($op->first->name eq "leave") {
4066 return $self->pp_leave($op->first, $cx);
4067 } elsif ($op->first->name eq "scope") {
4068 return $self->pp_scope($op->first, $cx);
4069 } elsif ($op->targ == OP_STRINGIFY) {
4070 return $self->dquote($op, $cx);
4071 } elsif ($op->targ == OP_GLOB) {
4072 return $self->pp_glob(
4073 $op->first # entersub
4079 } elsif (!null($op->first->sibling) and
4080 $op->first->sibling->name eq "readline" and
4081 $op->first->sibling->flags & OPf_STACKED) {
4082 return $self->maybe_parens($self->deparse($op->first, 7) . " = "
4083 . $self->deparse($op->first->sibling, 7),
4085 } elsif (!null($op->first->sibling) and
4086 $op->first->sibling->name =~ /^transr?\z/ and
4087 $op->first->sibling->flags & OPf_STACKED) {
4088 return $self->maybe_parens($self->deparse($op->first, 20) . " =~ "
4089 . $self->deparse($op->first->sibling, 20),
4091 } elsif ($op->flags & OPf_SPECIAL && $cx < 1 && !$op->targ) {
4092 return ($self->lex_in_scope("&do") ? "CORE::do" : "do")
4093 . " {\n\t". $self->deparse($op->first, $cx) ."\n\b};";
4094 } elsif (!null($op->first->sibling) and
4095 $op->first->sibling->name eq "null" and
4096 class($op->first->sibling) eq "UNOP" and
4097 $op->first->sibling->first->flags & OPf_STACKED and
4098 $op->first->sibling->first->name eq "rcatline") {
4099 return $self->maybe_parens($self->deparse($op->first, 18) . " .= "
4100 . $self->deparse($op->first->sibling, 18),
4103 return $self->deparse($op->first, $cx);
4110 return $self->padname_sv($targ)->PVX;
4116 return substr($self->padname($op->targ), 1); # skip $/@/%
4121 my($op, $cx, $forbid_parens) = @_;
4122 my $targ = $op->targ;
4123 return $self->maybe_my($op, $cx, $self->padname($targ),
4124 $self->padname_sv($targ),
4128 sub pp_padav { pp_padsv(@_) }
4133 # with OPpPADHV_ISKEYS the keys op is optimised away, except
4134 # in scalar context the old op is kept (but not executed) so its targ
4136 $keys = 'keys ' if ( ($op->private & OPpPADHV_ISKEYS)
4137 && !(($op->flags & OPf_WANT) == OPf_WANT_SCALAR));
4138 $keys . pp_padsv(@_);
4144 if (class($op) eq "PADOP") {
4145 return $self->padval($op->padix);
4146 } else { # class($op) eq "SVOP"
4154 my $gv = $self->gv_or_padgv($op);
4155 return $self->maybe_local($op, $cx, $self->stash_variable("\$",
4156 $self->gv_name($gv), $cx));
4162 my $gv = $self->gv_or_padgv($op);
4163 return $self->gv_name($gv);
4166 sub pp_aelemfast_lex {
4169 my $name = $self->padname($op->targ);
4171 my $i = $op->private;
4172 $i -= 256 if $i > 127;
4173 return $name . "[" . ($i + $self->{'arybase'}) . "]";
4179 # optimised PADAV, pre 5.15
4180 return $self->pp_aelemfast_lex(@_) if ($op->flags & OPf_SPECIAL);
4182 my $gv = $self->gv_or_padgv($op);
4183 my($name,$quoted) = $self->stash_variable_name('@',$gv);
4184 $name = $quoted ? "$name->" : '$' . $name;
4185 my $i = $op->private;
4186 $i -= 256 if $i > 127;
4187 return $name . "[" . ($i + $self->{'arybase'}) . "]";
4192 my($op, $cx, $type) = @_;
4194 if (class($op) eq 'NULL' || !$op->can("first")) {
4195 carp("Unexpected op in pp_rv2x");
4198 my $kid = $op->first;
4199 if ($kid->name eq "gv") {
4200 return $self->stash_variable($type, $self->deparse($kid, 0), $cx);
4201 } elsif (is_scalar $kid) {
4202 my $str = $self->deparse($kid, 0);
4203 if ($str =~ /^\$([^\w\d])\z/) {
4204 # "$$+" isn't a legal way to write the scalar dereference
4205 # of $+, since the lexer can't tell you aren't trying to
4206 # do something like "$$ + 1" to get one more than your
4207 # PID. Either "${$+}" or "$${+}" are workable
4208 # disambiguations, but if the programmer did the former,
4209 # they'd be in the "else" clause below rather than here.
4210 # It's not clear if this should somehow be unified with
4211 # the code in dq and re_dq that also adds lexer
4212 # disambiguation braces.
4213 $str = '$' . "{$1}"; #'
4215 return $type . $str;
4217 return $type . "{" . $self->deparse($kid, 0) . "}";
4221 sub pp_rv2sv { maybe_local(@_, rv2x(@_, "\$")) }
4222 sub pp_rv2gv { maybe_local(@_, rv2x(@_, "*")) }
4226 (($op->private & OPpRV2HV_ISKEYS) ? 'keys ' : '')
4227 . maybe_local(@_, rv2x(@_, "%"))
4234 if ($op->first->name eq "padav") {
4235 return $self->maybe_local($op, $cx, '$#' . $self->padany($op->first));
4237 return $self->maybe_local($op, $cx,
4238 $self->rv2x($op->first, $cx, '$#'));
4242 # skip down to the old, ex-rv2cv
4244 my ($self, $op, $cx) = @_;
4245 if (!null($op->first) && $op->first->name eq 'null' &&
4246 $op->first->targ == OP_LIST)
4248 return $self->rv2x($op->first->first->sibling, $cx, "&")
4251 return $self->rv2x($op, $cx, "")
4257 my($cx, @list) = @_;
4258 my @a = map $self->const($_, 6), @list;
4263 } elsif ( @a > 2 and !grep(!/^-?\d+$/, @a)) {
4264 # collapse (-1,0,1,2) into (-1..2)
4265 my ($s, $e) = @a[0,-1];
4267 return $self->maybe_parens("$s..$e", $cx, 9)
4268 unless grep $i++ != $_, @a;
4270 return $self->maybe_parens(join(", ", @a), $cx, 6);
4276 my $kid = $op->first;
4277 if ($kid->name eq "const") { # constant list
4278 my $av = $self->const_sv($kid);
4279 return $self->list_const($cx, $av->ARRAY);
4281 return $self->maybe_local($op, $cx, $self->rv2x($op, $cx, "\@"));
4285 sub is_subscriptable {
4287 if ($op->name =~ /^([ahg]elem|multideref$)/) {
4289 } elsif ($op->name eq "entersub") {
4290 my $kid = $op->first;
4291 return 0 unless null $kid->sibling;
4293 $kid = $kid->sibling until null $kid->sibling;
4294 return 0 if is_scope($kid);
4296 return 0 if $kid->name eq "gv" || $kid->name eq "padcv";
4297 return 0 if is_scalar($kid);
4298 return is_subscriptable($kid);
4304 sub elem_or_slice_array_name
4307 my ($array, $left, $padname, $allow_arrow) = @_;
4309 if ($array->name eq $padname) {
4310 return $self->padany($array);
4311 } elsif (is_scope($array)) { # ${expr}[0]
4312 return "{" . $self->deparse($array, 0) . "}";
4313 } elsif ($array->name eq "gv") {
4314 ($array, my $quoted) =
4315 $self->stash_variable_name(
4316 $left eq '[' ? '@' : '%', $self->gv_or_padgv($array)
4318 if (!$allow_arrow && $quoted) {
4319 # This cannot happen.
4320 die "Invalid variable name $array for slice";
4322 return $quoted ? "$array->" : $array;
4323 } elsif (!$allow_arrow || is_scalar $array) { # $x[0], $$x[0], ...
4324 return $self->deparse($array, 24);
4330 sub elem_or_slice_single_index
4335 $idx = $self->deparse($idx, 1);
4337 # Outer parens in an array index will confuse perl
4338 # if we're interpolating in a regular expression, i.e.
4339 # /$x$foo[(-1)]/ is *not* the same as /$x$foo[-1]/
4341 # If $self->{parens}, then an initial '(' will
4342 # definitely be paired with a final ')'. If
4343 # !$self->{parens}, the misleading parens won't
4344 # have been added in the first place.
4346 # [You might think that we could get "(...)...(...)"
4347 # where the initial and final parens do not match
4348 # each other. But we can't, because the above would
4349 # only happen if there's an infix binop between the
4350 # two pairs of parens, and *that* means that the whole
4351 # expression would be parenthesized as well.]
4353 $idx =~ s/^\((.*)\)$/$1/ if $self->{'parens'};
4355 # Hash-element braces will autoquote a bareword inside themselves.
4356 # We need to make sure that C<$hash{warn()}> doesn't come out as
4357 # C<$hash{warn}>, which has a quite different meaning. Currently
4358 # B::Deparse will always quote strings, even if the string was a
4359 # bareword in the original (i.e. the OPpCONST_BARE flag is ignored
4360 # for constant strings.) So we can cheat slightly here - if we see
4361 # a bareword, we know that it is supposed to be a function call.
4363 $idx =~ s/^([A-Za-z_]\w*)$/$1()/;
4370 my ($op, $cx, $left, $right, $padname) = @_;
4371 my($array, $idx) = ($op->first, $op->first->sibling);
4373 $idx = $self->elem_or_slice_single_index($idx);
4375 unless ($array->name eq $padname) { # Maybe this has been fixed
4376 $array = $array->first; # skip rv2av (or ex-rv2av in _53+)
4378 if (my $array_name=$self->elem_or_slice_array_name
4379 ($array, $left, $padname, 1)) {
4380 return ($array_name =~ /->\z/
4382 : $array_name eq '#' ? '${#}' : "\$" . $array_name)
4383 . $left . $idx . $right;
4385 # $x[20][3]{hi} or expr->[20]
4386 my $arrow = is_subscriptable($array) ? "" : "->";
4387 return $self->deparse($array, 24) . $arrow . $left . $idx . $right;
4392 # a simplified version of elem_or_slice_array_name()
4393 # for the use of pp_multideref
4395 sub multideref_var_name {
4397 my ($gv, $is_hash) = @_;
4399 my ($name, $quoted) =
4400 $self->stash_variable_name( $is_hash ? '%' : '@', $gv);
4401 return $quoted ? "$name->"
4403 ? '${#}' # avoid ${#}[1] => $#[1]
4408 # deparse an OP_MULTICONCAT. If $in_dq is 1, we're within
4409 # a double-quoted string, so for example.
4411 # might get compiled as
4412 # multiconcat("abc", metaquote(multiconcat("def", $x)), "bar")
4413 # and the inner multiconcat should be deparsed as C<def$x> rather than
4414 # the normal C<def . $x>
4415 # Ditto if $in_dq is 2, handle qr/...\Qdef$x\E.../.
4417 sub do_multiconcat {
4419 my($op, $cx, $in_dq) = @_;
4427 for ($kid = $op->first; !null $kid; $kid = $kid->sibling) {
4428 # skip the consts and/or padsv we've optimised away
4430 unless $kid->type == OP_NULL
4431 && ( $kid->targ == OP_PADSV
4432 || $kid->targ == OP_CONST
4433 || $kid->targ == OP_PUSHMARK);
4436 $append = ($op->private & OPpMULTICONCAT_APPEND);
4438 if ($op->private & OPpTARGET_MY) {
4439 # '$lex = ...' or '$lex .= ....' or 'my $lex = '
4440 $lhs = $self->padname($op->targ);
4441 $lhs = "my $lhs" if ($op->private & OPpLVAL_INTRO);
4444 elsif ($op->flags & OPf_STACKED) {
4445 # 'expr = ...' or 'expr .= ....'
4446 my $expr = $append ? shift(@kids) : pop(@kids);
4447 $lhs = $self->deparse($expr, 7);
4452 $lhs .= $append ? ' .= ' : ' = ';
4455 my ($nargs, $const_str, @const_lens) = $op->aux_list($self->{curcv});
4461 push @consts, undef;
4464 push @consts, substr($const_str, $i, $_);
4473 || (($op->private & OPpMULTICONCAT_STRINGIFY) && !$self->{'unquote'}))
4475 # "foo=$foo bar=$bar "
4478 $rhs = dq_disambiguate($rhs, $self->dq(shift(@kids), 18))
4481 my $c = shift @consts;
4484 # in pattern: don't convert&n