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

filter: Statementstmt 0.0% 0 / 2037 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 / 4166 CC 404 SCAR 45.2
CC
404
Cov
0%
ambient_pragmas
506.0
loop_common
462.0
pp_multideref
462.0
CRAP 92.0
1 # B::DeparseTree::P522.pm
2 # Copyright (c) 1998-2000, 2002, 2003, 2004, 2005, 2006 Stephen McCamant.
3 # Copyright (c) 2015, 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.22;
16  
17 use rlib '../..';
18  
19 package B::DeparseTree::P522;
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 RXf_PMf_CHARSET RXf_PMf_KEEPCOPY 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', {});
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', {});
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,
510 $cx, 16);
511 return info_from_list($op, $self, \@texts, '',
512 'maybe_local_array', {});
513 }
514 } else {
515 return info_from_text($op, $self, $text, 'maybe_local', {});
516 }
517 }
518  
519 # FIXME: This is weird. Regularize var_info
520 sub maybe_local {
521 my($self, $op, $cx, $var_info) = @_;
522 $var_info->{parent} = $$op;
523 return maybe_local_str($self, $op, $cx, $var_info->{text});
524 }
525  
526 sub maybe_my {
527 my $self = shift;
528 my($op, $cx, $text, $forbid_parens) = @_;
529 if ($op->private & OPpLVAL_INTRO and not $self->{'avoid_local'}{$$op}) {
530 my $my_str = $op->private & OPpPAD_STATE
531 ? $self->keyword("state")
532 : "my";
533 if ($forbid_parens || want_scalar($op)) {
534 return info_from_list($op, $self, [$my_str, $text], ' ',
535 'maybe_my_no_parens', {});
536 } else {
537 return info_from_list($op, $self, [$my_str, $text], ' ',
538 'maybe_my_parens',
539 {maybe_parens => [$self, $cx, 16]});
540 }
541 } else {
542 return info_from_text($op, $self, $text, 'maybe_my_avoid_local', {});
543 }
544 }
545  
546 # The following OPs don't have functions:
547  
548 # pp_padany -- does not exist after parsing
549  
550 sub AUTOLOAD {
551 if ($AUTOLOAD =~ s/^.*::pp_//) {
552 warn "unexpected OP_".uc $AUTOLOAD;
553 } else {
554 Carp::confess "Undefined subroutine $AUTOLOAD called";
555 }
556 }
557  
558 sub DESTROY {} # Do not AUTOLOAD
559  
560 # The BEGIN {} is used here because otherwise this code isn't executed
561 # when you run B::Deparse on itself.
562 my %globalnames;
563 BEGIN { map($globalnames{$_}++, "SIG", "STDIN", "STDOUT", "STDERR", "INC",
564 "ENV", "ARGV", "ARGVOUT", "_"); }
565  
566 sub gv_name {
567 my $self = shift;
568 my $gv = shift;
569 my $raw = shift;
570 # Carp::confess() unless ref($gv) eq "B::GV";
571 my $cv = $gv->FLAGS & SVf_ROK ? $gv->RV : 0;
572 my $stash = ($cv || $gv)->STASH->NAME;
573 my $name = $raw
574 ? $cv ? $cv->NAME_HEK || $cv->GV->NAME : $gv->NAME
575 : $cv
576 ? B::safename($cv->NAME_HEK || $cv->GV->NAME)
577 : $gv->SAFENAME;
578 if ($stash eq 'main' && $name =~ /^::/) {
579 $stash = '::';
580 }
581 elsif (($stash eq 'main'
582 && ($globalnames{$name} || $name =~ /^[^A-Za-z_:]/))
583 or ($stash eq $self->{'curstash'} && !$globalnames{$name}
584 && ($stash eq 'main' || $name !~ /::/))
585 )
586 {
587 $stash = "";
588 } else {
589 $stash = $stash . "::";
590 }
591 if (!$raw and $name =~ /^(\^..|{)/) {
592 $name = "{$name}"; # ${^WARNING_BITS}, etc and ${
593 }
594 return $stash . $name;
595 }
596  
597 # Return the name to use for a stash variable.
598 # If a lexical with the same name is in scope, or
599 # if strictures are enabled, it may need to be
600 # fully-qualified.
601 sub stash_variable {
602 my ($self, $prefix, $name, $cx) = @_;
603  
604 $name = $self->info2str($name);
605 return "$prefix$name" if $name =~ /::/;
606  
607 unless ($prefix eq '$' || $prefix eq '@' || $prefix eq '&' || #'
608 $prefix eq '%' || $prefix eq '$#') {
609 return "$prefix$name";
610 }
611  
612 if ($name =~ /^[^[:alpha:]_+-]$/) {
613 if (defined $cx && $cx == 26) {
614 if ($prefix eq '@') {
615 return "$prefix\{$name}";
616 }
617 elsif ($name eq '#') { return '${#}' } # "${#}a" vs "$#a"
618 }
619 if ($prefix eq '$#') {
620 return "\$#{$name}";
621 }
622 }
623  
624 return $prefix . $self->maybe_qualify($prefix, $name);
625 }
626  
627 sub lex_in_scope {
628 my ($self, $name, $our) = @_;
629 substr $name, 0, 0, = $our ? 'o' : 'm'; # our/my
630 $self->populate_curcvlex() if !defined $self->{'curcvlex'};
631  
632 return 0 if !defined($self->{'curcop'});
633 my $seq = $self->{'curcop'}->cop_seq;
634 return 0 if !exists $self->{'curcvlex'}{$name};
635 for my $a (@{$self->{'curcvlex'}{$name}}) {
636 my ($st, $en) = @$a;
637 return 1 if $seq > $st && $seq <= $en;
638 }
639 return 0;
640 }
641  
642 sub populate_curcvlex {
643 my $self = shift;
644 for (my $cv = $self->{'curcv'}; class($cv) eq "CV"; $cv = $cv->OUTSIDE) {
645 my $padlist = $cv->PADLIST;
646 # an undef CV still in lexical chain
647 next if class($padlist) eq "SPECIAL";
648 my @padlist = $padlist->ARRAY;
649 my @ns = $padlist[0]->ARRAY;
650  
651 for (my $i=0; $i<@ns; ++$i) {
652 next if class($ns[$i]) eq "SPECIAL";
653 if (class($ns[$i]) eq "PV") {
654 # Probably that pesky lexical @_
655 next;
656 }
657 my $name = $ns[$i]->PVX;
658 next unless defined $name;
659 my ($seq_st, $seq_en) =
660 ($ns[$i]->FLAGS & SVf_FAKE)
661 ? (0, 999999)
662 : ($ns[$i]->COP_SEQ_RANGE_LOW, $ns[$i]->COP_SEQ_RANGE_HIGH);
663  
664 push @{$self->{'curcvlex'}{
665 ($ns[$i]->FLAGS & SVpad_OUR ? 'o' : 'm') . $name
666 }}, [$seq_st, $seq_en, $ns[$i]];
667 }
668 }
669 }
670  
671 sub find_scope_st { ((find_scope(@_))[0]); }
672 sub find_scope_en { ((find_scope(@_))[1]); }
673  
674 # Recurses down the tree, looking for pad variable introductions and COPs
675 sub find_scope {
676 my ($self, $op, $scope_st, $scope_en) = @_;
677 carp("Undefined op in find_scope") if !defined $op;
678 return ($scope_st, $scope_en) unless $op->flags & OPf_KIDS;
679  
680 my @queue = ($op);
681 while(my $op = shift @queue ) {
682 for (my $o=$op->first; $$o; $o=$o->sibling) {
683 if ($o->name =~ /^pad.v$/ && $o->private & OPpLVAL_INTRO) {
684 my $s = int($self->padname_sv($o->targ)->COP_SEQ_RANGE_LOW);
685 my $e = $self->padname_sv($o->targ)->COP_SEQ_RANGE_HIGH;
686 $scope_st = $s if !defined($scope_st) || $s < $scope_st;
687 $scope_en = $e if !defined($scope_en) || $e > $scope_en;
688 return ($scope_st, $scope_en);
689 }
690 elsif (is_state($o)) {
691 my $c = $o->cop_seq;
692 $scope_st = $c if !defined($scope_st) || $c < $scope_st;
693 $scope_en = $c if !defined($scope_en) || $c > $scope_en;
694 return ($scope_st, $scope_en);
695 }
696 elsif ($o->flags & OPf_KIDS) {
697 unshift (@queue, $o);
698 }
699 }
700 }
701  
702 return ($scope_st, $scope_en);
703 }
704  
705 # Returns a list of subs which should be inserted before the COP
706 sub cop_subs {
707 my ($self, $op, $out_seq) = @_;
708 my $seq = $op->cop_seq;
709 # If we have nephews, then our sequence number indicates
710 # the cop_seq of the end of some sort of scope.
711 if (class($op->sibling) ne "NULL" && $op->sibling->flags & OPf_KIDS
712 and my $nseq = $self->find_scope_st($op->sibling) ) {
713 $seq = $nseq;
714 }
715 $seq = $out_seq if defined($out_seq) && $out_seq < $seq;
716 return $self->seq_subs($seq);
717 }
718  
719 my %feature_keywords = (
720 # keyword => 'feature',
721 state => 'state',
722 say => 'say',
723 given => 'switch',
724 when => 'switch',
725 default => 'switch',
726 break => 'switch',
727 evalbytes=>'evalbytes',
728 __SUB__ => '__SUB__',
729 fc => 'fc',
730 );
731  
732 # keywords that are strong and also have a prototype
733 #
734 my %strong_proto_keywords = map { $_ => 1 } qw(
735 pos
736 prototype
737 scalar
738 study
739 undef
740 );
741  
742 sub keyword {
743 my $self = shift;
744 my $name = shift;
745 return $name if $name =~ /^CORE::/; # just in case
746 if (exists $feature_keywords{$name}) {
747 my $hh;
748 my $hints = $self->{hints} & $feature::hint_mask;
749 if ($hints && $hints != $feature::hint_mask) {
750 $hh = _features_from_bundle($hints);
751 }
752 elsif ($hints) { $hh = $self->{'hinthash'} }
753 return "CORE::$name"
754 if !$hh
755 || !$hh->{"feature_$feature_keywords{$name}"}
756 }
757 if ($strong_proto_keywords{$name}
758 || ($name !~ /^(?:chom?p|do|exec|glob|s(?:elect|ystem))\z/
759 && !defined eval{prototype "CORE::$name"})
760 ) { return $name }
761 if (
762 exists $self->{subs_declared}{$name}
763 or
764 exists &{"$self->{curstash}::$name"}
765 ) {
766 return "CORE::$name"
767 }
768 return $name;
769 }
770  
771 sub pp_not
772 {
773 my($self, $op, $cx) = @_;
774 if ($cx <= 4) {
775 $self->listop($op, $cx, "not", $op->first);
776 } else {
777 $self->pfixop($op, $cx, "!", 21);
778 }
779 }
780  
781 # Note: maybe_local things can't be moved to PP yet.
782 sub pp_pos { maybe_local(@_, unop(@_, "pos")) }
783 sub pp_sin { maybe_targmy(@_, \&unop, "sin") }
784 sub pp_cos { maybe_targmy(@_, \&unop, "cos") }
785 sub pp_rand { maybe_targmy(@_, \&unop, "rand") }
786 sub pp_srand { unop(@_, "srand") }
787 sub pp_exp { maybe_targmy(@_, \&unop, "exp") }
788 sub pp_log { maybe_targmy(@_, \&unop, "log") }
789 sub pp_sqrt { maybe_targmy(@_, \&unop, "sqrt") }
790 sub pp_int { maybe_targmy(@_, \&unop, "int") }
791 sub pp_hex { maybe_targmy(@_, \&unop, "hex") }
792 sub pp_oct { maybe_targmy(@_, \&unop, "oct") }
793 sub pp_abs { maybe_targmy(@_, \&unop, "abs") }
794  
795 sub pp_length { maybe_targmy(@_, \&unop, "length") }
796 sub pp_ord { maybe_targmy(@_, \&unop, "ord") }
797 sub pp_chr { maybe_targmy(@_, \&unop, "chr") }
798  
799 sub pp_each { unop(@_, "each") }
800 sub pp_values { unop(@_, "values") }
801 sub pp_keys { unop(@_, "keys") }
802 { no strict 'refs'; *{"pp_r$_"} = *{"pp_$_"} for qw< keys each values >; }
803 sub pp_boolkeys
804 {
805 # no name because its an optimisation op that has no keyword
806 unop(@_,"");
807 }
808 sub pp_aeach { unop(@_, "each") }
809 sub pp_avalues { unop(@_, "values") }
810 sub pp_akeys { unop(@_, "keys") }
811 sub pp_pop { unop(@_, "pop") }
812 sub pp_shift { unop(@_, "shift") }
813  
814 sub pp_caller { unop(@_, "caller") }
815 sub pp_reset { unop(@_, "reset") }
816 sub pp_exit { unop(@_, "exit") }
817 sub pp_prototype { unop(@_, "prototype") }
818  
819 sub pp_close { unop(@_, "close") }
820 sub pp_fileno { unop(@_, "fileno") }
821 sub pp_umask { unop(@_, "umask") }
822 sub pp_untie { unop(@_, "untie") }
823 sub pp_tied { unop(@_, "tied") }
824 sub pp_dbmclose { unop(@_, "dbmclose") }
825 sub pp_getc { unop(@_, "getc") }
826 sub pp_eof { unop(@_, "eof") }
827 sub pp_tell { unop(@_, "tell") }
828 sub pp_getsockname { unop(@_, "getsockname") }
829 sub pp_getpeername { unop(@_, "getpeername") }
830  
831 sub pp_chroot { maybe_targmy(@_, \&unop, "chroot") }
832 sub pp_readlink { unop(@_, "readlink") }
833 sub pp_rmdir { maybe_targmy(@_, \&unop, "rmdir") }
834 sub pp_readdir { unop(@_, "readdir") }
835 sub pp_telldir { unop(@_, "telldir") }
836 sub pp_rewinddir { unop(@_, "rewinddir") }
837 sub pp_closedir { unop(@_, "closedir") }
838 sub pp_getpgrp { maybe_targmy(@_, \&unop, "getpgrp") }
839 sub pp_localtime { unop(@_, "localtime") }
840 sub pp_gmtime { unop(@_, "gmtime") }
841 sub pp_alarm { unop(@_, "alarm") }
842 sub pp_sleep { maybe_targmy(@_, \&unop, "sleep") }
843  
844 sub pp_dofile
845 {
846 my $code = unop(@_, "do", 1); # llafr does not apply
847 if ($code =~ s/^((?:CORE::)?do) \{/$1({/) { $code .= ')' }
848 $code;
849 }
850  
851 sub pp_ghbyname { unop(@_, "gethostbyname") }
852 sub pp_gnbyname { unop(@_, "getnetbyname") }
853 sub pp_gpbyname { unop(@_, "getprotobyname") }
854 sub pp_shostent { unop(@_, "sethostent") }
855 sub pp_snetent { unop(@_, "setnetent") }
856 sub pp_sprotoent { unop(@_, "setprotoent") }
857 sub pp_sservent { unop(@_, "setservent") }
858 sub pp_gpwnam { unop(@_, "getpwnam") }
859 sub pp_gpwuid { unop(@_, "getpwuid") }
860 sub pp_ggrnam { unop(@_, "getgrnam") }
861 sub pp_ggrgid { unop(@_, "getgrgid") }
862  
863 sub pp_lock { unop(@_, "lock") }
864  
865 sub pp_continue { unop(@_, "continue"); }
866 sub pp_break { unop(@_, "break"); }
867  
868 sub givwhen
869 {
870 my($self, $op, $cx, $givwhen) = @_;
871  
872 my $enterop = $op->first;
873 my ($head, $block);
874 if ($enterop->flags & OPf_SPECIAL) {
875 $head = $self->keyword("default");
876 $block = $self->deparse($enterop->first, 0, $enterop, $op);
877 }
878 else {
879 my $cond = $enterop->first;
880 my $cond_str = $self->deparse($cond, 1, $enterop, $op);
881 $head = "$givwhen ($cond_str)";
882 $block = $self->deparse($cond->sibling, 0, $enterop, $op);
883 }
884  
885 return info_from_list($op, $self, [$head, "{",
886 "\n\t", $block->{text}, "\n\b",
887 "}\cK"], '', 'givwhen',
888 {body => [$block]});
889 }
890  
891 sub pp_leavegiven { givwhen(@_, $_[0]->keyword("given")); }
892 sub pp_leavewhen { givwhen(@_, $_[0]->keyword("when")); }
893  
894 sub pp_delete
895 {
896 my($self, $op, $cx) = @_;
897 my $arg;
898 my ($info, $body, $type);
899 if ($op->private & OPpSLICE) {
900 if ($op->flags & OPf_SPECIAL) {
901 # Deleting from an array, not a hash
902 $info = $self->pp_aslice($op->first, 16);
903 $type = 'delete_slice';
904 }
905 } else {
906 if ($op->flags & OPf_SPECIAL) {
907 # Deleting from an array, not a hash
908 $info = $self->pp_aelem($op->first, 16);
909 $type = 'delete_array'
910 } else {
911 $info = $self->pp_helem($op->first, 16);
912 $type = 'delete_hash';
913 }
914 }
915 my @texts = $self->maybe_parens_func("delete",
916 $info->{text}, $cx, 16);
917 return info_from_list($op, $self, \@texts, '', $type, {body => [$info]});
918 }
919  
920 sub pp_require
921 {
922 my($self, $op, $cx) = @_;
923 my $opname = $op->flags & OPf_SPECIAL ? 'CORE::require' : 'require';
924 if (class($op) eq "UNOP" and $op->first->name eq "const"
925 and $op->first->private & OPpCONST_BARE) {
926 my $name = $self->const_sv($op->first)->PV;
927 $name =~ s[/][::]g;
928 $name =~ s/\.pm//g;
929 return info_from_list($op, $self, [$opname, $name], ' ',
930 'require',
931 {maybe_parens => [$self, $cx, 16]});
932 } else {
933 return $self->unop(
934 $op, $cx,
935 $op->first->name eq 'const'
936 && $op->first->private & OPpCONST_NOVER
937 ? "no"
938 : $opname,
939 1, # llafr does not apply
940 );
941 }
942 Carp::confess("unhandled condition in pp_require");
943 }
944  
945 sub pp_scalar
946 {
947 my($self, $op, $cx) = @_;
948 my $kid = $op->first;
949 if (not null $kid->sibling) {
950 # XXX Was a here-doc
951 return $self->dquote($op);
952 }
953 $self->unop($op, $cx, "scalar");
954 }
955  
956  
957 sub padval
958 {
959 my $self = shift;
960 my $targ = shift;
961 return $self->{'curcv'}->PADLIST->ARRAYelt(1)->ARRAYelt($targ);
962 }
963  
964 sub anon_hash_or_list
965 {
966 my $self = shift;
967 my($op, $cx) = @_;
968  
969 my $name = $op->name;
970 my($pre, $post) = @{{"anonlist" => ["[","]"],
971 "anonhash" => ["{","}"]}->{$name}};
972 my($expr, @exprs);
973 my $other_ops = [$op->first];
974 $op = $op->first->sibling; # skip pushmark
975 for (; !null($op); $op = $op->sibling) {
976 $expr = $self->deparse($op, 6, $op);
977 push @exprs, [$expr, $op];
978 }
979 if ($pre eq "{" and $cx < 1) {
980 # Disambiguate that it's not a block
981 $pre = "+{";
982 }
983 my $texts = [$pre, $self->combine(", ", \@exprs), $post];
984 return info_from_list($op, $self, $texts, '', $name,
985 {body => \@exprs,
986 other_ops => $other_ops
987 });
988 }
989  
990 sub pp_anonlist {
991 my $self = shift;
992 my ($op, $cx) = @_;
993 if ($op->flags & OPf_SPECIAL) {
994 return $self->anon_hash_or_list($op, $cx);
995 }
996 warn "Unexpected op pp_" . $op->name() . " without OPf_SPECIAL";
997 return info_from_text($op, $self, 'XXX', 'bad_anonlist', {});
998 }
999  
1000 *pp_anonhash = \&pp_anonlist;
1001  
1002 sub e_anoncode($$)
1003 {
1004 my ($self, $info) = @_;
1005 my $sub_info = $self->deparse_sub($info->{code});
1006 return info_from_list($sub_info->{op}, $self,
1007 ['sub', $sub_info->{text}], ' ', 'e_anoncode',
1008 {body=> [$sub_info]});
1009 }
1010  
1011 sub pp_refgen
1012 {
1013 my($self, $op, $cx) = @_;
1014 my $kid = $op->first;
1015 if ($kid->name eq "null") {
1016 my $other_ops = [$kid];
1017 my $anoncode = $kid = $kid->first;
1018 if ($anoncode->name eq "anonconst") {
1019 $anoncode = $anoncode->first->first->sibling;
1020 }
1021 if ($anoncode->name eq "anoncode"
1022 or !null($anoncode = $kid->sibling) and
1023 $anoncode->name eq "anoncode") {
1024 return $self->e_anoncode({ code => $self->padval($anoncode->targ) });
1025 } elsif ($kid->name eq "pushmark") {
1026 my $sib_name = $kid->sibling->name;
1027 if ($sib_name eq 'entersub') {
1028 my $kid_info = $self->deparse($kid->sibling, 1, $op);
1029 # Always show parens for \(&func()), but only with -p otherwise
1030 my @texts = ('\\', $kid_info->{text});
1031 if ($self->{'parens'} or $kid->sibling->private & OPpENTERSUB_AMPER) {
1032 @texts = ('(', "\\", $kid_info->{text}, ')');
1033 }
1034 return info_from_list($op, $self, \@texts, '', 'refgen_entersub',
1035 {body => [$kid_info],
1036 other_ops => $other_ops});
1037 }
1038 }
1039 }
1040 local $self->{'in_refgen'} = 1;
1041 $self->pfixop($op, $cx, "\\", 20);
1042 }
1043  
1044 sub pp_srefgen { pp_refgen(@_) }
1045  
1046 sub pp_readline {
1047 my $self = shift;
1048 my($op, $cx) = @_;
1049 my $kid = $op->first;
1050 $kid = $kid->first if $kid->name eq "rv2gv"; # <$fh>
1051 if (is_scalar($kid)) {
1052 my $body = [$self->deparse($kid, 1, $op)];
1053 return info_from_list($op, $self, ['<', $body->[0]{text}, '>'], '',
1054 'readline_scalar', {body=>$body});
1055 }
1056 return $self->unop($op, $cx, "readline");
1057 }
1058  
1059 sub pp_rcatline {
1060 my $self = shift;
1061 my($op) = @_;
1062 return info_from_list($op, $self, ["<", $self->gv_name($self->gv_or_padgv($op)), ">"],
1063 '', 'rcatline', {});
1064 }
1065  
1066 # Unary operators that can occur as pseudo-listops inside double quotes
1067 sub dq_unop
1068 {
1069 my($self, $op, $cx, $name, $prec, $flags) = (@_, 0, 0);
1070 my $kid;
1071 if ($op->flags & OPf_KIDS) {
1072 my $other_ops = undef;
1073 $kid = $op->first;
1074 if (not null $kid->sibling) {
1075 # If there's more than one kid, the first is an ex-pushmark.
1076 $other_ops = [$kid];
1077 $kid = $kid->sibling;
1078 }
1079 my $info = $self->maybe_parens_unop($name, $kid, $cx, $op);
1080 $info->{other_ops} = $other_ops if $other_ops;
1081 return $info;
1082 } else {
1083 my @texts = ($name);
1084 push @texts, '(', ')' if $op->flags & OPf_SPECIAL;
1085 return info_from_list($op, $self, \@texts, '', 'dq', {});
1086 }
1087 Carp::confess("unhandled condition in dq_unop");
1088 }
1089  
1090 sub pp_ucfirst { dq_unop(@_, "ucfirst") }
1091 sub pp_lcfirst { dq_unop(@_, "lcfirst") }
1092 sub pp_uc { dq_unop(@_, "uc") }
1093 sub pp_lc { dq_unop(@_, "lc") }
1094 sub pp_quotemeta { maybe_targmy(@_, \&dq_unop, "quotemeta") }
1095 sub pp_fc { dq_unop(@_, "fc") }
1096  
1097 # loop expressions
1098 sub loopex
1099 {
1100 my ($self, $op, $cx, $name) = @_;
1101 my $opts = {maybe_parens => [$self, $cx, 7]};
1102 my ($type, $body);
1103 if (class($op) eq "PVOP") {
1104 return info_from_list($op, $self, [$name, $op->pv], ' ', 'loopex_pvop', {});
1105 } elsif (class($op) eq "OP") {
1106 # no-op
1107 $type = 'loopex_op';
1108 return info_from_text($op, $self, $name, 'loopex_op', $opts);
1109 } elsif (class($op) eq "UNOP") {
1110 (my $kid_info = $self->deparse($op->first, 7, $op)) =~ s/^\cS//;
1111 $opts->{body} = [$kid_info];
1112 return info_from_list($op, $self, [$name, $op->pv], ' ', 'loopex_unop', $opts);
1113 } else {
1114 return info_from_text($op, $self, $name, 'loopex', $opts);
1115 }
1116 Carp::confess("unhandled condition in lopex");
1117 }
1118  
1119 sub pp_last { loopex(@_, "last") }
1120 sub pp_next { loopex(@_, "next") }
1121 sub pp_redo { loopex(@_, "redo") }
1122 sub pp_goto { loopex(@_, "goto") }
1123 sub pp_dump { loopex(@_, "CORE::dump") }
1124  
1125 sub ftst
1126 {
1127 my($self, $op, $cx, $name) = @_;
1128 if (class($op) eq "UNOP") {
1129 # Genuine '-X' filetests are exempt from the LLAFR, but not
1130 # l?stat()
1131 if ($name =~ /^-/) {
1132 (my $kid = $self->deparse($op->first, 16, $op)) =~ s/^\cS//;
1133 return info_from_list($op, $self, [$name, $kid->{text}], ' ',
1134 'ftst_unop_dash',
1135 {body => [$kid],
1136 maybe_parens => [$self, $cx, 16]});
1137 }
1138 return $self->maybe_parens_unop($name, $op->first, $cx, $op);
1139 } elsif (class($op) =~ /^(SV|PAD)OP$/) {
1140 my @list = $self->maybe_parens_func($name, $self->pp_gv($op, 1), $cx, 16);
1141 return info_from_list($op, $self, \@list, ' ', 'ftst_list', {});
1142 } else { # I don't think baseop filetests ever survive ck_ftst, but...
1143 return info_from_text($op, $self, $name, 'unop', {});
1144 }
1145 }
1146  
1147 sub pp_lstat { ftst(@_, "lstat") }
1148 sub pp_stat { ftst(@_, "stat") }
1149 sub pp_ftrread { ftst(@_, "-R") }
1150 sub pp_ftrwrite { ftst(@_, "-W") }
1151 sub pp_ftrexec { ftst(@_, "-X") }
1152 sub pp_fteread { ftst(@_, "-r") }
1153 sub pp_ftewrite { ftst(@_, "-w") }
1154 sub pp_fteexec { ftst(@_, "-x") }
1155 sub pp_ftis { ftst(@_, "-e") }
1156 sub pp_fteowned { ftst(@_, "-O") }
1157 sub pp_ftrowned { ftst(@_, "-o") }
1158 sub pp_ftzero { ftst(@_, "-z") }
1159 sub pp_ftsize { ftst(@_, "-s") }
1160 sub pp_ftmtime { ftst(@_, "-M") }
1161 sub pp_ftatime { ftst(@_, "-A") }
1162 sub pp_ftctime { ftst(@_, "-C") }
1163 sub pp_ftsock { ftst(@_, "-S") }
1164 sub pp_ftchr { ftst(@_, "-c") }
1165 sub pp_ftblk { ftst(@_, "-b") }
1166 sub pp_ftfile { ftst(@_, "-f") }
1167 sub pp_ftdir { ftst(@_, "-d") }
1168 sub pp_ftpipe { ftst(@_, "-p") }
1169 sub pp_ftlink { ftst(@_, "-l") }
1170 sub pp_ftsuid { ftst(@_, "-u") }
1171 sub pp_ftsgid { ftst(@_, "-g") }
1172 sub pp_ftsvtx { ftst(@_, "-k") }
1173 sub pp_fttty { ftst(@_, "-t") }
1174 sub pp_fttext { ftst(@_, "-T") }
1175 sub pp_ftbinary { ftst(@_, "-B") }
1176  
1177 sub SWAP_CHILDREN () { 1 }
1178 sub ASSIGN () { 2 } # has OP= variant
1179 sub LIST_CONTEXT () { 4 } # Assignment is in list context
1180  
1181 my(%left, %right);
1182  
1183 sub assoc_class {
1184 my $op = shift;
1185 my $name = $op->name;
1186 if ($name eq "concat" and $op->first->name eq "concat") {
1187 # avoid spurious '=' -- see comment in pp_concat
1188 return "concat";
1189 }
1190 if ($name eq "null" and class($op) eq "UNOP"
1191 and $op->first->name =~ /^(and|x?or)$/
1192 and null $op->first->sibling)
1193 {
1194 # Like all conditional constructs, OP_ANDs and OP_ORs are topped
1195 # with a null that's used as the common end point of the two
1196 # flows of control. For precedence purposes, ignore it.
1197 # (COND_EXPRs have these too, but we don't bother with
1198 # their associativity).
1199 return assoc_class($op->first);
1200 }
1201 return $name . ($op->flags & OPf_STACKED ? "=" : "");
1202 }
1203  
1204 # Left associative operators, like '+', for which
1205 # $a + $b + $c is equivalent to ($a + $b) + $c
1206  
1207 BEGIN {
1208 %left = ('multiply' => 19, 'i_multiply' => 19,
1209 'divide' => 19, 'i_divide' => 19,
1210 'modulo' => 19, 'i_modulo' => 19,
1211 'repeat' => 19,
1212 'add' => 18, 'i_add' => 18,
1213 'subtract' => 18, 'i_subtract' => 18,
1214 'concat' => 18,
1215 'left_shift' => 17, 'right_shift' => 17,
1216 'bit_and' => 13,
1217 'bit_or' => 12, 'bit_xor' => 12,
1218 'and' => 3,
1219 'or' => 2, 'xor' => 2,
1220 );
1221 }
1222  
1223 sub deparse_binop_left {
1224 my $self = shift;
1225 my($op, $left, $prec) = @_;
1226 if ($left{assoc_class($op)} && $left{assoc_class($left)}
1227 and $left{assoc_class($op)} == $left{assoc_class($left)})
1228 {
1229 return $self->deparse($left, $prec - .00001, $op);
1230 } else {
1231 return $self->deparse($left, $prec, $op);
1232 }
1233 }
1234  
1235 # Right associative operators, like '=', for which
1236 # $a = $b = $c is equivalent to $a = ($b = $c)
1237  
1238 BEGIN {
1239 %right = ('pow' => 22,
1240 'sassign=' => 7, 'aassign=' => 7,
1241 'multiply=' => 7, 'i_multiply=' => 7,
1242 'divide=' => 7, 'i_divide=' => 7,
1243 'modulo=' => 7, 'i_modulo=' => 7,
1244 'repeat=' => 7,
1245 'add=' => 7, 'i_add=' => 7,
1246 'subtract=' => 7, 'i_subtract=' => 7,
1247 'concat=' => 7,
1248 'left_shift=' => 7, 'right_shift=' => 7,
1249 'bit_and=' => 7,
1250 'bit_or=' => 7, 'bit_xor=' => 7,
1251 'andassign' => 7,
1252 'orassign' => 7,
1253 );
1254 }
1255  
1256 sub deparse_binop_right {
1257 my $self = shift;
1258 my($op, $right, $prec) = @_;
1259 if ($right{assoc_class($op)} && $right{assoc_class($right)}
1260 and $right{assoc_class($op)} == $right{assoc_class($right)})
1261 {
1262 return $self->deparse($right, $prec - .00001, $op);
1263 } else {
1264 return $self->deparse($right, $prec, $op);
1265 }
1266 }
1267  
1268 sub pp_add { maybe_targmy(@_, \&binop, "+", 18, ASSIGN) }
1269 sub pp_multiply { maybe_targmy(@_, \&binop, "*", 19, ASSIGN) }
1270 sub pp_subtract { maybe_targmy(@_, \&binop, "-",18, ASSIGN) }
1271 sub pp_divide { maybe_targmy(@_, \&binop, "/", 19, ASSIGN) }
1272 sub pp_modulo { maybe_targmy(@_, \&binop, "%", 19, ASSIGN) }
1273 sub pp_i_add { maybe_targmy(@_, \&binop, "+", 18, ASSIGN) }
1274 sub pp_i_multiply { maybe_targmy(@_, \&binop, "*", 19, ASSIGN) }
1275 sub pp_i_subtract { maybe_targmy(@_, \&binop, "-", 18, ASSIGN) }
1276 sub pp_i_divide { maybe_targmy(@_, \&binop, "/", 19, ASSIGN) }
1277 sub pp_i_modulo { maybe_targmy(@_, \&binop, "%", 19, ASSIGN) }
1278 sub pp_pow { maybe_targmy(@_, \&binop, "**", 22, ASSIGN) }
1279  
1280 sub pp_left_shift { maybe_targmy(@_, \&binop, "<<", 17, ASSIGN) }
1281 sub pp_right_shift { maybe_targmy(@_, \&binop, ">>", 17, ASSIGN) }
1282 sub pp_bit_and { maybe_targmy(@_, \&binop, "&", 13, ASSIGN) }
1283 sub pp_bit_or { maybe_targmy(@_, \&binop, "|", 12, ASSIGN) }
1284 sub pp_bit_xor { maybe_targmy(@_, \&binop, "^", 12, ASSIGN) }
1285  
1286 sub pp_eq { binop(@_, "==", 14) }
1287 sub pp_ne { binop(@_, "!=", 14) }
1288 sub pp_lt { binop(@_, "<", 15) }
1289 sub pp_gt { binop(@_, ">", 15) }
1290 sub pp_ge { binop(@_, ">=", 15) }
1291 sub pp_le { binop(@_, "<=", 15) }
1292 sub pp_ncmp { binop(@_, "<=>", 14) }
1293 sub pp_i_eq { binop(@_, "==", 14) }
1294 sub pp_i_ne { binop(@_, "!=", 14) }
1295 sub pp_i_lt { binop(@_, "<", 15) }
1296 sub pp_i_gt { binop(@_, ">", 15) }
1297 sub pp_i_ge { binop(@_, ">=", 15) }
1298 sub pp_i_le { binop(@_, "<=", 15) }
1299 sub pp_i_ncmp { binop(@_, "<=>", 14) }
1300  
1301 sub pp_seq { binop(@_, "eq", 14) }
1302 sub pp_sne { binop(@_, "ne", 14) }
1303 sub pp_slt { binop(@_, "lt", 15) }
1304 sub pp_sgt { binop(@_, "gt", 15) }
1305 sub pp_sge { binop(@_, "ge", 15) }
1306 sub pp_sle { binop(@_, "le", 15) }
1307 sub pp_scmp { binop(@_, "cmp", 14) }
1308  
1309 sub pp_sassign { binop(@_, "=", 7, SWAP_CHILDREN) }
1310 sub pp_aassign { binop(@_, "=", 7, SWAP_CHILDREN | LIST_CONTEXT) }
1311  
1312 sub pp_smartmatch {
1313 my ($self, $op, $cx) = @_;
1314 if ($op->flags & OPf_SPECIAL) {
1315 return $self->deparse($op->last, $cx, $op);
1316 }
1317 else {
1318 binop(@_, "~~", 14);
1319 }
1320 }
1321  
1322 sub bin_info_join($$$$$$$) {
1323 my ($self, $op, $lhs, $rhs, $mid, $sep, $type) = @_;
1324 my $texts = [$lhs->{text}, $mid, $rhs->{text}];
1325 return info_from_list($op, $self, $texts, ' ', $type, {})
1326 }
1327  
1328 sub bin_info_join_maybe_parens($$$$$$$$$) {
1329 my ($self, $op, $lhs, $rhs, $mid, $sep, $cx, $prec, $type) = @_;
1330 my $info = bin_info_join($self, $op, $lhs, $rhs, $mid, $sep, $type);
1331 $info->{text} = $self->maybe_parens($info->{text}, $cx, $prec);
1332 return $info;
1333 }
1334  
1335 # '.' is special because concats-of-concats are optimized to save copying
1336 # by making all but the first concat stacked. The effect is as if the
1337 # programmer had written '($a . $b) .= $c', except legal.
1338 sub pp_concat { maybe_targmy(@_, \&real_concat) }
1339 sub real_concat {
1340 my $self = shift;
1341 my($op, $cx) = @_;
1342 my $left = $op->first;
1343 my $right = $op->last;
1344 my $eq = "";
1345 my $prec = 18;
1346 if ($op->flags & OPf_STACKED and $op->first->name ne "concat") {
1347 $eq = "=";
1348 $prec = 7;
1349 }
1350 my $lhs = $self->deparse_binop_left($op, $left, $prec);
1351 my $rhs = $self->deparse_binop_right($op, $right, $prec);
1352 return $self->bin_info_join_maybe_parens($op, $lhs, $rhs, ".$eq", " ", $cx, $prec,
1353 'real_concat');
1354 }
1355  
1356 sub range {
1357 my $self = shift;
1358 my ($op, $cx, $type) = @_;
1359 my $left = $op->first;
1360 my $right = $left->sibling;
1361 $left = $self->deparse($left, 9, $op);
1362 $right = $self->deparse($right, 9, $op);
1363 return info_from_list($op, $self, [$left, $type, $right], ' ', 'range',
1364 {maybe_parens => [$self, $cx, 9]});
1365 }
1366  
1367 sub pp_flop
1368 {
1369 my $self = shift;
1370 my($op, $cx) = @_;
1371 my $flip = $op->first;
1372 my $type = ($flip->flags & OPf_SPECIAL) ? "..." : "..";
1373 return info_from_text($op, $self, $self->range($flip->first, $cx, $type), 'pp_flop', {});
1374 }
1375  
1376 sub logassignop
1377 {
1378 my ($self, $op, $cx, $opname) = @_;
1379 my $left = $op->first;
1380  
1381 my $right = $op->first->sibling->first; # skip sassign
1382 $left = $self->deparse($left, 7, $op);
1383 $right = $self->deparse($right, 7, $op);
1384 return info_from_list($op, $self, [$left->{text}, $opname, $right->{text}], ' ',
1385 'logassignop',
1386 {other_ops => [$op->first->sibling],
1387 body => [$left, $right],
1388 maybe_parens => [$self, $cx, 7]});
1389 }
1390  
1391 sub pp_andassign { logassignop(@_, "&&=") }
1392 sub pp_orassign { logassignop(@_, "||=") }
1393 sub pp_dorassign { logassignop(@_, "//=") }
1394  
1395 sub rv2gv_or_string {
1396 my($self,$op, $parent) = @_;
1397 if ($op->name eq "gv") { # could be open("open") or open("###")
1398 my($name,$quoted) =
1399 $self->stash_variable_name("", $self->gv_or_padgv($op));
1400 return info_from_text($op, $self, $quoted ? $name : "*$name", 'r2gv_or_string', {});
1401 }
1402 else {
1403 return $self->deparse($op, 6, $parent);
1404 }
1405 }
1406  
1407 sub pp_bless { listop(@_, "bless") }
1408 sub pp_atan2 { maybe_targmy(@_, \&listop, "atan2") }
1409 sub pp_substr {
1410 my ($self,$op,$cx) = @_;
1411 if ($op->private & OPpSUBSTR_REPL_FIRST) {
1412 my $left = listop($self, $op, 7, "substr", $op->first->sibling->sibling);
1413 my $right = $self->deparse($op->first->sibling, 7, $op);
1414 return info_from_list($op, $self,[$left, '=', $right], ' ',
1415 'substr_repl_first', {});
1416 }
1417 return maybe_local(@_, listop(@_, "substr"))
1418 }
1419 sub pp_vec { maybe_local(@_, listop(@_, "vec")) }
1420 sub pp_index { maybe_targmy(@_, \&listop, "index") }
1421 sub pp_rindex { maybe_targmy(@_, \&listop, "rindex") }
1422 sub pp_sprintf { maybe_targmy(@_, \&listop, "sprintf") }
1423 sub pp_formline { listop(@_, "formline") } # see also deparse_format
1424 sub pp_crypt { maybe_targmy(@_, \&listop, "crypt") }
1425 sub pp_unpack { listop(@_, "unpack") }
1426 sub pp_pack { listop(@_, "pack") }
1427 sub pp_join { maybe_targmy(@_, \&listop, "join") }
1428 sub pp_splice { listop(@_, "splice") }
1429 sub pp_push { maybe_targmy(@_, \&listop, "push") }
1430 sub pp_unshift { maybe_targmy(@_, \&listop, "unshift") }
1431 sub pp_reverse { listop(@_, "reverse") }
1432 sub pp_warn { listop(@_, "warn") }
1433 sub pp_die { listop(@_, "die") }
1434 sub pp_return { listop(@_, "return", undef, 1) } # llafr does not apply
1435 sub pp_open { listop(@_, "open") }
1436 sub pp_pipe_op { listop(@_, "pipe") }
1437 sub pp_tie { listop(@_, "tie") }
1438 sub pp_binmode { listop(@_, "binmode") }
1439 sub pp_dbmopen { listop(@_, "dbmopen") }
1440 sub pp_sselect { listop(@_, "select") }
1441 sub pp_select { listop(@_, "select") }
1442 sub pp_read { listop(@_, "read") }
1443 sub pp_sysopen { listop(@_, "sysopen") }
1444 sub pp_sysseek { listop(@_, "sysseek") }
1445 sub pp_sysread { listop(@_, "sysread") }
1446 sub pp_syswrite { listop(@_, "syswrite") }
1447 sub pp_send { listop(@_, "send") }
1448 sub pp_recv { listop(@_, "recv") }
1449 sub pp_seek { listop(@_, "seek") }
1450 sub pp_fcntl { listop(@_, "fcntl") }
1451 sub pp_ioctl { listop(@_, "ioctl") }
1452 sub pp_flock { maybe_targmy(@_, \&listop, "flock") }
1453 sub pp_socket { listop(@_, "socket") }
1454 sub pp_sockpair { listop(@_, "socketpair") }
1455 sub pp_bind { listop(@_, "bind") }
1456 sub pp_connect { listop(@_, "connect") }
1457 sub pp_listen { listop(@_, "listen") }
1458 sub pp_accept { listop(@_, "accept") }
1459 sub pp_shutdown { listop(@_, "shutdown") }
1460 sub pp_gsockopt { listop(@_, "getsockopt") }
1461 sub pp_ssockopt { listop(@_, "setsockopt") }
1462 sub pp_chown { maybe_targmy(@_, \&listop, "chown") }
1463 sub pp_unlink { maybe_targmy(@_, \&listop, "unlink") }
1464 sub pp_chmod { maybe_targmy(@_, \&listop, "chmod") }
1465 sub pp_utime { maybe_targmy(@_, \&listop, "utime") }
1466 sub pp_rename { maybe_targmy(@_, \&listop, "rename") }
1467 sub pp_link { maybe_targmy(@_, \&listop, "link") }
1468 sub pp_symlink { maybe_targmy(@_, \&listop, "symlink") }
1469 sub pp_mkdir { maybe_targmy(@_, \&listop, "mkdir") }
1470 sub pp_open_dir { listop(@_, "opendir") }
1471 sub pp_seekdir { listop(@_, "seekdir") }
1472 sub pp_waitpid { maybe_targmy(@_, \&listop, "waitpid") }
1473 sub pp_system { maybe_targmy(@_, \&listop, "system") }
1474 sub pp_exec { maybe_targmy(@_, \&listop, "exec") }
1475 sub pp_kill { maybe_targmy(@_, \&listop, "kill") }
1476 sub pp_setpgrp { maybe_targmy(@_, \&listop, "setpgrp") }
1477 sub pp_getpriority { maybe_targmy(@_, \&listop, "getpriority") }
1478 sub pp_setpriority { maybe_targmy(@_, \&listop, "setpriority") }
1479 sub pp_shmget { listop(@_, "shmget") }
1480 sub pp_shmctl { listop(@_, "shmctl") }
1481 sub pp_shmread { listop(@_, "shmread") }
1482 sub pp_shmwrite { listop(@_, "shmwrite") }
1483 sub pp_msgget { listop(@_, "msgget") }
1484 sub pp_msgctl { listop(@_, "msgctl") }
1485 sub pp_msgsnd { listop(@_, "msgsnd") }
1486 sub pp_msgrcv { listop(@_, "msgrcv") }
1487 sub pp_semget { listop(@_, "semget") }
1488 sub pp_semctl { listop(@_, "semctl") }
1489 sub pp_semop { listop(@_, "semop") }
1490 sub pp_ghbyaddr { listop(@_, "gethostbyaddr") }
1491 sub pp_gnbyaddr { listop(@_, "getnetbyaddr") }
1492 sub pp_gpbynumber { listop(@_, "getprotobynumber") }
1493 sub pp_gsbyname { listop(@_, "getservbyname") }
1494 sub pp_gsbyport { listop(@_, "getservbyport") }
1495 sub pp_syscall { listop(@_, "syscall") }
1496  
1497 sub pp_glob
1498 {
1499 my($self, $op, $cx) = @_;
1500  
1501 my $opts = {other_ops => [$op->first]};
1502 my $kid = $op->first->sibling; # skip pushmark
1503 my $keyword =
1504 $op->flags & OPf_SPECIAL ? 'glob' : $self->keyword('glob');
1505  
1506 if ($keyword =~ /^CORE::/ or $kid->name ne 'const') {
1507 my $kid_info = $self->dq($kid, $op);
1508 my $body = [$kid_info];
1509 my $text = $kid_info->{text};
1510 if ($text =~ /^\$?(\w|::|\`)+$/ # could look like a readline
1511 or $text =~ /[<>]/) {
1512 $kid_info = $self->deparse($kid, 0, $op);
1513 $body = [$kid_info];
1514 $text = $kid_info->{text};
1515 $opts->{body} = $body;
1516 if ($cx >= 5 || $self->{'parens'}) {
1517 return info_from_list($op, $self, [$keyword, '(', $text, ')'], '',
1518 'glob_paren', $opts);
1519 } else {
1520 return info_from_list($op, $self, [$keyword, $text], ' ',
1521 'glob_space', $opts);
1522 }
1523 } else {
1524 return info_from_list($op, $self, ['<', $text, '>'], '', 'glob_angle', $opts);
1525 }
1526 }
1527 return info_from_list($op, $self, ['<', '>'], '', 'glob_angle', $opts);
1528 }
1529  
1530 # Truncate is special because OPf_SPECIAL makes a bareword first arg
1531 # be a filehandle. This could probably be better fixed in the core
1532 # by moving the GV lookup into ck_truc.
1533  
1534 sub pp_truncate
1535 {
1536 my($self, $op, $cx) = @_;
1537 my(@exprs);
1538 my $parens = ($cx >= 5) || $self->{'parens'};
1539 my $kid = $op->first->sibling;
1540 my $fh;
1541 if ($op->flags & OPf_SPECIAL) {
1542 # $kid is an OP_CONST
1543 $fh = $self->const_sv($kid)->PV;
1544 } else {
1545 $fh = $self->deparse($kid, 6, $op);
1546 $fh = "+$fh" if not $parens and substr($fh, 0, 1) eq "(";
1547 }
1548 my $len = $self->deparse($kid->sibling, 6, $op);
1549 my $name = $self->keyword('truncate');
1550 my $opts = {body => [$fh, $len]};
1551 my $args = "$fh->{text}, $len->{text}";
1552 if ($parens) {
1553 return info_from_list($op, $self, [$name, '(', $args, ')'], '',
1554 'truncate_parens', $opts);
1555 return "$name($fh, $len)";
1556 } else {
1557 return info_from_list($op, $self, [$name, $args], '', 'truncate', $opts);
1558 }
1559 }
1560  
1561 sub pp_list
1562 {
1563 my($self, $op, $cx) = @_;
1564 my($expr, @exprs);
1565  
1566 my $other_op = $op->first;
1567 my $kid = $op->first->sibling; # skip a pushmark
1568  
1569 if (class($kid) eq 'NULL') {
1570 return info_from_text($op, $self, '', 'list_null',
1571 {other_ops => [$other_op]});
1572 }
1573 my $lop;
1574 my $local = "either"; # could be local(...), my(...), state(...) or our(...)
1575 for ($lop = $kid; !null($lop); $lop = $lop->sibling) {
1576 # This assumes that no other private flags equal 128, and that
1577 # OPs that store things other than flags in their op_private,
1578 # like OP_AELEMFAST, won't be immediate children of a list.
1579 #
1580 # OP_ENTERSUB and OP_SPLIT can break this logic, so check for them.
1581 # I suspect that open and exit can too.
1582 # XXX This really needs to be rewritten to accept only those ops
1583 # known to take the OPpLVAL_INTRO flag.
1584  
1585 if (!($lop->private & (OPpLVAL_INTRO|OPpOUR_INTRO)
1586 or $lop->name eq "undef")
1587 or $lop->name =~ /^(?:entersub|exit|open|split)\z/)
1588 {
1589 $local = ""; # or not
1590 last;
1591 }
1592 if ($lop->name =~ /^pad[ash]v$/) {
1593 if ($lop->private & OPpPAD_STATE) { # state()
1594 ($local = "", last) if $local =~ /^(?:local|our|my)$/;
1595 $local = "state";
1596 } else { # my()
1597 ($local = "", last) if $local =~ /^(?:local|our|state)$/;
1598 $local = "my";
1599 }
1600 } elsif ($lop->name =~ /^(gv|rv2)[ash]v$/
1601 && $lop->private & OPpOUR_INTRO
1602 or $lop->name eq "null" && $lop->first->name eq "gvsv"
1603 && $lop->first->private & OPpOUR_INTRO) { # our()
1604 ($local = "", last) if $local =~ /^(?:my|local|state)$/;
1605 $local = "our";
1606 } elsif ($lop->name ne "undef"
1607 # specifically avoid the "reverse sort" optimisation,
1608 # where "reverse" is nullified
1609 && !($lop->name eq 'sort' && ($lop->flags & OPpSORT_REVERSE)))
1610 {
1611 # local()
1612 ($local = "", last) if $local =~ /^(?:my|our|state)$/;
1613 $local = "local";
1614 }
1615 }
1616 $local = "" if $local eq "either"; # no point if it's all undefs
1617 if (null $kid->sibling and not $local) {
1618 my $info = $self->deparse($kid, $cx, $op);
1619 my @other_ops = $info->{other_ops} ? @{$info->{other_ops}} : ();
1620 push @other_ops, $other_op;
1621 $info->{other_ops} = \@other_ops;
1622 return $info;
1623 }
1624  
1625 for (; !null($kid); $kid = $kid->sibling) {
1626 if ($local) {
1627 if (class($kid) eq "UNOP" and $kid->first->name eq "gvsv") {
1628 $lop = $kid->first;
1629 } else {
1630 $lop = $kid;
1631 }
1632 $self->{'avoid_local'}{$$lop}++;
1633 $expr = $self->deparse($kid, 6, $op);
1634 delete $self->{'avoid_local'}{$$lop};
1635 } else {
1636 $expr = $self->deparse($kid, 6, $op);
1637 }
1638 push @exprs, $expr;
1639 }
1640  
1641 my $type;
1642 my @texts;
1643 my $opts = {};
1644 if ($local) {
1645 $type = 'list_local';
1646 @texts = ($local, '(', $self->combine(", ", \@exprs), ')');
1647 } else {
1648 $type = 'list';
1649 @texts = ($self->combine(", ", \@exprs));
1650 $opts->{maybe_parens} = [$self, $cx, 6];
1651  
1652 }
1653 return info_from_list($op, $self, \@texts, '', $type, $opts);
1654 }
1655  
1656 sub is_ifelse_cont
1657 {
1658 my $op = shift;
1659 return ($op->name eq "null" and class($op) eq "UNOP"
1660 and $op->first->name =~ /^(and|cond_expr)$/
1661 and is_scope($op->first->first->sibling));
1662 }
1663  
1664 sub loop_common
1665 {
1666 my $self = shift;
1667 my($op, $cx, $init) = @_;
1668 my $enter = $op->first;
1669 my $kid = $enter->sibling;
1670 local(@$self{qw'curstash warnings hints hinthash'})
1671 = @$self{qw'curstash warnings hints hinthash'};
1672  
1673 my ($body, @body, @other_ops, $type);
1674 my (@head, @head_text) = ((), ());
1675 my ($bare, $cond_info) = (0, undef);
1676  
1677 if ($kid->name eq "lineseq") {
1678 # bare or infinite loop
1679 $type = 'loop_bare_or_infinite';
1680  
1681 if ($kid->last->name eq "unstack") { # infinite
1682 @head_text = ("while", "(1)"); # Can't use for(;;) if there's a continue
1683 } else {
1684 $bare = 1;
1685 }
1686 $body = $kid;
1687 } elsif ($enter->name eq "enteriter") {
1688 # foreach
1689 $type = 'loop_foreach';
1690  
1691 push @other_ops, $enter->first;
1692 my $ary = $enter->first->sibling; # first was pushmark
1693 my @ary_info;
1694 my @ary_text;
1695 my $var = $ary->sibling;
1696 if ($ary->name eq 'null' and $enter->private & OPpITER_REVERSED) {
1697 # "reverse" was optimised away
1698 @ary_info = (listop($self, $ary->first->sibling, 1, 'reverse'));
1699 @ary_text = ($ary_info[0]->{text});
1700 } elsif ($enter->flags & OPf_STACKED
1701 and not null $ary->first->sibling->sibling) {
1702 @ary_info = ($self->deparse($ary->first->sibling, 9, $op),
1703 $self->deparse($ary->first->sibling->sibling, 9, $op));
1704 @ary_text = ($ary_info[0]->{text}, "..", $ary_info[1]->{text});
1705  
1706 } else {
1707 @ary_info = ($self->deparse($ary, 1, $op));
1708 @ary_text = $ary_info[0]->{text};
1709 }
1710  
1711 my @var_info = ();
1712 my @var_text;
1713 if (null $var) {
1714 if (($enter->flags & OPf_SPECIAL) && ($] < 5.009)) {
1715 # thread special var, under 5005threads
1716 @var_text = ($self->pp_threadsv($enter, 1));
1717 } else { # regular my() variable
1718 my $var_info = $self->pp_padsv($enter, 1, 1);
1719 @var_info =($var_info);
1720 @var_text = ($var_info->{text});
1721 }
1722 } elsif ($var->name eq "rv2gv") {
1723 @var_info = ($self->pp_rv2sv($var, 1));
1724 @var_text = ($var_info[0]->{text});
1725 if ($enter->private & OPpOUR_INTRO) {
1726 # our declarations don't have package names
1727 $var_text[0] =~ s/^(.).*::/$1/;
1728 unshift @var_text, 'our';
1729 }
1730 } elsif ($var->name eq "gv") {
1731 @var_info = ($self->deparse($var, 1, $op));
1732 @var_text = ("\$" . $var_info[0]->{text});
1733 }
1734  
1735 # skip OP_AND and OP_ITER
1736 push @other_ops, $kid->first, $kid->first->first;
1737 $body = $kid->first->first->sibling;
1738  
1739 @head = (@ary_info, @var_info);
1740 if (!is_state $body->first and $body->first->name !~ /^(?:stub|leave|scope)$/) {
1741 Carp::confess("var ne \$_") unless join('', @var_text) eq '$_';
1742 $body = $body->first;
1743 my $body_info = $self->deparse($body, 2, $op);
1744 push @head, $body_info;
1745 my @texts = ($body_info->{text}, "foreach", '(', @ary_text, ')');
1746 return info_from_list($body->{op}, $self, \@texts, ' ', 'loop_foreach_ary',
1747 {other_ops => \@other_ops});
1748 }
1749 @head_text = ("foreach", @var_text, '(', @ary_text, ')');
1750 } elsif ($kid->name eq "null") {
1751 # while/until
1752 $type = 'loop_while_until';
1753  
1754 $kid = $kid->first;
1755 my $name = {"and" => "while", "or" => "until"}->{$kid->name};
1756 $cond_info = $self->deparse($kid->first, 1, $op);
1757 @head_text = ($name, "($cond_info->{text})");
1758 $body = $kid->first->sibling;
1759 } elsif ($kid->name eq "stub") {
1760 # bare and empty
1761 return info_from_list($op, $self, ['{', ';', '}'], '', 'loop_stub', {});
1762 }
1763  
1764 # If there isn't a continue block, then the next pointer for the loop
1765 # will point to the unstack, which is kid's last child, except
1766 # in a bare loop, when it will point to the leaveloop. When neither of
1767 # these conditions hold, then the second-to-last child is the continue
1768 # block (or the last in a bare loop).
1769 my $cont_start = $enter->nextop;
1770 my ($cont, @cont_text, $body_info);
1771 my @cont = ();
1772 if ($$cont_start != $$op && ${$cont_start} != ${$body->last}) {
1773 $type .= '_continue';
1774  
1775 if ($bare) {
1776 $cont = $body->last;
1777 } else {
1778 $cont = $body->first;
1779 while (!null($cont->sibling->sibling)) {
1780 $cont = $cont->sibling;
1781 }
1782 }
1783 my $state = $body->first;
1784 my $cuddle = " ";
1785 my @states;
1786 for (; $$state != $$cont; $state = $state->sibling) {
1787 push @states, $state;
1788 }
1789 $body_info = $self->lineseq(undef, 0, @states);
1790 if (defined $cond_info and not is_scope $cont and $self->{'expand'} < 3) {
1791 my $cont_info = $self->deparse($cont, 1, $op);
1792 push @head, $cont_info;
1793 my $init_text = defined($init) ? $init->{text} : ' ';
1794 @head_text = ('for', '(', "$init_text;", $cont_info->{text}, ')');
1795 @cont_text = ("\cK");
1796 } else {
1797 my $cont_info = $self->deparse($cont, 0, $op);
1798 push @head, $cont_info;
1799 @cont_text = ($cuddle, 'continue', "{\n\t",
1800 $cont_info->{text} , "\n\b}\cK");
1801 }
1802 } else {
1803 return info_from_text($op, $self, [''], 'loop_no_body', {}) if !defined $body;
1804 if (defined $init) {
1805 @head_text = ('for', '(', "$init->{text};", "$cond_info->{text};", ")");
1806 }
1807 @cont_text = ("\cK");
1808 $body_info = $self->deparse($body, 0, $op);
1809 }
1810 (my $body_text = $body_info->{text}) =~ s/;?$/;\n/;
1811  
1812 my @texts = (@head_text, "{\n\t", $body_text, "\b}", @cont_text);
1813 return info_from_list($op, $self, \@texts, ' ', $type,
1814 {body => [$body_info]});
1815 }
1816  
1817 sub pp_leaveloop {
1818 shift->loop_common(@_, undef)
1819 }
1820  
1821 sub for_loop {
1822 my $self = shift;
1823 my($op, $cx, $parent) = @_;
1824 my $init = $self->deparse($op, 1, $parent);
1825 my $s = $op->sibling;
1826 my $ll = $s->name eq "unstack" ? $s->sibling : $s->first->sibling;
1827 return $self->loop_common($ll, $cx, $init);
1828 }
1829  
1830 sub pp_leavetry {
1831 my ($self, $op, $cx) = @_;
1832 my $leave_info = $self->pp_leave($op, $cx);
1833 return info_from_list($op, $self, ['eval', '{\n\t"', $leave_info->{text}, "\n\b}"],
1834 ' ', 'leavetry', {body=>[$leave_info]});
1835 }
1836  
1837 sub _op_is_or_was {
1838 my ($op, $expect_type) = @_;
1839 my $type = $op->type;
1840 return($type == $expect_type
1841 || ($type == OP_NULL && $op->targ == $expect_type));
1842 }
1843  
1844 sub padname {
1845 my $self = shift;
1846 my $targ = shift;
1847 return $self->padname_sv($targ)->PVX;
1848 }
1849  
1850 sub padany {
1851 my $self = shift;
1852 my $op = shift;
1853 return substr($self->padname($op->targ), 1); # skip $/@/%
1854 }
1855  
1856 sub pp_padsv {
1857 my $self = shift;
1858 my($op, $cx, $forbid_parens) = @_;
1859 return $self->maybe_my($op, $cx, $self->padname($op->targ),
1860 $forbid_parens);
1861 }
1862  
1863 sub pp_padav { pp_padsv(@_) }
1864 sub pp_padhv { pp_padsv(@_) }
1865  
1866 my @threadsv_names = B::threadsv_names;
1867 sub pp_threadsv {
1868 my $self = shift;
1869 my($op, $cx) = @_;
1870 return $self->maybe_local_str($op, $cx, "\$" . $threadsv_names[$op->targ]);
1871 }
1872  
1873 sub gv_or_padgv {
1874 my $self = shift;
1875 my $op = shift;
1876 if (class($op) eq "PADOP") {
1877 return $self->padval($op->padix);
1878 } else { # class($op) eq "SVOP"
1879 return $op->gv;
1880 }
1881 }
1882  
1883 # FIXME: adjust use of maybe_local_str
1884 sub pp_gvsv
1885 {
1886 my($self, $op, $cx) = @_;
1887 my $gv = $self->gv_or_padgv($op);
1888 return $self->maybe_local_str($op, $cx,
1889 $self->stash_variable("\$",
1890 $self->gv_name($gv), $cx));
1891 }
1892  
1893 sub pp_gv
1894 {
1895 my($self, $op, $cx) = @_;
1896 my $gv = $self->gv_or_padgv($op);
1897 return info_from_text($op, $self, $self->gv_name($gv),
1898 'global variable', {});
1899 }
1900  
1901 sub pp_aelemfast_lex
1902 {
1903 my($self, $op, $cx) = @_;
1904 my $name = $self->padname($op->targ);
1905 $name =~ s/^@/\$/;
1906 return info_from_list($op, $self, [$name, "[", ($op->private + $self->{'arybase'}), "]"],
1907 '', 'pp_aelemfast_lex', {});
1908 }
1909  
1910 sub pp_aelemfast
1911 {
1912 my($self, $op, $cx) = @_;
1913 # optimised PADAV, pre 5.15
1914 return $self->pp_aelemfast_lex(@_) if ($op->flags & OPf_SPECIAL);
1915  
1916 my $gv = $self->gv_or_padgv($op);
1917 my($name,$quoted) = $self->stash_variable_name('@',$gv);
1918 $name = $quoted ? "$name->" : '$' . $name;
1919 my $i = $op->private;
1920 $i -= 256 if $i > 127;
1921 return info_from_list($op, $self, [$name, "[", ($op->private + $self->{'arybase'}), "]"],
1922 '', 'pp_aelemfast', {});
1923 }
1924  
1925 sub rv2x
1926 {
1927 my($self, $op, $cx, $type) = @_;
1928  
1929 if (class($op) eq 'NULL' || !$op->can("first")) {
1930 carp("Unexpected op in pp_rv2x");
1931 return info_from_text($op, $self, 'XXX', 'bad_rv2x', {});
1932 }
1933 my ($info, $kid_info);
1934 my $kid = $op->first;
1935 if ($kid->name eq "gv") {
1936 $kid_info = $self->deparse($kid, 0, $op);
1937 my $str = $self->stash_variable($type, $kid_info->{text}, $cx);
1938 return info_from_text($op, $self, $str, 'rv2x_gv', {body => [$kid_info]});
1939 } elsif (is_scalar $kid) {
1940 $kid_info = $self->deparse($kid, 0, $op);
1941 my $str = $kid_info->{text};
1942 if ($str =~ /^\$([^\w\d])\z/) {
1943 # "$$+" isn't a legal way to write the scalar dereference
1944 # of $+, since the lexer can't tell you aren't trying to
1945 # do something like "$$ + 1" to get one more than your
1946 # PID. Either "${$+}" or "$${+}" are workable
1947 # disambiguations, but if the programmer did the former,
1948 # they'd be in the "else" clause below rather than here.
1949 # It's not clear if this should somehow be unified with
1950 # the code in dq and re_dq that also adds lexer
1951 # disambiguation braces.
1952 $str = '$' . "{$1}"; #'
1953 }
1954 return info_from_list($op, $self, [$type, $str], '', 'rv2x_scalar',
1955 {body => [$kid_info]});
1956 } else {
1957 my $kid_info = $self->deparse($kid, 0, $op);
1958 return info_from_list($op, $self, [$type, "{", "}"], '', 'rv2x',
1959 {body => [$kid_info]});
1960 }
1961 Carp::confess("unhandled condition in rv2x");
1962 }
1963  
1964 sub pp_rv2sv { maybe_local(@_, rv2x(@_, "\$")) }
1965 sub pp_rv2hv { maybe_local(@_, rv2x(@_, "%")) }
1966 sub pp_rv2gv { maybe_local(@_, rv2x(@_, "*")) }
1967  
1968 # skip rv2av
1969 sub pp_av2arylen {
1970 my $self = shift;
1971 my($op, $cx) = @_;
1972 if ($op->first->name eq "padav") {
1973 return $self->maybe_local_str($op, $cx, '$#' . $self->padany($op->first));
1974 } else {
1975 return $self->maybe_local($op, $cx,
1976 $self->rv2x($op->first, $cx, '$#'));
1977 }
1978 }
1979  
1980 # skip down to the old, ex-rv2cv
1981 sub pp_rv2cv {
1982 my ($self, $op, $cx) = @_;
1983 if (!null($op->first) && $op->first->name eq 'null' &&
1984 $op->first->targ == OP_LIST)
1985 {
1986 return $self->rv2x($op->first->first->sibling, $cx, "&")
1987 }
1988 else {
1989 return $self->rv2x($op, $cx, "")
1990 }
1991 }
1992  
1993 sub list_const($$$) {
1994 my $self = shift;
1995 my($op, $cx, @list) = @_;
1996 my @a = map $self->const($_, 6), @list;
1997 my @texts = $self->map_texts(\@a);
1998 my $type = 'list_const';
1999 my $prec = 6;
2000 if (@texts == 0) {
2001 return info_from_list($op, $self, ['(', ')'], '', 'list_const_null', {});
2002 } elsif (@texts == 1) {
2003 return info_from_text($op, $self, $texts[0], 'list_const_one',
2004 {body => \@a});
2005 } elsif ( @texts > 2 and !grep(!/^-?\d+$/, @texts)) {
2006 # collapse (-1,0,1,2) into (-1..2)
2007 my ($s, $e) = @texts[0,-1];
2008 my $i = $s;
2009 unless (grep $i++ != $_, @texts) {
2010 @texts = ($s, '..', $e);
2011 $type = 'list_const_range';
2012 $prec = 9;
2013 }
2014 }
2015 return info_from_list($op, $self, \@texts, '', $type,
2016 {maybe_parens => [$self, $cx, $prec]});
2017 }
2018  
2019 sub pp_rv2av {
2020 my $self = shift;
2021 my($op, $cx) = @_;
2022 my $kid = $op->first;
2023 if ($kid->name eq "const") { # constant list
2024 my $av = $self->const_sv($kid);
2025 return $self->list_const($kid, $cx, $av->ARRAY);
2026 } else {
2027 # FIXME?
2028 return $self->maybe_local($op, $cx, $self->rv2x($op, $cx, "\@"));
2029 }
2030 }
2031  
2032 sub elem_or_slice_array_name
2033 {
2034 my $self = shift;
2035 my ($array, $left, $padname, $allow_arrow) = @_;
2036  
2037 if ($array->name eq $padname) {
2038 return $self->padany($array);
2039 } elsif (is_scope($array)) { # ${expr}[0]
2040 return "{" . $self->deparse($array, 0) . "}";
2041 } elsif ($array->name eq "gv") {
2042 ($array, my $quoted) =
2043 $self->stash_variable_name(
2044 $left eq '[' ? '@' : '%', $self->gv_or_padgv($array)
2045 );
2046 if (!$allow_arrow && $quoted) {
2047 # This cannot happen.
2048 die "Invalid variable name $array for slice";
2049 }
2050 return $quoted ? "$array->" : $array;
2051 } elsif (!$allow_arrow || is_scalar $array) { # $x[0], $$x[0], ...
2052 return $self->deparse($array, 24)->{text};
2053 } else {
2054 return undef;
2055 }
2056 }
2057  
2058 sub elem_or_slice_single_index($$)
2059 {
2060 my ($self, $idx, $parent) = @_;
2061  
2062 my $idx_info = $self->deparse($idx, 1, $parent);
2063 my $idx_str = $idx_info->{text};
2064  
2065 # Outer parens in an array index will confuse perl
2066 # if we're interpolating in a regular expression, i.e.
2067 # /$x$foo[(-1)]/ is *not* the same as /$x$foo[-1]/
2068 #
2069 # If $self->{parens}, then an initial '(' will
2070 # definitely be paired with a final ')'. If
2071 # !$self->{parens}, the misleading parens won't
2072 # have been added in the first place.
2073 #
2074 # [You might think that we could get "(...)...(...)"
2075 # where the initial and final parens do not match
2076 # each other. But we can't, because the above would
2077 # only happen if there's an infix binop between the
2078 # two pairs of parens, and *that* means that the whole
2079 # expression would be parenthesized as well.]
2080 #
2081 $idx_str =~ s/^\((.*)\)$/$1/ if $self->{'parens'};
2082  
2083 # Hash-element braces will autoquote a bareword inside themselves.
2084 # We need to make sure that C<$hash{warn()}> doesn't come out as
2085 # C<$hash{warn}>, which has a quite different meaning. Currently
2086 # B::Deparse will always quote strings, even if the string was a
2087 # bareword in the original (i.e. the OPpCONST_BARE flag is ignored
2088 # for constant strings.) So we can cheat slightly here - if we see
2089 # a bareword, we know that it is supposed to be a function call.
2090 #
2091 $idx_str =~ s/^([A-Za-z_]\w*)$/$1()/;
2092  
2093 return info_from_text($idx_info->{op}, $self, $idx_str,
2094 'elem_or_slice_single_index',
2095 {body => [$idx_info]});
2096 }
2097  
2098 sub elem
2099 {
2100 my ($self, $op, $cx, $left, $right, $padname) = @_;
2101 my($array, $idx) = ($op->first, $op->first->sibling);
2102  
2103 my $idx_info = $self->elem_or_slice_single_index($idx, $op);
2104 my $opts = {body => [$idx_info]};
2105  
2106 unless ($array->name eq $padname) { # Maybe this has been fixed
2107 $opts->{other_ops} = [$array];
2108 $array = $array->first; # skip rv2av (or ex-rv2av in _53+)
2109 }
2110 my @texts = ();
2111 my $info;
2112 my $array_name=$self->elem_or_slice_array_name($array, $left, $padname, 1);
2113 if ($array_name) {
2114 if ($array_name !~ /->\z/) {
2115 if ($array_name eq '#') {
2116 $array_name = '${#}';
2117 } else {
2118 $array_name = '$' . $array_name ;
2119 }
2120 }
2121 push @texts, $array_name;
2122 push @texts, $left if $left;
2123 push @texts, $idx_info->{text}, $right;
2124 return info_from_list($op, $self, \@texts, '', 'elem', $opts)
2125 } else {
2126 # $x[20][3]{hi} or expr->[20]
2127 my $type;
2128 my $array_info = $self->deparse($array, 24, $op);
2129 push @{$info->{body}}, $array_info;
2130 @texts = ($array_info->{text});
2131 if (is_subscriptable($array)) {
2132 push @texts, $left, $idx_info->{text}, $right;
2133 $type = 'elem_no_arrow';
2134 } else {
2135 push @texts, '->', $left, $idx_info->{text}, $right;
2136 $type = 'elem_arrow';
2137 }
2138 return info_from_list($op, $self, \@texts, '', $type, $opts);
2139 }
2140 Carp::confess("unhandled condition in elem");
2141 }
2142  
2143 # a simplified version of elem_or_slice_array_name()
2144 # for the use of pp_multideref
2145  
2146 sub multideref_var_name($$$)
2147 {
2148 my ($self, $gv, $is_hash) = @_;
2149  
2150 my ($name, $quoted) =
2151 $self->stash_variable_name( $is_hash ? '%' : '@', $gv);
2152 return $quoted ? "$name->"
2153 : $name eq '#'
2154 ? '${#}' # avoid ${#}[1] => $#[1]
2155 : '$' . $name;
2156 }
2157  
2158 sub pp_multideref
2159 {
2160 my($self, $op, $cx) = @_;
2161 my @texts = ();
2162  
2163 if ($op->private & OPpMULTIDEREF_EXISTS) {
2164 @texts = ($self->keyword("exists"), ' ');
2165 }
2166 elsif ($op->private & OPpMULTIDEREF_DELETE) {
2167 @texts = ($self->keyword("delete"), ' ')
2168 }
2169 elsif ($op->private & OPpLVAL_INTRO) {
2170 @texts = ($self->keyword("local"), ' ')
2171 }
2172  
2173 if ($op->first && ($op->first->flags & OPf_KIDS)) {
2174 # arbitrary initial expression, e.g. f(1,2,3)->[...]
2175 my $first = $self->deparse($op->first, 24, $op);
2176 push @texts, $first->{text};
2177 }
2178  
2179 my @items = $op->aux_list($self->{curcv});
2180 my $actions = shift @items;
2181  
2182 my $is_hash;
2183 my $derefs = 0;
2184  
2185 while (1) {
2186 if (($actions & MDEREF_ACTION_MASK) == MDEREF_reload) {
2187 $actions = shift @items;
2188 next;
2189 }
2190  
2191 $is_hash = (
2192 ($actions & MDEREF_ACTION_MASK) == MDEREF_HV_pop_rv2hv_helem
2193 || ($actions & MDEREF_ACTION_MASK) == MDEREF_HV_gvsv_vivify_rv2hv_helem
2194 || ($actions & MDEREF_ACTION_MASK) == MDEREF_HV_padsv_vivify_rv2hv_helem
2195 || ($actions & MDEREF_ACTION_MASK) == MDEREF_HV_vivify_rv2hv_helem
2196 || ($actions & MDEREF_ACTION_MASK) == MDEREF_HV_padhv_helem
2197 || ($actions & MDEREF_ACTION_MASK) == MDEREF_HV_gvhv_helem
2198 );
2199  
2200 if ( ($actions & MDEREF_ACTION_MASK) == MDEREF_AV_padav_aelem
2201 || ($actions & MDEREF_ACTION_MASK) == MDEREF_HV_padhv_helem)
2202 {
2203 $derefs = 1;
2204 push @texts, '$' . substr($self->padname(shift @items), 1);
2205 }
2206 elsif ( ($actions & MDEREF_ACTION_MASK) == MDEREF_AV_gvav_aelem
2207 || ($actions & MDEREF_ACTION_MASK) == MDEREF_HV_gvhv_helem)
2208 {
2209 $derefs = 1;
2210 push @texts, $self->multideref_var_name(shift @items, $is_hash);
2211 }
2212 else {
2213 if ( ($actions & MDEREF_ACTION_MASK) ==
2214 MDEREF_AV_padsv_vivify_rv2av_aelem
2215 || ($actions & MDEREF_ACTION_MASK) ==
2216 MDEREF_HV_padsv_vivify_rv2hv_helem)
2217 {
2218 push @texts, $self->padname(shift @items);
2219 }
2220 elsif ( ($actions & MDEREF_ACTION_MASK) ==
2221 MDEREF_AV_gvsv_vivify_rv2av_aelem
2222 || ($actions & MDEREF_ACTION_MASK) ==
2223 MDEREF_HV_gvsv_vivify_rv2hv_helem)
2224 {
2225 push @texts, $self->multideref_var_name(shift @items, $is_hash);
2226 }
2227 elsif ( ($actions & MDEREF_ACTION_MASK) ==
2228 MDEREF_AV_pop_rv2av_aelem
2229 || ($actions & MDEREF_ACTION_MASK) ==
2230 MDEREF_HV_pop_rv2hv_helem)
2231 {
2232 if ( ($op->flags & OPf_KIDS)
2233 && ( _op_is_or_was($op->first, OP_RV2AV)
2234 || _op_is_or_was($op->first, OP_RV2HV))
2235 && ($op->first->flags & OPf_KIDS)
2236 && ( _op_is_or_was($op->first->first, OP_AELEM)
2237 || _op_is_or_was($op->first->first, OP_HELEM))
2238 )
2239 {
2240 $derefs++;
2241 }
2242 }
2243  
2244 push(@texts, '->') if !$derefs++;
2245 }
2246  
2247  
2248 if (($actions & MDEREF_INDEX_MASK) == MDEREF_INDEX_none) {
2249 last;
2250 }
2251  
2252 push(@texts, $is_hash ? '{' : '[');
2253  
2254 if (($actions & MDEREF_INDEX_MASK) == MDEREF_INDEX_const) {
2255 my $key = shift @items;
2256 if ($is_hash) {
2257 push @texts, $self->const($key, $cx)->{text};
2258 }
2259 else {
2260 push @texts, $key;
2261 }
2262 }
2263 elsif (($actions & MDEREF_INDEX_MASK) == MDEREF_INDEX_padsv) {
2264 push @texts, $self->padname(shift @items);
2265 }
2266 elsif (($actions & MDEREF_INDEX_MASK) == MDEREF_INDEX_gvsv) {
2267 push @texts,('$' . ($self->stash_variable_name('$', shift @items))[0]);
2268 }
2269  
2270 push(@texts, $is_hash ? '}' : ']');
2271  
2272 if ($actions & MDEREF_FLAG_last) {
2273 last;
2274 }
2275 $actions >>= MDEREF_SHIFT;
2276 }
2277  
2278 return info_from_list($op, $self, \@texts, '', 'multideref', {});
2279 }
2280  
2281 sub pp_aelem { maybe_local(@_, elem(@_, "[", "]", "padav")) }
2282 sub pp_helem { maybe_local(@_, elem(@_, "{", "}", "padhv")) }
2283  
2284 sub pp_gelem
2285 {
2286 my($self, $op, $cx) = @_;
2287 my($glob, $part) = ($op->first, $op->last);
2288 $glob = $glob->first; # skip rv2gv
2289 $glob = $glob->first if $glob->name eq "rv2gv"; # this one's a bug
2290 my $scope = is_scope($glob);
2291 $glob = $self->deparse($glob, 0);
2292 $part = $self->deparse($part, 1);
2293 return "*" . ($scope ? "{$glob}" : $glob) . "{$part}";
2294 }
2295  
2296 sub slice
2297 {
2298 my ($self, $op, $cx, $left, $right, $regname, $padname) = @_;
2299 my $last;
2300 my(@elems, $kid, $array);
2301 if (class($op) eq "LISTOP") {
2302 $last = $op->last;
2303 } else { # ex-hslice inside delete()
2304 for ($kid = $op->first; !null $kid->sibling; $kid = $kid->sibling) {}
2305 $last = $kid;
2306 }
2307 $array = $last;
2308 $array = $array->first
2309 if $array->name eq $regname or $array->name eq "null";
2310 my $array_info = $self->elem_or_slice_array_name($array, $left, $padname, 0);
2311 $kid = $op->first->sibling; # skip pushmark
2312  
2313 if ($kid->name eq "list") {
2314 # skip list, pushmark
2315 $kid = $kid->first->sibling;
2316 for (; !null $kid; $kid = $kid->sibling) {
2317 push @elems, $self->deparse($kid, 6, $op);
2318 }
2319 } else {
2320 @elems = ($self->elem_or_slice_single_index($kid, $op));
2321 }
2322 my $list = join(', ', map($_->{text}, @elems));
2323 my $lead = '@';
2324 $lead = '%' if $op->name =~ /^kv/i;
2325 my (@texts, $type);
2326 if ($array_info) {
2327 @texts = ($lead, $array_info, $left, $list, $right);
2328 $type='slice1';
2329 } else {
2330 @texts = ($lead, $left, $list, $right);
2331 $type='slice';
2332 }
2333 return info_from_list($op, $self, \@texts, '', $type, {body => \@elems});
2334 }
2335  
2336 sub pp_aslice { maybe_local(@_, slice(@_, "[", "]", "rv2av", "padav")) }
2337 sub pp_kvaslice { slice(@_, "[", "]", "rv2av", "padav") }
2338 sub pp_hslice { maybe_local(@_, slice(@_, "{", "}", "rv2hv", "padhv")) }
2339 sub pp_kvhslice { slice(@_, "{", "}", "rv2hv", "padhv") }
2340  
2341 sub pp_lslice
2342 {
2343 my ($self, $op, $cs) = @_;
2344 my $idx = $op->first;
2345 my $list = $op->last;
2346 my(@elems, $kid);
2347 my $list_info = $self->deparse($list, 1, $op);
2348 my $idx_info = $self->deparse($idx, 1, $op);
2349 return info_from_list($op, $self, ['(', $list_info->{text}, ')', '[', $idx_info->{text}, ']'],
2350 '', 'lslice', {body=>[$list_info, $idx_info]});
2351 }
2352  
2353 sub want_scalar
2354 {
2355 my $op = shift;
2356 return ($op->flags & OPf_WANT) == OPf_WANT_SCALAR;
2357 }
2358  
2359 sub want_list
2360 {
2361 my $op = shift;
2362 return ($op->flags & OPf_WANT) == OPf_WANT_LIST;
2363 }
2364  
2365 sub _method
2366 {
2367 my($self, $op, $cx) = @_;
2368 my @other_ops = ($op->first);
2369 my $kid = $op->first->sibling; # skip pushmark
2370 my($meth, $obj, @exprs);
2371 if ($kid->name eq "list" and want_list $kid) {
2372 # When an indirect object isn't a bareword but the args are in
2373 # parens, the parens aren't part of the method syntax (the LLAFR
2374 # doesn't apply), but they make a list with OPf_PARENS set that
2375 # doesn't get flattened by the append_elem that adds the method,
2376 # making a (object, arg1, arg2, ...) list where the object
2377 # usually is. This can be distinguished from
2378 # '($obj, $arg1, $arg2)->meth()' (which is legal if $arg2 is an
2379 # object) because in the later the list is in scalar context
2380 # as the left side of -> always is, while in the former
2381 # the list is in list context as method arguments always are.
2382 # (Good thing there aren't method prototypes!)
2383 $meth = $kid->sibling;
2384 push @other_ops, $kid->first;
2385 $kid = $kid->first->sibling; # skip pushmark
2386 $obj = $kid;
2387 $kid = $kid->sibling;
2388 for (; not null $kid; $kid = $kid->sibling) {
2389 push @exprs, $kid;
2390 }
2391 } else {
2392 $obj = $kid;
2393 $kid = $kid->sibling;
2394 for (; !null ($kid->sibling) && $kid->name!~/^method(?:_named)?\z/;
2395 $kid = $kid->sibling) {
2396 push @exprs, $kid
2397 }
2398 $meth = $kid;
2399 }
2400  
2401 if ($meth->name eq "method_named") {
2402 $meth = $self->meth_sv($meth)->PV;
2403 } elsif ($meth->name eq "method_super") {
2404 $meth = "SUPER::".$self->meth_sv($meth)->PV;
2405 } elsif ($meth->name eq "method_redir") {
2406 $meth = $self->meth_rclass_sv($meth)->PV.'::'.$self->meth_sv($meth)->PV;
2407 } elsif ($meth->name eq "method_redir_super") {
2408 $meth = $self->meth_rclass_sv($meth)->PV.'::SUPER::'.
2409 $self->meth_sv($meth)->PV;
2410 } else {
2411 $meth = $meth->first;
2412 if ($meth->name eq "const") {
2413 # As of 5.005_58, this case is probably obsoleted by the
2414 # method_named case above
2415 $meth = $self->const_sv($meth)->PV; # needs to be bare
2416 }
2417 }
2418  
2419 return {
2420 method => $meth,
2421 variable_method => ref($meth),
2422 object => $obj,
2423 args => \@exprs,
2424 other_ops => \@other_ops
2425 }, $cx;
2426 }
2427  
2428 sub e_method {
2429 my ($self, $op, $minfo, $cx) = @_;
2430 my $obj = $self->deparse($minfo->{object}, 24, $op);
2431 my @body = ($obj);
2432 my $other_ops = $minfo->{other_ops};
2433  
2434 my $meth_name = $minfo->{method};
2435 my $meth_info;
2436 if ($minfo->{variable_method}) {
2437 $meth_info = $self->deparse($meth_name, 1, $op);
2438 push @body, $meth_info;
2439 }
2440 my @args = map { $self->deparse($_, 6, $op) } @{$minfo->{args}};
2441 push @body, @args;
2442 my @args_texts = map $_->{text}, @args;
2443 my $args = join(", ", @args_texts);
2444  
2445 my $opts = {other_ops => $other_ops};
2446 my @texts = ();
2447 my $type;
2448  
2449  
2450 my $meth_object = $meth_info ? defined($meth_info) : $meth_name;
2451 if ($minfo->{object}->name eq 'scope' && want_list $minfo->{object}) {
2452 # method { $object }
2453 # This must be deparsed this way to preserve list context
2454 # of $object.
2455 my $need_paren = $cx >= 6;
2456 if ($need_paren) {
2457 @texts = ('(', $meth_object, substr($obj,2),
2458 $args, ')');
2459 $type = 'e_method list ()';
2460 } else {
2461 @texts = ($meth_object, substr($obj,2), $args);
2462 $type = 'e_method list, no ()';
2463 }
2464 return info_from_list($op, $self, \@texts, '', $type, $opts);
2465 }
2466  
2467 if (length $args) {
2468 @texts = ($obj, '->', $meth_object, '(', $args, ')');
2469 $type = 'e_method -> ()';
2470 } else {
2471 @texts = ($obj, '->', $meth_object);
2472 $type = 'e_method -> no ()';
2473 }
2474 return info_from_list($op, $self, \@texts, '', $type, $opts);
2475 }
2476  
2477 # returns "&" and the argument bodies if the prototype doesn't match the args,
2478 # or ("", $args_after_prototype_demunging) if it does.
2479 sub check_proto {
2480 my $self = shift;
2481 my $op = shift;
2482 return ('&', []) if $self->{'noproto'};
2483 my($proto, @args) = @_;
2484 my($arg, $real);
2485 my $doneok = 0;
2486 my @reals;
2487 # An unbackslashed @ or % gobbles up the rest of the args
2488 1 while $proto =~ s/(?<!\\)([@%])[^\]]+$/$1/;
2489 $proto =~ s/^\s*//;
2490 while ($proto) {
2491 $proto =~ s/^(\\?[\$\@&%*_]|\\\[[\$\@&%*]+\]|;)\s*//;
2492 my $chr = $1;
2493 if ($chr eq "") {
2494 return ('&', []) if @args;
2495 } elsif ($chr eq ";") {
2496 $doneok = 1;
2497 } elsif ($chr eq "@" or $chr eq "%") {
2498 push @reals, map($self->deparse($_, 6), @args, $op);
2499 @args = ();
2500 } else {
2501 $arg = shift @args;
2502 last unless $arg;
2503 if ($chr eq "\$" || $chr eq "_") {
2504 if (want_scalar $arg) {
2505 push @reals, $self->deparse($arg, 6, $op);
2506 } else {
2507 return ('&', []);
2508 }
2509 } elsif ($chr eq "&") {
2510 if ($arg->name =~ /^(s?refgen|undef)$/) {
2511 push @reals, $self->deparse($arg, 6, $op);
2512 } else {
2513 return ('&', []);
2514 }
2515 } elsif ($chr eq "*") {
2516 if ($arg->name =~ /^s?refgen$/
2517 and $arg->first->first->name eq "rv2gv")
2518 {
2519 $real = $arg->first->first; # skip refgen, null
2520 if ($real->first->name eq "gv") {
2521 push @reals, $self->deparse($real, 6, $op);
2522 } else {
2523 push @reals, $self->deparse($real->first, 6, $op);
2524 }
2525 } else {
2526 return ('&', []);
2527 }
2528 } elsif (substr($chr, 0, 1) eq "\\") {
2529 $chr =~ tr/\\[]//d;
2530 if ($arg->name =~ /^s?refgen$/ and
2531 !null($real = $arg->first) and
2532 ($chr =~ /\$/ && is_scalar($real->first)
2533 or ($chr =~ /@/
2534 && class($real->first->sibling) ne 'NULL'
2535 && $real->first->sibling->name
2536 =~ /^(rv2|pad)av$/)
2537 or ($chr =~ /%/
2538 && class($real->first->sibling) ne 'NULL'
2539 && $real->first->sibling->name
2540 =~ /^(rv2|pad)hv$/)
2541 #or ($chr =~ /&/ # This doesn't work
2542 # && $real->first->name eq "rv2cv")
2543 or ($chr =~ /\*/
2544 && $real->first->name eq "rv2gv")))
2545 {
2546 push @reals, $self->deparse($real, 6, $op);
2547 } else {
2548 return ('&', []);
2549 }
2550 }
2551 }
2552 }
2553 return ('&', []) if $proto and !$doneok; # too few args and no ';'
2554 return ('&', []) if @args; # too many args
2555 return ('', \@reals);
2556 }
2557  
2558 sub pp_enterwrite { unop(@_, "write") }
2559  
2560 # Split a floating point number into an integer mantissa and a binary
2561 # exponent. Assumes you've already made sure the number isn't zero or
2562 # some weird infinity or NaN.
2563 sub split_float {
2564 my($f) = @_;
2565 my $exponent = 0;
2566 if ($f == int($f)) {
2567 while ($f % 2 == 0) {
2568 $f /= 2;
2569 $exponent++;
2570 }
2571 } else {
2572 while ($f != int($f)) {
2573 $f *= 2;
2574 $exponent--;
2575 }
2576 }
2577 my $mantissa = sprintf("%.0f", $f);
2578 return ($mantissa, $exponent);
2579 }
2580  
2581 sub dq
2582 {
2583 my ($self, $op, $parent) = @_;
2584 my $type = $op->name;
2585 my $info;
2586 if ($type eq "const") {
2587 return info_from_text($op, $self, '$[', 'dq_const_ary', {}) if $op->private & OPpCONST_ARYBASE;
2588 return info_from_text($op, $self,
2589 B::Deparse::uninterp(B::Deparse::escape_str(B::Deparse::unback($self->const_sv($op)->as_string))),
2590 'dq_const', {});
2591 } elsif ($type eq "concat") {
2592 my $first = $self->dq($op->first, $op);
2593 my $last = $self->dq($op->last, $op);
2594  
2595 # Disambiguate "${foo}bar", "${foo}{bar}", "${foo}[1]", "$foo\::bar"
2596 ($last =~ /^[A-Z\\\^\[\]_?]/ &&
2597 $first =~ s/([\$@])\^$/${1}{^}/) # "${^}W" etc
2598 || ($last =~ /^[:'{\[\w_]/ && #'
2599 $first =~ s/([\$@])([A-Za-z_]\w*)$/${1}{$2}/);
2600  
2601 return info_from_list($op, $self, [$first->{text}, $last->{text}], '', 'dq_concat',
2602 {body => [$first, $last]});
2603 } elsif ($type eq "join") {
2604 return $self->deparse($op->last, 26, $op); # was join($", @ary)
2605 } else {
2606 return $self->deparse($op, 26, $parent);
2607 }
2608 my $kid = $self->dq($op->first->sibling, $op);
2609 my $kid_text = $kid->{text};
2610 if ($type eq "uc") {
2611 $info = info_from_lists(['\U', $kid, '\E'], '', 'dq_uc', {});
2612 } elsif ($type eq "lc") {
2613 $info = info_from_lists(['\L', $kid, '\E'], '', 'dq_lc', {});
2614 } elsif ($type eq "ucfirst") {
2615 $info = info_from_lists(['\u', $kid, '\E'], '', 'dq_ucfirst', {});
2616 } elsif ($type eq "lcfirst") {
2617 $info = info_from_lists(['\l', $kid, '\E'], '', 'dq_lcfirst', {});
2618 } elsif ($type eq "quotemeta") {
2619 $info = info_from_lists(['\Q', $kid, '\E'], '', 'dq_quotemeta', {});
2620 } elsif ($type eq "fc") {
2621 $info = info_from_lists(['\F', $kid, '\E'], '', 'dq_fc', {});
2622 }
2623 $info->{body} = [$kid];
2624 return $info;
2625 }
2626  
2627 sub pp_backtick
2628 {
2629 my($self, $op, $cx) = @_;
2630 # skip pushmark if it exists (readpipe() vs ``)
2631 my $child = $op->first->sibling->isa('B::NULL')
2632 ? $op->first : $op->first->sibling;
2633 if ($self->pure_string($child)) {
2634 return $self->single_delim($op, "qx", '`', $self->dq($child, 1)->{text});
2635 }
2636 unop($self, $op, $cx, "readpipe");
2637 }
2638  
2639 # OP_STRINGIFY is a listop, but it only ever has one arg
2640 sub pp_stringify {
2641 my ($self, $op, $cx) = @_;
2642 my $kid = $op->first->sibling;
2643 my @other_ops = ();
2644 while ($kid->name eq 'null' && !null($kid->first)) {
2645 push(@other_ops, $kid);
2646 $kid = $kid->first;
2647 }
2648 my $info;
2649 if ($kid->name =~ /^(?:const|padsv|rv2sv|av2arylen|gvsv|multideref
2650 |aelemfast(?:_lex)?|[ah]elem|join|concat)\z/x) {
2651 $info = maybe_targmy(@_, \&dquote);
2652 }
2653 else {
2654 # Actually an optimised join.
2655 my $info = listop(@_,"join");
2656 $info->{text} =~ s/join([( ])/join$1$self->{'ex_const'}, /;
2657 }
2658 push @{$info->{other_ops}}, @other_ops;
2659 return $info;
2660 }
2661  
2662  
2663 # tr/// and s/// (and tr[][], tr[]//, tr###, etc)
2664 # note that tr(from)/to/ is OK, but not tr/from/(to)
2665 sub double_delim {
2666 my($from, $to) = @_;
2667 my($succeed, $delim);
2668 if ($from !~ m[/] and $to !~ m[/]) {
2669 return "/$from/$to/";
2670 } elsif (($succeed, $from) = balanced_delim($from) and $succeed) {
2671 if (($succeed, $to) = balanced_delim($to) and $succeed) {
2672 return "$from$to";
2673 } else {
2674 for $delim ('/', '"', '#') { # note no "'" -- s''' is special
2675 return "$from$delim$to$delim" if index($to, $delim) == -1;
2676 }
2677 $to =~ s[/][\\/]g;
2678 return "$from/$to/";
2679 }
2680 } else {
2681 for $delim ('/', '"', '#') { # note no '
2682 return "$delim$from$delim$to$delim"
2683 if index($to . $from, $delim) == -1;
2684 }
2685 $from =~ s[/][\\/]g;
2686 $to =~ s[/][\\/]g;
2687 return "/$from/$to/";
2688 }
2689 }
2690  
2691 # Only used by tr///, so backslashes hyphens
2692 sub pchr { # ASCII
2693 my($n) = @_;
2694 if ($n == ord '\\') {
2695 return '\\\\';
2696 } elsif ($n == ord "-") {
2697 return "\\-";
2698 } elsif ($n >= ord(' ') and $n <= ord('~')) {
2699 return chr($n);
2700 } elsif ($n == ord "\a") {
2701 return '\\a';
2702 } elsif ($n == ord "\b") {
2703 return '\\b';
2704 } elsif ($n == ord "\t") {
2705 return '\\t';
2706 } elsif ($n == ord "\n") {
2707 return '\\n';
2708 } elsif ($n == ord "\e") {
2709 return '\\e';
2710 } elsif ($n == ord "\f") {
2711 return '\\f';
2712 } elsif ($n == ord "\r") {
2713 return '\\r';
2714 } elsif ($n >= ord("\cA") and $n <= ord("\cZ")) {
2715 return '\\c' . chr(ord("@") + $n);
2716 } else {
2717 # return '\x' . sprintf("%02x", $n);
2718 return '\\' . sprintf("%03o", $n);
2719 }
2720 }
2721  
2722 sub collapse {
2723 my(@chars) = @_;
2724 my($str, $c, $tr) = ("");
2725 for ($c = 0; $c < @chars; $c++) {
2726 $tr = $chars[$c];
2727 $str .= pchr($tr);
2728 if ($c <= $#chars - 2 and $chars[$c + 1] == $tr + 1 and
2729 $chars[$c + 2] == $tr + 2)
2730 {
2731 for (; $c <= $#chars-1 and $chars[$c + 1] == $chars[$c] + 1; $c++)
2732 {}
2733 $str .= "-";
2734 $str .= pchr($chars[$c]);
2735 }
2736 }
2737 return $str;
2738 }
2739  
2740 sub tr_decode_byte {
2741 my($table, $flags) = @_;
2742 my(@table) = unpack("s*", $table);
2743 splice @table, 0x100, 1; # Number of subsequent elements
2744 my($c, $tr, @from, @to, @delfrom, $delhyphen);
2745 if ($table[ord "-"] != -1 and
2746 $table[ord("-") - 1] == -1 || $table[ord("-") + 1] == -1)
2747 {
2748 $tr = $table[ord "-"];
2749 $table[ord "-"] = -1;
2750 if ($tr >= 0) {
2751 @from = ord("-");
2752 @to = $tr;
2753 } else { # -2 ==> delete
2754 $delhyphen = 1;
2755 }
2756 }
2757 for ($c = 0; $c < @table; $c++) {
2758 $tr = $table[$c];
2759 if ($tr >= 0) {
2760 push @from, $c; push @to, $tr;
2761 } elsif ($tr == -2) {
2762 push @delfrom, $c;
2763 }
2764 }
2765 @from = (@from, @delfrom);
2766 if ($flags & OPpTRANS_COMPLEMENT) {
2767 my @newfrom = ();
2768 my %from;
2769 @from{@from} = (1) x @from;
2770 for ($c = 0; $c < 256; $c++) {
2771 push @newfrom, $c unless $from{$c};
2772 }
2773 @from = @newfrom;
2774 }
2775 unless ($flags & OPpTRANS_DELETE || !@to) {
2776 pop @to while $#to and $to[$#to] == $to[$#to -1];
2777 }
2778 my($from, $to);
2779 $from = collapse(@from);
2780 $to = collapse(@to);
2781 $from .= "-" if $delhyphen;
2782 return ($from, $to);
2783 }
2784  
2785 sub tr_chr {
2786 my $x = shift;
2787 if ($x == ord "-") {
2788 return "\\-";
2789 } elsif ($x == ord "\\") {
2790 return "\\\\";
2791 } else {
2792 return chr $x;
2793 }
2794 }
2795  
2796 # XXX This doesn't yet handle all cases correctly either
2797  
2798 sub tr_decode_utf8 {
2799 my($swash_hv, $flags) = @_;
2800 my %swash = $swash_hv->ARRAY;
2801 my $final = undef;
2802 $final = $swash{'FINAL'}->IV if exists $swash{'FINAL'};
2803 my $none = $swash{"NONE"}->IV;
2804 my $extra = $none + 1;
2805 my(@from, @delfrom, @to);
2806 my $line;
2807 foreach $line (split /\n/, $swash{'LIST'}->PV) {
2808 my($min, $max, $result) = split(/\t/, $line);
2809 $min = hex $min;
2810 if (length $max) {
2811 $max = hex $max;
2812 } else {
2813 $max = $min;
2814 }
2815 $result = hex $result;
2816 if ($result == $extra) {
2817 push @delfrom, [$min, $max];
2818 } else {
2819 push @from, [$min, $max];
2820 push @to, [$result, $result + $max - $min];
2821 }
2822 }
2823 for my $i (0 .. $#from) {
2824 if ($from[$i][0] == ord '-') {
2825 unshift @from, splice(@from, $i, 1);
2826 unshift @to, splice(@to, $i, 1);
2827 last;
2828 } elsif ($from[$i][1] == ord '-') {
2829 $from[$i][1]--;
2830 $to[$i][1]--;
2831 unshift @from, ord '-';
2832 unshift @to, ord '-';
2833 last;
2834 }
2835 }
2836 for my $i (0 .. $#delfrom) {
2837 if ($delfrom[$i][0] == ord '-') {
2838 push @delfrom, splice(@delfrom, $i, 1);
2839 last;
2840 } elsif ($delfrom[$i][1] == ord '-') {
2841 $delfrom[$i][1]--;
2842 push @delfrom, ord '-';
2843 last;
2844 }
2845 }
2846 if (defined $final and $to[$#to][1] != $final) {
2847 push @to, [$final, $final];
2848 }
2849 push @from, @delfrom;
2850 if ($flags & OPpTRANS_COMPLEMENT) {
2851 my @newfrom;
2852 my $next = 0;
2853 for my $i (0 .. $#from) {
2854 push @newfrom, [$next, $from[$i][0] - 1];
2855 $next = $from[$i][1] + 1;
2856 }
2857 @from = ();
2858 for my $range (@newfrom) {
2859 if ($range->[0] <= $range->[1]) {
2860 push @from, $range;
2861 }
2862 }
2863 }
2864 my($from, $to, $diff);
2865 for my $chunk (@from) {
2866 $diff = $chunk->[1] - $chunk->[0];
2867 if ($diff > 1) {
2868 $from .= tr_chr($chunk->[0]) . "-" . tr_chr($chunk->[1]);
2869 } elsif ($diff == 1) {
2870 $from .= tr_chr($chunk->[0]) . tr_chr($chunk->[1]);
2871 } else {
2872 $from .= tr_chr($chunk->[0]);
2873 }
2874 }
2875 for my $chunk (@to) {
2876 $diff = $chunk->[1] - $chunk->[0];
2877 if ($diff > 1) {
2878 $to .= tr_chr($chunk->[0]) . "-" . tr_chr($chunk->[1]);
2879 } elsif ($diff == 1) {
2880 $to .= tr_chr($chunk->[0]) . tr_chr($chunk->[1]);
2881 } else {
2882 $to .= tr_chr($chunk->[0]);
2883 }
2884 }
2885 #$final = sprintf("%04x", $final) if defined $final;
2886 #$none = sprintf("%04x", $none) if defined $none;
2887 #$extra = sprintf("%04x", $extra) if defined $extra;
2888 #print STDERR "final: $final\n none: $none\nextra: $extra\n";
2889 #print STDERR $swash{'LIST'}->PV;
2890 return (B::Deparse::escape_str($from), B::Deparse::escape_str($to));
2891 }
2892  
2893 sub pp_trans {
2894 my $self = shift;
2895 my($op, $cx) = @_;
2896 my($from, $to);
2897 my $class = class($op);
2898 my $priv_flags = $op->private;
2899 if ($class eq "PVOP") {
2900 ($from, $to) = tr_decode_byte($op->pv, $priv_flags);
2901 } elsif ($class eq "PADOP") {
2902 ($from, $to)
2903 = tr_decode_utf8($self->padval($op->padix)->RV, $priv_flags);
2904 } else { # class($op) eq "SVOP"
2905 ($from, $to) = tr_decode_utf8($op->sv->RV, $priv_flags);
2906 }
2907 my $flags = "";
2908 $flags .= "c" if $priv_flags & OPpTRANS_COMPLEMENT;
2909 $flags .= "d" if $priv_flags & OPpTRANS_DELETE;
2910 $to = "" if $from eq $to and $flags eq "";
2911 $flags .= "s" if $priv_flags & OPpTRANS_SQUASH;
2912 return info_from_list($op, $self, ['tr', double_delim($from, $to), $flags],
2913 '', 'pp_trans', {});
2914 }
2915  
2916 sub pp_transr {
2917 my $self = $_[0];
2918 my $op = $_[1];
2919 my $info = pp_trans(@_);
2920 return info_from_text($op, $self, $info->{text} . 'r', 'pp_transr',
2921 {body => [$info]});
2922 }
2923  
2924 sub re_dq_disambiguate {
2925 my ($first, $last) = @_;
2926 # Disambiguate "${foo}bar", "${foo}{bar}", "${foo}[1]"
2927 ($last =~ /^[A-Z\\\^\[\]_?]/ &&
2928 $first =~ s/([\$@])\^$/${1}{^}/) # "${^}W" etc
2929 || ($last =~ /^[{\[\w_]/ &&
2930 $first =~ s/([\$@])([A-Za-z_]\w*)$/${1}{$2}/);
2931 return $first . $last;
2932 }
2933  
2934 # Like dq(), but different
2935 sub re_dq {
2936 my $self = shift;
2937 my ($op, $extended) = @_;
2938  
2939 my $type = $op->name;
2940 my ($re, @texts);
2941 my $opts = {};
2942 if ($type eq "const") {
2943 return info_from_text($op, $self, '$[', 're_dq_const', {})
2944 if $op->private & OPpCONST_ARYBASE;
2945 my $unbacked = B::Deparse::re_unback($self->const_sv($op)->as_string);
2946 return B::Deparse::re_uninterp_extended(escape_extended_re($unbacked))
2947 if $extended;
2948 return B::Deparse::re_uninterp(B::Deparse::escape_str($unbacked));
2949 } elsif ($type eq "concat") {
2950 my $first = $self->re_dq($op->first, $extended);
2951 my $last = $self->re_dq($op->last, $extended);
2952 return re_dq_disambiguate($first, $last);
2953 } elsif ($type eq "uc") {
2954 $re = $self->re_dq($op->first->sibling, $extended);
2955 @texts = ['\U', $re->{text},'\E'];
2956 $type = 're_dq_U';
2957 } elsif ($type eq "lc") {
2958 $re = $self->re_dq($op->first->sibling, $extended);
2959 @texts = ['\L', $re->{text},'\E'];
2960 $type = 're_dq_L';
2961 } elsif ($type eq "ucfirst") {
2962 $re = $self->re_dq($op->first->sibling, $extended);
2963 @texts = ['\u', $re->{text}];
2964 $type = 're_dq_u';
2965 } elsif ($type eq "lcfirst") {
2966 $re = $self->re_dq($op->first->sibling, $extended);
2967 @texts = ['\l', $re->{text}];
2968 $type = 're_dq_l';
2969 } elsif ($type eq "quotemeta") {
2970 $re = $self->re_dq($op->first->sibling, $extended);
2971 @texts = ['\Q', $re->{text},'\E'];
2972 $type = 're_dq_Q';
2973 } elsif ($type eq "fc") {
2974 $re = $self->re_dq($op->first->sibling, $extended);
2975 @texts = ['\F', $re->{text},'\E'];
2976 $type = 're_dq_Q';
2977 } elsif ($type eq "join") {
2978 return $self->deparse($op->last, 26, $op); # was join($", @ary)
2979 } else {
2980 my $info = $self->deparse($op, 26, $op);
2981 $info->{type} = 're_dq';
2982 $info->{text} =~ s/^\$([(|)])\z/\${$1}/; # $( $| $) need braces
2983 return $info;
2984 }
2985 return info_from_list($op, $self, \@texts, '', $type, $opts);
2986 }
2987  
2988 sub pure_string {
2989 my ($self, $op) = @_;
2990 return 0 if null $op;
2991 my $type = $op->name;
2992  
2993 if ($type eq 'const' || $type eq 'av2arylen') {
2994 return 1;
2995 }
2996 elsif ($type =~ /^(?:[ul]c(first)?|fc)$/ || $type eq 'quotemeta') {
2997 return $self->pure_string($op->first->sibling);
2998 }
2999 elsif ($type eq 'join') {
3000 my $join_op = $op->first->sibling; # Skip pushmark
3001 return 0 unless $join_op->name eq 'null' && $join_op->targ == OP_RV2SV;
3002  
3003 my $gvop = $join_op->first;
3004 return 0 unless $gvop->name eq 'gvsv';
3005 return 0 unless '"' eq $self->gv_name($self->gv_or_padgv($gvop));
3006  
3007 return 0 unless ${$join_op->sibling} eq ${$op->last};
3008 return 0 unless $op->last->name =~ /^(?:[ah]slice|(?:rv2|pad)av)$/;
3009 }
3010 elsif ($type eq 'concat') {
3011 return $self->pure_string($op->first)
3012 && $self->pure_string($op->last);
3013 }
3014 elsif (is_scalar($op) || $type =~ /^[ah]elem$/) {
3015 return 1;
3016 }
3017 elsif ($type eq "null" and $op->can('first') and not null $op->first and
3018 ($op->first->name eq "null" and $op->first->can('first')
3019 and not null $op->first->first and
3020 $op->first->first->name eq "aelemfast"
3021 or
3022 $op->first->name =~ /^aelemfast(?:_lex)?\z/
3023 )) {
3024 return 1;
3025 }
3026 else {
3027 return 0;
3028 }
3029  
3030 return 1;
3031 }
3032  
3033 sub regcomp
3034 {
3035 my($self, $op, $cx, $extended) = @_;
3036 my @other_ops = ();
3037 my $kid = $op->first;
3038 if ($kid->name eq "regcmaybe") {
3039 push @other_ops, $kid;
3040 $kid = $kid->first;
3041 }
3042 if ($kid->name eq "regcreset") {
3043 push @other_ops, $kid;
3044 $kid = $kid->first;
3045 }
3046 if ($kid->name eq "null" and !null($kid->first)
3047 and $kid->first->name eq 'pushmark') {
3048 my $str = '';
3049 push(@other_ops, $kid);
3050 $kid = $kid->first->sibling;
3051 my @body = ();
3052 while (!null($kid)) {
3053 my $first = $str;
3054 my $last = $self->re_dq($kid, $extended);
3055 push @body, $last;
3056 push(@other_ops, $kid);
3057 $str = re_dq_disambiguate($first, $last->{text});
3058 $kid = $kid->sibling;
3059 }
3060 return (info_from_text($op, $self, $str, 'regcomp',
3061 {other_ops => \@other_ops,
3062 body => \@body}), 1);
3063 }
3064  
3065 if ($self->pure_string($kid)) {
3066 my $info = $self->re_dq($kid, $extended);
3067 my @kid_ops = $info->{other_ops} ? @{$info->{other_ops}} : ();
3068 push @other_ops, @kid_ops;
3069 $info->{other_ops} = \@other_ops;
3070 return ($info, 1);
3071 }
3072 return ($self->deparse($kid, $cx, $op), 0, $op);
3073 }
3074  
3075 sub pp_regcomp
3076 {
3077 my ($self, $op, $cx) = @_;
3078 return (($self->regcomp($op, $cx, 0))[0]);
3079 }
3080  
3081 sub re_flags
3082 {
3083 my ($self, $op) = @_;
3084 my $flags = '';
3085 my $pmflags = $op->pmflags;
3086 $flags .= "g" if $pmflags & PMf_GLOBAL;
3087 $flags .= "i" if $pmflags & PMf_FOLD;
3088 $flags .= "m" if $pmflags & PMf_MULTILINE;
3089 $flags .= "o" if $pmflags & PMf_KEEP;
3090 $flags .= "s" if $pmflags & PMf_SINGLELINE;
3091 $flags .= "x" if $pmflags & PMf_EXTENDED;
3092 $flags .= "p" if $pmflags & RXf_PMf_KEEPCOPY;
3093 if (my $charset = $pmflags & RXf_PMf_CHARSET) {
3094 # Hardcoding this is fragile, but B does not yet export the
3095 # constants we need.
3096 $flags .= qw(d l u a aa)[$charset >> 5]
3097 }
3098 # The /d flag is indicated by 0; only show it if necessary.
3099 elsif ($self->{hinthash} and
3100 $self->{hinthash}{reflags_charset}
3101 || $self->{hinthash}{feature_unicode}
3102 or $self->{hints} & $feature::hint_mask
3103 && ($self->{hints} & $feature::hint_mask)
3104 != $feature::hint_mask
3105 && do {
3106 $self->{hints} & $feature::hint_uni8bit;
3107 }
3108 ) {
3109 $flags .= 'd';
3110 }
3111 $flags;
3112 }
3113  
3114 # osmic acid -- see osmium tetroxide
3115  
3116 my %matchwords;
3117 map($matchwords{join "", sort split //, $_} = $_, 'cig', 'cog', 'cos', 'cogs',
3118 'cox', 'go', 'is', 'ism', 'iso', 'mig', 'mix', 'osmic', 'ox', 'sic',
3119 'sig', 'six', 'smog', 'so', 'soc', 'sog', 'xi');
3120  
3121 sub matchop
3122 {
3123 my($self, $op, $cx, $name, $delim) = @_;
3124 my $kid = $op->first;
3125 my $info = {};
3126 my @body = ();
3127 my ($binop, $var, $re_str) = ("", "", "");
3128 my $re;
3129 if ($op->flags & OPf_STACKED) {
3130 $binop = 1;
3131 $var = $self->deparse($kid, 20, $op);
3132 push @body, $var;
3133 $kid = $kid->sibling;
3134 }
3135 my $quote = 1;
3136 my $pmflags = $op->pmflags;
3137 my $extended = ($pmflags & PMf_EXTENDED);
3138 my $rhs_bound_to_defsv;
3139 if (null $kid) {
3140 my $unbacked = B::Deparse::re_unback($op->precomp);
3141 if ($extended) {
3142 $re_str = B::Deparse::re_uninterp_extended(B::Deparse::escape_extended_re($unbacked));
3143 } else {
3144 $re_str = B::Deparse::re_uninterp(B::Deparse::escape_str(B::Deparse::re_unback($op->precomp)));
3145 }
3146 } elsif ($kid->name ne 'regcomp') {
3147 carp("found ".$kid->name." where regcomp expected");
3148 } else {
3149 ($re, $quote) = $self->regcomp($kid, 21, $extended);
3150 push @body, $re;
3151 $re_str = $re->{text};
3152 my $matchop = $kid->first;
3153 if ($matchop->name eq 'regcrest') {
3154 $matchop = $matchop->first;
3155 }
3156 if ($matchop->name =~ /^(?:match|transr?|subst)\z/
3157 && $matchop->flags & OPf_SPECIAL) {
3158 $rhs_bound_to_defsv = 1;
3159 }
3160 }
3161 my $flags = '';
3162 $flags .= "c" if $pmflags & PMf_CONTINUE;
3163 $flags .= $self->re_flags($op);
3164 $flags = join '', sort split //, $flags;
3165 $flags = $matchwords{$flags} if $matchwords{$flags};
3166  
3167 if ($pmflags & PMf_ONCE) { # only one kind of delimiter works here
3168 $re_str =~ s/\?/\\?/g;
3169 $re_str = "?$re_str?";
3170 } elsif ($quote) {
3171 my $re = $self->single_delim($kid, $name, $delim, $re_str);
3172 push @body, $re;
3173 $re_str = $re->{text};
3174 }
3175 my $opts = {body => \@body};
3176 my @texts;
3177 $re_str .= $flags if $quote;
3178 my $type;
3179 if ($binop) {
3180 if ($rhs_bound_to_defsv) {
3181 @texts = ($var->{text}, ' =~ ', "(", '$_', ' =~ ', $re_str, ')');
3182 } else {
3183 @texts = ($var->{text}, ' =~ ', $re_str);
3184 }
3185 $opts->{maybe_parens} = [$self, $cx, 20];
3186 $type = 'matchop_binop';
3187 } else {
3188 @texts = ($re_str);
3189 $type = 'matchop_unnop';
3190 }
3191 return info_from_list($op, $self, \@texts, '', $type, $opts);
3192 }
3193  
3194 sub pp_match { matchop(@_, "m", "/") }
3195 sub pp_pushre { matchop(@_, "m", "/") }
3196 sub pp_qr { matchop(@_, "qr", "") }
3197  
3198 sub pp_runcv { unop(@_, "__SUB__"); }
3199  
3200 sub pp_split {
3201 maybe_targmy(@_, \&split, "split");
3202 }
3203  
3204 sub split
3205 {
3206 my($self, $op, $cx) = @_;
3207 my($kid, @exprs, $ary_info, $expr);
3208 my $ary = '';
3209 my @body = ();
3210 my @other_ops = ();
3211 $kid = $op->first;
3212  
3213 # For our kid (an OP_PUSHRE), pmreplroot is never actually the
3214 # root of a replacement; it's either empty, or abused to point to
3215 # the GV for an array we split into (an optimization to save
3216 # assignment overhead). Depending on whether we're using ithreads,
3217 # this OP* holds either a GV* or a PADOFFSET. Luckily, B.xs
3218 # figures out for us which it is.
3219 my $replroot = $kid->pmreplroot;
3220 my $gv = 0;
3221 my $stacked = $op->flags & OPf_STACKED;
3222 if (ref($replroot) eq "B::GV") {
3223 $gv = $replroot;
3224 } elsif (!ref($replroot) and $replroot > 0) {
3225 $gv = $self->padval($replroot);
3226 } elsif ($kid->targ) {
3227 $ary = $self->padname($kid->targ)
3228 } elsif ($stacked) {
3229 $ary_info = $self->deparse($op->last, 7, $op);
3230 push @body, $ary_info;
3231 $ary = $ary_info->{text};
3232 }
3233 $ary_info = $self->maybe_local(@_,
3234 $self->stash_variable('@',
3235 $self->gv_name($gv),
3236 $cx))
3237 if $gv;
3238  
3239 # Skip the last kid when OPf_STACKED is set, since it is the array
3240 # on the left.
3241 for (; !null($stacked ? $kid->sibling : $kid); $kid = $kid->sibling) {
3242 push @exprs, $self->deparse($kid, 6, $op);
3243 }
3244  
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 # Note 5.20 and up
3365 sub pp_null
3366 {
3367 my($self, $op, $cx) = @_;
3368 my $info;
3369 if (class($op) eq "OP") {
3370 if ($op->targ == B::Deparse::OP_CONST) {
3371 # The Perl source constant value can't be recovered.
3372 # We'll use the 'ex_const' value as a substitute
3373 return info_from_text($op, $self, $self->{'ex_const'}, 'constant unrecoverable', {})
3374 } else {
3375 return info_from_text($op, $self, '', 'constant ""', {});
3376 }
3377 } elsif (class ($op) eq "COP") {
3378 return $self->pp_nextstate($op, $cx);
3379 }
3380 my $kid = $op->first;
3381 if ($op->first->name eq 'pushmark'
3382 or $op->first->name eq 'null'
3383 && $op->first->targ == B::Deparse::OP_PUSHMARK
3384 && B::Deparse::_op_is_or_was($op, B::Deparse::OP_LIST)) {
3385 return $self->pp_list($op, $cx);
3386 } elsif ($kid->name eq "enter") {
3387 return $self->pp_leave($op, $cx);
3388 } elsif ($kid->name eq "leave") {
3389 return $self->pp_leave($kid, $cx);
3390 } elsif ($kid->name eq "scope") {
3391 return $self->pp_scope($kid, $cx);
3392 } elsif ($op->targ == B::Deparse::OP_STRINGIFY) {
3393 return $self->dquote($op, $cx);
3394 } elsif ($op->targ == B::Deparse::OP_GLOB) {
3395 my @other_ops = ($kid, $kid->first, $kid->first->first);
3396 my $info = $self->pp_glob(
3397 $kid # entersub
3398 ->first # ex-list
3399 ->first # pushmark
3400 ->sibling, # glob
3401 $cx
3402 );
3403 push @{$info->{other_ops}}, @other_ops;
3404 return $info;
3405 } elsif (!null($kid->sibling) and
3406 $kid->sibling->name eq "readline" and
3407 $kid->sibling->flags & OPf_STACKED) {
3408 my $lhs = $self->deparse($kid, 7, $op);
3409 my $rhs = $self->deparse($kid->sibling, 7, $kid);
3410 return $self->bin_info_join_maybe_parens($op, $lhs, $rhs, '=', " ", $cx, 7,
3411 'readline');
3412 } elsif (!null($kid->sibling) and
3413 $kid->sibling->name eq "trans" and
3414 $kid->sibling->flags & OPf_STACKED) {
3415 my $lhs = $self->deparse($kid, 20, $op);
3416 my $rhs = $self->deparse($kid->sibling, 20, $op);
3417 return $self->bin_info_join_maybe_parens($op, $lhs, $rhs, '=~', " ", $cx, 20,
3418 'trans');
3419 } elsif ($op->flags & OPf_SPECIAL && $cx < 1 && !$op->targ) {
3420 my $kid_info = $self->deparse($kid, $cx, $op);
3421 return info_from_list($op, $self, ['do', "{\n\t", $kid_info->{text},
3422 "\n\b};"], '', 'null_special',
3423 {body => [$kid_info]});
3424 } elsif (!null($kid->sibling) and
3425 $kid->sibling->name eq "null" and
3426 class($kid->sibling) eq "UNOP" and
3427 $kid->sibling->first->flags & OPf_STACKED and
3428 $kid->sibling->first->name eq "rcatline") {
3429 my $lhs = $self->deparse($kid, 18, $op);
3430 my $rhs = $self->deparse($kid->sibling, 18, $op);
3431 return $self->bin_info_join_maybe_parens($op, $lhs, $rhs, '=', " ", $cx, 20,
3432 'null_rcatline');
3433 } else {
3434 return $self->deparse($kid, $cx, $op);
3435 }
3436 Carp::confess("unhandled condition in null");
3437 }
3438  
3439 sub pp_clonecv {
3440 my $self = shift;
3441 my($op, $cx) = @_;
3442 my $sv = $self->padname_sv($op->targ);
3443 my $name = substr $sv->PVX, 1; # skip &/$/@/%, like $self->padany
3444 return info_from_list($op, $self, ['my', 'sub', $name], ' ', 'clonev', {});
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;