blib/lib/B/DeparseTree/P526c.pm untested

filter: Statementstmt 0.0% 0 / 2180 Branchbran 0.0% 0 / 878 Conditioncond 0.0% 0 / 551 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 398 Podpod 0.0% 0 / 380 totaltotal 0.0% 0 / 4387 CC 436 SCAR 46.6
CC
436
Cov
0%
begin_is_use
600.0
ambient_pragmas
506.0
loop_common
462.0
CRAP 105.6
1 # B::DeparseTree::P526.pm
2 # Copyright (c) 1998-2000, 2002, 2003, 2004, 2005, 2006 Stephen McCamant.
3 # Copyright (c) 2015, 2017, 2018 Rocky Bernstein
4 # All rights reserved.
5 # This module is free software; you can redistribute and/or modify
6 # it under the same terms as Perl itself.
7  
8 # This is based on the module B::Deparse (for perl 5.22) by Stephen McCamant.
9 # It has been extended save tree structure, and is addressible
10 # by opcode address.
11  
12 # B::Parse in turn is based on the module of the same name by Malcolm Beattie,
13 # but essentially none of his code remains.
14  
15 use v5.26;
16  
17 use rlib '../..';
18  
19 package B::DeparseTree::P526c;
20 use Carp;
21  
22 use B qw(class main_root main_start main_cv svref_2object opnumber perlstring
23 OPf_WANT OPf_WANT_VOID OPf_WANT_SCALAR OPf_WANT_LIST
24 OPf_KIDS OPf_REF OPf_STACKED OPf_SPECIAL OPf_MOD OPf_PARENS
25 OPpLVAL_INTRO OPpOUR_INTRO OPpENTERSUB_AMPER OPpSLICE OPpCONST_BARE
26 OPpTRANS_SQUASH OPpTRANS_DELETE OPpTRANS_COMPLEMENT OPpTARGET_MY
27 OPpEXISTS_SUB OPpSORT_NUMERIC OPpSORT_INTEGER OPpREPEAT_DOLIST
28 OPpSORT_REVERSE OPpMULTIDEREF_EXISTS OPpMULTIDEREF_DELETE
29 OPpPADRANGE_COUNTSHIFT OPpSIGNATURE_FAKE
30 OPpSPLIT_ASSIGN OPpSPLIT_LEX
31 SVf_ROK SVpad_OUR SVf_FAKE SVs_RMG SVs_SMG
32 SVpad_TYPED
33 CVf_METHOD
34 PMf_KEEP PMf_GLOBAL PMf_CONTINUE PMf_EVAL PMf_ONCE
35 PMf_MULTILINE PMf_SINGLELINE PMf_FOLD PMf_EXTENDED PMf_EXTENDED_MORE
36 PADNAMEt_OUTER
37 MDEREF_reload
38 MDEREF_AV_pop_rv2av_aelem
39 MDEREF_AV_gvsv_vivify_rv2av_aelem
40 MDEREF_AV_padsv_vivify_rv2av_aelem
41 MDEREF_AV_vivify_rv2av_aelem
42 MDEREF_AV_padav_aelem
43 MDEREF_AV_gvav_aelem
44 MDEREF_HV_pop_rv2hv_helem
45 MDEREF_HV_gvsv_vivify_rv2hv_helem
46 MDEREF_HV_padsv_vivify_rv2hv_helem
47 MDEREF_HV_vivify_rv2hv_helem
48 MDEREF_HV_padhv_helem
49 MDEREF_HV_gvhv_helem
50 MDEREF_ACTION_MASK
51 MDEREF_INDEX_none
52 MDEREF_INDEX_const
53 MDEREF_INDEX_padsv
54 MDEREF_INDEX_gvsv
55 MDEREF_INDEX_MASK
56 MDEREF_FLAG_last
57 MDEREF_MASK
58 MDEREF_SHIFT
59  
60 SIGNATURE_reload
61 SIGNATURE_end
62 SIGNATURE_padintro
63 SIGNATURE_arg
64 SIGNATURE_arg_default_none
65 SIGNATURE_arg_default_undef
66 SIGNATURE_arg_default_0
67 SIGNATURE_arg_default_1
68 SIGNATURE_arg_default_iv
69 SIGNATURE_arg_default_const
70 SIGNATURE_arg_default_padsv
71 SIGNATURE_arg_default_gvsv
72 SIGNATURE_arg_default_op
73 SIGNATURE_array
74 SIGNATURE_hash
75 SIGNATURE_ACTION_MASK
76 SIGNATURE_FLAG_skip
77 SIGNATURE_MASK
78 SIGNATURE_SHIFT
79 );
80  
81 use B::DeparseTree::Common;
82 use B::DeparseTree::PP;
83  
84 use strict;
85 use vars qw/$AUTOLOAD/;
86 use warnings ();
87 require feature;
88 use types
89  
90 our($VERSION, @EXPORT, @ISA);
91 our $VERSION = '2.0c';
92  
93 @ISA = qw(Exporter B::DeparseTree::Common);
94 @EXPORT = qw(compile);
95  
96 BEGIN {
97 # List version-specific constants here.
98 # Easiest way to keep this code portable between version looks to
99 # be to fake up a dummy constant that will never actually be true.
100 foreach (qw(OPpSORT_INPLACE OPpSORT_DESCEND OPpITER_REVERSED OPpCONST_NOVER
101 OPpPAD_STATE PMf_SKIPWHITE RXf_SKIPWHITE
102 PMf_CHARSET PMf_KEEPCOPY PMf_NOCAPTURE CVf_ANONCONST
103 CVf_LOCKED OPpREVERSE_INPLACE OPpSUBSTR_REPL_FIRST
104 PMf_NONDESTRUCT OPpCONST_ARYBASE OPpEVAL_BYTES
105 OPpLVREF_TYPE OPpLVREF_SV OPpLVREF_AV OPpLVREF_HV
106 OPpLVREF_CV OPpLVREF_ELEM SVpad_STATE)) {
107 eval { B->import($_) };
108 no strict 'refs';
109 *{$_} = sub () {0} unless *{$_}{CODE};
110 }
111 }
112  
113 # See older source for inline comments recording changes prior to 2.0
114  
115 # In order to test modulie, we run this over Perl test suite.
116 # Then we run the test on the deparsed code. Slick, eh?
117  
118 # Todo:
119 # (See also BUGS section at the end of this file)
120 #
121 # - cperl: AELEM_U, AELEMFAST_LEX_U, PERL_FAKE_SIGNATURE
122 # - finish tr/// changes
123 # - add option for even more parens (generalize \&foo change)
124 # - left/right context
125 # - copy comments (look at real text with $^P?)
126 # - avoid semis in one-statement blocks
127 # - associativity of &&=, ||=, ?:
128 # - ',' => '=>' (auto-unquote?)
129 # - break long lines ("\r" as discretionary break?)
130 # - configurable syntax highlighting: ANSI color, HTML, TeX, etc.
131 # - more style options: brace style, hex vs. octal, quotes, ...
132 # - print big ints as hex/octal instead of decimal (heuristic?)
133 # - handle 'my $x if 0'?
134 # - version using op_next instead of op_first/sibling?
135 # - avoid string copies (pass arrays, one big join?)
136 # - here-docs?
137  
138 # Here are B::DeparseTree failure notes:
139 # Current test.deparse failures
140 # comp/hints 6 - location of BEGIN blocks wrt. block openings
141 # run/switchI 1 - missing -I switches entirely
142 # perl -Ifoo -e 'print @INC'
143 # op/caller 2 - warning mask propagates backwards before warnings::register
144 # 'use warnings; BEGIN {${^WARNING_BITS} eq "U"x12;} use warnings::register'
145 # op/getpid 2 - can't assign to shared my() declaration (threads only)
146 # 'my $x : shared = 5'
147 # op/override 7 - parens on overridden require change v-string interpretation
148 # 'BEGIN{*CORE::GLOBAL::require=sub {}} require v5.6'
149 # c.f. 'BEGIN { *f = sub {0} }; f 2'
150 # op/pat 774 - losing Unicode-ness of Latin1-only strings
151 # 'use charnames ":short"; $x="\N{latin:a with acute}"'
152 # op/recurse 12 - missing parens on recursive call makes it look like method
153 # 'sub f { f($x) }'
154 # op/subst 90 - inconsistent handling of utf8 under "use utf8"
155 # op/taint 29 - "use re 'taint'" deparsed in the wrong place wrt. block open
156 # op/tiehandle compile - "use strict" deparsed in the wrong place
157 # uni/tr_ several
158 # ext/B/t/xref 11 - line numbers when we add newlines to one-line subs
159 # ext/Data/Dumper/t/dumper compile
160 # ext/DB_file/several
161 # ext/Encode/several
162 # ext/Ernno/Errno warnings
163 # ext/IO/lib/IO/t/io_sel 23
164 # ext/PerlIO/t/encoding compile
165 # ext/POSIX/t/posix 6
166 # ext/Socket/Socket 8
167 # dist/Storable/t/croak compile
168 # lib/Attribute/Handlers/t/multi compile
169 # lib/bignum/ several
170 # lib/charnames 35
171 # lib/constant 32
172 # lib/English 40
173 # lib/ExtUtils/t/bytes 4
174 # lib/File/DosGlob compile
175 # lib/Filter/Simple/t/data 1
176 # lib/Math/BigInt/t/constant 1
177 # lib/Net/t/config Deparse-warning
178 # lib/overload compile
179 # lib/Switch/ several
180 # lib/Symbol 4
181 # lib/Test/Simple several
182 # lib/Term/Complete
183 # lib/Tie/File/t/29_downcopy 5
184 # lib/vars 22
185  
186 # Object fields:
187 #
188 # in_coderef2text:
189 # True when deparsing via $deparse->coderef2text; false when deparsing the
190 # main program.
191 #
192 # avoid_local:
193 # (local($a), local($b)) and local($a, $b) have the same internal
194 # representation but the short form looks better. We notice we can
195 # use a large-scale local when checking the list, but need to prevent
196 # individual locals too. This hash holds the addresses of OPs that
197 # have already had their local-ness accounted for. The same thing
198 # is done with my().
199 #
200 # curcv:
201 # CV for current sub (or main program) being deparsed
202 #
203 # curcvlex:
204 # Cached hash of lexical variables for curcv: keys are
205 # names prefixed with "m" or "o" (representing my/our), and
206 # each value is an array with two elements indicating the cop_seq
207 # of scopes in which a var of that name is valid and a third ele-
208 # ment referencing the pad name.
209 #
210 # curcop:
211 # COP for statement being deparsed
212 #
213 # curstash:
214 # name of the current package for deparsed code
215 #
216 # subs_todo:
217 # array of [cop_seq, CV, is_format?, name] for subs and formats we still
218 # want to deparse. The fourth element is a pad name thingy for lexical
219 # subs or a string for special blocks. For other subs, it is undef. For
220 # lexical subs, CV may be undef, indicating a stub declaration.
221 #
222 # protos_todo:
223 # as above, but [name, prototype] for subs that never got a GV
224 #
225 # subs_done, forms_done:
226 # keys are addresses of GVs for subs and formats we've already
227 # deparsed (or at least put into subs_todo)
228 #
229 # subs_declared
230 # keys are names of subs for which we've printed declarations.
231 # That means we can omit parentheses from the arguments. It also means we
232 # need to put CORE:: on core functions of the same name.
233 #
234 # in_subst_repl
235 # True when deparsing the replacement part of a substitution.
236 #
237 # in_refgen
238 # True when deparsing the argument to \.
239 #
240 # parens: -p
241 # linenums: -l
242 # unquote: -q
243 # cuddle: ' ' or '\n', depending on -sC
244 # indent_size: -si
245 # use_tabs: -sT
246 # ex_const: -sv
247  
248 # A little explanation of how precedence contexts and associativity
249 # work:
250 #
251 # deparse() calls each per-op subroutine with an argument $cx (short
252 # for context, but not the same as the cx* in the perl core), which is
253 # a number describing the op's parents in terms of precedence, whether
254 # they're inside an expression or at statement level, etc. (see
255 # chart below). When ops with children call deparse on them, they pass
256 # along their precedence. Fractional values are used to implement
257 # associativity ('($x + $y) + $z' => '$x + $y + $y') and related
258 # parentheses hacks. The major disadvantage of this scheme is that
259 # it doesn't know about right sides and left sides, so say if you
260 # assign a listop to a variable, it can't tell it's allowed to leave
261 # the parens off the listop.
262  
263 # Precedences:
264 # 26 [TODO] inside interpolation context ("")
265 # 25 left terms and list operators (leftward)
266 # 24 left ->
267 # 23 nonassoc ++ --
268 # 22 right **
269 # 21 right ! ~ \ and unary + and -
270 # 20 left =~ !~
271 # 19 left * / % x
272 # 18 left + - .
273 # 17 left << >>
274 # 16 nonassoc named unary operators
275 # 15 nonassoc < > <= >= lt gt le ge
276 # 14 nonassoc == != <=> eq ne cmp
277 # 13 left &
278 # 12 left | ^
279 # 11 left &&
280 # 10 left ||
281 # 9 nonassoc .. ...
282 # 8 right ?:
283 # 7 right = += -= *= etc.
284 # 6 left , =>
285 # 5 nonassoc list operators (rightward)
286 # 4 right not
287 # 3 left and
288 # 2 left or xor
289 # 1 statement modifiers
290 # 0.5 statements, but still print scopes as do { ... }
291 # 0 statement level
292 # -1 format body
293  
294 # Nonprinting characters with special meaning:
295 # \cS - steal parens (see maybe_parens_unop)
296 # \n - newline and indent
297 # \t - increase indent
298 # \b - decrease indent ('outdent')
299 # \f - flush left (no indent)
300 # \cK - kill following semicolon, if any
301  
302 # Semicolon handling:
303 # - Individual statements are not deparsed with trailing semicolons.
304 # (If necessary, \cK is tacked on to the end.)
305 # - Whatever code joins statements together or emits them (lineseq,
306 # scopeop, deparse_root) is responsible for adding semicolons where
307 # necessary.
308 # - use statements are deparsed with trailing semicolons because they are
309 # immediately concatenated with the following statement.
310 # - indent() removes semicolons wherever it sees \cK.
311  
312 BEGIN {
313 for (qw[ const stringify rv2sv list glob pushmark null aelem aelemfast
314 nextstate dbstate rv2av rv2hv helem custom ]) {
315 eval "sub OP_\U$_ () { " . opnumber($_) . "}"
316 }
317 # cperl only:
318 for (qw[ aelem_u aelemfast_lex_u s_aelem_u ]) {
319 eval "sub OP_\U$_ () { " . opnumber($_) . "}"
320 }
321 }
322  
323 # excluding aelemfast, aelemfast_lex, aelemfast_lex_u
324 sub is_aelem {
325 my $opnum = shift;
326 return ($opnum >= OP_AELEM && $opnum <= OP_S_AELEM_U) ?
327 $opnum != OP_AELEMFAST_LEX_U : 0;
328 }
329  
330 sub deparse_format
331 {
332 my ($self, $form, $parent) = @_;
333 my @texts;
334 local($self->{'curcv'}) = $form;
335 local($self->{'curcvlex'});
336 local($self->{'in_format'}) = 1;
337 local(@$self{qw'curstash warnings hints hinthash'})
338 = @$self{qw'curstash warnings hints hinthash'};
339 my $op = $form->ROOT;
340 local $B::overlay = {};
341 $self->pessimise($op, $form->START);
342 my $info = {
343 op => $op,
344 parent => $parent,
345 cop => $self->{'curcop'}
346 };
347 $self->{optree}{$$op} = $info;
348  
349 if ($op->first->name eq 'stub' || $op->first->name eq 'nextstate') {
350 my $info->{text} = "\f.";
351 return $info;
352 }
353  
354 $op->{other_ops} = [$op->first];
355 $op = $op->first->first; # skip leavewrite, lineseq
356 my $kid;
357 while (not null $op) {
358 push @{$op->{other_ops}}, $op;
359 $op = $op->sibling; # skip nextstate
360 my @body;
361 push @{$op->{other_ops}}, $op->first;
362 $kid = $op->first->sibling; # skip a pushmark
363 push @texts, "\f".$self->const_sv($kid)->PV;
364 push @{$op->{other_ops}}, $kid;
365 $kid = $kid->sibling;
366 for (; not null $kid; $kid = $kid->sibling) {
367 push @body, $self->deparse($kid, -1, $op);
368 $body[-1] =~ s/;\z//;
369 }
370 push @texts, "\f".join(", ", @body)."\n" if @body;
371 $op = $op->sibling;
372 }
373  
374 $info->{text} = $self->combine2str(\@texts) . "\f.";
375 $info->{texts} = \@texts;
376 return $info;
377 }
378  
379 # Return a "use" declaration for this BEGIN block, if appropriate
380 sub begin_is_use
381 {
382 my ($self, $cv) = @_;
383 my $root = $cv->ROOT;
384 local @$self{qw'curcv curcvlex'} = ($cv);
385 local $B::overlay = {};
386 $self->pessimise($root, $cv->START);
387 # require B::Debug;
388 # B::walkoptree($cv->ROOT, "debug");
389 my $lineseq = $root->first;
390 return if $lineseq->name ne "lineseq";
391  
392 my $req_op = $lineseq->first->sibling;
393 return if $req_op->name ne "require";
394  
395 my $module;
396 if ($req_op->first->private & OPpCONST_BARE) {
397 # Actually it should always be a bareword
398 $module = $self->const_sv($req_op->first)->PV;
399 $module =~ s[/][::]g;
400 $module =~ s/.pm$//;
401 }
402 else {
403 $module = $self->const($self->const_sv($req_op->first), 6);
404 }
405  
406 my $version;
407 my $version_op = $req_op->sibling;
408 return if class($version_op) eq "NULL";
409 if ($version_op->name eq "lineseq") {
410 # We have a version parameter; skip nextstate & pushmark
411 my $constop = $version_op->first->next->next;
412  
413 return unless $self->const_sv($constop)->PV eq $module;
414 $constop = $constop->sibling;
415 $version = $self->const_sv($constop);
416 if (class($version) eq "IV") {
417 $version = $version->int_value;
418 } elsif (class($version) eq "NV") {
419 $version = $version->NV;
420 } elsif (class($version) ne "PVMG") {
421 # Includes PVIV and PVNV
422 $version = $version->PV;
423 } else {
424 # version specified as a v-string
425 $version = 'v'.join '.', map ord, split //, $version->PV;
426 }
427 $constop = $constop->sibling;
428 return if $constop->name ne "method_named";
429 return if $self->meth_sv($constop)->PV ne "VERSION";
430 }
431  
432 $lineseq = $version_op->sibling;
433 return if $lineseq->name ne "lineseq";
434 my $entersub = $lineseq->first->sibling;
435 if ($entersub->name eq "stub") {
436 return "use $module $version ();\n" if defined $version;
437 return "use $module ();\n";
438 }
439 return if $entersub->name !~ /^enter(xs)?sub/;
440  
441 # See if there are import arguments
442 my $args = '';
443  
444 my $svop = $entersub->first->sibling; # Skip over pushmark
445 return unless $self->const_sv($svop)->PV eq $module;
446  
447 # Pull out the arguments
448 for ($svop=$svop->sibling; index($svop->name, "method_") != 0;
449 $svop = $svop->sibling) {
450 $args .= ", " if length($args);
451 $args .= $self->deparse($svop, 6, $root);
452 }
453  
454 my $use = 'use';
455 my $method_named = $svop;
456 return if $method_named->name ne "method_named";
457 my $method_name = $self->meth_sv($method_named)->PV;
458  
459 if ($method_name eq "unimport") {
460 $use = 'no';
461 }
462  
463 # Certain pragmas are dealt with using hint bits,
464 # so we ignore them here
465 if ($module eq 'strict' || $module eq 'integer'
466 || $module eq 'bytes' || $module eq 'warnings'
467 || $module eq 'feature') {
468 return "";
469 }
470  
471 if (defined $version && length $args) {
472 return "$use $module $version ($args);\n";
473 } elsif (defined $version) {
474 return "$use $module $version;\n";
475 } elsif (length $args) {
476 return "$use $module ($args);\n";
477 } else {
478 return "$use $module;\n";
479 }
480 }
481  
482 sub ambient_pragmas {
483 my $self = shift;
484 my ($arybase, $hint_bits, $warning_bits, $hinthash) = (0, 0);
485  
486 while (@_ > 1) {
487 my $name = shift();
488 my $val = shift();
489  
490 if ($name eq 'strict') {
491 require strict;
492  
493 if ($val eq 'none') {
494 $hint_bits &= $strict_bits{$_} for qw/refs subs vars/;
495 next();
496 }
497  
498 my @names;
499 if ($val eq "all") {
500 @names = qw/refs subs vars/;
501 }
502 elsif (ref $val) {
503 @names = @$val;
504 }
505 else {
506 @names = split' ', $val;
507 }
508 $hint_bits |= $strict_bits{$_} for @names;
509 }
510  
511 elsif ($name eq '$[') {
512 if (OPpCONST_ARYBASE) {
513 $arybase = $val;
514 } else {
515 croak "\$[ can't be non-zero on this perl" unless $val == 0;
516 }
517 }
518  
519 elsif ($name eq 'integer'
520 || $name eq 'bytes'
521 || $name eq 'utf8') {
522 require "$name.pm";
523 if ($val) {
524 $hint_bits |= ${$::{"${name}::"}{"hint_bits"}};
525 }
526 else {
527 $hint_bits &= ~${$::{"${name}::"}{"hint_bits"}};
528 }
529 }
530  
531 elsif ($name eq 're') {
532 require re;
533 if ($val eq 'none') {
534 $hint_bits &= ~re::bits(qw/taint eval/);
535 next();
536 }
537  
538 my @names;
539 if ($val eq 'all') {
540 @names = qw/taint eval/;
541 }
542 elsif (ref $val) {
543 @names = @$val;
544 }
545 else {
546 @names = split' ',$val;
547 }
548 $hint_bits |= re::bits(@names);
549 }
550  
551 elsif ($name eq 'warnings') {
552 if ($val eq 'none') {
553 $warning_bits = $warnings::NONE;
554 next();
555 }
556  
557 my @names;
558 if (ref $val) {
559 @names = @$val;
560 }
561 else {
562 @names = split/\s+/, $val;
563 }
564  
565 $warning_bits = $warnings::NONE if !defined ($warning_bits);
566 $warning_bits |= warnings::bits(@names);
567 }
568  
569 elsif ($name eq 'warning_bits') {
570 $warning_bits = $val;
571 }
572  
573 elsif ($name eq 'hint_bits') {
574 $hint_bits = $val;
575 }
576  
577 elsif ($name eq '%^H') {
578 $hinthash = $val;
579 }
580  
581 else {
582 croak "Unknown pragma type: $name";
583 }
584 }
585 if (@_) {
586 croak "The ambient_pragmas method expects an even number of args";
587 }
588  
589 $self->{'ambient_arybase'} = $arybase;
590 $self->{'ambient_warnings'} = $warning_bits;
591 $self->{'ambient_hints'} = $hint_bits;
592 $self->{'ambient_hinthash'} = $hinthash;
593 }
594  
595 # Sort of like maybe_parens in that we may possibly add (). However we take
596 # an op rather than text, and return a tree node. Also, we get around
597 # the 'if it looks like a function' rule.
598 sub maybe_parens_unop($self, $name, $op, $cx, $parent)
599 {
600 my $self = shift;
601 my($name, $op, $cx, $parent) = @_;
602 my $info = $self->deparse($op, 1, $parent);
603 my $text = $info->{text};
604 if ($name eq "umask" && $info->{text} =~ /^\d+$/) {
605 $text = sprintf("%#o", $text);
606 }
607 if ($cx > 16 or $self->{'parens'}) {
608 return info_from_list([$self->keyword($name), '(', $text, ')'], '',
609 'maybe_parens_unop_parens', {body => [$info]});
610 } else {
611 $name = $self->keyword($name);
612 if (substr($text, 0, 1) eq "\cS") {
613 # use op's parens
614 return info_from_list([$name, substr($text, 1)], '',
615 'maybe_parens_unop_cS', {body => [$info]});
616 } elsif (substr($text, 0, 1) eq "(") {
617 # avoid looks-like-a-function trap with extra parens
618 # ('+' can lead to ambiguities)
619 return info_from_list([$name, '(', $text, ')'], '',
620 "maybe_parens_unop_fn", {});
621 } else {
622 return info_from_list([$name, $text], ' ',
623 'maybe_parens_unop', {body => [$info]});
624 }
625 }
626 Carp::confess("unhandled condition in maybe_parens_unop");
627 }
628  
629 sub maybe_parens_func
630 {
631 my($self, $func, $params, $cx, $prec) = @_;
632 if ($prec <= $cx or substr($params, 0, 1) eq "(" or $self->{'parens'}) {
633 return ($func, '(', $params, ')');
634 } else {
635 return ($func, ' ', $params);
636 }
637 }
638  
639 sub maybe_local_str
640 {
641 my($self, $op, $cx, $text) = @_;
642 my $our_intro = ($op->name =~ /^(gv|rv2)[ash]v$/) ? OPpOUR_INTRO : 0;
643 if ($op->private & (OPpLVAL_INTRO|$our_intro)
644 and not $self->{'avoid_local'}{$$op}) {
645 my $our_local = ($op->private & OPpLVAL_INTRO) ? "local" : "our";
646 if( $our_local eq 'our' ) {
647 if ( $text !~ /^\W(\w+::)*\w+\z/
648 and !utf8::decode($text) || $text !~ /^\W(\w+::)*\w+\z/
649 ) {
650 die "Unexpected our($text)\n";
651 }
652 $text =~ s/(\w+::)+//;
653 }
654 if (want_scalar($op)) {
655 return info_from_list([$our_local, $text], ' ', 'maybe_local_scalar', {});
656 } else {
657 my @texts = $self->maybe_parens_func($our_local, $text, $cx, 16);
658 return info_from_list(\@texts, '', 'maybe_local_array', {});
659 }
660 } else {
661 return info_from_text($text, 'maybe_local', {});
662 }
663 }
664  
665 sub maybe_local {
666 my $self = shift;
667 my($op, $cx, $text) = @_;
668 my $name = $op->name;
669 my $our_intro = ($name =~ /^(?:(?:gv|rv2)[ash]v|split|refassign
670 |lv(?:av)?ref)$/x)
671 ? OPpOUR_INTRO
672 : 0;
673 my $lval_intro = $name eq 'split' ? 0 : OPpLVAL_INTRO;
674 # The @a in \(@a) isn't in ref context, but only when the
675 # parens are there.
676 my $need_parens = $self->{'in_refgen'} && $name =~ /[ah]v\z/
677 && ($op->flags & (OPf_PARENS|OPf_REF)) == OPf_PARENS;
678 if ((my $priv = $op->private) & ($lval_intro|$our_intro)) {
679 my @our_local;
680 push @our_local, "local" if $priv & $lval_intro;
681 push @our_local, "our" if $priv & $our_intro;
682 my $our_local = join " ", map $self->keyword($_), @our_local;
683 if( $our_local[-1] eq 'our' ) {
684 if ( $text !~ /^\W(\w+::)*\w+\z/
685 and !utf8::decode($text) || $text !~ /^\W(\w+::)*\w+\z/
686 ) {
687 die "Unexpected our($text)\n";
688 }
689 $text =~ s/(\w+::)+//;
690  
691 if (my $type = $self->find_our_type($text)) {
692 $our_local .= ' ' . $type;
693 }
694 }
695 return $need_parens ? "($text)" : $text
696 if $self->{'avoid_local'}{$$op};
697 if ($need_parens) {
698 return "$our_local($text)";
699 } elsif (want_scalar($op)) {
700 return "$our_local $text";
701 } else {
702 return $self->maybe_parens_func("$our_local", $text, $cx, 16);
703 }
704 } else {
705 return $need_parens ? "($text)" : $text;
706 }
707 }
708  
709 sub maybe_local {
710 my($self, $op, $cx, $var_info) = @_;
711 $var_info->{parent} = $$op;
712 return maybe_local_str($self, $op, $cx, $var_info->{text});
713 }
714  
715 sub padname_sv {
716 my $self = shift;
717 my $targ = shift;
718 return $self->{'curcv'}->PADLIST->ARRAYelt(0)->ARRAYelt($targ);
719 }
720  
721 sub maybe_my {
722 my $self = shift;
723 my($op, $cx, $text, $forbid_parens) = @_;
724 if ($op->private & OPpLVAL_INTRO and not $self->{'avoid_local'}{$$op}) {
725 my $my_str = $op->private & OPpPAD_STATE
726 ? $self->keyword("state")
727 : "my";
728 if ($forbid_parens || want_scalar($op)) {
729 return info_from_list([$my_str, $text], ' ', 'maybe_my_no_parens', {});
730 } else {
731 return info_from_list([$my_str, $text], ' ',
732 'maybe_my_parens',
733 {maybe_parens => [$self, $cx, 16]});
734 }
735 } else {
736 return info_from_text($text, 'maybe_my_avoid_local', {});
737 }
738 }
739  
740 # The following OPs don't have functions:
741  
742 # pp_padany -- does not exist after parsing
743  
744 sub AUTOLOAD {
745 if ($AUTOLOAD =~ s/^.*::pp_//) {
746 warn "unexpected OP_".
747 ($_[1]->type == OP_CUSTOM ? "CUSTOM ($AUTOLOAD)" : uc $AUTOLOAD);
748 return "XXX";
749 } else {
750 die "Undefined subroutine $AUTOLOAD called";
751 }
752 }
753  
754 sub DESTROY {} # Do not AUTOLOAD
755  
756 # The BEGIN {} is used here because otherwise this code isn't executed
757 # when you run B::Deparse on itself.
758 my %globalnames;
759 BEGIN { map($globalnames{$_}++, "SIG", "STDIN", "STDOUT", "STDERR", "INC",
760 "ENV", "ARGV", "ARGVOUT", "_"); }
761  
762 sub gv_name {
763 my $self = shift;
764 my $gv = shift;
765 my $raw = shift;
766 # Carp::confess() unless ref($gv) eq "B::GV";
767 my $cv = $gv->FLAGS & SVf_ROK ? $gv->RV : 0;
768 my $stash = ($cv || $gv)->STASH->NAME;
769 my $name = $raw
770 ? $cv ? $cv->NAME_HEK || $cv->GV->NAME : $gv->NAME
771 : $cv
772 ? B::safename($cv->NAME_HEK || $cv->GV->NAME)
773 : $gv->SAFENAME;
774 if ($stash eq 'main' && $name =~ /^::/) {
775 $stash = '::';
776 }
777 elsif (($stash eq 'main'
778 && ($globalnames{$name} || $name =~ /^[^A-Za-z_:]/))
779 or ($stash eq $self->{'curstash'} && !$globalnames{$name}
780 && ($stash eq 'main' || $name !~ /::/))
781 )
782 {
783 $stash = "";
784 } else {
785 $stash = $stash . "::";
786 }
787 if (!$raw and $name =~ /^(\^..|{)/) {
788 $name = "{$name}"; # ${^WARNING_BITS}, etc and ${
789 }
790 return $stash . $name;
791 }
792  
793 # Return the name to use for a stash variable.
794 # If a lexical with the same name is in scope, or
795 # if strictures are enabled, it may need to be
796 # fully-qualified.
797 sub stash_variable {
798 my ($self, $prefix, $name, $cx) = @_;
799  
800 return "$prefix$name" if $name =~ /::/;
801  
802 unless ($prefix eq '$' || $prefix eq '@' || $prefix eq '&' || #'
803 $prefix eq '%' || $prefix eq '$#') {
804 return "$prefix$name";
805 }
806  
807 if ($name =~ /^[^[:alpha:]_+-]$/) {
808 if (defined $cx && $cx == 26) {
809 if ($prefix eq '@') {
810 return "$prefix\{$name}";
811 }
812 elsif ($name eq '#') { return '${#}' } # "${#}a" vs "$#a"
813 }
814 if ($prefix eq '$#') {
815 return "\$#{$name}";
816 }
817 }
818  
819 return $prefix . $self->maybe_qualify($prefix, $name);
820 }
821  
822 my %unctrl = # portable to EBCDIC
823 (
824 "\c@" => '@', # unused
825 "\cA" => 'A',
826 "\cB" => 'B',
827 "\cC" => 'C',
828 "\cD" => 'D',
829 "\cE" => 'E',
830 "\cF" => 'F',
831 "\cG" => 'G',
832 "\cH" => 'H',
833 "\cI" => 'I',
834 "\cJ" => 'J',
835 "\cK" => 'K',
836 "\cL" => 'L',
837 "\cM" => 'M',
838 "\cN" => 'N',
839 "\cO" => 'O',
840 "\cP" => 'P',
841 "\cQ" => 'Q',
842 "\cR" => 'R',
843 "\cS" => 'S',
844 "\cT" => 'T',
845 "\cU" => 'U',
846 "\cV" => 'V',
847 "\cW" => 'W',
848 "\cX" => 'X',
849 "\cY" => 'Y',
850 "\cZ" => 'Z',
851 "\c[" => '[', # unused
852 "\c\\" => '\\', # unused
853 "\c]" => ']', # unused
854 "\c_" => '_', # unused
855 );
856  
857 sub lex_in_scope {
858 my ($self, $name, $our) = @_;
859 substr $name, 0, 0, = $our ? 'o' : 'm'; # our/my
860 $self->populate_curcvlex() if !defined $self->{'curcvlex'};
861  
862 return 0 if !defined($self->{'curcop'});
863 my $seq = $self->{'curcop'}->cop_seq;
864 return 0 if !exists $self->{'curcvlex'}{$name};
865 for my $a (@{$self->{'curcvlex'}{$name}}) {
866 my ($st, $en) = @$a;
867 return 1 if $seq > $st && $seq <= $en;
868 }
869 return 0;
870 }
871  
872 sub populate_curcvlex {
873 my $self = shift;
874 for (my $cv = $self->{'curcv'}; class($cv) eq "CV"; $cv = $cv->OUTSIDE) {
875 my $padlist = $cv->PADLIST;
876 # an undef CV still in lexical chain
877 next if class($padlist) eq "SPECIAL";
878 my @padlist = $padlist->ARRAY;
879 my @ns = $padlist[0]->ARRAY;
880  
881 for (my $i=0; $i<@ns; ++$i) {
882 next if class($ns[$i]) eq "SPECIAL";
883 if (class($ns[$i]) eq "PV") {
884 # Probably that pesky lexical @_
885 next;
886 }
887 my $name = $ns[$i]->PVX;
888 next unless defined $name;
889 my ($seq_st, $seq_en) =
890 ($ns[$i]->FLAGS & SVf_FAKE)
891 ? (0, 999999)
892 : ($ns[$i]->COP_SEQ_RANGE_LOW, $ns[$i]->COP_SEQ_RANGE_HIGH);
893  
894 push @{$self->{'curcvlex'}{
895 ($ns[$i]->FLAGS & SVpad_OUR ? 'o' : 'm') . $name
896 }}, [$seq_st, $seq_en, $ns[$i]];
897 }
898 }
899 }
900  
901 sub find_scope_st { ((find_scope(@_))[0]); }
902 sub find_scope_en { ((find_scope(@_))[1]); }
903  
904 # Recurses down the tree, looking for pad variable introductions and COPs
905 sub find_scope {
906 my ($self, $op, $scope_st, $scope_en) = @_;
907 carp("Undefined op in find_scope") if !defined $op;
908 return ($scope_st, $scope_en) unless $op->flags & OPf_KIDS;
909  
910 my @queue = ($op);
911 while(my $op = shift @queue ) {
912 for (my $o=$op->first; $$o; $o=$o->sibling) {
913 if ($o->name =~ /^pad.v$/ && $o->private & OPpLVAL_INTRO) {
914 my $s = int($self->padname_sv($o->targ)->COP_SEQ_RANGE_LOW);
915 my $e = $self->padname_sv($o->targ)->COP_SEQ_RANGE_HIGH;
916 $scope_st = $s if !defined($scope_st) || $s < $scope_st;
917 $scope_en = $e if !defined($scope_en) || $e > $scope_en;
918 return ($scope_st, $scope_en);
919 }
920 elsif (is_state($o)) {
921 my $c = $o->cop_seq;
922 $scope_st = $c if !defined($scope_st) || $c < $scope_st;
923 $scope_en = $c if !defined($scope_en) || $c > $scope_en;
924 return ($scope_st, $scope_en);
925 }
926 elsif ($o->flags & OPf_KIDS) {
927 unshift (@queue, $o);
928 }
929 }
930 }
931  
932 return ($scope_st, $scope_en);
933 }
934  
935 # Returns a list of subs which should be inserted before the COP
936 sub cop_subs {
937 my ($self, $op, $out_seq) = @_;
938 my $seq = $op->cop_seq;
939 # If we have nephews, then our sequence number indicates
940 # the cop_seq of the end of some sort of scope.
941 if (class($op->sibling) ne "NULL" && $op->sibling->flags & OPf_KIDS
942 and my $nseq = $self->find_scope_st($op->sibling) ) {
943 $seq = $nseq;
944 }
945 $seq = $out_seq if defined($out_seq) && $out_seq < $seq;
946 return $self->seq_subs($seq);
947 }
948  
949 sub pp_unstack {
950 # see also leaveloop
951 return info_from_text('', 'unstack', {});
952 }
953  
954 my %feature_keywords = (
955 # keyword => 'feature',
956 state => 'state',
957 say => 'say',
958 given => 'switch',
959 when => 'switch',
960 default => 'switch',
961 break => 'switch',
962 evalbytes=>'evalbytes',
963 __SUB__ => '__SUB__',
964 fc => 'fc',
965 );
966  
967 # keywords that are strong and also have a prototype
968 #
969 my %strong_proto_keywords = map { $_ => 1 } qw(
970 pos
971 prototype
972 scalar
973 study
974 undef
975 );
976  
977 sub feature_enabled {
978 my($self,$name) = @_;
979 my $hh;
980 my $hints = $self->{hints} & $feature::hint_mask;
981 if ($hints && $hints != $feature::hint_mask) {
982 $hh = _features_from_bundle($hints);
983 }
984 elsif ($hints) { $hh = $self->{'hinthash'} }
985 return $hh && $hh->{"feature_$feature_keywords{$name}"}
986 }
987  
988 sub keyword {
989 my $self = shift;
990 my $name = shift;
991 return $name if $name =~ /^CORE::/; # just in case
992 if (exists $feature_keywords{$name}) {
993 my $hh;
994 my $hints = $self->{hints} & $feature::hint_mask;
995 if ($hints && $hints != $feature::hint_mask) {
996 $hh = _features_from_bundle($hints);
997 }
998 elsif ($hints) { $hh = $self->{'hinthash'} }
999 return "CORE::$name"
1000 if !$hh
1001 || !$hh->{"feature_$feature_keywords{$name}"}
1002 }
1003 if ($strong_proto_keywords{$name}
1004 || ($name !~ /^(?:chom?p|do|exec|glob|s(?:elect|ystem))\z/
1005 && !defined eval{prototype "CORE::$name"})
1006 ) { return $name }
1007 if (
1008 exists $self->{subs_declared}{$name}
1009 or
1010 exists &{"$self->{curstash}::$name"}
1011 ) {
1012 return "CORE::$name"
1013 }
1014 return $name;
1015 }
1016  
1017 sub pp_not
1018 {
1019 my($self, $op, $cx) = @_;
1020 if ($cx <= 4) {
1021 $self->listop($op, $cx, "not", $op->first);
1022 } else {
1023 $self->pfixop($op, $cx, "!", 21);
1024 }
1025 }
1026  
1027 sub pp_pos { maybe_local(@_, unop(@_, "pos")) }
1028 sub pp_sin { maybe_targmy(@_, \&unop, "sin") }
1029 sub pp_cos { maybe_targmy(@_, \&unop, "cos") }
1030 sub pp_rand { maybe_targmy(@_, \&unop, "rand") }
1031 sub pp_srand { unop(@_, "srand") }
1032 sub pp_exp { maybe_targmy(@_, \&unop, "exp") }
1033 sub pp_log { maybe_targmy(@_, \&unop, "log") }
1034 sub pp_sqrt { maybe_targmy(@_, \&unop, "sqrt") }
1035 sub pp_int { maybe_targmy(@_, \&unop, "int") }
1036 sub pp_hex { maybe_targmy(@_, \&unop, "hex") }
1037 sub pp_oct { maybe_targmy(@_, \&unop, "oct") }
1038 sub pp_abs { maybe_targmy(@_, \&unop, "abs") }
1039  
1040 sub pp_length { maybe_targmy(@_, \&unop, "length") }
1041 sub pp_ord { maybe_targmy(@_, \&unop, "ord") }
1042 sub pp_chr { maybe_targmy(@_, \&unop, "chr") }
1043  
1044 sub pp_each { unop(@_, "each") }
1045 sub pp_values { unop(@_, "values") }
1046 sub pp_keys { unop(@_, "keys") }
1047 { no strict 'refs'; *{"pp_r$_"} = *{"pp_$_"} for qw< keys each values >; }
1048 sub pp_boolkeys
1049 {
1050 # no name because its an optimisation op that has no keyword
1051 unop(@_,"");
1052 }
1053 sub pp_aeach { unop(@_, "each") }
1054 sub pp_avalues { unop(@_, "values") }
1055 sub pp_akeys { unop(@_, "keys") }
1056 sub pp_pop { unop(@_, "pop") }
1057 sub pp_shift { unop(@_, "shift") }
1058  
1059 sub pp_caller { unop(@_, "caller") }
1060 sub pp_reset { unop(@_, "reset") }
1061 sub pp_exit { unop(@_, "exit") }
1062 sub pp_prototype { unop(@_, "prototype") }
1063  
1064 sub pp_close { unop(@_, "close") }
1065 sub pp_fileno { unop(@_, "fileno") }
1066 sub pp_umask { unop(@_, "umask") }
1067 sub pp_untie { unop(@_, "untie") }
1068 sub pp_tied { unop(@_, "tied") }
1069 sub pp_dbmclose { unop(@_, "dbmclose") }
1070 sub pp_getc { unop(@_, "getc") }
1071 sub pp_eof { unop(@_, "eof") }
1072 sub pp_tell { unop(@_, "tell") }
1073 sub pp_getsockname { unop(@_, "getsockname") }
1074 sub pp_getpeername { unop(@_, "getpeername") }
1075  
1076 sub pp_chdir {
1077 my ($self, $op, $cx) = @_;
1078 if (($op->flags & (OPf_SPECIAL|OPf_KIDS)) == (OPf_SPECIAL|OPf_KIDS)) {
1079 my $kw = $self->keyword("chdir");
1080 my $kid = $self->const_sv($op->first)->PV;
1081 my $code = $kw
1082 . ($cx >= 16 || $self->{'parens'} ? "($kid)" : " $kid");
1083 maybe_targmy(@_, sub { $_[3] }, $code);
1084 } else {
1085 maybe_targmy(@_, \&unop, "chdir")
1086 }
1087 }
1088  
1089 sub pp_chroot { maybe_targmy(@_, \&unop, "chroot") }
1090 sub pp_readlink { unop(@_, "readlink") }
1091 sub pp_rmdir { maybe_targmy(@_, \&unop, "rmdir") }
1092 sub pp_readdir { unop(@_, "readdir") }
1093 sub pp_telldir { unop(@_, "telldir") }
1094 sub pp_rewinddir { unop(@_, "rewinddir") }
1095 sub pp_closedir { unop(@_, "closedir") }
1096 sub pp_getpgrp { maybe_targmy(@_, \&unop, "getpgrp") }
1097 sub pp_localtime { unop(@_, "localtime") }
1098 sub pp_gmtime { unop(@_, "gmtime") }
1099 sub pp_alarm { unop(@_, "alarm") }
1100 sub pp_sleep { maybe_targmy(@_, \&unop, "sleep") }
1101  
1102 sub pp_dofile {
1103 my $code = unop(@_, "do", 1); # llafr does not apply
1104 if ($code =~ s/^((?:CORE::)?do) \{/$1({/) { $code .= ')' }
1105 $code;
1106 }
1107 sub pp_entereval {
1108 unop(
1109 @_,
1110 $_[1]->private & OPpEVAL_BYTES ? 'evalbytes' : "eval"
1111 )
1112 }
1113  
1114 sub pp_ghbyname { unop(@_, "gethostbyname") }
1115 sub pp_gnbyname { unop(@_, "getnetbyname") }
1116 sub pp_gpbyname { unop(@_, "getprotobyname") }
1117 sub pp_shostent { unop(@_, "sethostent") }
1118 sub pp_snetent { unop(@_, "setnetent") }
1119 sub pp_sprotoent { unop(@_, "setprotoent") }
1120 sub pp_sservent { unop(@_, "setservent") }
1121 sub pp_gpwnam { unop(@_, "getpwnam") }
1122 sub pp_gpwuid { unop(@_, "getpwuid") }
1123 sub pp_ggrnam { unop(@_, "getgrnam") }
1124 sub pp_ggrgid { unop(@_, "getgrgid") }
1125  
1126 sub pp_lock { unop(@_, "lock") }
1127  
1128 sub pp_continue { unop(@_, "continue"); }
1129 sub pp_break { unop(@_, "break"); }
1130  
1131 sub givwhen
1132 {
1133 my($self, $op, $cx, $givwhen) = @_;
1134  
1135 my $enterop = $op->first;
1136 my ($head, $block);
1137 if ($enterop->flags & OPf_SPECIAL) {
1138 $head = $self->keyword("default");
1139 $block = $self->deparse($enterop->first, 0, $enterop, $op);
1140 }
1141 else {
1142 my $cond = $enterop->first;
1143 my $cond_str = $self->deparse($cond, 1, $enterop, $op);
1144 $head = "$givwhen ($cond_str)";
1145 $block = $self->deparse($cond->sibling, 0, $enterop, $op);
1146 }
1147  
1148 return info_from_list([$head, "{",
1149 "\n\t", $block->{text}, "\n\b",
1150 "}\cK"], '', 'givwhen',
1151 {body => [$block]});
1152 }
1153  
1154 sub pp_leavegiven { givwhen(@_, $_[0]->keyword("given")); }
1155 sub pp_leavewhen { givwhen(@_, $_[0]->keyword("when")); }
1156  
1157 sub pp_delete
1158 {
1159 my($self, $op, $cx) = @_;
1160 my $arg;
1161 my ($info, $body, @texts, $type);
1162 if ($op->private & OPpSLICE) {
1163 if ($op->flags & OPf_SPECIAL) {
1164 # Deleting from an array, not a hash
1165 $info = $self->pp_aslice($op->first, 16);
1166 $type = 'delete_slice';
1167 }
1168 } else {
1169 if ($op->flags & OPf_SPECIAL) {
1170 # Deleting from an array, not a hash
1171 $info = $self->pp_aelem($op->first, 16);
1172 $type = 'delete_array'
1173 } else {
1174 $info = $self->pp_helem($op->first, 16);
1175 $type = 'delete_hash';
1176 }
1177 }
1178 my @texts = $self->maybe_parens_func("delete",
1179 $info->{text}, $cx, 16);
1180 return info_from_list(\@texts, '', $type, {body => [$info]});
1181 }
1182  
1183 sub pp_require
1184 {
1185 my($self, $op, $cx) = @_;
1186 my $opname = $op->flags & OPf_SPECIAL ? 'CORE::require' : 'require';
1187 if (class($op) eq "UNOP" and $op->first->name eq "const"
1188 and $op->first->private & OPpCONST_BARE) {
1189 my $name = $self->const_sv($op->first)->PV;
1190 $name =~ s[/][::]g;
1191 $name =~ s/\.pm//g;
1192 return info_from_list([$opname, $name], ' ',
1193 'require',
1194 {maybe_parens => [$self, $cx, 16]});
1195 } else {
1196 return $self->unop(
1197 $op, $cx,
1198 $op->first->name eq 'const'
1199 && $op->first->private & OPpCONST_NOVER
1200 ? "no"
1201 : $opname,
1202 1, # llafr does not apply
1203 );
1204 }
1205 Carp::confess("unhandled condition in pp_require");
1206 }
1207  
1208 sub pp_scalar
1209 {
1210 my($self, $op, $cx) = @_;
1211 my $kid = $op->first;
1212 if (not null $kid->sibling) {
1213 # XXX Was a here-doc
1214 return $self->dquote($op);
1215 }
1216 $self->unop($op, $cx, "scalar");
1217 }
1218  
1219  
1220 sub padval
1221 {
1222 my $self = shift;
1223 my $targ = shift;
1224 return $self->{'curcv'}->PADLIST->ARRAYelt(1)->ARRAYelt($targ);
1225 }
1226  
1227 sub anon_hash_or_list
1228 {
1229 my $self = shift;
1230 my($op, $cx) = @_;
1231  
1232 my $name = $op->name;
1233 my($pre, $post) = @{{"anonlist" => ["[","]"],
1234 "anonhash" => ["{","}"]}->{$name}};
1235 my($expr, @exprs);
1236 my $other_ops => [$op->first];
1237 $op = $op->first->sibling; # skip pushmark
1238 for (; !null($op); $op = $op->sibling) {
1239 $expr = $self->deparse($op, 6, $op);
1240 push @exprs, $expr;
1241 }
1242 if ($pre eq "{" and $cx < 1) {
1243 # Disambiguate that it's not a block
1244 $pre = "+{";
1245 }
1246 my $texts = [$pre, join(", ", map($_->{text}, @exprs), $post)];
1247 return info_from_list($texts, '', $name,
1248 {body => \@exprs,
1249 other_ops => $other_ops
1250 });
1251 }
1252  
1253 sub pp_anonlist {
1254 my $self = shift;
1255 my ($op, $cx) = @_;
1256 if ($op->flags & OPf_SPECIAL) {
1257 return $self->anon_hash_or_list($op, $cx);
1258 }
1259 warn "Unexpected op pp_" . $op->name() . " without OPf_SPECIAL";
1260 return info_from_text('XXX', 'bad_anonlist', {});
1261 }
1262  
1263 *pp_anonhash = \&pp_anonlist;
1264  
1265 sub e_anoncode
1266 {
1267 my ($self, $info) = @_;
1268 my $sub_info = $self->deparse_sub($info->{code});
1269 return info_from_list(['sub', $sub_info->{text}], ' ', 'e_anoncode',
1270 {body=> [$sub_info]});
1271 }
1272  
1273 sub pp_refgen
1274 {
1275 my($self, $op, $cx) = @_;
1276 my $kid = $op->first;
1277 if ($kid->name eq "null") {
1278 my $other_ops = [$kid];
1279 my $anoncode = $kid = $kid->first;
1280 if ($anoncode->name eq "anonconst") {
1281 $anoncode = $anoncode->first->first->sibling;
1282 }
1283 if ($anoncode->name eq "anoncode"
1284 or !null($anoncode = $kid->sibling) and
1285 $anoncode->name eq "anoncode") {
1286 return $self->e_anoncode({ code => $self->padval($anoncode->targ) });
1287 } elsif ($kid->name eq "pushmark") {
1288 my $sib_name = $kid->sibling->name;
1289 if ($sib_name =~ /^enter(xs)?sub/) {
1290 my $kid_info = $self->deparse($kid->sibling, 1, $op);
1291 # Always show parens for \(&func()), but only with -p otherwise
1292 my @texts = ('\\', $kid_info->{text});
1293 if ($self->{'parens'} or $kid->sibling->private & OPpENTERSUB_AMPER) {
1294 @texts = ('(', "\\", $kid_info->{text}, ')');
1295 }
1296 return info_from_list(\@texts, '', 'refgen_entersub',
1297 {body => [$kid_info],
1298 other_ops => $other_ops});
1299 }
1300 }
1301 }
1302 local $self->{'in_refgen'} = 1;
1303 $self->pfixop($op, $cx, "\\", 20);
1304 }
1305  
1306 sub pp_srefgen { pp_refgen(@_) }
1307  
1308 sub pp_readline {
1309 my $self = shift;
1310 my($op, $cx) = @_;
1311 my $kid = $op->first;
1312 $kid = $kid->first if $kid->name eq "rv2gv"; # <$fh>
1313 if (is_scalar($kid)
1314 and $op->flags & OPf_SPECIAL
1315 and $self->deparse($kid, 1) eq 'ARGV') {
1316 my $body = [$self->deparse($kid, 1, $op)];
1317 return info_from_list(['<', $body->[0]{text}, '>'], '',
1318 'readline_scalar', {body=>$body});
1319 }
1320 return $self->unop($op, $cx, "readline");
1321 }
1322  
1323 sub pp_rcatline {
1324 my $self = shift;
1325 my($op) = @_;
1326 return info_from_list(["<", $self->gv_name($self->gv_or_padgv($op)), ">"],
1327 '', 'rcatline', {});
1328 }
1329  
1330 # Unary operators that can occur as pseudo-listops inside double quotes
1331 sub dq_unop
1332 {
1333 my($self, $op, $cx, $name, $prec, $flags) = (@_, 0, 0);
1334 my $kid;
1335 if ($op->flags & OPf_KIDS) {
1336 my $other_ops = undef;
1337 $kid = $op->first;
1338 if (not null $kid->sibling) {
1339 # If there's more than one kid, the first is an ex-pushmark.
1340 $other_ops = [$kid];
1341 $kid = $kid->sibling;
1342 }
1343 my $info = $self->maybe_parens_unop($name, $kid, $cx, $op);
1344 $info->{other_ops} = $other_ops if $other_ops;
1345 return $info;
1346 } else {
1347 my @texts = ($name);
1348 push @texts, '(', ')' if $op->flags & OPf_SPECIAL;
1349 return info_from_list(\@texts, '', 'dq', {});
1350 }
1351 Carp::confess("unhandled condition in dq_unop");
1352 }
1353  
1354 sub pp_ucfirst { dq_unop(@_, "ucfirst") }
1355 sub pp_lcfirst { dq_unop(@_, "lcfirst") }
1356 sub pp_uc { dq_unop(@_, "uc") }
1357 sub pp_lc { dq_unop(@_, "lc") }
1358 sub pp_quotemeta { maybe_targmy(@_, \&dq_unop, "quotemeta") }
1359 sub pp_fc { dq_unop(@_, "fc") }
1360  
1361 # loop expressions
1362 sub loopex
1363 {
1364 my ($self, $op, $cx, $name) = @_;
1365 my $opts = {maybe_parens => [$self, $cx, 7]};
1366 my ($type, $body);
1367 if (class($op) eq "PVOP") {
1368 return info_from_list([$name, $op->pv], ' ', 'loopex_pvop', {});
1369 } elsif (class($op) eq "OP") {
1370 # no-op
1371 $type = 'loopex_op';
1372 return info_from_text($name, 'loopex_op', $opts);
1373 } elsif (class($op) eq "UNOP") {
1374 (my $kid_info = $self->deparse($op->first, 7, $op)) =~ s/^\cS//;
1375 $opts->{body} = [$kid_info];
1376 return info_from_list([$name, $op->pv], ' ', 'loopex_unop', $opts);
1377 } else {
1378 return info_from_text($name, 'loopex', $opts);
1379 }
1380 Carp::confess("unhandled condition in lopex");
1381 }
1382  
1383 sub pp_last { loopex(@_, "last") }
1384 sub pp_next { loopex(@_, "next") }
1385 sub pp_redo { loopex(@_, "redo") }
1386 sub pp_goto { loopex(@_, "goto") }
1387 sub pp_dump { loopex(@_, "CORE::dump") }
1388  
1389 sub ftst
1390 {
1391 my($self, $op, $cx, $name) = @_;
1392 if (class($op) eq "UNOP") {
1393 # Genuine '-X' filetests are exempt from the LLAFR, but not
1394 # l?stat()
1395 if ($name =~ /^-/) {
1396 (my $kid = $self->deparse($op->first, 16, $op)) =~ s/^\cS//;
1397 return info_from_list([$name, $kid->{text}], ' ',
1398 'ftst_unop_dash',
1399 {body => [$kid],
1400 maybe_parens => [$self, $cx, 16]});
1401 }
1402 return $self->maybe_parens_unop($name, $op->first, $cx, $op);
1403 } elsif (class($op) =~ /^(SV|PAD)OP$/) {
1404 my @list = $self->maybe_parens_func($name, $self->pp_gv($op, 1), $cx, 16);
1405 return info_from_list(\@list, ' ', 'ftst_list', {});
1406 } else { # I don't think baseop filetests ever survive ck_ftst, but...
1407 return info_from_text($name, 'unop', {});
1408 }
1409 }
1410  
1411 sub pp_lstat { ftst(@_, "lstat") }
1412 sub pp_stat { ftst(@_, "stat") }
1413 sub pp_ftrread { ftst(@_, "-R") }
1414 sub pp_ftrwrite { ftst(@_, "-W") }
1415 sub pp_ftrexec { ftst(@_, "-X") }
1416 sub pp_fteread { ftst(@_, "-r") }
1417 sub pp_ftewrite { ftst(@_, "-w") }
1418 sub pp_fteexec { ftst(@_, "-x") }
1419 sub pp_ftis { ftst(@_, "-e") }
1420 sub pp_fteowned { ftst(@_, "-O") }
1421 sub pp_ftrowned { ftst(@_, "-o") }
1422 sub pp_ftzero { ftst(@_, "-z") }
1423 sub pp_ftsize { ftst(@_, "-s") }
1424 sub pp_ftmtime { ftst(@_, "-M") }
1425 sub pp_ftatime { ftst(@_, "-A") }
1426 sub pp_ftctime { ftst(@_, "-C") }
1427 sub pp_ftsock { ftst(@_, "-S") }
1428 sub pp_ftchr { ftst(@_, "-c") }
1429 sub pp_ftblk { ftst(@_, "-b") }
1430 sub pp_ftfile { ftst(@_, "-f") }
1431 sub pp_ftdir { ftst(@_, "-d") }
1432 sub pp_ftpipe { ftst(@_, "-p") }
1433 sub pp_ftlink { ftst(@_, "-l") }
1434 sub pp_ftsuid { ftst(@_, "-u") }
1435 sub pp_ftsgid { ftst(@_, "-g") }
1436 sub pp_ftsvtx { ftst(@_, "-k") }
1437 sub pp_fttty { ftst(@_, "-t") }
1438 sub pp_fttext { ftst(@_, "-T") }
1439 sub pp_ftbinary { ftst(@_, "-B") }
1440  
1441 sub SWAP_CHILDREN () { 1 }
1442 sub ASSIGN () { 2 } # has OP= variant
1443 sub LIST_CONTEXT () { 4 } # Assignment is in list context
1444  
1445 my(%left, %right);
1446  
1447 sub assoc_class {
1448 my $op = shift;
1449 my $name = $op->name;
1450 if ($name eq "concat" and $op->first->name eq "concat") {
1451 # avoid spurious '=' -- see comment in pp_concat
1452 return "concat";
1453 }
1454 if ($name eq "null" and class($op) eq "UNOP"
1455 and $op->first->name =~ /^(and|x?or)$/
1456 and null $op->first->sibling)
1457 {
1458 # Like all conditional constructs, OP_ANDs and OP_ORs are topped
1459 # with a null that's used as the common end point of the two
1460 # flows of control. For precedence purposes, ignore it.
1461 # (COND_EXPRs have these too, but we don't bother with
1462 # their associativity).
1463 return assoc_class($op->first);
1464 }
1465 return $name . ($op->flags & OPf_STACKED ? "=" : "");
1466 }
1467  
1468 # Left associative operators, like '+', for which
1469 # $a + $b + $c is equivalent to ($a + $b) + $c
1470  
1471 BEGIN {
1472 %left = ('multiply' => 19, 'i_multiply' => 19,
1473 'divide' => 19, 'i_divide' => 19,
1474 'modulo' => 19, 'i_modulo' => 19,
1475 'repeat' => 19,
1476 'add' => 18, 'i_add' => 18,
1477 'subtract' => 18, 'i_subtract' => 18,
1478 'concat' => 18,
1479 'left_shift' => 17, 'right_shift' => 17,
1480 'bit_and' => 13,
1481 'bit_or' => 12, 'bit_xor' => 12,
1482 'and' => 3,
1483 'or' => 2, 'xor' => 2,
1484 );
1485 }
1486  
1487 sub deparse_binop_left {
1488 my $self = shift;
1489 my($op, $left, $prec) = @_;
1490 if ($left{assoc_class($op)} && $left{assoc_class($left)}
1491 and $left{assoc_class($op)} == $left{assoc_class($left)})
1492 {
1493 return $self->deparse($left, $prec - .00001, $op);
1494 } else {
1495 return $self->deparse($left, $prec, $op);
1496 }
1497 }
1498  
1499 # Right associative operators, like '=', for which
1500 # $a = $b = $c is equivalent to $a = ($b = $c)
1501  
1502 BEGIN {
1503 %right = ('pow' => 22,
1504 'sassign=' => 7, 'aassign=' => 7,
1505 'multiply=' => 7, 'i_multiply=' => 7,
1506 'divide=' => 7, 'i_divide=' => 7,
1507 'modulo=' => 7, 'i_modulo=' => 7,
1508 'repeat=' => 7, 'refassign' => 7, 'refassign=' => 7,
1509 'add=' => 7, 'i_add=' => 7,
1510 'subtract=' => 7, 'i_subtract=' => 7,
1511 'concat=' => 7,
1512 'left_shift=' => 7, 'right_shift=' => 7,
1513 'bit_and=' => 7, 'sbit_and=' => 7, 'nbit_and=' => 7,
1514 'nbit_or=' => 7, 'nbit_xor=' => 7,
1515 'sbit_or=' => 7, 'sbit_xor=' => 7,
1516 'andassign' => 7,
1517 'orassign' => 7,
1518 );
1519 }
1520  
1521 sub deparse_binop_right {
1522 my $self = shift;
1523 my($op, $right, $prec) = @_;
1524 if ($right{assoc_class($op)} && $right{assoc_class($right)}
1525 and $right{assoc_class($op)} == $right{assoc_class($right)})
1526 {
1527 return $self->deparse($right, $prec - .00001, $op);
1528 } else {
1529 return $self->deparse($right, $prec, $op);
1530 }
1531 }
1532  
1533 sub pp_add { maybe_targmy(@_, \&binop, "+", 18, ASSIGN) }
1534 sub pp_multiply { maybe_targmy(@_, \&binop, "*", 19, ASSIGN) }
1535 sub pp_subtract { maybe_targmy(@_, \&binop, "-",18, ASSIGN) }
1536 sub pp_divide { maybe_targmy(@_, \&binop, "/", 19, ASSIGN) }
1537 sub pp_modulo { maybe_targmy(@_, \&binop, "%", 19, ASSIGN) }
1538 sub pp_i_add { maybe_targmy(@_, \&binop, "+", 18, ASSIGN) }
1539 sub pp_i_multiply { maybe_targmy(@_, \&binop, "*", 19, ASSIGN) }
1540 sub pp_i_subtract { maybe_targmy(@_, \&binop, "-", 18, ASSIGN) }
1541 sub pp_i_divide { maybe_targmy(@_, \&binop, "/", 19, ASSIGN) }
1542 sub pp_i_modulo { maybe_targmy(@_, \&binop, "%", 19, ASSIGN) }
1543 sub pp_pow { maybe_targmy(@_, \&binop, "**", 22, ASSIGN) }
1544  
1545 sub pp_left_shift { maybe_targmy(@_, \&binop, "<<", 17, ASSIGN) }
1546 sub pp_right_shift { maybe_targmy(@_, \&binop, ">>", 17, ASSIGN) }
1547 sub pp_bit_and { maybe_targmy(@_, \&binop, "&", 13, ASSIGN) }
1548 sub pp_bit_or { maybe_targmy(@_, \&binop, "|", 12, ASSIGN) }
1549 sub pp_bit_xor { maybe_targmy(@_, \&binop, "^", 12, ASSIGN) }
1550 *pp_nbit_and = *pp_bit_and;
1551 *pp_nbit_or = *pp_bit_or;
1552 *pp_nbit_xor = *pp_bit_xor;
1553 sub pp_sbit_and { maybe_targmy(@_, \&binop, "&.", 13, ASSIGN) }
1554 sub pp_sbit_or { maybe_targmy(@_, \&binop, "|.", 12, ASSIGN) }
1555 sub pp_sbit_xor { maybe_targmy(@_, \&binop, "^.", 12, ASSIGN) }
1556  
1557 sub pp_eq { binop(@_, "==", 14) }
1558 sub pp_ne { binop(@_, "!=", 14) }
1559 sub pp_lt { binop(@_, "<", 15) }
1560 sub pp_gt { binop(@_, ">", 15) }
1561 sub pp_ge { binop(@_, ">=", 15) }
1562 sub pp_le { binop(@_, "<=", 15) }
1563 sub pp_cmp { binop(@_, "<=>", 14) }
1564 sub pp_i_eq { binop(@_, "==", 14) }
1565 sub pp_i_ne { binop(@_, "!=", 14) }
1566 sub pp_i_lt { binop(@_, "<", 15) }
1567 sub pp_i_gt { binop(@_, ">", 15) }
1568 sub pp_i_ge { binop(@_, ">=", 15) }
1569 sub pp_i_le { binop(@_, "<=", 15) }
1570 sub pp_i_cmp { maybe_targmy(@_, \&binop, "<=>", 14) }
1571  
1572 sub pp_s_eq { binop(@_, "eq", 14) }
1573 sub pp_s_ne { binop(@_, "ne", 14) }
1574 sub pp_s_lt { binop(@_, "lt", 15) }
1575 sub pp_s_gt { binop(@_, "gt", 15) }
1576 sub pp_s_ge { binop(@_, "ge", 15) }
1577 sub pp_s_le { binop(@_, "le", 15) }
1578 sub pp_s_cmp { maybe_targmy(@_, \&binop, "cmp", 14) }
1579  
1580 sub pp_sassign { binop(@_, "=", 7, SWAP_CHILDREN) }
1581 sub pp_aassign { binop(@_, "=", 7, SWAP_CHILDREN | LIST_CONTEXT) }
1582  
1583 sub pp_smartmatch {
1584 my ($self, $op, $cx) = @_;
1585 if ($op->flags & OPf_SPECIAL) {
1586 return $self->deparse($op->last, $cx, $op);
1587 }
1588 else {
1589 binop(@_, "~~", 14);
1590 }
1591 }
1592  
1593 sub bin_info_join($$$$$$$) {
1594 my ($self, $op, $lhs, $rhs, $mid, $sep, $type) = @_;
1595 my $texts = [$lhs->{text}, $mid, $rhs->{text}];
1596 return info_from_list($op, $self, $texts, ' ', $type, {})
1597 }
1598  
1599 sub bin_info_join_maybe_parens($$$$$$$$$) {
1600 my ($self, $op, $lhs, $rhs, $mid, $sep, $cx, $prec, $type) = @_;
1601 my $info = bin_info_join($self, $op, $lhs, $rhs, $mid, $sep, $type);
1602 $info->{text} = $self->maybe_parens($info->{text}, $cx, $prec);
1603 return $info;
1604 }
1605  
1606 # '.' is special because concats-of-concats are optimized to save copying
1607 # by making all but the first concat stacked. The effect is as if the
1608 # programmer had written '($a . $b) .= $c', except legal.
1609 sub pp_concat { maybe_targmy(@_, \&real_concat) }
1610 sub real_concat {
1611 my $self = shift;
1612 my($op, $cx) = @_;
1613 my $left = $op->first;
1614 my $right = $op->last;
1615 my $eq = "";
1616 my $prec = 18;
1617 if ($op->flags & OPf_STACKED and $op->first->name ne "concat") {
1618 $eq = "=";
1619 $prec = 7;
1620 }
1621 my $lhs = $self->deparse_binop_left($op, $left, $prec);
1622 my $rhs = $self->deparse_binop_right($op, $right, $prec);
1623 return $self->bin_info_join_maybe_parens($op, $lhs, $rhs, ".$eq", " ", $cx, $prec,
1624 'real_concat');
1625 }
1626  
1627 # 'x' is weird when the left arg is a list
1628 sub pp_repeat {
1629 my $self = shift;
1630 my($op, $cx) = @_;
1631 my $left = $op->first;
1632 my $right = $op->last;
1633 my $eq = "";
1634 my $prec = 19;
1635 my $other_ops = undef;
1636 if ($op->flags & OPf_STACKED) {
1637 $eq = "=";
1638 $prec = 7;
1639 }
1640 my @exprs = ();
1641 my ($left_info, @body);
1642 if (null($right)) {
1643 # list repeat; count is inside left-side ex-list
1644 $other_ops = [$left->first];
1645 my $kid = $left->first->sibling; # skip pushmark
1646 for (my $i=0; !null($kid->sibling); $kid = $kid->sibling) {
1647 my $expr = $self->deparse($kid, 6, $op);
1648 push @exprs, $expr;
1649 }
1650 $right = $kid;
1651 @body = @exprs;
1652 my $args = join(', ', map($_->{text}, @exprs));
1653 $left_info = info_from_list(["(", $args, ")"], '', 'repeat_left', {body => \@exprs});
1654 } else {
1655 $left_info = $self->deparse_binop_left($op, $left, $prec);
1656 }
1657 my $right_info = $self->deparse_binop_right($op, $right, $prec);
1658 my $texts = [$left_info->{text}, "x$eq", $right_info->{text}];
1659 my $info = info_from_list($texts, ' ', 'repeat',
1660 {body => [$left_info, $right_info],
1661 maybe_parens => [$self, $cx, $prec]});
1662 $info->{other_ops} = $other_ops if $other_ops;
1663 return $info
1664 }
1665  
1666 sub range {
1667 my $self = shift;
1668 my ($op, $cx, $type) = @_;
1669 my $left = $op->first;
1670 my $right = $left->sibling;
1671 $left = $self->deparse($left, 9, $op);
1672 $right = $self->deparse($right, 9, $op);
1673 return info_from_list([$left, $type, $right], ' ', 'range',
1674 {maybe_parens => [$self, $cx, 9]});
1675 }
1676  
1677 sub pp_flop {
1678 my $self = shift;
1679 my($op, $cx) = @_;
1680 my $flip = $op->first;
1681 my $type = ($flip->flags & OPf_SPECIAL) ? "..." : "..";
1682 return info_from_text $self->range($flip->first, $cx, $type), 'pp_flop', {};
1683 }
1684  
1685 sub logassignop {
1686 my ($self, $op, $cx, $opname) = @_;
1687 my $left = $op->first;
1688  
1689 my $right = $op->first->sibling->first; # skip sassign
1690 $left = $self->deparse($left, 7, $op);
1691 $right = $self->deparse($right, 7, $op);
1692 return info_from_list([$left->{text}, $opname, $right->{text}], ' ',
1693 'logassignop',
1694 {other_ops => [$op->first->sibling],
1695 body => [$left, $right],
1696 maybe_parens => [$self, $cx, 7]});
1697 }
1698  
1699 sub pp_andassign { logassignop(@_, "&&=") }
1700 sub pp_orassign { logassignop(@_, "||=") }
1701 sub pp_dorassign { logassignop(@_, "//=") }
1702  
1703 sub rv2gv_or_string {
1704 my($self,$op, $parent) = @_;
1705 if ($op->name eq "gv") { # could be open("open") or open("###")
1706 my($name,$quoted) =
1707 $self->stash_variable_name("", $self->gv_or_padgv($op));
1708 return info_from_text($quoted ? $name : "*$name", 'r2gv_or_string', {});
1709 }
1710 else {
1711 return $self->deparse($op, 6, $parent);
1712 }
1713 }
1714  
1715 sub pp_bless { listop(@_, "bless") }
1716 sub pp_atan2 { maybe_targmy(@_, \&listop, "atan2") }
1717 sub pp_substr {
1718 my ($self,$op,$cx) = @_;
1719 if ($op->private & OPpSUBSTR_REPL_FIRST) {
1720 my $left = listop($self, $op, 7, "substr", $op->first->sibling->sibling);
1721 my $right = $self->deparse($op->first->sibling, 7, $op);
1722 return info_from_list([$left->{text}, '=', $right->{text}], ' ',
1723 'substr_repl_first',
1724 {body => [$left, $right]});
1725 }
1726 return maybe_local(@_, listop(@_, "substr"))
1727 }
1728 sub pp_vec { maybe_local(@_, listop(@_, "vec")) }
1729 sub pp_index { maybe_targmy(@_, \&listop, "index") }
1730 sub pp_rindex { maybe_targmy(@_, \&listop, "rindex") }
1731 sub pp_sprintf { maybe_targmy(@_, \&listop, "sprintf") }
1732 sub pp_formline { listop(@_, "formline") } # see also deparse_format
1733 sub pp_crypt { maybe_targmy(@_, \&listop, "crypt") }
1734 sub pp_unpack { listop(@_, "unpack") }
1735 sub pp_pack { listop(@_, "pack") }
1736 sub pp_join { maybe_targmy(@_, \&listop, "join") }
1737 sub pp_splice { listop(@_, "splice") }
1738 sub pp_push { maybe_targmy(@_, \&listop, "push") }
1739 sub pp_unshift { maybe_targmy(@_, \&listop, "unshift") }
1740 sub pp_reverse { listop(@_, "reverse") }
1741 sub pp_warn { listop(@_, "warn") }
1742 sub pp_die { listop(@_, "die") }
1743 sub pp_return { listop(@_, "return", undef, 1) } # llafr does not apply
1744 sub pp_open { listop(@_, "open") }
1745 sub pp_pipe_op { listop(@_, "pipe") }
1746 sub pp_tie { listop(@_, "tie") }
1747 sub pp_binmode { listop(@_, "binmode") }
1748 sub pp_dbmopen { listop(@_, "dbmopen") }
1749 sub pp_sselect { listop(@_, "select") }
1750 sub pp_select { listop(@_, "select") }
1751 sub pp_read { listop(@_, "read") }
1752 sub pp_sysopen { listop(@_, "sysopen") }
1753 sub pp_sysseek { listop(@_, "sysseek") }
1754 sub pp_sysread { listop(@_, "sysread") }
1755 sub pp_syswrite { listop(@_, "syswrite") }
1756 sub pp_send { listop(@_, "send") }
1757 sub pp_recv { listop(@_, "recv") }
1758 sub pp_seek { listop(@_, "seek") }
1759 sub pp_fcntl { listop(@_, "fcntl") }
1760 sub pp_ioctl { listop(@_, "ioctl") }
1761 sub pp_flock { maybe_targmy(@_, \&listop, "flock") }
1762 sub pp_socket { listop(@_, "socket") }
1763 sub pp_sockpair { listop(@_, "socketpair") }
1764 sub pp_bind { listop(@_, "bind") }
1765 sub pp_connect { listop(@_, "connect") }
1766 sub pp_listen { listop(@_, "listen") }
1767 sub pp_accept { listop(@_, "accept") }
1768 sub pp_shutdown { listop(@_, "shutdown") }
1769 sub pp_gsockopt { listop(@_, "getsockopt") }
1770 sub pp_ssockopt { listop(@_, "setsockopt") }
1771 sub pp_chown { maybe_targmy(@_, \&listop, "chown") }
1772 sub pp_unlink { maybe_targmy(@_, \&listop, "unlink") }
1773 sub pp_chmod { maybe_targmy(@_, \&listop, "chmod") }
1774 sub pp_utime { maybe_targmy(@_, \&listop, "utime") }
1775 sub pp_rename { maybe_targmy(@_, \&listop, "rename") }
1776 sub pp_link { maybe_targmy(@_, \&listop, "link") }
1777 sub pp_symlink { maybe_targmy(@_, \&listop, "symlink") }
1778 sub pp_mkdir { maybe_targmy(@_, \&listop, "mkdir") }
1779 sub pp_open_dir { listop(@_, "opendir") }
1780 sub pp_seekdir { listop(@_, "seekdir") }
1781 sub pp_waitpid { maybe_targmy(@_, \&listop, "waitpid") }
1782 sub pp_system { maybe_targmy(@_, \&listop, "system") }
1783 sub pp_exec { maybe_targmy(@_, \&listop, "exec") }
1784 sub pp_kill { maybe_targmy(@_, \&listop, "kill") }
1785 sub pp_setpgrp { maybe_targmy(@_, \&listop, "setpgrp") }
1786 sub pp_getpriority { maybe_targmy(@_, \&listop, "getpriority") }
1787 sub pp_setpriority { maybe_targmy(@_, \&listop, "setpriority") }
1788 sub pp_shmget { listop(@_, "shmget") }
1789 sub pp_shmctl { listop(@_, "shmctl") }
1790 sub pp_shmread { listop(@_, "shmread") }
1791 sub pp_shmwrite { listop(@_, "shmwrite") }
1792 sub pp_msgget { listop(@_, "msgget") }
1793 sub pp_msgctl { listop(@_, "msgctl") }
1794 sub pp_msgsnd { listop(@_, "msgsnd") }
1795 sub pp_msgrcv { listop(@_, "msgrcv") }
1796 sub pp_semget { listop(@_, "semget") }
1797 sub pp_semctl { listop(@_, "semctl") }
1798 sub pp_semop { listop(@_, "semop") }
1799 sub pp_ghbyaddr { listop(@_, "gethostbyaddr") }
1800 sub pp_gnbyaddr { listop(@_, "getnetbyaddr") }
1801 sub pp_gpbynumber { listop(@_, "getprotobynumber") }
1802 sub pp_gsbyname { listop(@_, "getservbyname") }
1803 sub pp_gsbyport { listop(@_, "getservbyport") }
1804 sub pp_syscall { listop(@_, "syscall") }
1805  
1806 sub pp_glob
1807 {
1808 my($self, $op, $cx) = @_;
1809  
1810 my $opts = {other_ops => [$op->first]};
1811 my $kid = $op->first->sibling; # skip pushmark
1812 my $keyword =
1813 $op->flags & OPf_SPECIAL ? 'glob' : $self->keyword('glob');
1814  
1815 if ($keyword =~ /^CORE::/ or $kid->name ne 'const') {
1816 my $kid_info = $self->dq($kid, $op);
1817 my $body = [$kid_info];
1818 my $text = $kid_info->{text};
1819 if ($text =~ /^\$?(\w|::|\`)+$/ # could look like a readline
1820 or $text =~ /[<>]/) {
1821 $kid_info = $self->deparse($kid, 0, $op);
1822 $body = [$kid_info];
1823 $text = $kid_info->{text};
1824 $opts->{body} = $body;
1825 if ($cx >= 5 || $self->{'parens'}) {
1826 return info_from_list([$keyword, '(', $text, ')'], '',
1827 'glob_paren', $opts);
1828 } else {
1829 return info_from_list([$keyword, $text], ' ',
1830 'glob_space', $opts);
1831 }
1832 } else {
1833 return info_from_list(['<', $text, '>'], '', 'glob_angle', $opts);
1834 }
1835 }
1836 return info_from_list(['<', '>'], '', 'glob_angle', $opts);
1837 }
1838  
1839 # Truncate is special because OPf_SPECIAL makes a bareword first arg
1840 # be a filehandle. This could probably be better fixed in the core
1841 # by moving the GV lookup into ck_truc.
1842  
1843 sub pp_truncate
1844 {
1845 my($self, $op, $cx) = @_;
1846 my(@exprs);
1847 my $parens = ($cx >= 5) || $self->{'parens'};
1848 my $kid = $op->first->sibling;
1849 my $fh;
1850 if ($op->flags & OPf_SPECIAL) {
1851 # $kid is an OP_CONST
1852 $fh = $self->const_sv($kid)->PV;
1853 } else {
1854 $fh = $self->deparse($kid, 6, $op);
1855 $fh = "+$fh" if not $parens and substr($fh, 0, 1) eq "(";
1856 }
1857 my $len = $self->deparse($kid->sibling, 6, $op);
1858 my $name = $self->keyword('truncate');
1859 my $opts = {body => [$fh, $len]};
1860 my $args = "$fh->{text}, $len->{text}";
1861 if ($parens) {
1862 return info_from_list([$name, '(', $args, ')'], '',
1863 'truncate_parens', $opts);
1864 return "$name($fh, $len)";
1865 } else {
1866 return info_from_list([$name, $args], '', 'truncate', $opts);
1867 }
1868 }
1869  
1870 sub pp_list
1871 {
1872 my($self, $op, $cx) = @_;
1873 my($expr, @exprs);
1874  
1875 my $other_op = $op->first;
1876 my $kid = $op->first->sibling; # skip a pushmark
1877  
1878 if (class($kid) eq 'NULL') {
1879 return info_from_text('', 'list_null',
1880 {other_ops => [$other_op]});
1881 }
1882 my $lop;
1883 my $local = "either"; # could be local(...), my(...), state(...) or our(...)
1884 for ($lop = $kid; !null($lop); $lop = $lop->sibling) {
1885 # This assumes that no other private flags equal 128, and that
1886 # OPs that store things other than flags in their op_private,
1887 # like OP_AELEMFAST, won't be immediate children of a list.
1888 #
1889 # OP_ENTERSUB and OP_SPLIT can break this logic, so check for them.
1890 # I suspect that open and exit can too.
1891 # XXX This really needs to be rewritten to accept only those ops
1892 # known to take the OPpLVAL_INTRO flag.
1893  
1894 if (!($lop->private & (OPpLVAL_INTRO|OPpOUR_INTRO)
1895 or $lop->name eq "undef")
1896 or $lop->name =~ /^(?:entersub|exit|open|split)\z/)
1897 {
1898 $local = ""; # or not
1899 last;
1900 }
1901 if ($lop->name =~ /^pad[ash]v$/) {
1902 if ($lop->private & OPpPAD_STATE) { # state()
1903 ($local = "", last) if $local =~ /^(?:local|our|my)$/;
1904 $local = "state";
1905 } else { # my()
1906 ($local = "", last) if $local =~ /^(?:local|our|state)$/;
1907 $local = "my";
1908 }
1909 } elsif ($lop->name =~ /^(gv|rv2)[ash]v$/
1910 && $lop->private & OPpOUR_INTRO
1911 or $lop->name eq "null" && $lop->first->name eq "gvsv"
1912 && $lop->first->private & OPpOUR_INTRO) { # our()
1913 ($local = "", last) if $local =~ /^(?:my|local|state)$/;
1914 $local = "our";
1915 } elsif ($lop->name ne "undef"
1916 # specifically avoid the "reverse sort" optimisation,
1917 # where "reverse" is nullified
1918 && !($lop->name eq 'sort' && ($lop->flags & OPpSORT_REVERSE)))
1919 {
1920 # local()
1921 ($local = "", last) if $local =~ /^(?:my|our|state)$/;
1922 $local = "local";
1923 }
1924 }
1925 $local = "" if $local eq "either"; # no point if it's all undefs
1926 if (null $kid->sibling and not $local) {
1927 my $info = $self->deparse($kid, $cx, $op);
1928 my @other_ops = $info->{other_ops} ? @{$info->{other_ops}} : ();
1929 push @other_ops, $other_op;
1930 $info->{other_ops} = \@other_ops;
1931 return $info;
1932 }
1933  
1934 for (; !null($kid); $kid = $kid->sibling) {
1935 if ($local) {
1936 if (class($kid) eq "UNOP" and $kid->first->name eq "gvsv") {
1937 $lop = $kid->first;
1938 } else {
1939 $lop = $kid;
1940 }
1941 $self->{'avoid_local'}{$$lop}++;
1942 $expr = $self->deparse($kid, 6, $op);
1943 delete $self->{'avoid_local'}{$$lop};
1944 } else {
1945 $expr = $self->deparse($kid, 6, $op);
1946 }
1947 push @exprs, $expr;
1948 }
1949  
1950 my $type;
1951 my @texts;
1952 my $opts = {};
1953 if ($local) {
1954 $type = 'list_local';
1955 @texts = ($local, '(', $self->combine(", ", \@exprs), ')');
1956 } else {
1957 $type = 'list';
1958 @texts = ($self->combine(", ", \@exprs));
1959 $opts->{maybe_parens} = [$self, $cx, 6];
1960  
1961 }
1962 return info_from_list(\@texts, '', $type, $opts);
1963 }
1964  
1965 sub is_ifelse_cont
1966 {
1967 my $op = shift;
1968 return ($op->name eq "null" and class($op) eq "UNOP"
1969 and $op->first->name =~ /^(and|cond_expr)$/
1970 and is_scope($op->first->first->sibling));
1971 }
1972  
1973 sub loop_common
1974 {
1975 my $self = shift;
1976 my($op, $cx, $init) = @_;
1977 my $enter = $op->first;
1978 my $kid = $enter->sibling;
1979 local(@$self{qw'curstash warnings hints hinthash'})
1980 = @$self{qw'curstash warnings hints hinthash'};
1981  
1982 my ($body, @body, $type);
1983 my (@head, @head_text) = ((), ());
1984 my ($bare, $cond_info) = (0, undef);
1985  
1986 if ($kid->name eq "lineseq") {
1987 # bare or infinite loop
1988 $type = 'loop_bare_or_infinite';
1989  
1990 if ($kid->last->name eq "unstack") { # infinite
1991 @head_text = ("while", "(1)"); # Can't use for(;;) if there's a continue
1992 } else {
1993 $bare = 1;
1994 }
1995 $body = $kid;
1996 } elsif ($enter->name eq "enteriter") {
1997 # foreach
1998 $type = 'loop_foreach';
1999  
2000 my $ary = $enter->first->sibling; # first was pushmark
2001 my @ary_info;
2002 my @ary_text;
2003 my $var = $ary->sibling;
2004 if ($ary->name eq 'null' and $enter->private & OPpITER_REVERSED) {
2005 # "reverse" was optimised away
2006 @ary_info = (listop($self, $ary->first->sibling, 1, 'reverse'));
2007 @ary_text = ($ary_info[0]->{text});
2008 } elsif ($enter->flags & OPf_STACKED
2009 and not null $ary->first->sibling->sibling) {
2010 @ary_info = ($self->deparse($ary->first->sibling, 9, $op),
2011 $self->deparse($ary->first->sibling->sibling, 9, $op));
2012 @ary_text = ($ary_info[0]->{text}, "..", $ary_info[1]->{text});
2013  
2014 } else {
2015 @ary_info = ($self->deparse($ary, 1, $op));
2016 @ary_text = $ary_info[0]->{text};
2017 }
2018  
2019 my @var_info = ();
2020 my @var_text;
2021 if (null $var) {
2022 if (($enter->flags & OPf_SPECIAL) && ($] < 5.009)) {
2023 # thread special var, under 5005threads
2024 @var_text = ($self->pp_threadsv($enter, 1));
2025 } else { # regular my() variable
2026 my $var_info = $self->pp_padsv($enter, 1, 1);
2027 @var_info =($var_info);
2028 @var_text = ($var_info->{text});
2029 }
2030 } elsif ($var->name eq "rv2gv") {
2031 @var_info = ($self->pp_rv2sv($var, 1));
2032 @var_text = ($var_info[0]->{text});
2033 if ($enter->private & OPpOUR_INTRO) {
2034 # our declarations don't have package names
2035 $var_text[0] =~ s/^(.).*::/$1/;
2036 unshift @var_text, 'our';
2037 }
2038 } elsif ($var->name eq "gv") {
2039 @var_info = ($self->deparse($var, 1, $op));
2040 @var_text = ("\$" . $var_info[0]->{text});
2041 }
2042  
2043 # skip OP_AND and OP_ITER
2044 my $other_ops = [$kid->first, $kid->first->first];
2045 $body = $kid->first->first->sibling;
2046  
2047 @head = (@ary_info, @var_info);
2048 if (!is_state $body->first and $body->first->name !~ /^(?:stub|leave|scope)$/) {
2049 Carp::confess("var ne \$_") unless join('', @var_text) eq '$_';
2050 $body = $body->first;
2051 my $body_info = $self->deparse($body, 2, $op);
2052 push @head, $body_info;
2053 my @texts = ($body_info->{text}, "foreach", '(', @ary_text, ')');
2054 return info_from_list(\@texts, ' ', 'loop_foreach_ary',
2055 {body => \@head, other_ops => $other_ops});
2056 }
2057 @head_text = ("foreach", @var_text, '(', @ary_text, ')');
2058 } elsif ($kid->name eq "null") {
2059 # while/until
2060 $type = 'loop_while_until';
2061  
2062 $kid = $kid->first;
2063 my $name = {"and" => "while", "or" => "until"}->{$kid->name};
2064 $cond_info = $self->deparse($kid->first, 1, $op);
2065 @head_text = ($name, "($cond_info->{text})");
2066 $body = $kid->first->sibling;
2067 } elsif ($kid->name eq "stub") {
2068 # bare and empty
2069 return info_from_list(['{', ';', '}'], '', 'loop_stub', {});
2070 }
2071  
2072 # If there isn't a continue block, then the next pointer for the loop
2073 # will point to the unstack, which is kid's last child, except
2074 # in a bare loop, when it will point to the leaveloop. When neither of
2075 # these conditions hold, then the second-to-last child is the continue
2076 # block (or the last in a bare loop).
2077 my $cont_start = $enter->nextop;
2078 my ($cont, @cont_text, $body_info);
2079 my @cont = ();
2080 if ($$cont_start != $$op && ${$cont_start} != ${$body->last}) {
2081 $type .= '_continue';
2082  
2083 if ($bare) {
2084 $cont = $body->last;
2085 } else {
2086 $cont = $body->first;
2087 while (!null($cont->sibling->sibling)) {
2088 $cont = $cont->sibling;
2089 }
2090 }
2091 my $state = $body->first;
2092 my $cuddle = " ";
2093 my @states;
2094 for (; $$state != $$cont; $state = $state->sibling) {
2095 push @states, $state;
2096 }
2097 $body_info = $self->lineseq(undef, 0, @states);
2098 if (defined $cond_info and not is_scope $cont and $self->{'expand'} < 3) {
2099 my $cont_info = $self->deparse($cont, 1, $op);
2100 push @head, $cont_info;
2101 my $init_text = defined($init) ? $init->{text} : ' ';
2102 @head_text = ('for', '(', "$init_text;", $cont_info->{text}, ')');
2103 @cont_text = ("\cK");
2104 } else {
2105 my $cont_info = $self->deparse($cont, 0, $op);
2106 push @head, $cont_info;
2107 @cont_text = ($cuddle, 'continue', "{\n\t",
2108 $cont_info->{text} , "\n\b}\cK");
2109 }
2110 } else {
2111 return info_from_text([''], 'loop_no_body', {}) if !defined $body;
2112 if (defined $init) {
2113 @head_text = ('for', '(', "$init->{text};", "$cond_info->{text};", ")");
2114 }
2115 @cont_text = ("\cK");
2116 $body_info = $self->deparse($body, 0, $op);
2117 }
2118 (my $body_text = $body_info->{text}) =~ s/;?$/;\n/;
2119  
2120 my @texts = (@head_text, "{\n\t", $body_text, "\b}", @cont_text);
2121 return info_from_list(\@texts, ' ', $type,
2122 {body => [$body_info]});
2123 }
2124  
2125 sub pp_leaveloop {
2126 shift->loop_common(@_, undef)
2127 }
2128  
2129 sub for_loop {
2130 my $self = shift;
2131 my($op, $cx, $parent) = @_;
2132 my $init = $self->deparse($op, 1, $parent);
2133 my $s = $op->sibling;
2134 my $ll = $s->name eq "unstack" ? $s->sibling : $s->first->sibling;
2135 return $self->loop_common($ll, $cx, $init);
2136 }
2137  
2138 sub pp_leavetry {
2139 my $self = shift;
2140 my $leave_info = $self->pp_leave(@_);
2141 return info_from_list(['eval', '{\n\t"', $leave_info->{text}, "\n\b}"],
2142 ' ', 'leavetry', {body=>[$leave_info]});
2143 }
2144  
2145 sub _op_is_or_was {
2146 my ($op, $expect_type) = @_;
2147 my $type = $op->type;
2148 return($type == $expect_type
2149 || ($type == OP_NULL && $op->targ == $expect_type));
2150 }
2151  
2152 sub padname {
2153 my $self = shift;
2154 my $targ = shift;
2155 return $self->padname_sv($targ)->PVX;
2156 }
2157  
2158 sub padany {
2159 my $self = shift;
2160 my $op = shift;
2161 return substr($self->padname($op->targ), 1); # skip $/@/%
2162 }
2163  
2164 sub pp_padsv {
2165 my $self = shift;
2166 my($op, $cx, $forbid_parens) = @_;
2167 return $self->maybe_my($op, $cx, $self->padname($op->targ),
2168 $forbid_parens);
2169 }
2170  
2171 sub pp_padav { pp_padsv(@_) }
2172 sub pp_padhv { pp_padsv(@_) }
2173  
2174 my @threadsv_names = B::threadsv_names;
2175 sub pp_threadsv {
2176 my $self = shift;
2177 my($op, $cx) = @_;
2178 return $self->maybe_local_str($op, $cx, "\$" . $threadsv_names[$op->targ]);
2179 }
2180  
2181 sub gv_or_padgv {
2182 my $self = shift;
2183 my $op = shift;
2184 if (class($op) eq "PADOP") {
2185 return $self->padval($op->padix);
2186 } else { # class($op) eq "SVOP"
2187 return $op->gv;
2188 }
2189 }
2190  
2191 sub pp_gvsv
2192 {
2193 my($self, $op, $cx) = @_;
2194 my $gv = $self->gv_or_padgv($op);
2195 return $self->maybe_local_str($op, $cx,
2196 $self->stash_variable("\$",
2197 $self->gv_name($gv), $cx));
2198 }
2199  
2200 sub pp_gv
2201 {
2202 my($self, $op, $cx) = @_;
2203 my $gv = $self->gv_or_padgv($op);
2204 return info_from_text($self->gv_name($gv), 'pp_gv', {});
2205 }
2206  
2207 sub pp_aelemfast_lex
2208 {
2209 my($self, $op, $cx) = @_;
2210 my $name = $self->padname($op->targ);
2211 $name =~ s/^@/\$/;
2212 return info_from_list([$name, "[", ($op->private + $self->{'arybase'}), "]"],
2213 '', 'pp_aelemfast_lex', {});
2214 }
2215  
2216 sub pp_aelemfast
2217 {
2218 my($self, $op, $cx) = @_;
2219 # optimised PADAV, pre 5.15
2220 return $self->pp_aelemfast_lex(@_) if ($op->flags & OPf_SPECIAL);
2221  
2222 my $gv = $self->gv_or_padgv($op);
2223 my($name,$quoted) = $self->stash_variable_name('@',$gv);
2224 $name = $quoted ? "$name->" : '$' . $name;
2225 my $i = $op->private;
2226 $i -= 256 if $i > 127;
2227 return info_from_list([$name, "[", ($op->private + $self->{'arybase'}), "]"],
2228 '', 'pp_aelemfast', {});
2229 }
2230  
2231 sub rv2x
2232 {
2233 my($self, $op, $cx, $type) = @_;
2234  
2235 if (class($op) eq 'NULL' || !$op->can("first")) {
2236 carp("Unexpected op in pp_rv2x");
2237 return info_from_text('XXX', 'bad_rv2x', {});
2238 }
2239 my ($info, $kid_info);
2240 my $kid = $op->first;
2241 if ($kid->name eq "gv") {
2242 $kid_info = $self->deparse($kid, 0, $op);
2243 my $str = $self->stash_variable($type, $kid_info->{text}, $cx);
2244 return info_from_text($str, 'rv2x_gv', {body => [$kid_info]});
2245 } elsif (is_scalar $kid) {
2246 $kid_info = $self->deparse($kid, 0, $op);
2247 my $str = $kid_info->{text};
2248 if ($str =~ /^\$([^\w\d])\z/) {
2249 # "$$+" isn't a legal way to write the scalar dereference
2250 # of $+, since the lexer can't tell you aren't trying to
2251 # do something like "$$ + 1" to get one more than your
2252 # PID. Either "${$+}" or "$${+}" are workable
2253 # disambiguations, but if the programmer did the former,
2254 # they'd be in the "else" clause below rather than here.
2255 # It's not clear if this should somehow be unified with
2256 # the code in dq and re_dq that also adds lexer
2257 # disambiguation braces.
2258 $str = '$' . "{$1}"; #'
2259 }
2260 return info_from_list([$type, $str], '', 'rv2x_scalar',
2261 {body => [$kid_info]});
2262 } else {
2263 my $kid_info = $self->deparse($kid, 0, $op);
2264 return info_from_list([$type, "{", "}"], '', 'rv2x',
2265 {body => [$kid_info]});
2266 }
2267 Carp::confess("unhandled condition in rv2x");
2268 }
2269  
2270 sub pp_rv2sv { maybe_local(@_, rv2x(@_, "\$")) }
2271 sub pp_rv2hv { maybe_local(@_, rv2x(@_, "%")) }
2272 sub pp_rv2gv { maybe_local(@_, rv2x(@_, "*")) }
2273  
2274 # skip rv2av
2275 sub pp_av2arylen {
2276 my $self = shift;
2277 my($op, $cx) = @_;
2278 if ($op->first->name eq "padav") {
2279 return $self->maybe_local_str($op, $cx, '$#' . $self->padany($op->first));
2280 } else {
2281 return $self->maybe_local($op, $cx,
2282 $self->rv2x($op->first, $cx, '$#'));
2283 }
2284 }
2285  
2286 # skip down to the old, ex-rv2cv
2287 sub pp_rv2cv {
2288 my ($self, $op, $cx) = @_;
2289 if (!null($op->first) && $op->first->name eq 'null' &&
2290 $op->first->targ == OP_LIST)
2291 {
2292 return $self->rv2x($op->first->first->sibling, $cx, "&")
2293 }
2294 else {
2295 return $self->rv2x($op, $cx, "")
2296 }
2297 }
2298  
2299 sub list_const {
2300 my $self = shift;
2301 my($cx, @list) = @_;
2302 my @a = map $self->const($_, 6), @list;
2303 my @texts = map $_->{text}, @a;
2304 my $type = 'list_const';
2305 my $prec = 6;
2306 if (@texts == 0) {
2307 return info_from_list(['(', ')'], '', 'list_const_null', {});
2308 } elsif (@texts == 1) {
2309 return info_from_text($texts[0], 'list_const_one',
2310 {body => \@a});
2311 } elsif ( @texts > 2 and !grep(!/^-?\d+$/, @texts)) {
2312 # collapse (-1,0,1,2) into (-1..2)
2313 my ($s, $e) = @texts[0,-1];
2314 my $i = $s;
2315 unless (grep $i++ != $_, @texts) {
2316 @texts = ($s, '..', $e);
2317 $type = 'list_const_range';
2318 $prec = 9;
2319 }
2320 }
2321 return info_from_list(\@texts, '', $type,
2322 {maybe_parens => [$self, $cx, $prec]});
2323 }
2324  
2325 sub pp_rv2av {
2326 my $self = shift;
2327 my($op, $cx) = @_;
2328 my $kid = $op->first;
2329 if ($kid->name eq "const") { # constant list
2330 my $av = $self->const_sv($kid);
2331 return $self->list_const($cx, $av->ARRAY);
2332 } else {
2333 return $self->maybe_local($op, $cx, $self->rv2x($op, $cx, "\@"));
2334 }
2335 }
2336  
2337 sub elem_or_slice_array_name
2338 {
2339 my $self = shift;
2340 my ($array, $left, $padname, $allow_arrow) = @_;
2341  
2342 if ($array->name eq $padname) {
2343 return $self->padany($array);
2344 } elsif (is_scope($array)) { # ${expr}[0]
2345 return "{" . $self->deparse($array, 0) . "}";
2346 } elsif ($array->name eq "gv") {
2347 ($array, my $quoted) =
2348 $self->stash_variable_name(
2349 $left eq '[' ? '@' : '%', $self->gv_or_padgv($array)
2350 );
2351 if (!$allow_arrow && $quoted) {
2352 # This cannot happen.
2353 die "Invalid variable name $array for slice";
2354 }
2355 return $quoted ? "$array->" : $array;
2356 } elsif (!$allow_arrow || is_scalar $array) { # $x[0], $$x[0], ...
2357 return $self->deparse($array, 24)->{text};
2358 } else {
2359 return undef;
2360 }
2361 }
2362  
2363 sub elem_or_slice_single_index($$)
2364 {
2365 my ($self, $idx, $parent) = @_;
2366  
2367 my $idx_info = $self->deparse($idx, 1, $parent);
2368 my $idx_str = $idx_info->{text};
2369  
2370 # Outer parens in an array index will confuse perl
2371 # if we're interpolating in a regular expression, i.e.
2372 # /$x$foo[(-1)]/ is *not* the same as /$x$foo[-1]/
2373 #
2374 # If $self->{parens}, then an initial '(' will
2375 # definitely be paired with a final ')'. If
2376 # !$self->{parens}, the misleading parens won't
2377 # have been added in the first place.
2378 #
2379 # [You might think that we could get "(...)...(...)"
2380 # where the initial and final parens do not match
2381 # each other. But we can't, because the above would
2382 # only happen if there's an infix binop between the
2383 # two pairs of parens, and *that* means that the whole
2384 # expression would be parenthesized as well.]
2385 #
2386 $idx_str =~ s/^\((.*)\)$/$1/ if $self->{'parens'};
2387  
2388 # Hash-element braces will autoquote a bareword inside themselves.
2389 # We need to make sure that C<$hash{warn()}> doesn't come out as
2390 # C<$hash{warn}>, which has a quite different meaning. Currently
2391 # B::Deparse will always quote strings, even if the string was a
2392 # bareword in the original (i.e. the OPpCONST_BARE flag is ignored
2393 # for constant strings.) So we can cheat slightly here - if we see
2394 # a bareword, we know that it is supposed to be a function call.
2395 #
2396 $idx_str =~ s/^([A-Za-z_]\w*)$/$1()/;
2397  
2398 return info_from_text($idx_str, 'elem_or_slice_single_index',
2399 {body => [$idx_info]});
2400 }
2401  
2402 sub elem
2403 {
2404 my ($self, $op, $cx, $left, $right, $padname) = @_;
2405 my($array, $idx) = ($op->first, $op->first->sibling);
2406  
2407 my $idx_info = $self->elem_or_slice_single_index($idx, $op);
2408 my $opts = {body => [$idx_info]};
2409  
2410 unless ($array->name eq $padname) { # Maybe this has been fixed
2411 $opts->{other_ops} = [$array];
2412 $array = $array->first; # skip rv2av (or ex-rv2av in _53+)
2413 }
2414 my @texts = ();
2415 my $info;
2416 my $array_name=$self->elem_or_slice_array_name($array, $left, $padname, 1);
2417 if ($array_name) {
2418 if ($array_name !~ /->\z/) {
2419 if ($array_name eq '#') {
2420 $array_name = '${#}';
2421 } else {
2422 $array_name = '$' . $array_name ;
2423 }
2424 }
2425 push @texts, $array_name;
2426 push @texts, $left if $left;
2427 push @texts, $idx_info->{text}, $right;
2428 return info_from_list(\@texts, '', 'elem', $opts)
2429 } else {
2430 # $x[20][3]{hi} or expr->[20]
2431 my $type;
2432 my $array_info = $self->deparse($array, 24, $op);
2433 push @{$info->{body}}, $array_info;
2434 @texts = ($array_info->{text});
2435 if (is_subscriptable($array)) {
2436 push @texts, $left, $idx_info->{text}, $right;
2437 $type = 'elem_no_arrow';
2438 } else {
2439 push @texts, '->', $left, $idx_info->{text}, $right;
2440 $type = 'elem_arrow';
2441 }
2442 return info_from_list(\@texts, '', $type, $opts);
2443 }
2444 Carp::confess("unhandled condition in elem");
2445 }
2446  
2447 # a simplified version of elem_or_slice_array_name()
2448 # for the use of pp_multideref
2449  
2450 sub multideref_var_name($$$)
2451 {
2452 my ($self, $gv, $is_hash) = @_;
2453  
2454 my ($name, $quoted) =
2455 $self->stash_variable_name( $is_hash ? '%' : '@', $gv);
2456 return $quoted ? "$name->"
2457 : $name eq '#'
2458 ? '${#}' # avoid ${#}[1] => $#[1]
2459 : '$' . $name;
2460 }
2461  
2462 sub pp_multideref
2463 {
2464 my($self, $op, $cx) = @_;
2465 my @texts = ();
2466  
2467 if ($op->private & OPpMULTIDEREF_EXISTS) {
2468 @texts = ($self->keyword("exists"), ' ');
2469 }
2470 elsif ($op->private & OPpMULTIDEREF_DELETE) {
2471 @texts = ($self->keyword("delete"), ' ')
2472 }
2473 elsif ($op->private & OPpLVAL_INTRO) {
2474 @texts = ($self->keyword("local"), ' ')
2475 }
2476  
2477 if ($op->first && ($op->first->flags & OPf_KIDS)) {
2478 # arbitrary initial expression, e.g. f(1,2,3)->[...]
2479 my $first = $self->deparse($op->first, 24, $op);
2480 push @texts, $first->{text};
2481 }
2482  
2483 my @items = $op->aux_list($self->{curcv});
2484 my $actions = shift @items;
2485  
2486 my $is_hash;
2487 my $derefs = 0;
2488  
2489 while (1) {
2490 if (($actions & MDEREF_ACTION_MASK) == MDEREF_reload) {
2491 $actions = shift @items;
2492 next;
2493 }
2494  
2495 $is_hash = (
2496 ($actions & MDEREF_ACTION_MASK) == MDEREF_HV_pop_rv2hv_helem
2497 || ($actions & MDEREF_ACTION_MASK) == MDEREF_HV_gvsv_vivify_rv2hv_helem
2498 || ($actions & MDEREF_ACTION_MASK) == MDEREF_HV_padsv_vivify_rv2hv_helem
2499 || ($actions & MDEREF_ACTION_MASK) == MDEREF_HV_vivify_rv2hv_helem
2500 || ($actions & MDEREF_ACTION_MASK) == MDEREF_HV_padhv_helem
2501 || ($actions & MDEREF_ACTION_MASK) == MDEREF_HV_gvhv_helem
2502 );
2503  
2504 if ( ($actions & MDEREF_ACTION_MASK) == MDEREF_AV_padav_aelem
2505 || ($actions & MDEREF_ACTION_MASK) == MDEREF_HV_padhv_helem)
2506 {
2507 $derefs = 1;
2508 push @texts, '$' . substr($self->padname(shift @items), 1);
2509 }
2510 elsif ( ($actions & MDEREF_ACTION_MASK) == MDEREF_AV_gvav_aelem
2511 || ($actions & MDEREF_ACTION_MASK) == MDEREF_HV_gvhv_helem)
2512 {
2513 $derefs = 1;
2514 push @texts, $self->multideref_var_name(shift @items, $is_hash);
2515 }
2516 else {
2517 if ( ($actions & MDEREF_ACTION_MASK) ==
2518 MDEREF_AV_padsv_vivify_rv2av_aelem
2519 || ($actions & MDEREF_ACTION_MASK) ==
2520 MDEREF_HV_padsv_vivify_rv2hv_helem)
2521 {
2522 push @texts, $self->padname(shift @items);
2523 }
2524 elsif ( ($actions & MDEREF_ACTION_MASK) ==
2525 MDEREF_AV_gvsv_vivify_rv2av_aelem
2526 || ($actions & MDEREF_ACTION_MASK) ==
2527 MDEREF_HV_gvsv_vivify_rv2hv_helem)
2528 {
2529 push @texts, $self->multideref_var_name(shift @items, $is_hash);
2530 }
2531 elsif ( ($actions & MDEREF_ACTION_MASK) ==
2532 MDEREF_AV_pop_rv2av_aelem
2533 || ($actions & MDEREF_ACTION_MASK) ==
2534 MDEREF_HV_pop_rv2hv_helem)
2535 {
2536 if ( ($op->flags & OPf_KIDS)
2537 && ( _op_is_or_was($op->first, OP_RV2AV)
2538 || _op_is_or_was($op->first, OP_RV2HV))
2539 && ($op->first->flags & OPf_KIDS)
2540 && ( _op_is_or_was($op->first->first, OP_AELEM)
2541 || _op_is_or_was($op->first->first, OP_HELEM))
2542 )
2543 {
2544 $derefs++;
2545 }
2546 }
2547  
2548 push(@texts, '->') if !$derefs++;
2549 }
2550  
2551  
2552 if (($actions & MDEREF_INDEX_MASK) == MDEREF_INDEX_none) {
2553 last;
2554 }
2555  
2556 push(@texts, $is_hash ? '{' : '[');
2557  
2558 if (($actions & MDEREF_INDEX_MASK) == MDEREF_INDEX_const) {
2559 my $key = shift @items;
2560 if ($is_hash) {
2561 push @texts, $self->const($key, $cx)->{text};
2562 }
2563 else {
2564 push @texts, $key;
2565 }
2566 }
2567 elsif (($actions & MDEREF_INDEX_MASK) == MDEREF_INDEX_padsv) {
2568 push @texts, $self->padname(shift @items);
2569 }
2570 elsif (($actions & MDEREF_INDEX_MASK) == MDEREF_INDEX_gvsv) {
2571 push @texts,('$' . ($self->stash_variable_name('$', shift @items))[0]);
2572 }
2573  
2574 push(@texts, $is_hash ? '}' : ']');
2575  
2576 if ($actions & MDEREF_FLAG_last) {
2577 last;
2578 }
2579 $actions >>= MDEREF_SHIFT;
2580 }
2581  
2582 return info_from_list(\@texts, '', 'multideref', {});
2583 }
2584  
2585 sub pp_aelem { maybe_local(@_, elem(@_, "[", "]", "padav")) }
2586 sub pp_helem { maybe_local(@_, elem(@_, "{", "}", "padhv")) }
2587  
2588 sub pp_gelem
2589 {
2590 my($self, $op, $cx) = @_;
2591 my($glob, $part) = ($op->first, $op->last);
2592 $glob = $glob->first; # skip rv2gv
2593 $glob = $glob->first if $glob->name eq "rv2gv"; # this one's a bug
2594 my $scope = is_scope($glob);
2595 $glob = $self->deparse($glob, 0);
2596 $part = $self->deparse($part, 1);
2597 return "*" . ($scope ? "{$glob}" : $glob) . "{$part}";
2598 }
2599  
2600 sub slice
2601 {
2602 my ($self, $op, $cx, $left, $right, $regname, $padname) = @_;
2603 my $last;
2604 my(@elems, $kid, $array);
2605 if (class($op) eq "LISTOP") {
2606 $last = $op->last;
2607 } else { # ex-hslice inside delete()
2608 for ($kid = $op->first; !null $kid->sibling; $kid = $kid->sibling) {}
2609 $last = $kid;
2610 }
2611 $array = $last;
2612 $array = $array->first
2613 if $array->name eq $regname or $array->name eq "null";
2614 my $array_info = $self->elem_or_slice_array_name($array, $left, $padname, 0);
2615 $kid = $op->first->sibling; # skip pushmark
2616  
2617 if ($kid->name eq "list") {
2618 # skip list, pushmark
2619 $kid = $kid->first->sibling;
2620 for (; !null $kid; $kid = $kid->sibling) {
2621 push @elems, $self->deparse($kid, 6, $op);
2622 }
2623 } else {
2624 @elems = ($self->elem_or_slice_single_index($kid, $op));
2625 }
2626 my $list = join(', ', map($_->{text}, @elems));
2627 my $lead = '@';
2628 $lead = '%' if $op->name =~ /^kv/i;
2629 my (@texts, $type);
2630 if ($array_info) {
2631 @texts = ($lead, $array_info, $left, $list, $right);
2632 $type='slice1';
2633 } else {
2634 @texts = ($lead, $left, $list, $right);
2635 $type='slice';
2636 }
2637 return info_from_list(\@texts, '', $type, {body => \@elems});
2638 }
2639  
2640 sub pp_aslice { maybe_local(@_, slice(@_, "[", "]", "rv2av", "padav")) }
2641 sub pp_kvaslice { slice(@_, "[", "]", "rv2av", "padav") }
2642 sub pp_hslice { maybe_local(@_, slice(@_, "{", "}", "rv2hv", "padhv")) }
2643 sub pp_kvhslice { slice(@_, "{", "}", "rv2hv", "padhv") }
2644  
2645 sub pp_lslice
2646 {
2647 my ($self, $op, $cs) = @_;
2648 my $idx = $op->first;
2649 my $list = $op->last;
2650 my(@elems, $kid);
2651 my $list_info = $self->deparse($list, 1, $op);
2652 my $idx_info = $self->deparse($idx, 1, $op);
2653 return info_from_list(['(', $list_info->{text}, ')', '[', $idx_info->{text}, ']'],
2654 '', 'lslice', {body=>[$list_info, $idx_info]});
2655 }
2656  
2657 sub want_scalar
2658 {
2659 my $op = shift;
2660 return ($op->flags & OPf_WANT) == OPf_WANT_SCALAR;
2661 }
2662  
2663 sub want_list
2664 {
2665 my $op = shift;
2666 return ($op->flags & OPf_WANT) == OPf_WANT_LIST;
2667 }
2668  
2669 sub _method
2670 {
2671 my($self, $op, $cx) = @_;
2672 my @other_ops = ($op->first);
2673 my $kid = $op->first->sibling; # skip pushmark
2674 my($meth, $obj, @exprs);
2675 if ($kid->name eq "list" and want_list $kid) {
2676 # When an indirect object isn't a bareword but the args are in
2677 # parens, the parens aren't part of the method syntax (the LLAFR
2678 # doesn't apply), but they make a list with OPf_PARENS set that
2679 # doesn't get flattened by the append_elem that adds the method,
2680 # making a (object, arg1, arg2, ...) list where the object
2681 # usually is. This can be distinguished from
2682 # '($obj, $arg1, $arg2)->meth()' (which is legal if $arg2 is an
2683 # object) because in the later the list is in scalar context
2684 # as the left side of -> always is, while in the former
2685 # the list is in list context as method arguments always are.
2686 # (Good thing there aren't method prototypes!)
2687 $meth = $kid->sibling;
2688 push @other_ops, $kid->first;
2689 $kid = $kid->first->sibling; # skip pushmark
2690 $obj = $kid;
2691 $kid = $kid->sibling;
2692 for (; not null $kid; $kid = $kid->sibling) {
2693 push @exprs, $kid;
2694 }
2695 } else {
2696 $obj = $kid;
2697 $kid = $kid->sibling;
2698 for (; !null ($kid->sibling) && $kid->name!~/^method(?:_named)?\z/;
2699 $kid = $kid->sibling) {
2700 push @exprs, $kid
2701 }
2702 $meth = $kid;
2703 }
2704  
2705 if ($meth->name eq "method_named") {
2706 $meth = $self->meth_sv($meth)->PV;
2707 } elsif ($meth->name eq "method_super") {
2708 $meth = "SUPER::".$self->meth_sv($meth)->PV;
2709 } elsif ($meth->name eq "method_redir") {
2710 $meth = $self->meth_rclass_sv($meth)->PV.'::'.$self->meth_sv($meth)->PV;
2711 } elsif ($meth->name eq "method_redir_super") {
2712 $meth = $self->meth_rclass_sv($meth)->PV.'::SUPER::'.
2713 $self->meth_sv($meth)->PV;
2714 } else {
2715 $meth = $meth->first;
2716 if ($meth->name eq "const") {
2717 # As of 5.005_58, this case is probably obsoleted by the
2718 # method_named case above
2719 $meth = $self->const_sv($meth)->PV; # needs to be bare
2720 }
2721 }
2722  
2723 return {
2724 method => $meth,
2725 variable_method => ref($meth),
2726 object => $obj,
2727 args => \@exprs,
2728 other_ops => \@other_ops
2729 }, $cx;
2730 }
2731  
2732 sub e_method {
2733 my ($self, $op, $minfo, $cx) = @_;
2734 my $obj = $self->deparse($minfo->{object}, 24, $op);
2735 my @body = ($obj);
2736 my $other_ops = $minfo->{other_ops};
2737  
2738 my $meth = $minfo->{method};
2739 my $meth_info;
2740 if ($minfo->{variable_method}) {
2741 $meth_info = $self->deparse($meth, 1, $op);
2742 push @body, $meth_info;
2743 }
2744 my @args = map { $self->deparse($_, 6, $op) } @{$minfo->{args}};
2745 push @body, @args;
2746 my @args_texts = map $_->{text}, @args;
2747 my $args = join(", ", @args_texts);
2748  
2749 my $opts = {body => \@body, other_ops => $other_ops};
2750 my @texts = ();
2751 my $type;
2752  
2753 if ($minfo->{object}->name eq 'scope' && want_list $minfo->{object}) {
2754 # method { $object }
2755 # This must be deparsed this way to preserve list context
2756 # of $object.
2757 my $need_paren = $cx >= 6;
2758 if ($need_paren) {
2759 @texts = ('(', $meth, substr($obj,2),
2760 $args, ')');
2761 $type = 'e_method_list_paren';
2762 } else {
2763 @texts = ($meth, substr($obj,2), $args);
2764 $type = 'e_method_list';
2765 }
2766 return info_from_list(\@texts, '', $type, $opts);
2767 }
2768 if (length $args) {
2769 @texts = ($obj->{text}, '->', $meth, '(', $args, ')');
2770 $type = 'e_method_args';
2771 } else {
2772 @texts = ($obj->{text}, '->', $meth);
2773 $type = 'e_method_null';
2774 }
2775 return info_from_list(\@texts, '', $type, $opts);
2776 }
2777  
2778 # returns "&" and the argument bodies if the prototype doesn't match the args,
2779 # or ("", $args_after_prototype_demunging) if it does.
2780 sub check_proto {
2781 my $self = shift;
2782 my $op = shift;
2783 return ('&', []) if $self->{'noproto'};
2784 my($proto, @args) = @_;
2785 my($arg, $real);
2786 my $doneok = 0;
2787 my @reals;
2788 # An unbackslashed @ or % gobbles up the rest of the args
2789 1 while $proto =~ s/(?<!\\)([@%])[^\]]+$/$1/;
2790 $proto =~ s/^\s*//;
2791 while ($proto) {
2792 $proto =~ s/^(\\?[\$\@&%*_]|\\\[[\$\@&%*]+\]|;)\s*//;
2793 my $chr = $1;
2794 if ($chr eq "") {
2795 return ('&', []) if @args;
2796 } elsif ($chr eq ";") {
2797 $doneok = 1;
2798 } elsif ($chr eq "@" or $chr eq "%") {
2799 push @reals, map($self->deparse($_, 6), @args, $op);
2800 @args = ();
2801 } else {
2802 $arg = shift @args;
2803 last unless $arg;
2804 if ($chr eq "\$" || $chr eq "_") {
2805 if (want_scalar $arg) {
2806 push @reals, $self->deparse($arg, 6, $op);
2807 } else {
2808 return ('&', []);
2809 }
2810 } elsif ($chr eq "&") {
2811 if ($arg->name =~ /^(s?refgen|undef)$/) {
2812 push @reals, $self->deparse($arg, 6, $op);
2813 } else {
2814 return ('&', []);
2815 }
2816 } elsif ($chr eq "*") {
2817 if ($arg->name =~ /^s?refgen$/
2818 and $arg->first->first->name eq "rv2gv")
2819 {
2820 $real = $arg->first->first; # skip refgen, null
2821 if ($real->first->name eq "gv") {
2822 push @reals, $self->deparse($real, 6, $op);
2823 } else {
2824 push @reals, $self->deparse($real->first, 6, $op);
2825 }
2826 } else {
2827 return ('&', []);
2828 }
2829 } elsif (substr($chr, 0, 1) eq "\\") {
2830 $chr =~ tr/\\[]//d;
2831 if ($arg->name =~ /^s?refgen$/ and
2832 !null($real = $arg->first) and
2833 ($chr =~ /\$/ && is_scalar($real->first)
2834 or ($chr =~ /@/
2835 && class($real->first->sibling) ne 'NULL'
2836 && $real->first->sibling->name
2837 =~ /^(rv2|pad)av$/)
2838 or ($chr =~ /%/
2839 && class($real->first->sibling) ne 'NULL'
2840 && $real->first->sibling->name
2841 =~ /^(rv2|pad)hv$/)
2842 #or ($chr =~ /&/ # This doesn't work
2843 # && $real->first->name eq "rv2cv")
2844 or ($chr =~ /\*/
2845 && $real->first->name eq "rv2gv")))
2846 {
2847 push @reals, $self->deparse($real, 6, $op);
2848 } else {
2849 return ('&', []);
2850 }
2851 }
2852 }
2853 }
2854 return ('&', []) if $proto and !$doneok; # too few args and no ';'
2855 return ('&', []) if @args; # too many args
2856 return ('', \@reals);
2857 }
2858  
2859 sub pp_enterwrite { unop(@_, "write") }
2860  
2861 # escape things that cause interpolation in double quotes,
2862 # but not character escapes
2863 sub uninterp {
2864 my($str) = @_;
2865 $str =~ s/(^|\G|[^\\])((?:\\\\)*)([\$\@]|\\[uUlLQE])/$1$2\\$3/g;
2866 return $str;
2867 }
2868  
2869 {
2870 my $bal;
2871 BEGIN {
2872 use re "eval";
2873 # Matches any string which is balanced with respect to {braces}
2874 $bal = qr(
2875 (?:
2876 [^\\{}]
2877 | \\\\
2878 | \\[{}]
2879 | \{(??{$bal})\}
2880 )*
2881 )x;
2882 }
2883  
2884 my %unctrl = # portable to EBCDIC
2885 (
2886 "\c@" => '\c@', # unused
2887 "\cA" => '\cA',
2888 "\cB" => '\cB',
2889 "\cC" => '\cC',
2890 "\cD" => '\cD',
2891 "\cE" => '\cE',
2892 "\cF" => '\cF',
2893 "\cG" => '\cG',
2894 "\cH" => '\cH',
2895 "\cI" => '\cI',
2896 "\cJ" => '\cJ',
2897 "\cK" => '\cK',
2898 "\cL" => '\cL',
2899 "\cM" => '\cM',
2900 "\cN" => '\cN',
2901 "\cO" => '\cO',
2902 "\cP" => '\cP',
2903 "\cQ" => '\cQ',
2904 "\cR" => '\cR',
2905 "\cS" => '\cS',
2906 "\cT" => '\cT',
2907 "\cU" => '\cU',
2908 "\cV" => '\cV',
2909 "\cW" => '\cW',
2910 "\cX" => '\cX',
2911 "\cY" => '\cY',
2912 "\cZ" => '\cZ',
2913 "\c[" => '\c[', # unused
2914 "\c\\" => '\c\\', # unused
2915 "\c]" => '\c]', # unused
2916 "\c_" => '\c_', # unused
2917 );
2918  
2919 # Split a floating point number into an integer mantissa and a binary
2920 # exponent. Assumes you've already made sure the number isn't zero or
2921 # some weird infinity or NaN.
2922 sub split_float {
2923 my($f) = @_;
2924 my $exponent = 0;
2925 if ($f == int($f)) {
2926 while ($f % 2 == 0) {
2927 $f /= 2;
2928 $exponent++;
2929 }
2930 } else {
2931 while ($f != int($f)) {
2932 $f *= 2;
2933 $exponent--;
2934 }
2935 }
2936 my $mantissa = sprintf("%.0f", $f);
2937 return ($mantissa, $exponent);
2938 }
2939  
2940 sub pp_const {
2941 my $self = shift;
2942 my($op, $cx) = @_;
2943 if ($op->private & OPpCONST_ARYBASE) {
2944 return info_from_text('$[', 'const_ary', {});
2945 }
2946 # if ($op->private & OPpCONST_BARE) { # trouble with '=>' autoquoting
2947 # return $self->const_sv($op)->PV;
2948 # }
2949 my $sv = $self->const_sv($op);
2950 return $self->const($sv, $cx);;
2951 }
2952  
2953 sub dq
2954 {
2955 my ($self, $op, $parent) = @_;
2956 my $type = $op->name;
2957 my $info;
2958 if ($type eq "const") {
2959 return info_from_text('$[', 'dq_const_ary', {}) if $op->private & OPpCONST_ARYBASE;
2960 return info_from_text(uninterp(B::Deparse::escape_str(B::Deparse::unback($self->const_sv($op)->as_string))),
2961 'dq_const', {});
2962 } elsif ($type eq "concat") {
2963 my $first = $self->dq($op->first, $op);
2964 my $last = $self->dq($op->last, $op);
2965  
2966 # Disambiguate "${foo}bar", "${foo}{bar}", "${foo}[1]", "$foo\::bar"
2967 ($last =~ /^[A-Z\\\^\[\]_?]/ &&
2968 $first =~ s/([\$@])\^$/${1}{^}/) # "${^}W" etc
2969 || ($last =~ /^[:'{\[\w_]/ && #'
2970 $first =~ s/([\$@])([A-Za-z_]\w*)$/${1}{$2}/);
2971  
2972 return info_from_list([$first->{text}, $last->{text}], '', 'dq_concat',
2973 {body => [$first, $last]});
2974 } elsif ($type eq "join") {
2975 return $self->deparse($op->last, 26, $op); # was join($", @ary)
2976 } else {
2977 return $self->deparse($op, 26, $parent);
2978 }
2979 my $kid = $self->dq($op->first->sibling, $op);
2980 my $kid_text = $kid->{text};
2981 if ($type eq "uc") {
2982 $info = info_from_lists(['\U', $kid, '\E'], '', 'dq_uc', {});
2983 } elsif ($type eq "lc") {
2984 $info = info_from_lists(['\L', $kid, '\E'], '', 'dq_lc', {});
2985 } elsif ($type eq "ucfirst") {
2986 $info = info_from_lists(['\u', $kid, '\E'], '', 'dq_ucfirst', {});
2987 } elsif ($type eq "lcfirst") {
2988 $info = info_from_lists(['\l', $kid, '\E'], '', 'dq_lcfirst', {});
2989 } elsif ($type eq "quotemeta") {
2990 $info = info_from_lists(['\Q', $kid, '\E'], '', 'dq_quotemeta', {});
2991 } elsif ($type eq "fc") {
2992 $info = info_from_lists(['\F', $kid, '\E'], '', 'dq_fc', {});
2993 }
2994 $info->{body} = [$kid];
2995 return $info;
2996 }
2997  
2998 sub pp_backtick
2999 {
3000 my($self, $op, $cx) = @_;
3001 # skip pushmark if it exists (readpipe() vs ``)
3002 my $child = $op->first->sibling->isa('B::NULL')
3003 ? $op->first : $op->first->sibling;
3004 if ($self->pure_string($child)) {
3005 return $self->single_delim($op, "qx", '`', $self->dq($child, 1)->{text});
3006 }
3007 unop($self, $op, $cx, "readpipe");
3008 }
3009  
3010 # OP_STRINGIFY is a listop, but it only ever has one arg
3011 sub pp_stringify {
3012 my ($self, $op, $cx) = @_;
3013 my $kid = $op->first->sibling;
3014 my @other_ops = ();
3015 while ($kid->name eq 'null' && !null($kid->first)) {
3016 push(@other_ops, $kid);
3017 $kid = $kid->first;
3018 }
3019 my $info;
3020 if ($kid->name =~ /^(?:const|padsv|rv2sv|av2arylen|gvsv|multideref
3021 |aelemfast(?:_lex)?|[ah]elem|join|concat)\z/x) {
3022 $info = maybe_targmy(@_, \&dquote);
3023 }
3024 else {
3025 # Actually an optimised join.
3026 my $info = listop(@_,"join");
3027 $info->{text} =~ s/join([( ])/join$1$self->{'ex_const'}, /;
3028 }
3029 push @{$info->{other_ops}}, @other_ops;
3030 return $info;
3031 }
3032  
3033  
3034 # tr/// and s/// (and tr[][], tr[]//, tr###, etc)
3035 # note that tr(from)/to/ is OK, but not tr/from/(to)
3036 sub double_delim {
3037 my($from, $to) = @_;
3038 my($succeed, $delim);
3039 if ($from !~ m[/] and $to !~ m[/]) {
3040 return "/$from/$to/";
3041 } elsif (($succeed, $from) = balanced_delim($from) and $succeed) {
3042 if (($succeed, $to) = balanced_delim($to) and $succeed) {
3043 return "$from$to";
3044 } else {
3045 for $delim ('/', '"', '#') { # note no "'" -- s''' is special
3046 return "$from$delim$to$delim" if index($to, $delim) == -1;
3047 }
3048 $to =~ s[/][\\/]g;
3049 return "$from/$to/";
3050 }
3051 } else {
3052 for $delim ('/', '"', '#') { # note no '
3053 return "$delim$from$delim$to$delim"
3054 if index($to . $from, $delim) == -1;
3055 }
3056 $from =~ s[/][\\/]g;
3057 $to =~ s[/][\\/]g;
3058 return "/$from/$to/";
3059 }
3060 }
3061  
3062 # Only used by tr///, so backslashes hyphens
3063 sub pchr { # ASCII
3064 my($n) = @_;
3065 if ($n == ord '\\') {
3066 return '\\\\';
3067 } elsif ($n == ord "-") {
3068 return "\\-";
3069 } elsif ($n >= ord(' ') and $n <= ord('~')) {
3070 return chr($n);
3071 } elsif ($n == ord "\a") {
3072 return '\\a';
3073 } elsif ($n == ord "\b") {
3074 return '\\b';
3075 } elsif ($n == ord "\t") {
3076 return '\\t';
3077 } elsif ($n == ord "\n") {
3078 return '\\n';
3079 } elsif ($n == ord "\e") {
3080 return '\\e';
3081 } elsif ($n == ord "\f") {
3082 return '\\f';
3083 } elsif ($n == ord "\r") {
3084 return '\\r';
3085 } elsif ($n >= ord("\cA") and $n <= ord("\cZ")) {
3086 return '\\c' . chr(ord("@") + $n);
3087 } else {
3088 # return '\x' . sprintf("%02x", $n);
3089 return '\\' . sprintf("%03o", $n);
3090 }
3091 }
3092  
3093 sub collapse {
3094 my(@chars) = @_;
3095 my($str, $c, $tr) = ("");
3096 for ($c = 0; $c < @chars; $c++) {
3097 $tr = $chars[$c];
3098 $str .= pchr($tr);
3099 if ($c <= $#chars - 2 and $chars[$c + 1] == $tr + 1 and
3100 $chars[$c + 2] == $tr + 2)
3101 {
3102 for (; $c <= $#chars-1 and $chars[$c + 1] == $chars[$c] + 1; $c++)
3103 {}
3104 $str .= "-";
3105 $str .= pchr($chars[$c]);
3106 }
3107 }
3108 return $str;
3109 }
3110  
3111 sub tr_decode_byte {
3112 my($table, $flags) = @_;
3113 my(@table) = unpack("s*", $table);
3114 splice @table, 0x100, 1; # Number of subsequent elements
3115 my($c, $tr, @from, @to, @delfrom, $delhyphen);
3116 if ($table[ord "-"] != -1 and
3117 $table[ord("-") - 1] == -1 || $table[ord("-") + 1] == -1)
3118 {
3119 $tr = $table[ord "-"];
3120 $table[ord "-"] = -1;
3121 if ($tr >= 0) {
3122 @from = ord("-");
3123 @to = $tr;
3124 } else { # -2 ==> delete
3125 $delhyphen = 1;
3126 }
3127 }
3128 for ($c = 0; $c < @table; $c++) {
3129 $tr = $table[$c];
3130 if ($tr >= 0) {
3131 push @from, $c; push @to, $tr;
3132 } elsif ($tr == -2) {
3133 push @delfrom, $c;
3134 }
3135 }
3136 @from = (@from, @delfrom);
3137 if ($flags & OPpTRANS_COMPLEMENT) {
3138 my @newfrom = ();
3139 my %from;
3140 @from{@from} = (1) x @from;
3141 for ($c = 0; $c < 256; $c++) {
3142 push @newfrom, $c unless $from{$c};
3143 }
3144 @from = @newfrom;
3145 }
3146 unless ($flags & OPpTRANS_DELETE || !@to) {
3147 pop @to while $#to and $to[$#to] == $to[$#to -1];
3148 }
3149 my($from, $to);
3150 $from = collapse(@from);
3151 $to = collapse(@to);
3152 $from .= "-" if $delhyphen;
3153 return ($from, $to);
3154 }
3155  
3156 sub tr_chr {
3157 my $x = shift;
3158 if ($x == ord "-") {
3159 return "\\-";
3160 } elsif ($x == ord "\\") {
3161 return "\\\\";
3162 } else {
3163 return chr $x;
3164 }
3165 }
3166  
3167 # XXX This doesn't yet handle all cases correctly either
3168  
3169 sub tr_decode_utf8 {
3170 my($swash_hv, $flags) = @_;
3171 my %swash = $swash_hv->ARRAY;
3172 my $final = undef;
3173 $final = $swash{'FINAL'}->IV if exists $swash{'FINAL'};
3174 my $none = $swash{"NONE"}->IV;
3175 my $extra = $none + 1;
3176 my(@from, @delfrom, @to);
3177 my $line;
3178 foreach $line (split /\n/, $swash{'LIST'}->PV) {
3179 my($min, $max, $result) = split(/\t/, $line);
3180 $min = hex $min;
3181 if (length $max) {
3182 $max = hex $max;
3183 } else {
3184 $max = $min;
3185 }
3186 $result = hex $result;
3187 if ($result == $extra) {
3188 push @delfrom, [$min, $max];
3189 } else {
3190 push @from, [$min, $max];
3191 push @to, [$result, $result + $max - $min];
3192 }
3193 }
3194 for my $i (0 .. $#from) {
3195 if ($from[$i][0] == ord '-') {
3196 unshift @from, splice(@from, $i, 1);
3197 unshift @to, splice(@to, $i, 1);
3198 last;
3199 } elsif ($from[$i][1] == ord '-') {
3200 $from[$i][1]--;
3201 $to[$i][1]--;
3202 unshift @from, ord '-';
3203 unshift @to, ord '-';
3204 last;
3205 }
3206 }
3207 for my $i (0 .. $#delfrom) {
3208 if ($delfrom[$i][0] == ord '-') {
3209 push @delfrom, splice(@delfrom, $i, 1);
3210 last;
3211 } elsif ($delfrom[$i][1] == ord '-') {
3212 $delfrom[$i][1]--;
3213 push @delfrom, ord '-';
3214 last;
3215 }
3216 }
3217 if (defined $final and $to[$#to][1] != $final) {
3218 push @to, [$final, $final];
3219 }
3220 push @from, @delfrom;
3221 if ($flags & OPpTRANS_COMPLEMENT) {
3222 my @newfrom;
3223 my $next = 0;
3224 for my $i (0 .. $#from) {
3225 push @newfrom, [$next, $from[$i][0] - 1];
3226 $next = $from[$i][1] + 1;
3227 }
3228 @from = ();
3229 for my $range (@newfrom) {
3230 if ($range->[0] <= $range->[1]) {
3231 push @from, $range;
3232 }
3233 }
3234 }
3235 my($from, $to, $diff);
3236 for my $chunk (@from) {
3237 $diff = $chunk->[1] - $chunk->[0];
3238 if ($diff > 1) {
3239 $from .= tr_chr($chunk->[0]) . "-" . tr_chr($chunk->[1]);
3240 } elsif ($diff == 1) {
3241 $from .= tr_chr($chunk->[0]) . tr_chr($chunk->[1]);
3242 } else {
3243 $from .= tr_chr($chunk->[0]);
3244 }
3245 }
3246 for my $chunk (@to) {
3247 $diff = $chunk->[1] - $chunk->[0];
3248 if ($diff > 1) {
3249 $to .= tr_chr($chunk->[0]) . "-" . tr_chr($chunk->[1]);
3250 } elsif ($diff == 1) {
3251 $to .= tr_chr($chunk->[0]) . tr_chr($chunk->[1]);
3252 } else {
3253 $to .= tr_chr($chunk->[0]);
3254 }
3255 }
3256 #$final = sprintf("%04x", $final) if defined $final;
3257 #$none = sprintf("%04x", $none) if defined $none;
3258 #$extra = sprintf("%04x", $extra) if defined $extra;
3259 #print STDERR "final: $final\n none: $none\nextra: $extra\n";
3260 #print STDERR $swash{'LIST'}->PV;
3261 return (B::Deparse::escape_str($from), B::Deparse::escape_str($to));
3262 }
3263  
3264 sub pp_trans {
3265 my $self = shift;
3266 my($op, $cx) = @_;
3267 my($from, $to);
3268 my $class = class($op);
3269 my $priv_flags = $op->private;
3270 if ($class eq "PVOP") {
3271 ($from, $to) = tr_decode_byte($op->pv, $priv_flags);
3272 } elsif ($class eq "PADOP") {
3273 ($from, $to)
3274 = tr_decode_utf8($self->padval($op->padix)->RV, $priv_flags);
3275 } else { # class($op) eq "SVOP"
3276 ($from, $to) = tr_decode_utf8($op->sv->RV, $priv_flags);
3277 }
3278 my $flags = "";
3279 $flags .= "c" if $priv_flags & OPpTRANS_COMPLEMENT;
3280 $flags .= "d" if $priv_flags & OPpTRANS_DELETE;
3281 $to = "" if $from eq $to and $flags eq "";
3282 $flags .= "s" if $priv_flags & OPpTRANS_SQUASH;
3283 return info_from_list(['tr', double_delim($from, $to), $flags],
3284 '', 'pp_trans', {});
3285 }
3286  
3287 sub pp_transr {
3288 my $info = pp_trans @_;
3289 return info_from_text($info->{text} . 'r', 'pp_transr',
3290 {body => [$info]});
3291 }
3292  
3293 sub re_dq_disambiguate {
3294 my ($first, $last) = @_;
3295 # Disambiguate "${foo}bar", "${foo}{bar}", "${foo}[1]"
3296 ($last =~ /^[A-Z\\\^\[\]_?]/ &&
3297 $first =~ s/([\$@])\^$/${1}{^}/) # "${^}W" etc
3298 || ($last =~ /^[{\[\w_]/ &&
3299 $first =~ s/([\$@])([A-Za-z_]\w*)$/${1}{$2}/);
3300 return $first . $last;
3301 }
3302  
3303 # Like dq(), but different
3304 sub re_dq {
3305 my $self = shift;
3306 my ($op, $extended) = @_;
3307  
3308 my $type = $op->name;
3309 my ($re, @texts, $type);
3310 my $opts = {};
3311 if ($type eq "const") {
3312 return info_from_text('$[', 're_dq_const', {})
3313 if $op->private & OPpCONST_ARYBASE;
3314 my $unbacked = B::Deparse::re_unback($self->const_sv($op)->as_string);
3315 return B::Deparse::re_uninterp_extended(escape_extended_re($unbacked))
3316 if $extended;
3317 return B::Deparse::re_uninterp(B::Deparse::escape_str($unbacked));
3318 } elsif ($type eq "concat") {
3319 my $first = $self->re_dq($op->first, $extended);
3320 my $last = $self->re_dq($op->last, $extended);
3321 return re_dq_disambiguate($first, $last);
3322 } elsif ($type eq "uc") {
3323 $re = $self->re_dq($op->first->sibling, $extended);
3324 @texts = ['\U', $re->{text},'\E'];
3325 $type = 're_dq_U';
3326 } elsif ($type eq "lc") {
3327 $re = $self->re_dq($op->first->sibling, $extended);
3328 @texts = ['\L', $re->{text},'\E'];
3329 $type = 're_dq_L';
3330 } elsif ($type eq "ucfirst") {
3331 $re = $self->re_dq($op->first->sibling, $extended);
3332 @texts = ['\u', $re->{text}];
3333 $type = 're_dq_u';
3334 } elsif ($type eq "lcfirst") {
3335 $re = $self->re_dq($op->first->sibling, $extended);
3336 @texts = ['\l', $re->{text}];
3337 $type = 're_dq_l';
3338 } elsif ($type eq "quotemeta") {
3339 $re = $self->re_dq($op->first->sibling, $extended);
3340 @texts = ['\Q', $re->{text},'\E'];
3341 $type = 're_dq_Q';
3342 } elsif ($type eq "fc") {
3343 $re = $self->re_dq($op->first->sibling, $extended);
3344 @texts = ['\F', $re->{text},'\E'];
3345 $type = 're_dq_Q';
3346 } elsif ($type eq "join") {
3347 return $self->deparse($op->last, 26, $op); # was join($", @ary)
3348 } else {
3349 my $info = $self->deparse($op, 26, $op);
3350 $info->{type} = 're_dq';
3351 $info->{text} =~ s/^\$([(|)])\z/\${$1}/; # $( $| $) need braces
3352 return $info;
3353 }
3354 $opts->{body} = [$re];
3355 return info_from_list(\@texts, '', $type, $opts);
3356 }
3357  
3358 sub pure_string {
3359 my ($self, $op) = @_;
3360 return 0 if null $op;
3361 my $type = $op->name;
3362  
3363 if ($type eq 'const' || $type eq 'av2arylen') {
3364 return 1;
3365 }
3366 elsif ($type =~ /^(?:[ul]c(first)?|fc)$/ || $type eq 'quotemeta') {
3367 return $self->pure_string($op->first->sibling);
3368 }
3369 elsif ($type eq 'join') {
3370 my $join_op = $op->first->sibling; # Skip pushmark
3371 return 0 unless $join_op->name eq 'null' && $join_op->targ == OP_RV2SV;
3372  
3373 my $gvop = $join_op->first;
3374 return 0 unless $gvop->name eq 'gvsv';
3375 return 0 unless '"' eq $self->gv_name($self->gv_or_padgv($gvop));
3376  
3377 return 0 unless ${$join_op->sibling} eq ${$op->last};
3378 return 0 unless $op->last->name =~ /^(?:[ah]slice|(?:rv2|pad)av)$/;
3379 }
3380 elsif ($type eq 'concat') {
3381 return $self->pure_string($op->first)
3382 && $self->pure_string($op->last);
3383 }
3384 elsif (is_scalar($op) || $type =~ /^[ah]elem$/) {
3385 return 1;
3386 }
3387 elsif ($type eq "null" and $op->can('first') and not null $op->first and
3388 ($op->first->name eq "null" and $op->first->can('first')
3389 and not null $op->first->first and
3390 $op->first->first->name eq "aelemfast"
3391 or
3392 $op->first->name =~ /^aelemfast(?:_lex)?\z/
3393 )) {
3394 return 1;
3395 }
3396 else {
3397 return 0;
3398 }
3399  
3400 return 1;
3401 }
3402  
3403 sub regcomp
3404 {
3405 my($self, $op, $cx, $extended) = @_;
3406 my @other_ops = ();
3407 my $kid = $op->first;
3408 if ($kid->name eq "regcmaybe") {
3409 push @other_ops, $kid;
3410 $kid = $kid->first;
3411 }
3412 if ($kid->name eq "regcreset") {
3413 push @other_ops, $kid;
3414 $kid = $kid->first;
3415 }
3416 if ($kid->name eq "null" and !null($kid->first)
3417 and $kid->first->name eq 'pushmark') {
3418 my $str = '';
3419 push(@other_ops, $kid);
3420 $kid = $kid->first->sibling;
3421 my @body = ();
3422 while (!null($kid)) {
3423 my $first = $str;
3424 my $last = $self->re_dq($kid, $extended);
3425 push @body, $last;
3426 push(@other_ops, $kid);
3427 $str = re_dq_disambiguate($first, $last->{text});
3428 $kid = $kid->sibling;
3429 }
3430 return (info_from_text($str, 'regcomp',
3431 {other_ops => \@other_ops,
3432 body => \@body}), 1);
3433 }
3434  
3435 if ($self->pure_string($kid)) {
3436 my $info = $self->re_dq($kid, $extended);
3437 my @kid_ops = $info->{other_ops} ? @{$info->{other_ops}} : ();
3438 push @other_ops, @kid_ops;
3439 $info->{other_ops} = \@other_ops;
3440 return ($info, 1);
3441 }
3442 return ($self->deparse($kid, $cx, $op), 0, $op);
3443 }
3444  
3445 sub pp_regcomp
3446 {
3447 my ($self, $op, $cx) = @_;
3448 return (($self->regcomp($op, $cx, 0))[0]);
3449 }
3450  
3451 sub re_flags
3452 {
3453 my ($self, $op) = @_;
3454 my $flags = '';
3455 my $pmflags = $op->pmflags;
3456 $flags .= "g" if $pmflags & PMf_GLOBAL;
3457 $flags .= "i" if $pmflags & PMf_FOLD;
3458 $flags .= "m" if $pmflags & PMf_MULTILINE;
3459 $flags .= "o" if $pmflags & PMf_KEEP;
3460 $flags .= "s" if $pmflags & PMf_SINGLELINE;
3461 $flags .= "x" if $pmflags & PMf_EXTENDED;
3462 $flags .= "p" if $pmflags & PMf_KEEPCOPY;
3463 $flags .= "n" if $pmflags & PMf_NOCAPTURE;
3464 if (my $charset = $pmflags & PMf_CHARSET) {
3465 # Hardcoding this is fragile, but B does not yet export the
3466 # constants we need.
3467 $flags .= qw(d l u a aa)[$charset >> 5]
3468 }
3469 # The /d flag is indicated by 0; only show it if necessary.
3470 elsif ($self->{hinthash} and
3471 $self->{hinthash}{reflags_charset}
3472 || $self->{hinthash}{feature_unicode}
3473 or $self->{hints} & $feature::hint_mask
3474 && ($self->{hints} & $feature::hint_mask)
3475 != $feature::hint_mask
3476 && $self->{hints} & $feature::hint_uni8bit
3477 ) {
3478 $flags .= 'd';
3479 }
3480 $flags;
3481 }
3482  
3483 # osmic acid -- see osmium tetroxide
3484  
3485 my %matchwords;
3486 map($matchwords{join "", sort split //, $_} = $_, 'cig', 'cog', 'cos', 'cogs',
3487 'cox', 'go', 'is', 'ism', 'iso', 'mig', 'mix', 'osmic', 'ox', 'sic',
3488 'sig', 'six', 'smog', 'so', 'soc', 'sog', 'xi');
3489  
3490 sub matchop
3491 {
3492 my($self, $op, $cx, $name, $delim) = @_;
3493 my $kid = $op->first;
3494 my $info = {};
3495 my @body = ();
3496 my ($binop, $var, $re_str) = ("", "", "");
3497 my $re;
3498 if ($op->flags & OPf_STACKED) {
3499 $binop = 1;
3500 $var = $self->deparse($kid, 20, $op);
3501 push @body, $var;
3502 $kid = $kid->sibling;
3503 }
3504 my $quote = 1;
3505 my $pmflags = $op->pmflags;
3506 my $extended = ($pmflags & PMf_EXTENDED);
3507 my $rhs_bound_to_defsv;
3508 if (null $kid) {
3509 my $unbacked = B::Deparse::re_unback($op->precomp);
3510 if ($extended) {
3511 $re_str = B::Deparse::re_uninterp_extended(escape_extended_re($unbacked));
3512 } else {
3513 $re_str = B::Deparse::re_uninterp(B::Deparse::escape_str(re_unback($op->precomp)));
3514 }
3515 } elsif ($kid->name ne 'regcomp') {
3516 carp("found ".$kid->name." where regcomp expected");
3517 } else {
3518 ($re, $quote) = $self->regcomp($kid, 21, $extended);
3519 push @body, $re;
3520 $re_str = $re->{text};
3521 my $matchop = $kid->first;
3522 if ($matchop->name eq 'regcrest') {
3523 $matchop = $matchop->first;
3524 }
3525 if ($matchop->name =~ /^(?:match|transr?|subst)\z/
3526 && $matchop->flags & OPf_SPECIAL) {
3527 $rhs_bound_to_defsv = 1;
3528 }
3529 }
3530 my $flags = '';
3531 $flags .= "c" if $pmflags & PMf_CONTINUE;
3532 $flags .= $self->re_flags($op);
3533 $flags = join '', sort split //, $flags;
3534 $flags = $matchwords{$flags} if $matchwords{$flags};
3535  
3536 if ($pmflags & PMf_ONCE) { # only one kind of delimiter works here
3537 $re_str =~ s/\?/\\?/g;
3538 $re_str = "?$re_str?";
3539 } elsif ($quote) {
3540 my $re = $self->single_delim($kid, $name, $delim, $re_str);
3541 push @body, $re;
3542 $re_str = $re->{text};
3543 }
3544 my $opts = {body => \@body};
3545 my @texts;
3546 $re_str .= $flags if $quote;
3547 my $type;
3548 if ($binop) {
3549 if ($rhs_bound_to_defsv) {
3550 @texts = ($var->{text}, ' =~ ', "(", '$_', ' =~ ', $re_str, ')');
3551 } else {
3552 @texts = ($var->{text}, ' =~ ', $re_str);
3553 }
3554 $opts->{maybe_parens} = [$self, $cx, 20];
3555 $type = 'matchop_binop';
3556 } else {
3557 @texts = ($re_str);
3558 $type = 'matchop_unnop';
3559 }
3560 return info_from_list(\@texts, '', $type, $opts);
3561 }
3562  
3563 sub pp_match { matchop(@_, "m", "/") }
3564 sub pp_pushre { matchop(@_, "m", "/") }
3565 sub pp_qr { matchop(@_, "qr", "") }
3566  
3567 sub pp_runcv { unop(@_, "__SUB__"); }
3568  
3569 sub pp_split
3570 {
3571 my($self, $op, $cx) = @_;
3572 my($kid, @exprs, $ary_info, $expr);
3573 my $stacked = $op->flags & OPf_STACKED;
3574 my $ary = '';
3575 my @body = ();
3576 my @other_ops = ();
3577 $kid = $op->first;
3578  
3579 $kid = $op->first;
3580 $kid = $kid->sibling if $kid->name eq 'regcomp';
3581 for (; !null($kid); $kid = $kid->sibling) {
3582 push @exprs, $self->deparse($kid, 6, $op);
3583 }
3584  
3585 unshift @exprs, $self->matchop($op, $cx, "m", "/");
3586  
3587 if ($op->private & OPpSPLIT_ASSIGN) {
3588 # With C<@array = split(/pat/, str);>,
3589 # array is stored in split's pmreplroot; either
3590 # as an integer index into the pad (for a lexical array)
3591 # or as GV for a package array (which will be a pad index
3592 # on threaded builds)
3593 # With my/our @array = split(/pat/, str), the array is instead
3594 # accessed via an extra padav/rv2av op at the end of the
3595 # split's kid ops.
3596  
3597 if ($stacked) {
3598 $ary = pop @exprs;
3599 }
3600 else {
3601 if ($op->private & OPpSPLIT_LEX) {
3602 $ary = $self->padname($op->pmreplroot);
3603 }
3604 else {
3605 # union with op_pmtargetoff, op_pmtargetgv
3606 my $gv = $op->pmreplroot;
3607 $gv = $self->padval($gv) if !ref($gv);
3608 $ary = $self->maybe_local(@_,
3609 $self->stash_variable('@',
3610 $self->gv_name($gv),
3611 $cx))
3612 }
3613 if ($op->private & OPpLVAL_INTRO) {
3614 $ary = $op->private & OPpSPLIT_LEX ? "my $ary" : "local $ary";
3615 }
3616 }
3617 }
3618  
3619 push @body, @exprs;
3620 my $opts = {body => \@exprs};
3621  
3622 # handle special case of split(), and split(' ') that compiles to /\s+/
3623 if (($op->reflags // 0) & RXf_SKIPWHITE()) {
3624 my $expr0 = $exprs[0];
3625 my $expr0b0 = $expr0->{body}[0];
3626 my $bsep = $expr0b0->{sep};
3627 my $sep = $expr0->{sep};
3628 $expr0b0->{texts}[1] = ' ';
3629 substr($expr0b0->{text}, 1, 0) = ' ';
3630 substr($expr0->{texts}[0], 1, 0) = ' ';
3631 substr($expr0->{text}, 1, 0) = ' ';
3632 }
3633 my @args_texts = map $_->{text}, @exprs;
3634  
3635 my $sep = '';
3636 my $type;
3637 my @expr_texts;
3638 if ($ary) {
3639 @expr_texts = ("$ary", '=', join(', ', @args_texts));
3640 $sep = ' ';
3641 $type = 'split_array';
3642 $opts->{maybe_parens} = [$self, $cx, 7];
3643 } else {
3644 @expr_texts = ('split', '(', join(', ', @args_texts), ')');
3645 $type = 'split';
3646  
3647 }
3648 return info_from_list(\@expr_texts, $sep, $type, $opts);
3649 }
3650  
3651 # Kind of silly, but we prefer, subst regexp flags joined together to
3652 # make words. For example: s/a/b/xo => s/a/b/ox
3653  
3654 # oxime -- any of various compounds obtained chiefly by the action of
3655 # hydroxylamine on aldehydes and ketones and characterized by the
3656 # bivalent grouping C=NOH [Webster's Tenth]
3657  
3658 my %substwords;
3659 map($substwords{join "", sort split //, $_} = $_, 'ego', 'egoism', 'em',
3660 'es', 'ex', 'exes', 'gee', 'go', 'goes', 'ie', 'ism', 'iso', 'me',
3661 'meese', 'meso', 'mig', 'mix', 'os', 'ox', 'oxime', 'see', 'seem',
3662 'seg', 'sex', 'sig', 'six', 'smog', 'sog', 'some', 'xi', 'rogue',
3663 'sir', 'rise', 'smore', 'more', 'seer', 'rome', 'gore', 'grim', 'grime',
3664 'or', 'rose', 'rosie');
3665  
3666 sub pp_subst
3667 {
3668 my($self, $op, $cx) = @_;
3669 my $kid = $op->first;
3670 my($binop, $var, $re, @other_ops) = ("", "", "", ());
3671 my @body = ();
3672 my ($repl, $repl_info);
3673 if ($op->flags & OPf_STACKED) {
3674 $binop = 1;
3675 $var = $self->deparse($kid, 20, $op);
3676 $kid = $kid->sibling;
3677 }
3678 my $flags = "";
3679 my $pmflags = $op->pmflags;
3680 if (null($op->pmreplroot)) {
3681 $repl = $kid;
3682 $kid = $kid->sibling;
3683 } else {
3684 push @other_ops, $op->pmreplroot;
3685 $repl = $op->pmreplroot->first; # skip substcont
3686 }
3687 while ($repl->name eq "entereval") {
3688 push @other_ops, $repl;
3689 $repl = $repl->first;
3690 $flags .= "e";
3691 }
3692 {
3693 local $self->{in_subst_repl} = 1;
3694 if ($pmflags & PMf_EVAL) {
3695 $repl_info = $self->deparse($repl->first, 0, $repl);
3696 } else {
3697 $repl_info = $self->dq($repl);
3698 }
3699 }
3700 my $extended = ($pmflags & PMf_EXTENDED);
3701 if (null $kid) {
3702 my $unbacked = B::Deparse::re_unback($op->precomp);
3703 if ($extended) {
3704 $re = B::Deparse::re_uninterp_extended(escape_extended_re($unbacked));
3705 }
3706 else {
3707 $re = B::Deparse::re_uninterp(B::Deparse::escape_str($unbacked));
3708 }
3709 } else {
3710 my ($re_info, $junk) = $self->regcomp($kid, 1, $extended);
3711 push @body, $re_info;
3712 $re = $re_info->{text};
3713 }
3714 $flags .= "r" if $pmflags & PMf_NONDESTRUCT;
3715 $flags .= "e" if $pmflags & PMf_EVAL;
3716 $flags .= $self->re_flags($op);
3717 $flags = join '', sort split //, $flags;
3718 $flags = $substwords{$flags} if $substwords{$flags};
3719 my $info;
3720 push @body, $repl_info;
3721 my $repl_text = $repl_info->{text};
3722 my $opts = {body => \@body};
3723 $opts->{other_ops} = \@other_ops if @other_ops;
3724 if ($binop) {
3725 my @texts = ($var->{text}, " ", "=~", " ", "s", double_delim($re, $repl_text), $flags);
3726 $opts->{maybe_parens} = [$self, $cx, 20];
3727 return info_from_list(\@texts, '', 'subst_binop', $opts);
3728 } else {
3729 return info_from_list(['s', double_delim($re, $repl_text)], '', 'subst',
3730 $opts);
3731 }
3732 Carp::confess("unhandled condition in pp_subst");
3733 }
3734  
3735 sub pp_introcv
3736 {
3737 my($self, $op, $cx) = @_;
3738 # For now, deparsing doesn't worry about the distinction between introcv
3739 # and clonecv, so pretend this op doesn't exist:
3740 return info_from_text('', 'introcv', {});
3741 }
3742  
3743 sub pp_clonecv {
3744 my $self = shift;
3745 my($op, $cx) = @_;
3746 my $sv = $self->padname_sv($op->targ);
3747 my $name = substr $sv->PVX, 1; # skip &/$/@/%, like $self->padany
3748 return info_from_list(['my', 'sub', $name], ' ', 'clonev', {});
3749 }
3750  
3751 sub pp_padcv {
3752 my($self, $op, $cx) = @_;
3753 return info_from_text($self->padany($op), 'padcv', {});
3754 }
3755  
3756 unless (caller) {
3757 eval "use Data::Printer;";
3758  
3759 eval {
3760 sub fib($) {
3761 my $x = shift;
3762 return 1 if $x <= 1;
3763 return(fib($x-1) + fib($x-2))
3764 }
3765 sub baz {
3766 no strict;
3767 CORE::wait;
3768 }
3769 };
3770  
3771 # use B::Deparse;
3772 # my $deparse_old = B::Deparse->new("-l", "-sC");
3773 # print $deparse_old->coderef2text(\&baz);
3774 # exit 1;
3775 my $deparse = __PACKAGE__->new("-l", "-c", "-sC");
3776 my $info = $deparse->coderef2info(\&baz);
3777 import Data::Printer colored => 0;
3778 Data::Printer::p($info);
3779 print "\n", '=' x 30, "\n";
3780 # print $deparse->indent($deparse->deparse_subname('fib')->{text});
3781 # print "\n", '=' x 30, "\n";
3782 # print "\n", '-' x 30, "\n";
3783 while (my($key, $value) = each %{$deparse->{optree}}) {
3784 my $parent_op_name = 'undef';
3785 if ($value->{parent}) {
3786 my $parent = $deparse->{optree}{$value->{parent}};
3787 $parent_op_name = $parent->{op}->name if $parent->{op};
3788 }
3789 printf("0x%x %s/%s of %s |\n%s",
3790 $key, $value->{op}->name, $value->{type},
3791 $parent_op_name, $deparse->indent($value->{text}));
3792 printf " ## line %s\n", $value->{cop} ? $value->{cop}->line : 'undef';
3793 print '-' x 30, "\n";
3794 }
3795 }
3796  
3797 # FIXME:
3798 # Different in 5.20. Go over differences to see if okay in 5.20.
3799 sub pp_chdir {
3800 my ($self, $op, $cx) = @_;
3801 if (($op->flags & (OPf_SPECIAL|OPf_KIDS)) == (OPf_SPECIAL|OPf_KIDS)) {
3802 my $kw = $self->keyword("chdir");
3803 my $kid = $self->const_sv($op->first)->PV;
3804 my $code = $kw
3805 . ($cx >= 16 || $self->{'parens'} ? "($kid)" : " $kid");
3806 maybe_targmy(@_, sub { $_[3] }, $code);
3807 } else {
3808 maybe_targmy(@_, \&unop, "chdir")
3809 }
3810 }
3811  
3812 sub pp_entereval
3813 {
3814 unop(
3815 @_,
3816 $_[1]->private & OPpEVAL_BYTES ? 'evalbytes' : "eval"
3817 )
3818 }
3819  
3820  
3821 # Not in Perl 5.20 and presumeably < 5.20. No harm in adding to 5.20?
3822 *pp_ncomplement = *pp_complement;
3823 sub pp_scomplement { maybe_targmy(@_, \&pfixop, "~.", 21) }
3824  
3825 1;