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

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