2 # Copyright (c) 1998 Stephen McCamant. All rights reserved.
3 # This module is free software; you can redistribute and/or modify
4 # it under the same terms as Perl itself.
6 # This is based on the module of the same name by Malcolm Beattie,
7 # but essentially none of his code remains.
11 use B qw(class main_root main_start main_cv svref_2object);
15 # Changes between 0.50 and 0.51:
16 # - fixed nulled leave with live enter in sort { }
17 # - fixed reference constants (\"str")
18 # - handle empty programs gracefully
19 # - handle infinte loops (for (;;) {}, while (1) {})
20 # - differentiate between `for my $x ...' and `my $x; for $x ...'
21 # - various minor cleanups
22 # - moved globals into an object
23 # - added `-u', like B::C
24 # - package declarations using cop_stash
25 # - subs, formats and code sorted by cop_seq
26 # Changes between 0.51 and 0.52:
27 # - added pp_threadsv (special variables under USE_THREADS)
28 # - added documentation
29 # Changes between 0.52 and 0.53
30 # - many changes adding precedence contexts and associativity
31 # - added `-p' and `-s' output style options
32 # - various other minor fixes
33 # Changes between 0.53 and 0.54
34 # - added support for new `for (1..100)' optimization,
36 # Changes between 0.54 and 0.55
37 # - added support for new qr// construct
38 # - added support for new pp_regcreset OP
41 # - {} around variables in strings ("${var}letters")
42 # - left/right context
43 # - associativity of &&=, ||=, ?:
44 # - ',' => '=>' (auto-unquote?)
45 # - break long lines ("\r" as discretionary break?)
46 # - version using op_next instead of op_first/sibling?
47 # - avoid string copies (pass arrays, one big join?)
49 # - while{} with one-statement continue => for(; XXX; XXX) {}?
50 # - -uPackage:: descend recursively?
54 # Object fields (were globals):
57 # (local($a), local($b)) and local($a, $b) have the same internal
58 # representation but the short form looks better. We notice we can
59 # use a large-scale local when checking the list, but need to prevent
60 # individual locals too. This hash holds the addresses of OPs that
61 # have already had their local-ness accounted for. The same thing
65 # CV for current sub (or main program) being deparsed
68 # name of the current package for deparsed code
71 # array of [cop_seq, GV, is_format?] for subs and formats we still
74 # subs_done, forms_done:
75 # keys are addresses of GVs for subs and formats we've already
76 # deparsed (or at least put into subs_todo)
79 # cuddle: ` ' or `\n', depending on -sC
81 # A little explanation of how precedence contexts and associativity
84 # deparse() calls each per-op subroutine with an argument $cx (short
85 # for context, but not the same as the cx* in the perl core), which is
86 # a number describing the op's parents in terms of precedence, whether
87 # they're inside and expression or at statement level, etc. (see
88 # chart below). When ops with children call deparse on them, they pass
89 # along their precedence. Fractional values are used to implement
90 # associativity (`($x + $y) + $z' => `$x + $y + $y') and related
91 # parentheses hacks. The major disadvantage of this scheme is that
92 # it doesn't know about right sides and left sides, so say if you
93 # assign a listop to a variable, it can't tell it's allowed to leave
94 # the parens off the listop.
97 # 26 [TODO] inside interpolation context ("")
98 # 25 left terms and list operators (leftward)
102 # 21 right ! ~ \ and unary + and -
107 # 16 nonassoc named unary operators
108 # 15 nonassoc < > <= >= lt gt le ge
109 # 14 nonassoc == != <=> eq ne cmp
116 # 7 right = += -= *= etc.
118 # 5 nonassoc list operators (rightward)
122 # 1 statement modifiers
125 # Nonprinting characters with special meaning:
126 # \cS - steal parens (see maybe_parens_unop)
127 # \n - newline and indent
128 # \t - increase indent
129 # \b - decrease indent (`outdent')
130 # \cK - kill following semicolon, if any
134 return class($op) eq "NULL";
139 my($gv, $cv, $is_form) = @_;
141 if (!null($cv->START) and is_state($cv->START)) {
142 $seq = $cv->START->cop_seq;
146 push @{$self->{'subs_todo'}}, [$seq, $gv, $is_form];
151 my $ent = shift @{$self->{'subs_todo'}};
152 my $name = $self->gv_name($ent->[1]);
154 return "format $name =\n"
155 . $self->deparse_format($ent->[1]->FORM). "\n";
157 return "sub $name " .
158 $self->deparse_sub($ent->[1]->CV);
162 sub OPf_KIDS () { 4 }
167 if ($op->flags & OPf_KIDS) {
169 for ($kid = $op->first; not null $kid; $kid = $kid->sibling) {
170 walk_tree($kid, $sub);
179 $op = shift if null $op;
180 return if !$op or null $op;
183 if ($op->ppaddr eq "pp_gv") {
184 if ($op->next->ppaddr eq "pp_entersub") {
185 next if $self->{'subs_done'}{$ {$op->gv}}++;
186 next if class($op->gv->CV) eq "SPECIAL";
187 $self->todo($op->gv, $op->gv->CV, 0);
188 $self->walk_sub($op->gv->CV);
189 } elsif ($op->next->ppaddr eq "pp_enterwrite"
190 or ($op->next->ppaddr eq "pp_rv2gv"
191 and $op->next->next->ppaddr eq "pp_enterwrite")) {
192 next if $self->{'forms_done'}{$ {$op->gv}}++;
193 next if class($op->gv->FORM) eq "SPECIAL";
194 $self->todo($op->gv, $op->gv->FORM, 1);
195 $self->walk_sub($op->gv->FORM);
205 { no strict 'refs'; %stash = svref_2object(\%{$pack . "::"})->ARRAY }
207 while (($key, $val) = each %stash) {
208 next unless class($val) eq "GV";
209 if (class($val->CV) ne "SPECIAL") {
210 next if $self->{'subs_done'}{$$val}++;
211 $self->todo($val, $val->CV, 0);
212 $self->walk_sub($val->CV);
214 if (class($val->FORM) ne "SPECIAL") {
215 next if $self->{'forms_done'}{$$val}++;
216 $self->todo($val, $val->FORM, 1);
217 $self->walk_sub($val->FORM);
226 while (length($opt = substr($opts, 0, 1))) {
228 $self->{'cuddle'} = " ";
230 $opts = substr($opts, 1);
239 $self->{'subs_todo'} = [];
240 $self->stash_subs("main");
241 $self->{'curcv'} = main_cv;
242 $self->{'curstash'} = "main";
243 $self->{'cuddle'} = "\n";
244 while ($arg = shift @args) {
245 if (substr($arg, 0, 2) eq "-u") {
246 $self->stash_subs(substr($arg, 2));
247 } elsif ($arg eq "-p") {
248 $self->{'parens'} = 1;
249 } elsif (substr($arg, 0, 2) eq "-s") {
250 $self->style_opts(substr $arg, 2);
253 $self->walk_sub(main_cv, main_start);
254 @{$self->{'subs_todo'}} =
255 sort {$a->[0] <=> $b->[0]} @{$self->{'subs_todo'}};
256 print indent($self->deparse(main_root, 0)), "\n" unless null main_root;
258 while (scalar(@{$self->{'subs_todo'}})) {
259 push @text, $self->next_todo;
261 print indent(join("", @text)), "\n" if @text;
268 # cluck if class($op) eq "NULL";
269 my $meth = $op->ppaddr;
270 return $self->$meth($op, $cx);
275 my @lines = split(/\n/, $txt);
279 if (substr($line, 0, 1) eq "\t") {
280 $leader = $leader . " ";
281 $line = substr($line, 1);
282 } elsif (substr($line, 0, 1) eq "\b") {
283 $leader = substr($leader, 0, length($leader) - 4);
284 $line = substr($line, 1);
287 $line = $leader . $line;
289 return join("\n", @lines);
292 sub SVf_POK () {0x40000}
298 if ($cv->FLAGS & SVf_POK) {
299 $proto = "(". $cv->PV . ") ";
301 local($self->{'curcv'}) = $cv;
302 local($self->{'curstash'}) = $self->{'curstash'};
303 if (not null $cv->ROOT) {
305 return $proto . "{\n\t" .
306 $self->deparse($cv->ROOT->first, 0) . "\n\b}\n";
308 return $proto . "{}\n";
316 local($self->{'curcv'}) = $form;
317 local($self->{'curstash'}) = $self->{'curstash'};
318 my $op = $form->ROOT;
320 $op = $op->first->first; # skip leavewrite, lineseq
321 while (not null $op) {
322 $op = $op->sibling; # skip nextstate
324 $kid = $op->first->sibling; # skip pushmark
325 push @text, $kid->sv->PV;
326 $kid = $kid->sibling;
327 for (; not null $kid; $kid = $kid->sibling) {
328 push @exprs, $self->deparse($kid, 0);
330 push @text, join(", ", @exprs)."\n" if @exprs;
333 return join("", @text) . ".";
336 # the aassign in-common check messes up SvCUR (always setting it
337 # to a value >= 100), but it's probably safe to assume there
338 # won't be any NULs in the names of my() variables. (with
339 # stash variables, I wouldn't be so sure)
342 $str = substr($str, 0, index($str, "\0")) if index($str, "\0") != -1;
348 return $op->ppaddr eq "pp_leave" || $op->ppaddr eq "pp_scope"
349 || $op->ppaddr eq "pp_lineseq"
350 || ($op->ppaddr eq "pp_null" && class($op) eq "UNOP"
351 && (is_scope($op->first) || $op->first->ppaddr eq "pp_enter"));
355 my $name = $_[0]->ppaddr;
356 return $name eq "pp_nextstate" || $name eq "pp_dbstate";
359 sub is_miniwhile { # check for one-line loop (`foo() while $y--')
361 return (!null($op) and null($op->sibling)
362 and $op->ppaddr eq "pp_null" and class($op) eq "UNOP"
363 and (($op->first->ppaddr =~ /^pp_(and|or)$/
364 and $op->first->first->sibling->ppaddr eq "pp_lineseq")
365 or ($op->first->ppaddr eq "pp_lineseq"
366 and not null $op->first->first->sibling
367 and $op->first->first->sibling->ppaddr eq "pp_unstack")
373 return ($op->ppaddr eq "pp_rv2sv" or
374 $op->ppaddr eq "pp_padsv" or
375 $op->ppaddr eq "pp_gv" or # only in array/hash constructs
376 !null($op->first) && $op->first->ppaddr eq "pp_gvsv");
381 my($text, $cx, $prec) = @_;
382 if ($prec < $cx # unary ops nest just fine
383 or $prec == $cx and $cx != 4 and $cx != 16 and $cx != 21
384 or $self->{'parens'})
387 # In a unop, let parent reuse our parens; see maybe_parens_unop
388 $text = "\cS" . $text if $cx == 16;
395 # same as above, but get around the `if it looks like a function' rule
396 sub maybe_parens_unop {
398 my($name, $kid, $cx) = @_;
399 if ($cx > 16 or $self->{'parens'}) {
400 return "$name(" . $self->deparse($kid, 1) . ")";
402 $kid = $self->deparse($kid, 16);
403 if (substr($kid, 0, 1) eq "\cS") {
405 return $name . substr($kid, 1);
406 } elsif (substr($kid, 0, 1) eq "(") {
407 # avoid looks-like-a-function trap with extra parens
408 # (`+' can lead to ambiguities)
409 return "$name(" . $kid . ")";
416 sub maybe_parens_func {
418 my($func, $text, $cx, $prec) = @_;
419 if ($prec <= $cx or substr($text, 0, 1) eq "(" or $self->{'parens'}) {
420 return "$func($text)";
422 return "$func $text";
426 sub OPp_LVAL_INTRO () { 128 }
430 my($op, $cx, $text) = @_;
431 if ($op->private & OPp_LVAL_INTRO and not $self->{'avoid_local'}{$$op}) {
432 return $self->maybe_parens_func("local", $text, $cx, 16);
441 return (($self->{'curcv'}->PADLIST->ARRAY)[0]->ARRAY)[$targ];
446 my($op, $cx, $text) = @_;
447 if ($op->private & OPp_LVAL_INTRO and not $self->{'avoid_local'}{$$op}) {
448 return $self->maybe_parens_func("my", $text, $cx, 16);
454 # The following OPs don't have functions:
456 # pp_padany -- does not exist after parsing
457 # pp_rcatline -- does not exist
459 sub pp_enter { # see also leave
460 cluck "unexpected OP_ENTER";
464 sub pp_pushmark { # see also list
465 cluck "unexpected OP_PUSHMARK";
469 sub pp_leavesub { # see also deparse_sub
470 cluck "unexpected OP_LEAVESUB";
474 sub pp_leavewrite { # see also deparse_format
475 cluck "unexpected OP_LEAVEWRITE";
479 sub pp_method { # see also entersub
480 cluck "unexpected OP_METHOD";
484 sub pp_regcmaybe { # see also regcomp
485 cluck "unexpected OP_REGCMAYBE";
489 sub pp_regcreset { # see also regcomp
490 cluck "unexpected OP_REGCRESET";
494 sub pp_substcont { # see also subst
495 cluck "unexpected OP_SUBSTCONT";
499 sub pp_grepstart { # see also grepwhile
500 cluck "unexpected OP_GREPSTART";
504 sub pp_mapstart { # see also mapwhile
505 cluck "unexpected OP_MAPSTART";
509 sub pp_flip { # see also flop
510 cluck "unexpected OP_FLIP";
514 sub pp_iter { # see also leaveloop
515 cluck "unexpected OP_ITER";
519 sub pp_enteriter { # see also leaveloop
520 cluck "unexpected OP_ENTERITER";
524 sub pp_enterloop { # see also leaveloop
525 cluck "unexpected OP_ENTERLOOP";
529 sub pp_leaveeval { # see also entereval
530 cluck "unexpected OP_LEAVEEVAL";
534 sub pp_entertry { # see also leavetry
535 cluck "unexpected OP_ENTERTRY";
539 # leave and scope/lineseq should probably share code
545 local($self->{'curstash'}) = $self->{'curstash'};
546 $kid = $op->first->sibling; # skip enter
547 if (is_miniwhile($kid)) {
548 my $top = $kid->first;
549 my $name = $top->ppaddr;
550 if ($name eq "pp_and") {
552 } elsif ($name eq "pp_or") {
554 } else { # no conditional -> while 1 or until 0
555 return $self->deparse($top->first, 1) . " while 1";
557 my $cond = $top->first;
558 my $body = $cond->sibling->first; # skip lineseq
559 $cond = $self->deparse($cond, 1);
560 $body = $self->deparse($body, 1);
561 return "$body $name $cond";
563 for (; !null($kid); $kid = $kid->sibling) {
566 $expr = $self->deparse($kid, 0);
567 $kid = $kid->sibling;
570 $expr .= $self->deparse($kid, 0);
571 push @exprs, $expr if $expr;
573 if ($cx > 0) { # inside an expression
574 return "do { " . join(";\n", @exprs) . " }";
576 return join(";\n", @exprs);
585 for ($kid = $op->first; !null($kid); $kid = $kid->sibling) {
588 $expr = $self->deparse($kid, 0);
589 $kid = $kid->sibling;
592 $expr .= $self->deparse($kid, 0);
593 push @exprs, $expr if $expr;
595 if ($cx > 0) { # inside an expression, (a do {} while for lineseq)
596 return "do { " . join(";\n", @exprs) . " }";
598 return join(";\n", @exprs);
602 sub pp_lineseq { pp_scope(@_) }
604 # The BEGIN {} is used here because otherwise this code isn't executed
605 # when you run B::Deparse on itself.
607 BEGIN { map($globalnames{$_}++, "SIG", "STDIN", "STDOUT", "STDERR", "INC",
608 "ENV", "ARGV", "ARGVOUT", "_"); }
613 my $stash = $gv->STASH->NAME;
614 my $name = $gv->NAME;
615 if ($stash eq $self->{'curstash'} or $globalnames{$name}
616 or $name =~ /^[^A-Za-z_]/)
620 $stash = $stash . "::";
622 if ($name =~ /^([\cA-\cZ])$/) {
623 $name = "^" . chr(64 + ord($1));
625 return $stash . $name;
628 # Notice how subs and formats are inserted between statements here
633 @text = $op->label . ": " if $op->label;
634 my $seq = $op->cop_seq;
635 while (scalar(@{$self->{'subs_todo'}})
636 and $seq > $self->{'subs_todo'}[0][0]) {
637 push @text, $self->next_todo;
639 my $stash = $op->stash->NAME;
640 if ($stash ne $self->{'curstash'}) {
641 push @text, "package $stash;\n";
642 $self->{'curstash'} = $stash;
644 return join("", @text);
647 sub pp_dbstate { pp_nextstate(@_) }
649 sub pp_unstack { return "" } # see also leaveloop
653 my($op, $cx, $name) = @_;
657 sub pp_stub { baseop(@_, "()") }
658 sub pp_wantarray { baseop(@_, "wantarray") }
659 sub pp_fork { baseop(@_, "fork") }
660 sub pp_wait { baseop(@_, "wait") }
661 sub pp_getppid { baseop(@_, "getppid") }
662 sub pp_time { baseop(@_, "time") }
663 sub pp_tms { baseop(@_, "times") }
664 sub pp_ghostent { baseop(@_, "gethostent") }
665 sub pp_gnetent { baseop(@_, "getnetent") }
666 sub pp_gprotoent { baseop(@_, "getprotoent") }
667 sub pp_gservent { baseop(@_, "getservent") }
668 sub pp_ehostent { baseop(@_, "endhostent") }
669 sub pp_enetent { baseop(@_, "endnetent") }
670 sub pp_eprotoent { baseop(@_, "endprotoent") }
671 sub pp_eservent { baseop(@_, "endservent") }
672 sub pp_gpwent { baseop(@_, "getpwent") }
673 sub pp_spwent { baseop(@_, "setpwent") }
674 sub pp_epwent { baseop(@_, "endpwent") }
675 sub pp_ggrent { baseop(@_, "getgrent") }
676 sub pp_sgrent { baseop(@_, "setgrent") }
677 sub pp_egrent { baseop(@_, "endgrent") }
678 sub pp_getlogin { baseop(@_, "getlogin") }
682 # I couldn't think of a good short name, but this is the category of
683 # symbolic unary operators with interesting precedence
687 my($op, $cx, $name, $prec, $flags) = (@_, 0);
688 my $kid = $op->first;
689 $kid = $self->deparse($kid, $prec);
690 return $self->maybe_parens(($flags & POSTFIX) ? "$kid$name" : "$name$kid",
694 sub pp_preinc { pfixop(@_, "++", 23) }
695 sub pp_predec { pfixop(@_, "--", 23) }
696 sub pp_postinc { pfixop(@_, "++", 23, POSTFIX) }
697 sub pp_postdec { pfixop(@_, "--", 23, POSTFIX) }
698 sub pp_i_preinc { pfixop(@_, "++", 23) }
699 sub pp_i_predec { pfixop(@_, "--", 23) }
700 sub pp_i_postinc { pfixop(@_, "++", 23, POSTFIX) }
701 sub pp_i_postdec { pfixop(@_, "--", 23, POSTFIX) }
702 sub pp_complement { pfixop(@_, "~", 21) }
707 if ($op->first->ppaddr =~ /^pp_(i_)?negate$/) {
709 $self->pfixop($op, $cx, "-", 21.5);
711 $self->pfixop($op, $cx, "-", 21);
714 sub pp_i_negate { pp_negate(@_) }
720 $self->pfixop($op, $cx, "not ", 4);
722 $self->pfixop($op, $cx, "!", 21);
726 sub OPf_SPECIAL () { 128 }
730 my($op, $cx, $name, $prec, $flags) = (@_, 0, 0);
732 if ($op->flags & OPf_KIDS) {
734 return $self->maybe_parens_unop($name, $kid, $cx);
736 return $name . ($op->flags & OPf_SPECIAL ? "()" : "");
740 sub pp_chop { unop(@_, "chop") }
741 sub pp_chomp { unop(@_, "chomp") }
742 sub pp_schop { unop(@_, "chop") }
743 sub pp_schomp { unop(@_, "chomp") }
744 sub pp_defined { unop(@_, "defined") }
745 sub pp_undef { unop(@_, "undef") }
746 sub pp_study { unop(@_, "study") }
747 sub pp_ref { unop(@_, "ref") }
748 sub pp_pos { maybe_local(@_, unop(@_, "pos")) }
750 sub pp_sin { unop(@_, "sin") }
751 sub pp_cos { unop(@_, "cos") }
752 sub pp_rand { unop(@_, "rand") }
753 sub pp_srand { unop(@_, "srand") }
754 sub pp_exp { unop(@_, "exp") }
755 sub pp_log { unop(@_, "log") }
756 sub pp_sqrt { unop(@_, "sqrt") }
757 sub pp_int { unop(@_, "int") }
758 sub pp_hex { unop(@_, "hex") }
759 sub pp_oct { unop(@_, "oct") }
760 sub pp_abs { unop(@_, "abs") }
762 sub pp_length { unop(@_, "length") }
763 sub pp_ord { unop(@_, "ord") }
764 sub pp_chr { unop(@_, "chr") }
765 sub pp_ucfirst { unop(@_, "ucfirst") }
766 sub pp_lcfirst { unop(@_, "lcfirst") }
767 sub pp_uc { unop(@_, "uc") }
768 sub pp_lc { unop(@_, "lc") }
769 sub pp_quotemeta { unop(@_, "quotemeta") }
771 sub pp_each { unop(@_, "each") }
772 sub pp_values { unop(@_, "values") }
773 sub pp_keys { unop(@_, "keys") }
774 sub pp_pop { unop(@_, "pop") }
775 sub pp_shift { unop(@_, "shift") }
777 sub pp_caller { unop(@_, "caller") }
778 sub pp_reset { unop(@_, "reset") }
779 sub pp_exit { unop(@_, "exit") }
780 sub pp_prototype { unop(@_, "prototype") }
782 sub pp_close { unop(@_, "close") }
783 sub pp_fileno { unop(@_, "fileno") }
784 sub pp_umask { unop(@_, "umask") }
785 sub pp_binmode { unop(@_, "binmode") }
786 sub pp_untie { unop(@_, "untie") }
787 sub pp_tied { unop(@_, "tied") }
788 sub pp_dbmclose { unop(@_, "dbmclose") }
789 sub pp_getc { unop(@_, "getc") }
790 sub pp_eof { unop(@_, "eof") }
791 sub pp_tell { unop(@_, "tell") }
792 sub pp_getsockname { unop(@_, "getsockname") }
793 sub pp_getpeername { unop(@_, "getpeername") }
795 sub pp_chdir { unop(@_, "chdir") }
796 sub pp_chroot { unop(@_, "chroot") }
797 sub pp_readlink { unop(@_, "readlink") }
798 sub pp_rmdir { unop(@_, "rmdir") }
799 sub pp_readdir { unop(@_, "readdir") }
800 sub pp_telldir { unop(@_, "telldir") }
801 sub pp_rewinddir { unop(@_, "rewinddir") }
802 sub pp_closedir { unop(@_, "closedir") }
803 sub pp_getpgrp { unop(@_, "getpgrp") }
804 sub pp_localtime { unop(@_, "localtime") }
805 sub pp_gmtime { unop(@_, "gmtime") }
806 sub pp_alarm { unop(@_, "alarm") }
807 sub pp_sleep { unop(@_, "sleep") }
809 sub pp_dofile { unop(@_, "do") }
810 sub pp_entereval { unop(@_, "eval") }
812 sub pp_ghbyname { unop(@_, "gethostbyname") }
813 sub pp_gnbyname { unop(@_, "getnetbyname") }
814 sub pp_gpbyname { unop(@_, "getprotobyname") }
815 sub pp_shostent { unop(@_, "sethostent") }
816 sub pp_snetent { unop(@_, "setnetent") }
817 sub pp_sprotoent { unop(@_, "setprotoent") }
818 sub pp_sservent { unop(@_, "setservent") }
819 sub pp_gpwnam { unop(@_, "getpwnam") }
820 sub pp_gpwuid { unop(@_, "getpwuid") }
821 sub pp_ggrnam { unop(@_, "getgrnam") }
822 sub pp_ggrgid { unop(@_, "getgrgid") }
824 sub pp_lock { unop(@_, "lock") }
829 return $self->maybe_parens_func("exists", $self->pp_helem($op->first, 16),
833 sub OPpSLICE () { 64 }
839 if ($op->private & OPpSLICE) {
840 return $self->maybe_parens_func("delete",
841 $self->pp_hslice($op->first, 16),
844 return $self->maybe_parens_func("delete",
845 $self->pp_helem($op->first, 16),
850 sub OPp_CONST_BARE () { 64 }
855 if (class($op) eq "UNOP" and $op->first->ppaddr eq "pp_const"
856 and $op->first->private & OPp_CONST_BARE)
858 my $name = $op->first->sv->PV;
861 return "require($name)";
863 $self->unop($op, $cx, "require");
870 my $kid = $op->first;
871 if (not null $kid->sibling) {
873 return $self->dquote($op);
875 $self->unop(@_, "scalar");
882 return (($self->{'curcv'}->PADLIST->ARRAY)[1]->ARRAY)[$targ];
885 sub OPf_REF () { 16 }
890 my $kid = $op->first;
891 if ($kid->ppaddr eq "pp_null") {
893 if ($kid->ppaddr eq "pp_anonlist" || $kid->ppaddr eq "pp_anonhash") {
894 my($pre, $post) = @{{"pp_anonlist" => ["[","]"],
895 "pp_anonhash" => ["{","}"]}->{$kid->ppaddr}};
897 $kid = $kid->first->sibling; # skip pushmark
898 for (; !null($kid); $kid = $kid->sibling) {
899 $expr = $self->deparse($kid, 6);
902 return $pre . join(", ", @exprs) . $post;
903 } elsif (!null($kid->sibling) and
904 $kid->sibling->ppaddr eq "pp_anoncode") {
906 $self->deparse_sub($self->padval($kid->sibling->targ));
907 } elsif ($kid->ppaddr eq "pp_pushmark"
908 and $kid->sibling->ppaddr =~ /^pp_(pad|rv2)[ah]v$/
909 and not $kid->sibling->flags & OPf_REF) {
910 # The @a in \(@a) isn't in ref context, but only when the
912 return "\\(" . $self->deparse($kid->sibling, 1) . ")";
915 $self->pfixop($op, $cx, "\\", 20);
918 sub pp_srefgen { pp_refgen(@_) }
923 my $kid = $op->first;
924 $kid = $kid->first if $kid->ppaddr eq "pp_rv2gv"; # <$fh>
925 if ($kid->ppaddr eq "pp_rv2gv") {
928 return "<" . $self->deparse($kid, 1) . ">";
933 my ($op, $cx, $name) = @_;
934 if (class($op) eq "PVOP") {
935 return "$name " . $op->pv;
936 } elsif (class($op) eq "OP") {
938 } elsif (class($op) eq "UNOP") {
939 # Note -- loop exits are actually exempt from the
940 # looks-like-a-func rule, but a few extra parens won't hurt
941 return $self->maybe_parens_unop($name, $op->first, $cx);
945 sub pp_last { loopex(@_, "last") }
946 sub pp_next { loopex(@_, "next") }
947 sub pp_redo { loopex(@_, "redo") }
948 sub pp_goto { loopex(@_, "goto") }
949 sub pp_dump { loopex(@_, "dump") }
953 my($op, $cx, $name) = @_;
954 if (class($op) eq "UNOP") {
955 # Genuine `-X' filetests are exempt from the LLAFR, but not
956 # l?stat(); for the sake of clarity, give'em all parens
957 return $self->maybe_parens_unop($name, $op->first, $cx);
958 } elsif (class($op) eq "GVOP") {
959 return $self->maybe_parens_func($name, $self->pp_gv($op, 1), $cx, 16);
960 } else { # I don't think baseop filetests ever survive ck_ftst, but...
965 sub pp_lstat { ftst(@_, "lstat") }
966 sub pp_stat { ftst(@_, "stat") }
967 sub pp_ftrread { ftst(@_, "-R") }
968 sub pp_ftrwrite { ftst(@_, "-W") }
969 sub pp_ftrexec { ftst(@_, "-X") }
970 sub pp_fteread { ftst(@_, "-r") }
971 sub pp_ftewrite { ftst(@_, "-r") }
972 sub pp_fteexec { ftst(@_, "-r") }
973 sub pp_ftis { ftst(@_, "-e") }
974 sub pp_fteowned { ftst(@_, "-O") }
975 sub pp_ftrowned { ftst(@_, "-o") }
976 sub pp_ftzero { ftst(@_, "-z") }
977 sub pp_ftsize { ftst(@_, "-s") }
978 sub pp_ftmtime { ftst(@_, "-M") }
979 sub pp_ftatime { ftst(@_, "-A") }
980 sub pp_ftctime { ftst(@_, "-C") }
981 sub pp_ftsock { ftst(@_, "-S") }
982 sub pp_ftchr { ftst(@_, "-c") }
983 sub pp_ftblk { ftst(@_, "-b") }
984 sub pp_ftfile { ftst(@_, "-f") }
985 sub pp_ftdir { ftst(@_, "-d") }
986 sub pp_ftpipe { ftst(@_, "-p") }
987 sub pp_ftlink { ftst(@_, "-l") }
988 sub pp_ftsuid { ftst(@_, "-u") }
989 sub pp_ftsgid { ftst(@_, "-g") }
990 sub pp_ftsvtx { ftst(@_, "-k") }
991 sub pp_fttty { ftst(@_, "-t") }
992 sub pp_fttext { ftst(@_, "-T") }
993 sub pp_ftbinary { ftst(@_, "-B") }
995 sub SWAP_CHILDREN () { 1 }
996 sub ASSIGN () { 2 } # has OP= variant
998 sub OPf_STACKED () { 64 }
1004 my $name = $op->ppaddr;
1005 if ($name eq "pp_concat" and $op->first->ppaddr eq "pp_concat") {
1006 # avoid spurious `=' -- see comment in pp_concat
1009 if ($name eq "pp_null" and class($op) eq "UNOP"
1010 and $op->first->ppaddr =~ /^pp_(and|x?or)$/
1011 and null $op->first->sibling)
1013 # Like all conditional constructs, OP_ANDs and OP_ORs are topped
1014 # with a null that's used as the common end point of the two
1015 # flows of control. For precedence purposes, ignore it.
1016 # (COND_EXPRs have these too, but we don't bother with
1017 # their associativity).
1018 return assoc_class($op->first);
1020 return $name . ($op->flags & OPf_STACKED ? "=" : "");
1023 # Left associative operators, like `+', for which
1024 # $a + $b + $c is equivalent to ($a + $b) + $c
1027 %left = ('pp_multiply' => 19, 'pp_i_multiply' => 19,
1028 'pp_divide' => 19, 'pp_i_divide' => 19,
1029 'pp_modulo' => 19, 'pp_i_modulo' => 19,
1031 'pp_add' => 18, 'pp_i_add' => 18,
1032 'pp_subtract' => 18, 'pp_i_subtract' => 18,
1034 'pp_left_shift' => 17, 'pp_right_shift' => 17,
1036 'pp_bit_or' => 12, 'pp_bit_xor' => 12,
1038 'pp_or' => 2, 'pp_xor' => 2,
1042 sub deparse_binop_left {
1044 my($op, $left, $prec) = @_;
1045 if ($left{assoc_class($op)}
1046 and $left{assoc_class($op)} == $left{assoc_class($left)})
1048 return $self->deparse($left, $prec - .00001);
1050 return $self->deparse($left, $prec);
1054 # Right associative operators, like `=', for which
1055 # $a = $b = $c is equivalent to $a = ($b = $c)
1058 %right = ('pp_pow' => 22,
1059 'pp_sassign=' => 7, 'pp_aassign=' => 7,
1060 'pp_multiply=' => 7, 'pp_i_multiply=' => 7,
1061 'pp_divide=' => 7, 'pp_i_divide=' => 7,
1062 'pp_modulo=' => 7, 'pp_i_modulo=' => 7,
1064 'pp_add=' => 7, 'pp_i_add=' => 7,
1065 'pp_subtract=' => 7, 'pp_i_subtract=' => 7,
1067 'pp_left_shift=' => 7, 'pp_right_shift=' => 7,
1069 'pp_bit_or=' => 7, 'pp_bit_xor=' => 7,
1070 'pp_andassign' => 7,
1075 sub deparse_binop_right {
1077 my($op, $right, $prec) = @_;
1078 if ($right{assoc_class($op)}
1079 and $right{assoc_class($op)} == $right{assoc_class($right)})
1081 return $self->deparse($right, $prec - .00001);
1083 return $self->deparse($right, $prec);
1089 my ($op, $cx, $opname, $prec, $flags) = (@_, 0);
1090 my $left = $op->first;
1091 my $right = $op->last;
1093 if ($op->flags & OPf_STACKED && $flags & ASSIGN) {
1097 if ($flags & SWAP_CHILDREN) {
1098 ($left, $right) = ($right, $left);
1100 $left = $self->deparse_binop_left($op, $left, $prec);
1101 $right = $self->deparse_binop_right($op, $right, $prec);
1102 return $self->maybe_parens("$left $opname$eq $right", $cx, $prec);
1105 sub pp_add { binop(@_, "+", 18, ASSIGN) }
1106 sub pp_multiply { binop(@_, "*", 19, ASSIGN) }
1107 sub pp_subtract { binop(@_, "-",18, ASSIGN) }
1108 sub pp_divide { binop(@_, "/", 19, ASSIGN) }
1109 sub pp_modulo { binop(@_, "%", 19, ASSIGN) }
1110 sub pp_i_add { binop(@_, "+", 18, ASSIGN) }
1111 sub pp_i_multiply { binop(@_, "*", 19, ASSIGN) }
1112 sub pp_i_subtract { binop(@_, "-", 18, ASSIGN) }
1113 sub pp_i_divide { binop(@_, "/", 19, ASSIGN) }
1114 sub pp_i_modulo { binop(@_, "%", 19, ASSIGN) }
1115 sub pp_pow { binop(@_, "**", 22, ASSIGN) }
1117 sub pp_left_shift { binop(@_, "<<", 17, ASSIGN) }
1118 sub pp_right_shift { binop(@_, ">>", 17, ASSIGN) }
1119 sub pp_bit_and { binop(@_, "&", 13, ASSIGN) }
1120 sub pp_bit_or { binop(@_, "|", 12, ASSIGN) }
1121 sub pp_bit_xor { binop(@_, "^", 12, ASSIGN) }
1123 sub pp_eq { binop(@_, "==", 14) }
1124 sub pp_ne { binop(@_, "!=", 14) }
1125 sub pp_lt { binop(@_, "<", 15) }
1126 sub pp_gt { binop(@_, ">", 15) }
1127 sub pp_ge { binop(@_, ">=", 15) }
1128 sub pp_le { binop(@_, "<=", 15) }
1129 sub pp_ncmp { binop(@_, "<=>", 14) }
1130 sub pp_i_eq { binop(@_, "==", 14) }
1131 sub pp_i_ne { binop(@_, "!=", 14) }
1132 sub pp_i_lt { binop(@_, "<", 15) }
1133 sub pp_i_gt { binop(@_, ">", 15) }
1134 sub pp_i_ge { binop(@_, ">=", 15) }
1135 sub pp_i_le { binop(@_, "<=", 15) }
1136 sub pp_i_ncmp { binop(@_, "<=>", 14) }
1138 sub pp_seq { binop(@_, "eq", 14) }
1139 sub pp_sne { binop(@_, "ne", 14) }
1140 sub pp_slt { binop(@_, "lt", 15) }
1141 sub pp_sgt { binop(@_, "gt", 15) }
1142 sub pp_sge { binop(@_, "ge", 15) }
1143 sub pp_sle { binop(@_, "le", 15) }
1144 sub pp_scmp { binop(@_, "cmp", 14) }
1146 sub pp_sassign { binop(@_, "=", 7, SWAP_CHILDREN) }
1147 sub pp_aassign { binop(@_, "=", 7, SWAP_CHILDREN) }
1149 # `.' is special because concats-of-concats are optimized to save copying
1150 # by making all but the first concat stacked. The effect is as if the
1151 # programmer had written `($a . $b) .= $c', except legal.
1155 my $left = $op->first;
1156 my $right = $op->last;
1159 if ($op->flags & OPf_STACKED and $op->first->ppaddr ne "pp_concat") {
1163 $left = $self->deparse_binop_left($op, $left, $prec);
1164 $right = $self->deparse_binop_right($op, $right, $prec);
1165 return $self->maybe_parens("$left .$eq $right", $cx, $prec);
1168 # `x' is weird when the left arg is a list
1172 my $left = $op->first;
1173 my $right = $op->last;
1176 if ($op->flags & OPf_STACKED) {
1180 if (null($right)) { # list repeat; count is inside left-side ex-list
1181 my $kid = $left->first->sibling; # skip pushmark
1183 for (; !null($kid->sibling); $kid = $kid->sibling) {
1184 push @exprs, $self->deparse($kid, 6);
1187 $left = "(" . join(", ", @exprs). ")";
1189 $left = $self->deparse_binop_left($op, $left, $prec);
1191 $right = $self->deparse_binop_right($op, $right, $prec);
1192 return $self->maybe_parens("$left x$eq $right", $cx, $prec);
1197 my ($op, $cx, $type) = @_;
1198 my $left = $op->first;
1199 my $right = $left->sibling;
1200 $left = $self->deparse($left, 9);
1201 $right = $self->deparse($right, 9);
1202 return $self->maybe_parens("$left $type $right", $cx, 9);
1208 my $flip = $op->first;
1209 my $type = ($flip->flags & OPf_SPECIAL) ? "..." : "..";
1210 return $self->range($flip->first, $cx, $type);
1213 # one-line while/until is handled in pp_leave
1217 my ($op, $cx, $lowop, $lowprec, $highop, $highprec, $blockname) = @_;
1218 my $left = $op->first;
1219 my $right = $op->first->sibling;
1220 if ($cx == 0 and is_scope($right) and $blockname) { # if ($a) {$b}
1221 $left = $self->deparse($left, 1);
1222 $right = $self->deparse($right, 0);
1223 return "$blockname ($left) {\n\t$right\n\b}\cK";
1224 } elsif ($cx == 0 and $blockname and not $self->{'parens'}) { # $b if $a
1225 $right = $self->deparse($right, 1);
1226 $left = $self->deparse($left, 1);
1227 return "$right $blockname $left";
1228 } elsif ($cx > $lowprec and $highop) { # $a && $b
1229 $left = $self->deparse_binop_left($op, $left, $highprec);
1230 $right = $self->deparse_binop_right($op, $right, $highprec);
1231 return $self->maybe_parens("$left $highop $right", $cx, $highprec);
1232 } else { # $a and $b
1233 $left = $self->deparse_binop_left($op, $left, $lowprec);
1234 $right = $self->deparse_binop_right($op, $right, $lowprec);
1235 return $self->maybe_parens("$left $lowop $right", $cx, $lowprec);
1239 sub pp_and { logop(@_, "and", 3, "&&", 11, "if") }
1240 sub pp_or { logop(@_, "or", 2, "||", 10, "unless") }
1241 sub pp_xor { logop(@_, "xor", 2, "", 0, "") }
1245 my ($op, $cx, $opname) = @_;
1246 my $left = $op->first;
1247 my $right = $op->first->sibling->first; # skip sassign
1248 $left = $self->deparse($left, 7);
1249 $right = $self->deparse($right, 7);
1250 return $self->maybe_parens("$left $opname $right", $cx, 7);
1253 sub pp_andassign { logassignop(@_, "&&=") }
1254 sub pp_orassign { logassignop(@_, "||=") }
1258 my($op, $cx, $name) = @_;
1260 my $parens = ($cx >= 5) || $self->{'parens'};
1261 my $kid = $op->first->sibling;
1262 return $name if null $kid;
1263 my $first = $self->deparse($kid, 6);
1264 $first = "+$first" if not $parens and substr($first, 0, 1) eq "(";
1265 push @exprs, $first;
1266 $kid = $kid->sibling;
1267 for (; !null($kid); $kid = $kid->sibling) {
1268 push @exprs, $self->deparse($kid, 6);
1271 return "$name(" . join(", ", @exprs) . ")";
1273 return "$name " . join(", ", @exprs);
1277 sub pp_bless { listop(@_, "bless") }
1278 sub pp_atan2 { listop(@_, "atan2") }
1279 sub pp_substr { maybe_local(@_, listop(@_, "substr")) }
1280 sub pp_vec { maybe_local(@_, listop(@_, "vec")) }
1281 sub pp_index { listop(@_, "index") }
1282 sub pp_rindex { listop(@_, "rindex") }
1283 sub pp_sprintf { listop(@_, "sprintf") }
1284 sub pp_formline { listop(@_, "formline") } # see also deparse_format
1285 sub pp_crypt { listop(@_, "crypt") }
1286 sub pp_unpack { listop(@_, "unpack") }
1287 sub pp_pack { listop(@_, "pack") }
1288 sub pp_join { listop(@_, "join") }
1289 sub pp_splice { listop(@_, "splice") }
1290 sub pp_push { listop(@_, "push") }
1291 sub pp_unshift { listop(@_, "unshift") }
1292 sub pp_reverse { listop(@_, "reverse") }
1293 sub pp_warn { listop(@_, "warn") }
1294 sub pp_die { listop(@_, "die") }
1295 # Actually, return is exempt from the LLAFR (see examples in this very
1296 # module!), but for consistency's sake, ignore that fact
1297 sub pp_return { listop(@_, "return") }
1298 sub pp_open { listop(@_, "open") }
1299 sub pp_pipe_op { listop(@_, "pipe") }
1300 sub pp_tie { listop(@_, "tie") }
1301 sub pp_dbmopen { listop(@_, "dbmopen") }
1302 sub pp_sselect { listop(@_, "select") }
1303 sub pp_select { listop(@_, "select") }
1304 sub pp_read { listop(@_, "read") }
1305 sub pp_sysopen { listop(@_, "sysopen") }
1306 sub pp_sysseek { listop(@_, "sysseek") }
1307 sub pp_sysread { listop(@_, "sysread") }
1308 sub pp_syswrite { listop(@_, "syswrite") }
1309 sub pp_send { listop(@_, "send") }
1310 sub pp_recv { listop(@_, "recv") }
1311 sub pp_seek { listop(@_, "seek") }
1312 sub pp_truncate { listop(@_, "truncate") }
1313 sub pp_fcntl { listop(@_, "fcntl") }
1314 sub pp_ioctl { listop(@_, "ioctl") }
1315 sub pp_flock { listop(@_, "flock") }
1316 sub pp_socket { listop(@_, "socket") }
1317 sub pp_sockpair { listop(@_, "sockpair") }
1318 sub pp_bind { listop(@_, "bind") }
1319 sub pp_connect { listop(@_, "connect") }
1320 sub pp_listen { listop(@_, "listen") }
1321 sub pp_accept { listop(@_, "accept") }
1322 sub pp_shutdown { listop(@_, "shutdown") }
1323 sub pp_gsockopt { listop(@_, "getsockopt") }
1324 sub pp_ssockopt { listop(@_, "setsockopt") }
1325 sub pp_chown { listop(@_, "chown") }
1326 sub pp_unlink { listop(@_, "unlink") }
1327 sub pp_chmod { listop(@_, "chmod") }
1328 sub pp_utime { listop(@_, "utime") }
1329 sub pp_rename { listop(@_, "rename") }
1330 sub pp_link { listop(@_, "link") }
1331 sub pp_symlink { listop(@_, "symlink") }
1332 sub pp_mkdir { listop(@_, "mkdir") }
1333 sub pp_open_dir { listop(@_, "opendir") }
1334 sub pp_seekdir { listop(@_, "seekdir") }
1335 sub pp_waitpid { listop(@_, "waitpid") }
1336 sub pp_system { listop(@_, "system") }
1337 sub pp_exec { listop(@_, "exec") }
1338 sub pp_kill { listop(@_, "kill") }
1339 sub pp_setpgrp { listop(@_, "setpgrp") }
1340 sub pp_getpriority { listop(@_, "getpriority") }
1341 sub pp_setpriority { listop(@_, "setpriority") }
1342 sub pp_shmget { listop(@_, "shmget") }
1343 sub pp_shmctl { listop(@_, "shmctl") }
1344 sub pp_shmread { listop(@_, "shmread") }
1345 sub pp_shmwrite { listop(@_, "shmwrite") }
1346 sub pp_msgget { listop(@_, "msgget") }
1347 sub pp_msgctl { listop(@_, "msgctl") }
1348 sub pp_msgsnd { listop(@_, "msgsnd") }
1349 sub pp_msgrcv { listop(@_, "msgrcv") }
1350 sub pp_semget { listop(@_, "semget") }
1351 sub pp_semctl { listop(@_, "semctl") }
1352 sub pp_semop { listop(@_, "semop") }
1353 sub pp_ghbyaddr { listop(@_, "gethostbyaddr") }
1354 sub pp_gnbyaddr { listop(@_, "getnetbyaddr") }
1355 sub pp_gpbynumber { listop(@_, "getprotobynumber") }
1356 sub pp_gsbyname { listop(@_, "getservbyname") }
1357 sub pp_gsbyport { listop(@_, "getservbyport") }
1358 sub pp_syscall { listop(@_, "syscall") }
1363 my $text = $self->dq($op->first->sibling); # skip pushmark
1364 if ($text =~ /^\$?(\w|::|\`)+$/ # could look like a readline
1365 or $text =~ /[<>]/) {
1366 return 'glob(' . single_delim('qq', '"', $text) . ')';
1368 return '<' . $text . '>';
1374 my($op, $cx, $name) = @_;
1376 my $kid = $op->first->sibling;
1378 if ($op->flags & OPf_STACKED) {
1380 $indir = $indir->first; # skip rv2gv
1381 if (is_scope($indir)) {
1382 $indir = "{" . $self->deparse($indir, 0) . "}";
1384 $indir = $self->deparse($indir, 24);
1386 $indir = $indir . " ";
1387 $kid = $kid->sibling;
1389 for (; !null($kid); $kid = $kid->sibling) {
1390 $expr = $self->deparse($kid, 6);
1393 return $self->maybe_parens_func($name,
1394 $indir . join(", ", @exprs),
1398 sub pp_prtf { indirop(@_, "printf") }
1399 sub pp_print { indirop(@_, "print") }
1400 sub pp_sort { indirop(@_, "sort") }
1404 my($op, $cx, $name) = @_;
1406 my $kid = $op->first; # this is the (map|grep)start
1407 $kid = $kid->first->sibling; # skip a pushmark
1408 my $code = $kid->first; # skip a null
1409 if (is_scope $code) {
1410 $code = "{" . $self->deparse($code, 1) . "} ";
1412 $code = $self->deparse($code, 24) . ", ";
1414 $kid = $kid->sibling;
1415 for (; !null($kid); $kid = $kid->sibling) {
1416 $expr = $self->deparse($kid, 6);
1417 push @exprs, $expr if $expr;
1419 return $self->maybe_parens_func($name, $code . join(", ", @exprs), $cx, 5);
1422 sub pp_mapwhile { mapop(@_, "map") }
1423 sub pp_grepwhile { mapop(@_, "grep") }
1429 my $kid = $op->first->sibling; # skip pushmark
1430 return $self->deparse($kid, $cx) if null $kid->sibling;
1432 my $local = "either"; # could be local(...) or my(...)
1433 for ($lop = $kid; !null($lop); $lop = $lop->sibling) {
1434 # This assumes that no other private flags equal 128, and that
1435 # OPs that store things other than flags in their op_private,
1436 # like OP_AELEMFAST, won't be immediate children of a list.
1437 unless ($lop->private & OPp_LVAL_INTRO or $lop->ppaddr eq "pp_undef")
1439 $local = ""; # or not
1442 if ($lop->ppaddr =~ /^pp_pad[ash]v$/) { # my()
1443 ($local = "", last) if $local eq "local";
1445 } elsif ($lop->ppaddr ne "pp_undef") { # local()
1446 ($local = "", last) if $local eq "my";
1450 $local = "" if $local eq "either"; # no point if it's all undefs
1451 for (; !null($kid); $kid = $kid->sibling) {
1453 if (class($kid) eq "UNOP" and $kid->first->ppaddr eq "pp_gvsv") {
1458 $self->{'avoid_local'}{$$lop}++;
1459 $expr = $self->deparse($kid, 6);
1460 delete $self->{'avoid_local'}{$$lop};
1462 $expr = $self->deparse($kid, 6);
1467 return "$local(" . join(", ", @exprs) . ")";
1469 return $self->maybe_parens( join(", ", @exprs), $cx, 6);
1476 my $cond = $op->first;
1477 my $true = $cond->sibling;
1478 my $false = $true->sibling;
1479 my $cuddle = $self->{'cuddle'};
1480 $cond = $self->deparse($cond, 1);
1481 unless ($cx == 0 and is_scope($true) and is_scope($false)) {
1482 $true = $self->deparse($true, 8);
1483 $false = $self->deparse($false, 8);
1484 return $self->maybe_parens("$cond ? $true : $false", $cx, 8);
1486 $true = $self->deparse($true, 0);
1487 if ($false->ppaddr eq "pp_lineseq") { # braces w/o scope => elsif
1488 my $head = "if ($cond) {\n\t$true\n\b}";
1490 while (!null($false) and $false->ppaddr eq "pp_lineseq") {
1491 my $newop = $false->first->sibling->first;
1492 my $newcond = $newop->first;
1493 my $newtrue = $newcond->sibling;
1494 $false = $newtrue->sibling; # last in chain is OP_AND => no else
1495 $newcond = $self->deparse($newcond, 1);
1496 $newtrue = $self->deparse($newtrue, 0);
1497 push @elsifs, "elsif ($newcond) {\n\t$newtrue\n\b}";
1499 if (!null($false)) {
1500 $false = $cuddle . "else {\n\t" .
1501 $self->deparse($false, 0) . "\n\b}\cK";
1505 return $head . join($cuddle, "", @elsifs) . $false;
1507 $false = $self->deparse($false, 0);
1508 return "if ($cond) {\n\t$true\n\b}${cuddle}else {\n\t$false\n\b}\cK";
1514 my $enter = $op->first;
1515 my $kid = $enter->sibling;
1516 local($self->{'curstash'}) = $self->{'curstash'};
1519 if ($kid->ppaddr eq "pp_lineseq") { # bare or infinite loop
1520 if (is_state $kid->last) { # infinite
1521 $head = "for (;;) "; # shorter than while (1)
1525 } elsif ($enter->ppaddr eq "pp_enteriter") { # foreach
1526 my $ary = $enter->first->sibling; # first was pushmark
1527 my $var = $ary->sibling;
1528 if ($enter->flags & OPf_STACKED) {
1529 $ary = $self->deparse($ary->first->sibling, 9) . " .. " .
1530 $self->deparse($ary->first->sibling->sibling, 9);
1532 $ary = $self->deparse($ary, 1);
1535 if ($enter->flags & OPf_SPECIAL) { # thread special var
1536 $var = $self->pp_threadsv($enter, 1);
1537 } else { # regular my() variable
1538 $var = $self->pp_padsv($enter, 1);
1539 if ($self->padname_sv($enter->targ)->IVX ==
1540 $kid->first->first->sibling->last->cop_seq)
1542 # If the scope of this variable closes at the last
1543 # statement of the loop, it must have been
1545 $var = "my " . $var;
1548 } elsif ($var->ppaddr eq "pp_rv2gv") {
1549 $var = $self->pp_rv2sv($var, 1);
1550 } elsif ($var->ppaddr eq "pp_gv") {
1551 $var = "\$" . $self->deparse($var, 1);
1553 $head = "foreach $var ($ary) ";
1554 $kid = $kid->first->first->sibling; # skip OP_AND and OP_ITER
1555 } elsif ($kid->ppaddr eq "pp_null") { # while/until
1557 my $name = {"pp_and" => "while", "pp_or" => "until"}
1559 $head = "$name (" . $self->deparse($kid->first, 1) . ") ";
1560 $kid = $kid->first->sibling;
1561 } elsif ($kid->ppaddr eq "pp_stub") { # bare and empty
1562 return "{;}"; # {} could be a hashref
1564 # The third-to-last kid is the continue block if the pointer used
1565 # by `next BLOCK' points to its first OP, which happens to be the
1566 # the op_next of the head of the _previous_ statement.
1567 # Unless it's a bare loop, in which case it's last, since there's
1568 # no unstack or extra nextstate.
1569 my($cont, $precont);
1571 $cont = $kid->first;
1572 while (!null($cont->sibling)) {
1574 $cont = $cont->sibling;
1577 $cont = $kid->first;
1578 while (!null($cont->sibling->sibling->sibling)) {
1580 $cont = $cont->sibling;
1583 # cluck $self->{'curcv'}->GV->NAME unless $precont;
1584 if ($precont and $ {$precont->next} == $ {$enter->nextop}) {
1585 my $state = $kid->first;
1586 my $cuddle = $self->{'cuddle'};
1588 for (; $$state != $$cont; $state = $state->sibling) {
1590 if (is_state $state) {
1591 $expr = $self->deparse($state, 0);
1592 $state = $state->sibling;
1595 $expr .= $self->deparse($state, 0);
1596 push @exprs, $expr if $expr;
1598 $kid = join(";\n", @exprs);
1599 $cont = $cuddle . "continue {\n\t" .
1600 $self->deparse($cont, 0) . "\n\b}\cK";
1603 $kid = $self->deparse($kid, 0);
1605 return $head . "{\n\t" . $kid . "\n\b}" . $cont;
1610 return "eval {\n\t" . $self->pp_leave(@_) . "\n\b}";
1613 sub OP_CONST () { 5 }
1614 sub OP_STRINGIFY () { 65 }
1619 if (class($op) eq "OP") {
1620 return "'???'" if $op->targ == OP_CONST; # old value is lost
1621 } elsif ($op->first->ppaddr eq "pp_pushmark") {
1622 return $self->pp_list($op, $cx);
1623 } elsif ($op->first->ppaddr eq "pp_enter") {
1624 return $self->pp_leave($op, $cx);
1625 } elsif ($op->targ == OP_STRINGIFY) {
1626 return $self->dquote($op);
1627 } elsif (!null($op->first->sibling) and
1628 $op->first->sibling->ppaddr eq "pp_readline" and
1629 $op->first->sibling->flags & OPf_STACKED) {
1630 return $self->maybe_parens($self->deparse($op->first, 7) . " = "
1631 . $self->deparse($op->first->sibling, 7),
1633 } elsif (!null($op->first->sibling) and
1634 $op->first->sibling->ppaddr eq "pp_trans" and
1635 $op->first->sibling->flags & OPf_STACKED) {
1636 return $self->maybe_parens($self->deparse($op->first, 20) . " =~ "
1637 . $self->deparse($op->first->sibling, 20),
1640 return $self->deparse($op->first, $cx);
1647 my $str = $self->padname_sv($targ)->PV;
1648 return padname_fix($str);
1654 return substr($self->padname($op->targ), 1); # skip $/@/%
1660 return $self->maybe_my($op, $cx, $self->padname($op->targ));
1663 sub pp_padav { pp_padsv(@_) }
1664 sub pp_padhv { pp_padsv(@_) }
1669 @threadsv_names = ("_", "1", "2", "3", "4", "5", "6", "7", "8", "9",
1670 "&", "`", "'", "+", "/", ".", ",", "\\", '"', ";",
1671 "^", "-", "%", "=", "|", "~", ":", "^A", "^E",
1678 return $self->maybe_local($op, $cx, "\$" . $threadsv_names[$op->targ]);
1684 return $self->maybe_local($op, $cx, "\$" . $self->gv_name($op->gv));
1690 return $self->gv_name($op->gv);
1697 return "\$" . $self->gv_name($gv) . "[" . $op->private . "]";
1702 my($op, $cx, $type) = @_;
1703 my $kid = $op->first;
1704 my $scope = is_scope($kid);
1705 $kid = $self->deparse($kid, 0);
1706 return $type . ($scope ? "{$kid}" : $kid);
1709 sub pp_rv2sv { maybe_local(@_, rv2x(@_, "\$")) }
1710 sub pp_rv2hv { maybe_local(@_, rv2x(@_, "%")) }
1711 sub pp_rv2gv { maybe_local(@_, rv2x(@_, "*")) }
1717 if ($op->first->ppaddr eq "pp_padav") {
1718 return $self->maybe_local($op, $cx, '$#' . $self->padany($op->first));
1720 return $self->maybe_local($op, $cx, $self->rv2x($op->first, '$#'));
1724 # skip down to the old, ex-rv2cv
1725 sub pp_rv2cv { $_[0]->rv2x($_[1]->first->first->sibling, $_[2], "&") }
1730 my $kid = $op->first;
1731 if ($kid->ppaddr eq "pp_const") { # constant list
1733 return "(" . join(", ", map(const($_), $av->ARRAY)) . ")";
1735 return $self->maybe_local($op, $cx, $self->rv2x($op, $cx, "\@"));
1742 my ($op, $cx, $left, $right, $padname) = @_;
1743 my($array, $idx) = ($op->first, $op->first->sibling);
1744 unless ($array->ppaddr eq $padname) { # Maybe this has been fixed
1745 $array = $array->first; # skip rv2av (or ex-rv2av in _53+)
1747 if ($array->ppaddr eq $padname) {
1748 $array = $self->padany($array);
1749 } elsif (is_scope($array)) { # ${expr}[0]
1750 $array = "{" . $self->deparse($array, 0) . "}";
1751 } elsif (is_scalar $array) { # $x[0], $$x[0], ...
1752 $array = $self->deparse($array, 24);
1754 # $x[20][3]{hi} or expr->[20]
1756 $arrow = "->" if $array->ppaddr !~ /^pp_[ah]elem$/;
1757 return $self->deparse($array, 24) . $arrow .
1758 $left . $self->deparse($idx, 1) . $right;
1760 $idx = $self->deparse($idx, 1);
1761 return "\$" . $array . $left . $idx . $right;
1764 sub pp_aelem { maybe_local(@_, elem(@_, "[", "]", "pp_padav")) }
1765 sub pp_helem { maybe_local(@_, elem(@_, "{", "}", "pp_padhv")) }
1770 my($glob, $part) = ($op->first, $op->last);
1771 $glob = $glob->first; # skip rv2gv
1772 $glob = $glob->first if $glob->ppaddr eq "pp_rv2gv"; # this one's a bug
1773 my $scope = is_scope($glob);
1774 $glob = $self->deparse($glob, 0);
1775 $part = $self->deparse($part, 1);
1776 return "*" . ($scope ? "{$glob}" : $glob) . "{$part}";
1781 my ($op, $cx, $left, $right, $regname, $padname) = @_;
1783 my(@elems, $kid, $array, $list);
1784 if (class($op) eq "LISTOP") {
1786 } else { # ex-hslice inside delete()
1787 for ($kid = $op->first; !null $kid->sibling; $kid = $kid->sibling) {}
1791 $array = $array->first
1792 if $array->ppaddr eq $regname or $array->ppaddr eq "pp_null";
1793 if (is_scope($array)) {
1794 $array = "{" . $self->deparse($array, 0) . "}";
1795 } elsif ($array->ppaddr eq $padname) {
1796 $array = $self->padany($array);
1798 $array = $self->deparse($array, 24);
1800 $kid = $op->first->sibling; # skip pushmark
1801 if ($kid->ppaddr eq "pp_list") {
1802 $kid = $kid->first->sibling; # skip list, pushmark
1803 for (; !null $kid; $kid = $kid->sibling) {
1804 push @elems, $self->deparse($kid, 6);
1806 $list = join(", ", @elems);
1808 $list = $self->deparse($kid, 1);
1810 return "\@" . $array . $left . $list . $right;
1813 sub pp_aslice { maybe_local(@_, slice(@_, "[", "]",
1814 "pp_rv2av", "pp_padav")) }
1815 sub pp_hslice { maybe_local(@_, slice(@_, "{", "}",
1816 "pp_rv2hv", "pp_padhv")) }
1821 my $idx = $op->first;
1822 my $list = $op->last;
1824 $list = $self->deparse($list, 1);
1825 $idx = $self->deparse($idx, 1);
1826 return "($list)" . "[$idx]";
1829 sub OPpENTERSUB_AMPER () { 8 }
1831 sub OPf_WANT () { 3 }
1832 sub OPf_WANT_VOID () { 1 }
1833 sub OPf_WANT_SCALAR () { 2 }
1834 sub OPf_WANT_LIST () { 2 }
1838 return ($op->flags & OPf_WANT) == OPf_WANT_SCALAR;
1848 my($kid, $args, @exprs);
1849 if (not null $op->first->sibling) { # method
1850 $kid = $op->first->sibling; # skip pushmark
1851 my $obj = $self->deparse($kid, 24);
1852 $kid = $kid->sibling;
1853 for (; not null $kid->sibling; $kid = $kid->sibling) {
1854 push @exprs, $self->deparse($kid, 6);
1856 my $meth = $kid->first;
1857 if ($meth->ppaddr eq "pp_const") {
1858 $meth = $meth->sv->PV; # needs to be bare
1860 $meth = $self->deparse($meth, 1);
1862 $args = join(", ", @exprs);
1863 $kid = $obj . "->" . $meth;
1865 return $kid . "(" . $args . ")"; # parens mandatory
1867 return $kid; # toke.c fakes parens
1870 # else, not a method
1871 if ($op->flags & OPf_SPECIAL) {
1873 } elsif ($op->private & OPpENTERSUB_AMPER) {
1877 $kid = $kid->first->sibling; # skip ex-list, pushmark
1878 for (; not null $kid->sibling; $kid = $kid->sibling) {
1881 if (is_scope($kid)) {
1883 $kid = "{" . $self->deparse($kid, 0) . "}";
1884 } elsif ($kid->first->ppaddr eq "pp_gv") {
1885 my $gv = $kid->first->gv;
1886 if (class($gv->CV) ne "SPECIAL") {
1887 $proto = $gv->CV->PV if $gv->CV->FLAGS & SVf_POK;
1890 $kid = $self->deparse($kid, 24);
1891 } elsif (is_scalar $kid->first) {
1893 $kid = $self->deparse($kid, 24);
1896 $kid = $self->deparse($kid, 24) . "->";
1898 if (defined $proto and not $amper) {
1903 $proto =~ s/([^\\]|^)([@%])(.*)$/$1$2/;
1905 $proto =~ s/^ *([\\]?[\$\@&%*]|;)//;
1908 undef $proto if @args;
1909 } elsif ($chr eq ";") {
1911 } elsif ($chr eq "@" or $chr eq "%") {
1912 push @reals, map($self->deparse($_, 6), @args);
1916 undef $proto, last unless $arg;
1918 if (want_scalar $arg) {
1919 push @reals, $self->deparse($arg, 6);
1923 } elsif ($chr eq "&") {
1924 if ($arg->ppaddr =~ /pp_(s?refgen|undef)/) {
1925 push @reals, $self->deparse($arg, 6);
1929 } elsif ($chr eq "*") {
1930 if ($arg->ppaddr =~ /^pp_s?refgen$/
1931 and $arg->first->first->ppaddr eq "pp_rv2gv")
1933 $real = $arg->first->first; # skip refgen, null
1934 if ($real->first->ppaddr eq "pp_gv") {
1935 push @reals, $self->deparse($real, 6);
1937 push @reals, $self->deparse($real->first, 6);
1942 } elsif (substr($chr, 0, 1) eq "\\") {
1943 $chr = substr($chr, 1);
1944 if ($arg->ppaddr =~ /^pp_s?refgen$/ and
1945 !null($real = $arg->first) and
1946 ($chr eq "\$" && is_scalar($real->first)
1948 && $real->first->sibling->ppaddr
1949 =~ /^pp_(rv2|pad)av$/)
1951 && $real->first->sibling->ppaddr
1952 =~ /^pp_(rv2|pad)hv$/)
1953 #or ($chr eq "&" # This doesn't work
1954 # && $real->first->ppaddr eq "pp_rv2cv")
1956 && $real->first->ppaddr eq "pp_rv2gv")))
1958 push @reals, $self->deparse($real, 6);
1965 undef $proto if $proto and !$doneok;
1966 undef $proto if @args;
1967 $args = join(", ", @reals);
1969 unless (defined $proto) {
1971 $args = join(", ", map($self->deparse($_, 6), @exprs));
1974 $args = join(", ", map($self->deparse($_, 6), @exprs));
1976 if ($prefix or $amper) {
1977 if ($op->flags & OPf_STACKED) {
1978 return $prefix . $amper . $kid . "(" . $args . ")";
1980 return $prefix . $amper. $kid;
1983 if (defined $proto and $proto eq "") {
1985 } elsif ($proto eq "\$") {
1986 return $self->maybe_parens_func($kid, $args, $cx, 16);
1987 } elsif ($proto or $simple) {
1988 return $self->maybe_parens_func($kid, $args, $cx, 5);
1990 return "$kid(" . $args . ")";
1995 sub pp_enterwrite { unop(@_, "write") }
1997 # escape things that cause interpolation in double quotes,
1998 # but not character escapes
2001 $str =~ s/(^|[^\\])([\$\@]|\\[uUlLQE])/$1\\$2/g;
2005 # the same, but treat $|, $), and $ at the end of the string differently
2008 $str =~ s/(^|[^\\])(\@|\\[uUlLQE])/$1\\$2/g;
2009 $str =~ s/(^|[^\\])(\$[^)|])/$1\\$2/g;
2013 # character escapes, but not delimiters that might need to be escaped
2014 sub escape_str { # ASCII
2017 # $str =~ s/\cH/\\b/g; # \b means someting different in a regex
2023 $str =~ s/([\cA-\cZ])/'\\c' . chr(ord('@') + ord($1))/ge;
2024 $str =~ s/([\0\033-\037\177-\377])/'\\' . sprintf("%03o", ord($1))/ge;
2028 # Don't do this for regexen
2031 $str =~ s/\\/\\\\/g;
2035 sub balanced_delim {
2037 my @str = split //, $str;
2038 my($ar, $open, $close, $fail, $c, $cnt);
2039 for $ar (['[',']'], ['(',')'], ['<','>'], ['{','}']) {
2040 ($open, $close) = @$ar;
2041 $fail = 0; $cnt = 0;
2045 } elsif ($c eq $close) {
2053 $fail = 1 if $cnt != 0;
2054 return ($open, "$open$str$close") if not $fail;
2060 my($q, $default, $str) = @_;
2061 return "$default$str$default" if $default and index($str, $default) == -1;
2062 my($succeed, $delim);
2063 ($succeed, $str) = balanced_delim($str);
2064 return "$q$str" if $succeed;
2065 for $delim ('/', '"', '#') {
2066 return "$q$delim" . $str . $delim if index($str, $delim) == -1;
2069 $str =~ s/$default/\\$default/g;
2070 return "$default$str$default";
2077 sub SVf_IOK () {0x10000}
2078 sub SVf_NOK () {0x20000}
2079 sub SVf_ROK () {0x80000}
2083 if (class($sv) eq "SPECIAL") {
2084 return ('undef', '1', '0')[$$sv-1];
2085 } elsif ($sv->FLAGS & SVf_IOK) {
2087 } elsif ($sv->FLAGS & SVf_NOK) {
2089 } elsif ($sv->FLAGS & SVf_ROK) {
2090 return "\\(" . const($sv->RV) . ")"; # constant folded
2093 if ($str =~ /[^ -~]/) { # ASCII
2094 return single_delim("qq", '"', uninterp escape_str unback $str);
2096 $str =~ s/\\/\\\\/g;
2097 return single_delim("q", "'", $str);
2105 # if ($op->private & OPp_CONST_BARE) { # trouble with `=>' autoquoting
2106 # return $op->sv->PV;
2108 return const($op->sv);
2114 my $type = $op->ppaddr;
2115 if ($type eq "pp_const") {
2116 return uninterp(escape_str(unback($op->sv->PV)));
2117 } elsif ($type eq "pp_concat") {
2118 return $self->dq($op->first) . $self->dq($op->last);
2119 } elsif ($type eq "pp_uc") {
2120 return '\U' . $self->dq($op->first->sibling) . '\E';
2121 } elsif ($type eq "pp_lc") {
2122 return '\L' . $self->dq($op->first->sibling) . '\E';
2123 } elsif ($type eq "pp_ucfirst") {
2124 return '\u' . $self->dq($op->first->sibling);
2125 } elsif ($type eq "pp_lcfirst") {
2126 return '\l' . $self->dq($op->first->sibling);
2127 } elsif ($type eq "pp_quotemeta") {
2128 return '\Q' . $self->dq($op->first->sibling) . '\E';
2129 } elsif ($type eq "pp_join") {
2130 return $self->deparse($op->last, 26); # was join($", @ary)
2132 return $self->deparse($op, 26);
2140 return single_delim("qx", '`', $self->dq($op->first->sibling));
2146 # skip ex-stringify, pushmark
2147 return single_delim("qq", '"', $self->dq($op->first->sibling));
2150 # OP_STRINGIFY is a listop, but it only ever has one arg (?)
2151 sub pp_stringify { dquote(@_) }
2153 # tr/// and s/// (and tr[][], tr[]//, tr###, etc)
2154 # note that tr(from)/to/ is OK, but not tr/from/(to)
2156 my($from, $to) = @_;
2157 my($succeed, $delim);
2158 if ($from !~ m[/] and $to !~ m[/]) {
2159 return "/$from/$to/";
2160 } elsif (($succeed, $from) = balanced_delim($from) and $succeed) {
2161 if (($succeed, $to) = balanced_delim($to) and $succeed) {
2164 for $delim ('/', '"', '#') { # note no `'' -- s''' is special
2165 return "$from$delim$to$delim" if index($to, $delim) == -1;
2168 return "$from/$to/";
2171 for $delim ('/', '"', '#') { # note no '
2172 return "$delim$from$delim$to$delim"
2173 if index($to . $from, $delim) == -1;
2175 $from =~ s[/][\\/]g;
2177 return "/$from/$to/";
2183 if ($n == ord '\\') {
2185 } elsif ($n >= ord(' ') and $n <= ord('~')) {
2187 } elsif ($n == ord "\a") {
2189 } elsif ($n == ord "\b") {
2191 } elsif ($n == ord "\t") {
2193 } elsif ($n == ord "\n") {
2195 } elsif ($n == ord "\e") {
2197 } elsif ($n == ord "\f") {
2199 } elsif ($n == ord "\r") {
2201 } elsif ($n >= ord("\cA") and $n <= ord("\cZ")) {
2202 return '\\c' . chr(ord("@") + $n);
2204 # return '\x' . sprintf("%02x", $n);
2205 return '\\' . sprintf("%03o", $n);
2212 for ($c = 0; $c < @chars; $c++) {
2215 if ($c <= $#chars - 2 and $chars[$c + 1] == $tr + 1 and
2216 $chars[$c + 2] == $tr + 2)
2218 for (; $c <= $#chars and $chars[$c + 1] == $chars[$c] + 1; $c++) {}
2220 $str .= pchr($chars[$c]);
2226 sub OPpTRANS_SQUASH () { 16 }
2227 sub OPpTRANS_DELETE () { 32 }
2228 sub OPpTRANS_COMPLEMENT () { 64 }
2233 my(@table) = unpack("s256", $op->pv);
2234 my($c, $tr, @from, @to, @delfrom, $delhyphen);
2235 if ($table[ord "-"] != -1 and
2236 $table[ord("-") - 1] == -1 || $table[ord("-") + 1] == -1)
2238 $tr = $table[ord "-"];
2239 $table[ord "-"] = -1;
2243 } else { # -2 ==> delete
2247 for ($c = 0; $c < 256; $c++) {
2250 push @from, $c; push @to, $tr;
2251 } elsif ($tr == -2) {
2256 @from = (@from, @delfrom);
2257 if ($op->private & OPpTRANS_COMPLEMENT) {
2261 @from{@from} = (1) x @from;
2262 for ($c = 0; $c < 256; $c++) {
2263 push @newfrom, $c unless $from{$c};
2267 if ($op->private & OPpTRANS_DELETE) {
2270 pop @to while $#to and $to[$#to] == $to[$#to -1];
2272 $flags .= "s" if $op->private & OPpTRANS_SQUASH;
2274 $from = collapse(@from);
2275 $to = collapse(@to);
2276 $from .= "-" if $delhyphen;
2277 return "tr" . double_delim($from, $to) . $flags;
2280 # Like dq(), but different
2284 my $type = $op->ppaddr;
2285 if ($type eq "pp_const") {
2286 return uninterp($op->sv->PV);
2287 } elsif ($type eq "pp_concat") {
2288 return $self->re_dq($op->first) . $self->re_dq($op->last);
2289 } elsif ($type eq "pp_uc") {
2290 return '\U' . $self->re_dq($op->first->sibling) . '\E';
2291 } elsif ($type eq "pp_lc") {
2292 return '\L' . $self->re_dq($op->first->sibling) . '\E';
2293 } elsif ($type eq "pp_ucfirst") {
2294 return '\u' . $self->re_dq($op->first->sibling);
2295 } elsif ($type eq "pp_lcfirst") {
2296 return '\l' . $self->re_dq($op->first->sibling);
2297 } elsif ($type eq "pp_quotemeta") {
2298 return '\Q' . $self->re_dq($op->first->sibling) . '\E';
2299 } elsif ($type eq "pp_join") {
2300 return $self->deparse($op->last, 26); # was join($", @ary)
2302 return $self->deparse($op, 26);
2309 my $kid = $op->first;
2310 $kid = $kid->first if $kid->ppaddr eq "pp_regcmaybe";
2311 $kid = $kid->first if $kid->ppaddr eq "pp_regcreset";
2312 return $self->re_dq($kid);
2315 sub OPp_RUNTIME () { 64 }
2317 sub PMf_ONCE () { 0x2 }
2318 sub PMf_SKIPWHITE () { 0x10 }
2319 sub PMf_CONST () { 0x40 }
2320 sub PMf_KEEP () { 0x80 }
2321 sub PMf_GLOBAL () { 0x100 }
2322 sub PMf_CONTINUE () { 0x200 }
2323 sub PMf_EVAL () { 0x400 }
2324 sub PMf_LOCALE () { 0x800 }
2325 sub PMf_MULTILINE () { 0x1000 }
2326 sub PMf_SINGLELINE () { 0x2000 }
2327 sub PMf_FOLD () { 0x4000 }
2328 sub PMf_EXTENDED () { 0x8000 }
2330 # osmic acid -- see osmium tetroxide
2333 map($matchwords{join "", sort split //, $_} = $_, 'cig', 'cog', 'cos', 'cogs',
2334 'cox', 'go', 'is', 'ism', 'iso', 'mig', 'mix', 'osmic', 'ox', 'sic',
2335 'sig', 'six', 'smog', 'so', 'soc', 'sog', 'xi');
2339 my($op, $cx, $name, $delim) = @_;
2340 my $kid = $op->first;
2341 my ($binop, $var, $re) = ("", "", "");
2342 if ($op->flags & OPf_STACKED) {
2344 $var = $self->deparse($kid, 20);
2345 $kid = $kid->sibling;
2348 $re = re_uninterp(escape_str($op->precomp));
2350 $re = $self->deparse($kid, 1);
2353 $flags .= "c" if $op->pmflags & PMf_CONTINUE;
2354 $flags .= "g" if $op->pmflags & PMf_GLOBAL;
2355 $flags .= "i" if $op->pmflags & PMf_FOLD;
2356 $flags .= "m" if $op->pmflags & PMf_MULTILINE;
2357 $flags .= "o" if $op->pmflags & PMf_KEEP;
2358 $flags .= "s" if $op->pmflags & PMf_SINGLELINE;
2359 $flags .= "x" if $op->pmflags & PMf_EXTENDED;
2360 $flags = $matchwords{$flags} if $matchwords{$flags};
2361 if ($op->pmflags & PMf_ONCE) { # only one kind of delimiter works here
2365 $re = single_delim($name, $delim, $re);
2369 return $self->maybe_parens("$var =~ $re", $cx, 20);
2375 sub pp_match { matchop(@_, "m", "/") }
2376 sub pp_pushre { matchop(@_, "m", "/") }
2377 sub pp_qr { matchop(@_, "qr", "") }
2382 my($kid, @exprs, $ary, $expr);
2384 if ($ {$kid->pmreplroot}) {
2385 $ary = '@' . $self->gv_name($kid->pmreplroot);
2387 for (; !null($kid); $kid = $kid->sibling) {
2388 push @exprs, $self->deparse($kid, 6);
2390 $expr = "split(" . join(", ", @exprs) . ")";
2392 return $self->maybe_parens("$ary = $expr", $cx, 7);
2398 # oxime -- any of various compounds obtained chiefly by the action of
2399 # hydroxylamine on aldehydes and ketones and characterized by the
2400 # bivalent grouping C=NOH [Webster's Tenth]
2403 map($substwords{join "", sort split //, $_} = $_, 'ego', 'egoism', 'em',
2404 'es', 'ex', 'exes', 'gee', 'go', 'goes', 'ie', 'ism', 'iso', 'me',
2405 'meese', 'meso', 'mig', 'mix', 'os', 'ox', 'oxime', 'see', 'seem',
2406 'seg', 'sex', 'sig', 'six', 'smog', 'sog', 'some', 'xi');
2411 my $kid = $op->first;
2412 my($binop, $var, $re, $repl) = ("", "", "", "");
2413 if ($op->flags & OPf_STACKED) {
2415 $var = $self->deparse($kid, 20);
2416 $kid = $kid->sibling;
2419 if (null($op->pmreplroot)) {
2420 $repl = $self->dq($kid);
2421 $kid = $kid->sibling;
2423 $repl = $op->pmreplroot->first; # skip substcont
2424 while ($repl->ppaddr eq "pp_entereval") {
2425 $repl = $repl->first;
2428 $repl = $self->dq($repl);
2431 $re = re_uninterp(escape_str($op->precomp));
2433 $re = $self->deparse($kid, 1);
2435 $flags .= "e" if $op->pmflags & PMf_EVAL;
2436 $flags .= "g" if $op->pmflags & PMf_GLOBAL;
2437 $flags .= "i" if $op->pmflags & PMf_FOLD;
2438 $flags .= "m" if $op->pmflags & PMf_MULTILINE;
2439 $flags .= "o" if $op->pmflags & PMf_KEEP;
2440 $flags .= "s" if $op->pmflags & PMf_SINGLELINE;
2441 $flags .= "x" if $op->pmflags & PMf_EXTENDED;
2442 $flags = $substwords{$flags} if $substwords{$flags};
2444 return $self->maybe_parens("$var =~ s"
2445 . double_delim($re, $repl) . $flags,
2448 return "s". double_delim($re, $repl) . $flags;
2457 B::Deparse - Perl compiler backend to produce perl code
2461 B<perl> B<-MO=Deparse>[B<,-u>I<PACKAGE>][B<,-p>][B<,-s>I<LETTERS>] I<prog.pl>
2465 B::Deparse is a backend module for the Perl compiler that generates
2466 perl source code, based on the internal compiled structure that perl
2467 itself creates after parsing a program. The output of B::Deparse won't
2468 be exactly the same as the original source, since perl doesn't keep
2469 track of comments or whitespace, and there isn't a one-to-one
2470 correspondence between perl's syntactical constructions and their
2471 compiled form, but it will often be close. When you use the B<-p>
2472 option, the output also includes parentheses even when they are not
2473 required by precedence, which can make it easy to see if perl is
2474 parsing your expressions the way you intended.
2476 Please note that this module is mainly new and untested code and is
2477 still under development, so it may change in the future.
2481 As with all compiler backend options, these must follow directly after
2482 the '-MO=Deparse', separated by a comma but not any white space.
2488 Print extra parentheses. Without this option, B::Deparse includes
2489 parentheses in its output only when they are needed, based on the
2490 structure of your program. With B<-p>, it uses parentheses (almost)
2491 whenever they would be legal. This can be useful if you are used to
2492 LISP, or if you want to see how perl parses your input. If you say
2494 if ($var & 0x7f == 65) {print "Gimme an A!"}
2495 print ($which ? $a : $b), "\n";
2496 $name = $ENV{USER} or "Bob";
2498 C<B::Deparse,-p> will print
2501 print('Gimme an A!')
2503 (print(($which ? $a : $b)), '???');
2504 (($name = $ENV{'USER'}) or '???')
2506 which probably isn't what you intended (the C<'???'> is a sign that
2507 perl optimized away a constant value).
2509 =item B<-u>I<PACKAGE>
2511 Normally, B::Deparse deparses the main code of a program, all the subs
2512 called by the main program (and all the subs called by them,
2513 recursively), and any other subs in the main:: package. To include
2514 subs in other packages that aren't called directly, such as AUTOLOAD,
2515 DESTROY, other subs called automatically by perl, and methods, which
2516 aren't resolved to subs until runtime, use the B<-u> option. The
2517 argument to B<-u> is the name of a package, and should follow directly
2518 after the 'u'. Multiple B<-u> options may be given, separated by
2519 commas. Note that unlike some other backends, B::Deparse doesn't
2520 (yet) try to guess automatically when B<-u> is needed -- you must
2523 =item B<-s>I<LETTERS>
2525 Tweak the style of B::Deparse's output. At the moment, only one style
2526 option is implemented:
2532 Cuddle C<elsif>, C<else>, and C<continue> blocks. For example, print
2549 The default is not to cuddle.
2557 See the 'to do' list at the beginning of the module file.
2561 Stephen McCamant <alias@mcs.com>, based on an earlier version by
2562 Malcolm Beattie <mbeattie@sable.ox.ac.uk>.