Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

3128 lines
94 KiB

  1. # B::Deparse.pm
  2. # Copyright (c) 1998, 1999, 2000 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.
  5. # This is based on the module of the same name by Malcolm Beattie,
  6. # but essentially none of his code remains.
  7. package B::Deparse;
  8. use Carp 'cluck', 'croak';
  9. use B qw(class main_root main_start main_cv svref_2object opnumber
  10. OPf_WANT OPf_WANT_VOID OPf_WANT_SCALAR OPf_WANT_LIST
  11. OPf_KIDS OPf_REF OPf_STACKED OPf_SPECIAL
  12. OPpLVAL_INTRO OPpENTERSUB_AMPER OPpSLICE OPpCONST_BARE
  13. OPpTRANS_SQUASH OPpTRANS_DELETE OPpTRANS_COMPLEMENT OPpTARGET_MY
  14. SVf_IOK SVf_NOK SVf_ROK SVf_POK
  15. CVf_METHOD CVf_LOCKED CVf_LVALUE
  16. PMf_KEEP PMf_GLOBAL PMf_CONTINUE PMf_EVAL PMf_ONCE
  17. PMf_MULTILINE PMf_SINGLELINE PMf_FOLD PMf_EXTENDED);
  18. $VERSION = 0.60;
  19. use strict;
  20. # Changes between 0.50 and 0.51:
  21. # - fixed nulled leave with live enter in sort { }
  22. # - fixed reference constants (\"str")
  23. # - handle empty programs gracefully
  24. # - handle infinte loops (for (;;) {}, while (1) {})
  25. # - differentiate between `for my $x ...' and `my $x; for $x ...'
  26. # - various minor cleanups
  27. # - moved globals into an object
  28. # - added `-u', like B::C
  29. # - package declarations using cop_stash
  30. # - subs, formats and code sorted by cop_seq
  31. # Changes between 0.51 and 0.52:
  32. # - added pp_threadsv (special variables under USE_THREADS)
  33. # - added documentation
  34. # Changes between 0.52 and 0.53:
  35. # - many changes adding precedence contexts and associativity
  36. # - added `-p' and `-s' output style options
  37. # - various other minor fixes
  38. # Changes between 0.53 and 0.54:
  39. # - added support for new `for (1..100)' optimization,
  40. # thanks to Gisle Aas
  41. # Changes between 0.54 and 0.55:
  42. # - added support for new qr// construct
  43. # - added support for new pp_regcreset OP
  44. # Changes between 0.55 and 0.56:
  45. # - tested on base/*.t, cmd/*.t, comp/*.t, io/*.t
  46. # - fixed $# on non-lexicals broken in last big rewrite
  47. # - added temporary fix for change in opcode of OP_STRINGIFY
  48. # - fixed problem in 0.54's for() patch in `for (@ary)'
  49. # - fixed precedence in conditional of ?:
  50. # - tweaked list paren elimination in `my($x) = @_'
  51. # - made continue-block detection trickier wrt. null ops
  52. # - fixed various prototype problems in pp_entersub
  53. # - added support for sub prototypes that never get GVs
  54. # - added unquoting for special filehandle first arg in truncate
  55. # - print doubled rv2gv (a bug) as `*{*GV}' instead of illegal `**GV'
  56. # - added semicolons at the ends of blocks
  57. # - added -l `#line' declaration option -- fixes cmd/subval.t 27,28
  58. # Changes between 0.56 and 0.561:
  59. # - fixed multiply-declared my var in pp_truncate (thanks to Sarathy)
  60. # - used new B.pm symbolic constants (done by Nick Ing-Simmons)
  61. # Changes between 0.561 and 0.57:
  62. # - stylistic changes to symbolic constant stuff
  63. # - handled scope in s///e replacement code
  64. # - added unquote option for expanding "" into concats, etc.
  65. # - split method and proto parts of pp_entersub into separate functions
  66. # - various minor cleanups
  67. # Changes after 0.57:
  68. # - added parens in \&foo (patch by Albert Dvornik)
  69. # Changes between 0.57 and 0.58:
  70. # - fixed `0' statements that weren't being printed
  71. # - added methods for use from other programs
  72. # (based on patches from James Duncan and Hugo van der Sanden)
  73. # - added -si and -sT to control indenting (also based on a patch from Hugo)
  74. # - added -sv to print something else instead of '???'
  75. # - preliminary version of utf8 tr/// handling
  76. # Changes after 0.58:
  77. # - uses of $op->ppaddr changed to new $op->name (done by Sarathy)
  78. # - added support for Hugo's new OP_SETSTATE (like nextstate)
  79. # Changes between 0.58 and 0.59
  80. # - added support for Chip's OP_METHOD_NAMED
  81. # - added support for Ilya's OPpTARGET_MY optimization
  82. # - elided arrows before `()' subscripts when possible
  83. # Changes between 0.59 and 0.60
  84. # - support for method attribues was added
  85. # - some warnings fixed
  86. # - separate recognition of constant subs
  87. # - rewrote continue block handling, now recoginizing for loops
  88. # - added more control of expanding control structures
  89. # Todo:
  90. # - finish tr/// changes
  91. # - add option for even more parens (generalize \&foo change)
  92. # - {} around variables in strings ("${var}letters")
  93. # base/lex.t 25-27
  94. # comp/term.t 11
  95. # - left/right context
  96. # - recognize `use utf8', `use integer', etc
  97. # - treat top-level block specially for incremental output
  98. # - interpret high bit chars in string as utf8 \x{...} (when?)
  99. # - copy comments (look at real text with $^P?)
  100. # - avoid semis in one-statement blocks
  101. # - associativity of &&=, ||=, ?:
  102. # - ',' => '=>' (auto-unquote?)
  103. # - break long lines ("\r" as discretionary break?)
  104. # - configurable syntax highlighting: ANSI color, HTML, TeX, etc.
  105. # - more style options: brace style, hex vs. octal, quotes, ...
  106. # - print big ints as hex/octal instead of decimal (heuristic?)
  107. # - handle `my $x if 0'?
  108. # - include values of variables (e.g. set in BEGIN)
  109. # - coordinate with Data::Dumper (both directions? see previous)
  110. # - version using op_next instead of op_first/sibling?
  111. # - avoid string copies (pass arrays, one big join?)
  112. # - auto-apply `-u'?
  113. # - -uPackage:: descend recursively?
  114. # - here-docs?
  115. # - <DATA>?
  116. # Tests that will always fail:
  117. # comp/redef.t -- all (redefinition happens at compile time)
  118. # Object fields (were globals):
  119. #
  120. # avoid_local:
  121. # (local($a), local($b)) and local($a, $b) have the same internal
  122. # representation but the short form looks better. We notice we can
  123. # use a large-scale local when checking the list, but need to prevent
  124. # individual locals too. This hash holds the addresses of OPs that
  125. # have already had their local-ness accounted for. The same thing
  126. # is done with my().
  127. #
  128. # curcv:
  129. # CV for current sub (or main program) being deparsed
  130. #
  131. # curstash:
  132. # name of the current package for deparsed code
  133. #
  134. # subs_todo:
  135. # array of [cop_seq, GV, is_format?] for subs and formats we still
  136. # want to deparse
  137. #
  138. # protos_todo:
  139. # as above, but [name, prototype] for subs that never got a GV
  140. #
  141. # subs_done, forms_done:
  142. # keys are addresses of GVs for subs and formats we've already
  143. # deparsed (or at least put into subs_todo)
  144. #
  145. # parens: -p
  146. # linenums: -l
  147. # unquote: -q
  148. # cuddle: ` ' or `\n', depending on -sC
  149. # indent_size: -si
  150. # use_tabs: -sT
  151. # ex_const: -sv
  152. # A little explanation of how precedence contexts and associativity
  153. # work:
  154. #
  155. # deparse() calls each per-op subroutine with an argument $cx (short
  156. # for context, but not the same as the cx* in the perl core), which is
  157. # a number describing the op's parents in terms of precedence, whether
  158. # they're inside an expression or at statement level, etc. (see
  159. # chart below). When ops with children call deparse on them, they pass
  160. # along their precedence. Fractional values are used to implement
  161. # associativity (`($x + $y) + $z' => `$x + $y + $y') and related
  162. # parentheses hacks. The major disadvantage of this scheme is that
  163. # it doesn't know about right sides and left sides, so say if you
  164. # assign a listop to a variable, it can't tell it's allowed to leave
  165. # the parens off the listop.
  166. # Precedences:
  167. # 26 [TODO] inside interpolation context ("")
  168. # 25 left terms and list operators (leftward)
  169. # 24 left ->
  170. # 23 nonassoc ++ --
  171. # 22 right **
  172. # 21 right ! ~ \ and unary + and -
  173. # 20 left =~ !~
  174. # 19 left * / % x
  175. # 18 left + - .
  176. # 17 left << >>
  177. # 16 nonassoc named unary operators
  178. # 15 nonassoc < > <= >= lt gt le ge
  179. # 14 nonassoc == != <=> eq ne cmp
  180. # 13 left &
  181. # 12 left | ^
  182. # 11 left &&
  183. # 10 left ||
  184. # 9 nonassoc .. ...
  185. # 8 right ?:
  186. # 7 right = += -= *= etc.
  187. # 6 left , =>
  188. # 5 nonassoc list operators (rightward)
  189. # 4 right not
  190. # 3 left and
  191. # 2 left or xor
  192. # 1 statement modifiers
  193. # 0 statement level
  194. # Nonprinting characters with special meaning:
  195. # \cS - steal parens (see maybe_parens_unop)
  196. # \n - newline and indent
  197. # \t - increase indent
  198. # \b - decrease indent (`outdent')
  199. # \f - flush left (no indent)
  200. # \cK - kill following semicolon, if any
  201. sub null {
  202. my $op = shift;
  203. return class($op) eq "NULL";
  204. }
  205. sub todo {
  206. my $self = shift;
  207. my($gv, $cv, $is_form) = @_;
  208. my $seq;
  209. if (!null($cv->START) and is_state($cv->START)) {
  210. $seq = $cv->START->cop_seq;
  211. } else {
  212. $seq = 0;
  213. }
  214. push @{$self->{'subs_todo'}}, [$seq, $gv, $is_form];
  215. }
  216. sub next_todo {
  217. my $self = shift;
  218. my $ent = shift @{$self->{'subs_todo'}};
  219. my $name = $self->gv_name($ent->[1]);
  220. if ($ent->[2]) {
  221. return "format $name =\n"
  222. . $self->deparse_format($ent->[1]->FORM). "\n";
  223. } else {
  224. return "sub $name " . $self->deparse_sub($ent->[1]->CV);
  225. }
  226. }
  227. sub walk_tree {
  228. my($op, $sub) = @_;
  229. $sub->($op);
  230. if ($op->flags & OPf_KIDS) {
  231. my $kid;
  232. for ($kid = $op->first; not null $kid; $kid = $kid->sibling) {
  233. walk_tree($kid, $sub);
  234. }
  235. }
  236. }
  237. sub walk_sub {
  238. my $self = shift;
  239. my $cv = shift;
  240. my $op = $cv->ROOT;
  241. $op = shift if null $op;
  242. return if !$op or null $op;
  243. walk_tree($op, sub {
  244. my $op = shift;
  245. if ($op->name eq "gv") {
  246. my $gv = $self->gv_or_padgv($op);
  247. if ($op->next->name eq "entersub") {
  248. return if $self->{'subs_done'}{$$gv}++;
  249. return if class($gv->CV) eq "SPECIAL";
  250. $self->todo($gv, $gv->CV, 0);
  251. $self->walk_sub($gv->CV);
  252. } elsif ($op->next->name eq "enterwrite"
  253. or ($op->next->name eq "rv2gv"
  254. and $op->next->next->name eq "enterwrite")) {
  255. return if $self->{'forms_done'}{$$gv}++;
  256. return if class($gv->FORM) eq "SPECIAL";
  257. $self->todo($gv, $gv->FORM, 1);
  258. $self->walk_sub($gv->FORM);
  259. }
  260. }
  261. });
  262. }
  263. sub stash_subs {
  264. my $self = shift;
  265. my $pack = shift;
  266. my(%stash, @ret);
  267. { no strict 'refs'; %stash = svref_2object(\%{$pack . "::"})->ARRAY }
  268. if ($pack eq "main") {
  269. $pack = "";
  270. } else {
  271. $pack = $pack . "::";
  272. }
  273. my($key, $val);
  274. while (($key, $val) = each %stash) {
  275. my $class = class($val);
  276. if ($class eq "PV") {
  277. # Just a prototype
  278. push @{$self->{'protos_todo'}}, [$pack . $key, $val->PV];
  279. } elsif ($class eq "IV") {
  280. # Just a name
  281. push @{$self->{'protos_todo'}}, [$pack . $key, undef];
  282. } elsif ($class eq "GV") {
  283. if (class($val->CV) ne "SPECIAL") {
  284. next if $self->{'subs_done'}{$$val}++;
  285. $self->todo($val, $val->CV, 0);
  286. $self->walk_sub($val->CV);
  287. }
  288. if (class($val->FORM) ne "SPECIAL") {
  289. next if $self->{'forms_done'}{$$val}++;
  290. $self->todo($val, $val->FORM, 1);
  291. $self->walk_sub($val->FORM);
  292. }
  293. }
  294. }
  295. }
  296. sub print_protos {
  297. my $self = shift;
  298. my $ar;
  299. my @ret;
  300. foreach $ar (@{$self->{'protos_todo'}}) {
  301. my $proto = (defined $ar->[1] ? " (". $ar->[1] . ")" : "");
  302. push @ret, "sub " . $ar->[0] . "$proto;\n";
  303. }
  304. delete $self->{'protos_todo'};
  305. return @ret;
  306. }
  307. sub style_opts {
  308. my $self = shift;
  309. my $opts = shift;
  310. my $opt;
  311. while (length($opt = substr($opts, 0, 1))) {
  312. if ($opt eq "C") {
  313. $self->{'cuddle'} = " ";
  314. $opts = substr($opts, 1);
  315. } elsif ($opt eq "i") {
  316. $opts =~ s/^i(\d+)//;
  317. $self->{'indent_size'} = $1;
  318. } elsif ($opt eq "T") {
  319. $self->{'use_tabs'} = 1;
  320. $opts = substr($opts, 1);
  321. } elsif ($opt eq "v") {
  322. $opts =~ s/^v([^.]*)(.|$)//;
  323. $self->{'ex_const'} = $1;
  324. }
  325. }
  326. }
  327. sub new {
  328. my $class = shift;
  329. my $self = bless {}, $class;
  330. $self->{'subs_todo'} = [];
  331. $self->{'curstash'} = "main";
  332. $self->{'cuddle'} = "\n";
  333. $self->{'indent_size'} = 4;
  334. $self->{'use_tabs'} = 0;
  335. $self->{'expand'} = 0;
  336. $self->{'unquote'} = 0;
  337. $self->{'linenums'} = 0;
  338. $self->{'parens'} = 0;
  339. $self->{'ex_const'} = "'???'";
  340. while (my $arg = shift @_) {
  341. if (substr($arg, 0, 2) eq "-u") {
  342. $self->stash_subs(substr($arg, 2));
  343. } elsif ($arg eq "-p") {
  344. $self->{'parens'} = 1;
  345. } elsif ($arg eq "-l") {
  346. $self->{'linenums'} = 1;
  347. } elsif ($arg eq "-q") {
  348. $self->{'unquote'} = 1;
  349. } elsif (substr($arg, 0, 2) eq "-s") {
  350. $self->style_opts(substr $arg, 2);
  351. } elsif ($arg =~ /^-x(\d)$/) {
  352. $self->{'expand'} = $1;
  353. }
  354. }
  355. return $self;
  356. }
  357. sub compile {
  358. my(@args) = @_;
  359. return sub {
  360. my $self = B::Deparse->new(@args);
  361. $self->stash_subs("main");
  362. $self->{'curcv'} = main_cv;
  363. $self->walk_sub(main_cv, main_start);
  364. print $self->print_protos;
  365. @{$self->{'subs_todo'}} =
  366. sort {$a->[0] <=> $b->[0]} @{$self->{'subs_todo'}};
  367. print $self->indent($self->deparse(main_root, 0)), "\n"
  368. unless null main_root;
  369. my @text;
  370. while (scalar(@{$self->{'subs_todo'}})) {
  371. push @text, $self->next_todo;
  372. }
  373. print $self->indent(join("", @text)), "\n" if @text;
  374. }
  375. }
  376. sub coderef2text {
  377. my $self = shift;
  378. my $sub = shift;
  379. croak "Usage: ->coderef2text(CODEREF)" unless ref($sub) eq "CODE";
  380. return $self->indent($self->deparse_sub(svref_2object($sub)));
  381. }
  382. sub deparse {
  383. my $self = shift;
  384. my($op, $cx) = @_;
  385. # cluck if class($op) eq "NULL";
  386. # cluck unless $op;
  387. # return $self->$ {\("pp_" . $op->name)}($op, $cx);
  388. my $meth = "pp_" . $op->name;
  389. return $self->$meth($op, $cx);
  390. }
  391. sub indent {
  392. my $self = shift;
  393. my $txt = shift;
  394. my @lines = split(/\n/, $txt);
  395. my $leader = "";
  396. my $level = 0;
  397. my $line;
  398. for $line (@lines) {
  399. my $cmd = substr($line, 0, 1);
  400. if ($cmd eq "\t" or $cmd eq "\b") {
  401. $level += ($cmd eq "\t" ? 1 : -1) * $self->{'indent_size'};
  402. if ($self->{'use_tabs'}) {
  403. $leader = "\t" x ($level / 8) . " " x ($level % 8);
  404. } else {
  405. $leader = " " x $level;
  406. }
  407. $line = substr($line, 1);
  408. }
  409. if (substr($line, 0, 1) eq "\f") {
  410. $line = substr($line, 1); # no indent
  411. } else {
  412. $line = $leader . $line;
  413. }
  414. $line =~ s/\cK;?//g;
  415. }
  416. return join("\n", @lines);
  417. }
  418. sub deparse_sub {
  419. my $self = shift;
  420. my $cv = shift;
  421. my $proto = "";
  422. if ($cv->FLAGS & SVf_POK) {
  423. $proto = "(". $cv->PV . ") ";
  424. }
  425. if ($cv->CvFLAGS & (CVf_METHOD|CVf_LOCKED|CVf_LVALUE)) {
  426. $proto .= ": ";
  427. $proto .= "lvalue " if $cv->CvFLAGS & CVf_LVALUE;
  428. $proto .= "locked " if $cv->CvFLAGS & CVf_LOCKED;
  429. $proto .= "method " if $cv->CvFLAGS & CVf_METHOD;
  430. }
  431. local($self->{'curcv'}) = $cv;
  432. local($self->{'curstash'}) = $self->{'curstash'};
  433. if (not null $cv->ROOT) {
  434. # skip leavesub
  435. return $proto . "{\n\t" .
  436. $self->deparse($cv->ROOT->first, 0) . "\n\b}\n";
  437. } else { # XSUB?
  438. return $proto . "{}\n";
  439. }
  440. }
  441. sub deparse_format {
  442. my $self = shift;
  443. my $form = shift;
  444. my @text;
  445. local($self->{'curcv'}) = $form;
  446. local($self->{'curstash'}) = $self->{'curstash'};
  447. my $op = $form->ROOT;
  448. my $kid;
  449. $op = $op->first->first; # skip leavewrite, lineseq
  450. while (not null $op) {
  451. $op = $op->sibling; # skip nextstate
  452. my @exprs;
  453. $kid = $op->first->sibling; # skip pushmark
  454. push @text, $self->const_sv($kid)->PV;
  455. $kid = $kid->sibling;
  456. for (; not null $kid; $kid = $kid->sibling) {
  457. push @exprs, $self->deparse($kid, 0);
  458. }
  459. push @text, join(", ", @exprs)."\n" if @exprs;
  460. $op = $op->sibling;
  461. }
  462. return join("", @text) . ".";
  463. }
  464. sub is_scope {
  465. my $op = shift;
  466. return $op->name eq "leave" || $op->name eq "scope"
  467. || $op->name eq "lineseq"
  468. || ($op->name eq "null" && class($op) eq "UNOP"
  469. && (is_scope($op->first) || $op->first->name eq "enter"));
  470. }
  471. sub is_state {
  472. my $name = $_[0]->name;
  473. return $name eq "nextstate" || $name eq "dbstate" || $name eq "setstate";
  474. }
  475. sub is_miniwhile { # check for one-line loop (`foo() while $y--')
  476. my $op = shift;
  477. return (!null($op) and null($op->sibling)
  478. and $op->name eq "null" and class($op) eq "UNOP"
  479. and (($op->first->name =~ /^(and|or)$/
  480. and $op->first->first->sibling->name eq "lineseq")
  481. or ($op->first->name eq "lineseq"
  482. and not null $op->first->first->sibling
  483. and $op->first->first->sibling->name eq "unstack")
  484. ));
  485. }
  486. sub is_scalar {
  487. my $op = shift;
  488. return ($op->name eq "rv2sv" or
  489. $op->name eq "padsv" or
  490. $op->name eq "gv" or # only in array/hash constructs
  491. $op->flags & OPf_KIDS && !null($op->first)
  492. && $op->first->name eq "gvsv");
  493. }
  494. sub maybe_parens {
  495. my $self = shift;
  496. my($text, $cx, $prec) = @_;
  497. if ($prec < $cx # unary ops nest just fine
  498. or $prec == $cx and $cx != 4 and $cx != 16 and $cx != 21
  499. or $self->{'parens'})
  500. {
  501. $text = "($text)";
  502. # In a unop, let parent reuse our parens; see maybe_parens_unop
  503. $text = "\cS" . $text if $cx == 16;
  504. return $text;
  505. } else {
  506. return $text;
  507. }
  508. }
  509. # same as above, but get around the `if it looks like a function' rule
  510. sub maybe_parens_unop {
  511. my $self = shift;
  512. my($name, $kid, $cx) = @_;
  513. if ($cx > 16 or $self->{'parens'}) {
  514. return "$name(" . $self->deparse($kid, 1) . ")";
  515. } else {
  516. $kid = $self->deparse($kid, 16);
  517. if (substr($kid, 0, 1) eq "\cS") {
  518. # use kid's parens
  519. return $name . substr($kid, 1);
  520. } elsif (substr($kid, 0, 1) eq "(") {
  521. # avoid looks-like-a-function trap with extra parens
  522. # (`+' can lead to ambiguities)
  523. return "$name(" . $kid . ")";
  524. } else {
  525. return "$name $kid";
  526. }
  527. }
  528. }
  529. sub maybe_parens_func {
  530. my $self = shift;
  531. my($func, $text, $cx, $prec) = @_;
  532. if ($prec <= $cx or substr($text, 0, 1) eq "(" or $self->{'parens'}) {
  533. return "$func($text)";
  534. } else {
  535. return "$func $text";
  536. }
  537. }
  538. sub maybe_local {
  539. my $self = shift;
  540. my($op, $cx, $text) = @_;
  541. if ($op->private & OPpLVAL_INTRO and not $self->{'avoid_local'}{$$op}) {
  542. if (want_scalar($op)) {
  543. return "local $text";
  544. } else {
  545. return $self->maybe_parens_func("local", $text, $cx, 16);
  546. }
  547. } else {
  548. return $text;
  549. }
  550. }
  551. sub maybe_targmy {
  552. my $self = shift;
  553. my($op, $cx, $func, @args) = @_;
  554. if ($op->private & OPpTARGET_MY) {
  555. my $var = $self->padname($op->targ);
  556. my $val = $func->($self, $op, 7, @args);
  557. return $self->maybe_parens("$var = $val", $cx, 7);
  558. } else {
  559. return $func->($self, $op, $cx, @args);
  560. }
  561. }
  562. sub padname_sv {
  563. my $self = shift;
  564. my $targ = shift;
  565. return (($self->{'curcv'}->PADLIST->ARRAY)[0]->ARRAY)[$targ];
  566. }
  567. sub maybe_my {
  568. my $self = shift;
  569. my($op, $cx, $text) = @_;
  570. if ($op->private & OPpLVAL_INTRO and not $self->{'avoid_local'}{$$op}) {
  571. if (want_scalar($op)) {
  572. return "my $text";
  573. } else {
  574. return $self->maybe_parens_func("my", $text, $cx, 16);
  575. }
  576. } else {
  577. return $text;
  578. }
  579. }
  580. # The following OPs don't have functions:
  581. # pp_padany -- does not exist after parsing
  582. # pp_rcatline -- does not exist
  583. sub pp_enter { # see also leave
  584. cluck "unexpected OP_ENTER";
  585. return "XXX";
  586. }
  587. sub pp_pushmark { # see also list
  588. cluck "unexpected OP_PUSHMARK";
  589. return "XXX";
  590. }
  591. sub pp_leavesub { # see also deparse_sub
  592. cluck "unexpected OP_LEAVESUB";
  593. return "XXX";
  594. }
  595. sub pp_leavewrite { # see also deparse_format
  596. cluck "unexpected OP_LEAVEWRITE";
  597. return "XXX";
  598. }
  599. sub pp_method { # see also entersub
  600. cluck "unexpected OP_METHOD";
  601. return "XXX";
  602. }
  603. sub pp_regcmaybe { # see also regcomp
  604. cluck "unexpected OP_REGCMAYBE";
  605. return "XXX";
  606. }
  607. sub pp_regcreset { # see also regcomp
  608. cluck "unexpected OP_REGCRESET";
  609. return "XXX";
  610. }
  611. sub pp_substcont { # see also subst
  612. cluck "unexpected OP_SUBSTCONT";
  613. return "XXX";
  614. }
  615. sub pp_grepstart { # see also grepwhile
  616. cluck "unexpected OP_GREPSTART";
  617. return "XXX";
  618. }
  619. sub pp_mapstart { # see also mapwhile
  620. cluck "unexpected OP_MAPSTART";
  621. return "XXX";
  622. }
  623. sub pp_flip { # see also flop
  624. cluck "unexpected OP_FLIP";
  625. return "XXX";
  626. }
  627. sub pp_iter { # see also leaveloop
  628. cluck "unexpected OP_ITER";
  629. return "XXX";
  630. }
  631. sub pp_enteriter { # see also leaveloop
  632. cluck "unexpected OP_ENTERITER";
  633. return "XXX";
  634. }
  635. sub pp_enterloop { # see also leaveloop
  636. cluck "unexpected OP_ENTERLOOP";
  637. return "XXX";
  638. }
  639. sub pp_leaveeval { # see also entereval
  640. cluck "unexpected OP_LEAVEEVAL";
  641. return "XXX";
  642. }
  643. sub pp_entertry { # see also leavetry
  644. cluck "unexpected OP_ENTERTRY";
  645. return "XXX";
  646. }
  647. sub lineseq {
  648. my $self = shift;
  649. my(@ops) = @_;
  650. my($expr, @exprs);
  651. for (my $i = 0; $i < @ops; $i++) {
  652. $expr = "";
  653. if (is_state $ops[$i]) {
  654. $expr = $self->deparse($ops[$i], 0);
  655. $i++;
  656. last if $i > $#ops;
  657. }
  658. if (!is_state $ops[$i] and $ops[$i+1] and !null($ops[$i+1]) and
  659. $ops[$i+1]->name eq "leaveloop" and $self->{'expand'} < 3)
  660. {
  661. push @exprs, $expr . $self->for_loop($ops[$i], 0);
  662. $i++;
  663. next;
  664. }
  665. $expr .= $self->deparse($ops[$i], 0);
  666. push @exprs, $expr if length $expr;
  667. }
  668. return join(";\n", @exprs);
  669. }
  670. sub scopeop {
  671. my($real_block, $self, $op, $cx) = @_;
  672. my $kid;
  673. my @kids;
  674. local($self->{'curstash'}) = $self->{'curstash'} if $real_block;
  675. if ($real_block) {
  676. $kid = $op->first->sibling; # skip enter
  677. if (is_miniwhile($kid)) {
  678. my $top = $kid->first;
  679. my $name = $top->name;
  680. if ($name eq "and") {
  681. $name = "while";
  682. } elsif ($name eq "or") {
  683. $name = "until";
  684. } else { # no conditional -> while 1 or until 0
  685. return $self->deparse($top->first, 1) . " while 1";
  686. }
  687. my $cond = $top->first;
  688. my $body = $cond->sibling->first; # skip lineseq
  689. $cond = $self->deparse($cond, 1);
  690. $body = $self->deparse($body, 1);
  691. return "$body $name $cond";
  692. }
  693. } else {
  694. $kid = $op->first;
  695. }
  696. for (; !null($kid); $kid = $kid->sibling) {
  697. push @kids, $kid;
  698. }
  699. if ($cx > 0) { # inside an expression, (a do {} while for lineseq)
  700. return "do { " . $self->lineseq(@kids) . " }";
  701. } else {
  702. return $self->lineseq(@kids) . ";";
  703. }
  704. }
  705. sub pp_scope { scopeop(0, @_); }
  706. sub pp_lineseq { scopeop(0, @_); }
  707. sub pp_leave { scopeop(1, @_); }
  708. # The BEGIN {} is used here because otherwise this code isn't executed
  709. # when you run B::Deparse on itself.
  710. my %globalnames;
  711. BEGIN { map($globalnames{$_}++, "SIG", "STDIN", "STDOUT", "STDERR", "INC",
  712. "ENV", "ARGV", "ARGVOUT", "_"); }
  713. sub gv_name {
  714. my $self = shift;
  715. my $gv = shift;
  716. my $stash = $gv->STASH->NAME;
  717. my $name = $gv->SAFENAME;
  718. if ($stash eq $self->{'curstash'} or $globalnames{$name}
  719. or $name =~ /^[^A-Za-z_]/)
  720. {
  721. $stash = "";
  722. } else {
  723. $stash = $stash . "::";
  724. }
  725. if ($name =~ /^\^../) {
  726. $name = "{$name}"; # ${^WARNING_BITS} etc
  727. }
  728. return $stash . $name;
  729. }
  730. # Notice how subs and formats are inserted between statements here
  731. sub pp_nextstate {
  732. my $self = shift;
  733. my($op, $cx) = @_;
  734. my @text;
  735. @text = $op->label . ": " if $op->label;
  736. my $seq = $op->cop_seq;
  737. while (scalar(@{$self->{'subs_todo'}})
  738. and $seq > $self->{'subs_todo'}[0][0]) {
  739. push @text, $self->next_todo;
  740. }
  741. my $stash = $op->stashpv;
  742. if ($stash ne $self->{'curstash'}) {
  743. push @text, "package $stash;\n";
  744. $self->{'curstash'} = $stash;
  745. }
  746. if ($self->{'linenums'}) {
  747. push @text, "\f#line " . $op->line .
  748. ' "' . $op->file, qq'"\n';
  749. }
  750. return join("", @text);
  751. }
  752. sub pp_dbstate { pp_nextstate(@_) }
  753. sub pp_setstate { pp_nextstate(@_) }
  754. sub pp_unstack { return "" } # see also leaveloop
  755. sub baseop {
  756. my $self = shift;
  757. my($op, $cx, $name) = @_;
  758. return $name;
  759. }
  760. sub pp_stub { baseop(@_, "()") }
  761. sub pp_wantarray { baseop(@_, "wantarray") }
  762. sub pp_fork { baseop(@_, "fork") }
  763. sub pp_wait { maybe_targmy(@_, \&baseop, "wait") }
  764. sub pp_getppid { maybe_targmy(@_, \&baseop, "getppid") }
  765. sub pp_time { maybe_targmy(@_, \&baseop, "time") }
  766. sub pp_tms { baseop(@_, "times") }
  767. sub pp_ghostent { baseop(@_, "gethostent") }
  768. sub pp_gnetent { baseop(@_, "getnetent") }
  769. sub pp_gprotoent { baseop(@_, "getprotoent") }
  770. sub pp_gservent { baseop(@_, "getservent") }
  771. sub pp_ehostent { baseop(@_, "endhostent") }
  772. sub pp_enetent { baseop(@_, "endnetent") }
  773. sub pp_eprotoent { baseop(@_, "endprotoent") }
  774. sub pp_eservent { baseop(@_, "endservent") }
  775. sub pp_gpwent { baseop(@_, "getpwent") }
  776. sub pp_spwent { baseop(@_, "setpwent") }
  777. sub pp_epwent { baseop(@_, "endpwent") }
  778. sub pp_ggrent { baseop(@_, "getgrent") }
  779. sub pp_sgrent { baseop(@_, "setgrent") }
  780. sub pp_egrent { baseop(@_, "endgrent") }
  781. sub pp_getlogin { baseop(@_, "getlogin") }
  782. sub POSTFIX () { 1 }
  783. # I couldn't think of a good short name, but this is the category of
  784. # symbolic unary operators with interesting precedence
  785. sub pfixop {
  786. my $self = shift;
  787. my($op, $cx, $name, $prec, $flags) = (@_, 0);
  788. my $kid = $op->first;
  789. $kid = $self->deparse($kid, $prec);
  790. return $self->maybe_parens(($flags & POSTFIX) ? "$kid$name" : "$name$kid",
  791. $cx, $prec);
  792. }
  793. sub pp_preinc { pfixop(@_, "++", 23) }
  794. sub pp_predec { pfixop(@_, "--", 23) }
  795. sub pp_postinc { maybe_targmy(@_, \&pfixop, "++", 23, POSTFIX) }
  796. sub pp_postdec { maybe_targmy(@_, \&pfixop, "--", 23, POSTFIX) }
  797. sub pp_i_preinc { pfixop(@_, "++", 23) }
  798. sub pp_i_predec { pfixop(@_, "--", 23) }
  799. sub pp_i_postinc { maybe_targmy(@_, \&pfixop, "++", 23, POSTFIX) }
  800. sub pp_i_postdec { maybe_targmy(@_, \&pfixop, "--", 23, POSTFIX) }
  801. sub pp_complement { maybe_targmy(@_, \&pfixop, "~", 21) }
  802. sub pp_negate { maybe_targmy(@_, \&real_negate) }
  803. sub real_negate {
  804. my $self = shift;
  805. my($op, $cx) = @_;
  806. if ($op->first->name =~ /^(i_)?negate$/) {
  807. # avoid --$x
  808. $self->pfixop($op, $cx, "-", 21.5);
  809. } else {
  810. $self->pfixop($op, $cx, "-", 21);
  811. }
  812. }
  813. sub pp_i_negate { pp_negate(@_) }
  814. sub pp_not {
  815. my $self = shift;
  816. my($op, $cx) = @_;
  817. if ($cx <= 4) {
  818. $self->pfixop($op, $cx, "not ", 4);
  819. } else {
  820. $self->pfixop($op, $cx, "!", 21);
  821. }
  822. }
  823. sub unop {
  824. my $self = shift;
  825. my($op, $cx, $name) = @_;
  826. my $kid;
  827. if ($op->flags & OPf_KIDS) {
  828. $kid = $op->first;
  829. return $self->maybe_parens_unop($name, $kid, $cx);
  830. } else {
  831. return $name . ($op->flags & OPf_SPECIAL ? "()" : "");
  832. }
  833. }
  834. sub pp_chop { maybe_targmy(@_, \&unop, "chop") }
  835. sub pp_chomp { maybe_targmy(@_, \&unop, "chomp") }
  836. sub pp_schop { maybe_targmy(@_, \&unop, "chop") }
  837. sub pp_schomp { maybe_targmy(@_, \&unop, "chomp") }
  838. sub pp_defined { unop(@_, "defined") }
  839. sub pp_undef { unop(@_, "undef") }
  840. sub pp_study { unop(@_, "study") }
  841. sub pp_ref { unop(@_, "ref") }
  842. sub pp_pos { maybe_local(@_, unop(@_, "pos")) }
  843. sub pp_sin { maybe_targmy(@_, \&unop, "sin") }
  844. sub pp_cos { maybe_targmy(@_, \&unop, "cos") }
  845. sub pp_rand { maybe_targmy(@_, \&unop, "rand") }
  846. sub pp_srand { unop(@_, "srand") }
  847. sub pp_exp { maybe_targmy(@_, \&unop, "exp") }
  848. sub pp_log { maybe_targmy(@_, \&unop, "log") }
  849. sub pp_sqrt { maybe_targmy(@_, \&unop, "sqrt") }
  850. sub pp_int { maybe_targmy(@_, \&unop, "int") }
  851. sub pp_hex { maybe_targmy(@_, \&unop, "hex") }
  852. sub pp_oct { maybe_targmy(@_, \&unop, "oct") }
  853. sub pp_abs { maybe_targmy(@_, \&unop, "abs") }
  854. sub pp_length { maybe_targmy(@_, \&unop, "length") }
  855. sub pp_ord { maybe_targmy(@_, \&unop, "ord") }
  856. sub pp_chr { maybe_targmy(@_, \&unop, "chr") }
  857. sub pp_each { unop(@_, "each") }
  858. sub pp_values { unop(@_, "values") }
  859. sub pp_keys { unop(@_, "keys") }
  860. sub pp_pop { unop(@_, "pop") }
  861. sub pp_shift { unop(@_, "shift") }
  862. sub pp_caller { unop(@_, "caller") }
  863. sub pp_reset { unop(@_, "reset") }
  864. sub pp_exit { unop(@_, "exit") }
  865. sub pp_prototype { unop(@_, "prototype") }
  866. sub pp_close { unop(@_, "close") }
  867. sub pp_fileno { unop(@_, "fileno") }
  868. sub pp_umask { unop(@_, "umask") }
  869. sub pp_untie { unop(@_, "untie") }
  870. sub pp_tied { unop(@_, "tied") }
  871. sub pp_dbmclose { unop(@_, "dbmclose") }
  872. sub pp_getc { unop(@_, "getc") }
  873. sub pp_eof { unop(@_, "eof") }
  874. sub pp_tell { unop(@_, "tell") }
  875. sub pp_getsockname { unop(@_, "getsockname") }
  876. sub pp_getpeername { unop(@_, "getpeername") }
  877. sub pp_chdir { maybe_targmy(@_, \&unop, "chdir") }
  878. sub pp_chroot { maybe_targmy(@_, \&unop, "chroot") }
  879. sub pp_readlink { unop(@_, "readlink") }
  880. sub pp_rmdir { maybe_targmy(@_, \&unop, "rmdir") }
  881. sub pp_readdir { unop(@_, "readdir") }
  882. sub pp_telldir { unop(@_, "telldir") }
  883. sub pp_rewinddir { unop(@_, "rewinddir") }
  884. sub pp_closedir { unop(@_, "closedir") }
  885. sub pp_getpgrp { maybe_targmy(@_, \&unop, "getpgrp") }
  886. sub pp_localtime { unop(@_, "localtime") }
  887. sub pp_gmtime { unop(@_, "gmtime") }
  888. sub pp_alarm { unop(@_, "alarm") }
  889. sub pp_sleep { maybe_targmy(@_, \&unop, "sleep") }
  890. sub pp_dofile { unop(@_, "do") }
  891. sub pp_entereval { unop(@_, "eval") }
  892. sub pp_ghbyname { unop(@_, "gethostbyname") }
  893. sub pp_gnbyname { unop(@_, "getnetbyname") }
  894. sub pp_gpbyname { unop(@_, "getprotobyname") }
  895. sub pp_shostent { unop(@_, "sethostent") }
  896. sub pp_snetent { unop(@_, "setnetent") }
  897. sub pp_sprotoent { unop(@_, "setprotoent") }
  898. sub pp_sservent { unop(@_, "setservent") }
  899. sub pp_gpwnam { unop(@_, "getpwnam") }
  900. sub pp_gpwuid { unop(@_, "getpwuid") }
  901. sub pp_ggrnam { unop(@_, "getgrnam") }
  902. sub pp_ggrgid { unop(@_, "getgrgid") }
  903. sub pp_lock { unop(@_, "lock") }
  904. sub pp_exists {
  905. my $self = shift;
  906. my($op, $cx) = @_;
  907. return $self->maybe_parens_func("exists", $self->pp_helem($op->first, 16),
  908. $cx, 16);
  909. }
  910. sub pp_delete {
  911. my $self = shift;
  912. my($op, $cx) = @_;
  913. my $arg;
  914. if ($op->private & OPpSLICE) {
  915. return $self->maybe_parens_func("delete",
  916. $self->pp_hslice($op->first, 16),
  917. $cx, 16);
  918. } else {
  919. return $self->maybe_parens_func("delete",
  920. $self->pp_helem($op->first, 16),
  921. $cx, 16);
  922. }
  923. }
  924. sub pp_require {
  925. my $self = shift;
  926. my($op, $cx) = @_;
  927. if (class($op) eq "UNOP" and $op->first->name eq "const"
  928. and $op->first->private & OPpCONST_BARE)
  929. {
  930. my $name = $self->const_sv($op->first)->PV;
  931. $name =~ s[/][::]g;
  932. $name =~ s/\.pm//g;
  933. return "require($name)";
  934. } else {
  935. $self->unop($op, $cx, "require");
  936. }
  937. }
  938. sub pp_scalar {
  939. my $self = shift;
  940. my($op, $cv) = @_;
  941. my $kid = $op->first;
  942. if (not null $kid->sibling) {
  943. # XXX Was a here-doc
  944. return $self->dquote($op);
  945. }
  946. $self->unop(@_, "scalar");
  947. }
  948. sub padval {
  949. my $self = shift;
  950. my $targ = shift;
  951. #cluck "curcv was undef" unless $self->{curcv};
  952. return (($self->{'curcv'}->PADLIST->ARRAY)[1]->ARRAY)[$targ];
  953. }
  954. sub pp_refgen {
  955. my $self = shift;
  956. my($op, $cx) = @_;
  957. my $kid = $op->first;
  958. if ($kid->name eq "null") {
  959. $kid = $kid->first;
  960. if ($kid->name eq "anonlist" || $kid->name eq "anonhash") {
  961. my($pre, $post) = @{{"anonlist" => ["[","]"],
  962. "anonhash" => ["{","}"]}->{$kid->name}};
  963. my($expr, @exprs);
  964. $kid = $kid->first->sibling; # skip pushmark
  965. for (; !null($kid); $kid = $kid->sibling) {
  966. $expr = $self->deparse($kid, 6);
  967. push @exprs, $expr;
  968. }
  969. return $pre . join(", ", @exprs) . $post;
  970. } elsif (!null($kid->sibling) and
  971. $kid->sibling->name eq "anoncode") {
  972. return "sub " .
  973. $self->deparse_sub($self->padval($kid->sibling->targ));
  974. } elsif ($kid->name eq "pushmark") {
  975. my $sib_name = $kid->sibling->name;
  976. if ($sib_name =~ /^(pad|rv2)[ah]v$/
  977. and not $kid->sibling->flags & OPf_REF)
  978. {
  979. # The @a in \(@a) isn't in ref context, but only when the
  980. # parens are there.
  981. return "\\(" . $self->deparse($kid->sibling, 1) . ")";
  982. } elsif ($sib_name eq 'entersub') {
  983. my $text = $self->deparse($kid->sibling, 1);
  984. # Always show parens for \(&func()), but only with -p otherwise
  985. $text = "($text)" if $self->{'parens'}
  986. or $kid->sibling->private & OPpENTERSUB_AMPER;
  987. return "\\$text";
  988. }
  989. }
  990. }
  991. $self->pfixop($op, $cx, "\\", 20);
  992. }
  993. sub pp_srefgen { pp_refgen(@_) }
  994. sub pp_readline {
  995. my $self = shift;
  996. my($op, $cx) = @_;
  997. my $kid = $op->first;
  998. $kid = $kid->first if $kid->name eq "rv2gv"; # <$fh>
  999. return "<" . $self->deparse($kid, 1) . ">";
  1000. }
  1001. # Unary operators that can occur as pseudo-listops inside double quotes
  1002. sub dq_unop {
  1003. my $self = shift;
  1004. my($op, $cx, $name, $prec, $flags) = (@_, 0, 0);
  1005. my $kid;
  1006. if ($op->flags & OPf_KIDS) {
  1007. $kid = $op->first;
  1008. # If there's more than one kid, the first is an ex-pushmark.
  1009. $kid = $kid->sibling if not null $kid->sibling;
  1010. return $self->maybe_parens_unop($name, $kid, $cx);
  1011. } else {
  1012. return $name . ($op->flags & OPf_SPECIAL ? "()" : "");
  1013. }
  1014. }
  1015. sub pp_ucfirst { dq_unop(@_, "ucfirst") }
  1016. sub pp_lcfirst { dq_unop(@_, "lcfirst") }
  1017. sub pp_uc { dq_unop(@_, "uc") }
  1018. sub pp_lc { dq_unop(@_, "lc") }
  1019. sub pp_quotemeta { maybe_targmy(@_, \&dq_unop, "quotemeta") }
  1020. sub loopex {
  1021. my $self = shift;
  1022. my ($op, $cx, $name) = @_;
  1023. if (class($op) eq "PVOP") {
  1024. return "$name " . $op->pv;
  1025. } elsif (class($op) eq "OP") {
  1026. return $name;
  1027. } elsif (class($op) eq "UNOP") {
  1028. # Note -- loop exits are actually exempt from the
  1029. # looks-like-a-func rule, but a few extra parens won't hurt
  1030. return $self->maybe_parens_unop($name, $op->first, $cx);
  1031. }
  1032. }
  1033. sub pp_last { loopex(@_, "last") }
  1034. sub pp_next { loopex(@_, "next") }
  1035. sub pp_redo { loopex(@_, "redo") }
  1036. sub pp_goto { loopex(@_, "goto") }
  1037. sub pp_dump { loopex(@_, "dump") }
  1038. sub ftst {
  1039. my $self = shift;
  1040. my($op, $cx, $name) = @_;
  1041. if (class($op) eq "UNOP") {
  1042. # Genuine `-X' filetests are exempt from the LLAFR, but not
  1043. # l?stat(); for the sake of clarity, give'em all parens
  1044. return $self->maybe_parens_unop($name, $op->first, $cx);
  1045. } elsif (class($op) eq "SVOP") {
  1046. return $self->maybe_parens_func($name, $self->pp_gv($op, 1), $cx, 16);
  1047. } else { # I don't think baseop filetests ever survive ck_ftst, but...
  1048. return $name;
  1049. }
  1050. }
  1051. sub pp_lstat { ftst(@_, "lstat") }
  1052. sub pp_stat { ftst(@_, "stat") }
  1053. sub pp_ftrread { ftst(@_, "-R") }
  1054. sub pp_ftrwrite { ftst(@_, "-W") }
  1055. sub pp_ftrexec { ftst(@_, "-X") }
  1056. sub pp_fteread { ftst(@_, "-r") }
  1057. sub pp_ftewrite { ftst(@_, "-r") }
  1058. sub pp_fteexec { ftst(@_, "-r") }
  1059. sub pp_ftis { ftst(@_, "-e") }
  1060. sub pp_fteowned { ftst(@_, "-O") }
  1061. sub pp_ftrowned { ftst(@_, "-o") }
  1062. sub pp_ftzero { ftst(@_, "-z") }
  1063. sub pp_ftsize { ftst(@_, "-s") }
  1064. sub pp_ftmtime { ftst(@_, "-M") }
  1065. sub pp_ftatime { ftst(@_, "-A") }
  1066. sub pp_ftctime { ftst(@_, "-C") }
  1067. sub pp_ftsock { ftst(@_, "-S") }
  1068. sub pp_ftchr { ftst(@_, "-c") }
  1069. sub pp_ftblk { ftst(@_, "-b") }
  1070. sub pp_ftfile { ftst(@_, "-f") }
  1071. sub pp_ftdir { ftst(@_, "-d") }
  1072. sub pp_ftpipe { ftst(@_, "-p") }
  1073. sub pp_ftlink { ftst(@_, "-l") }
  1074. sub pp_ftsuid { ftst(@_, "-u") }
  1075. sub pp_ftsgid { ftst(@_, "-g") }
  1076. sub pp_ftsvtx { ftst(@_, "-k") }
  1077. sub pp_fttty { ftst(@_, "-t") }
  1078. sub pp_fttext { ftst(@_, "-T") }
  1079. sub pp_ftbinary { ftst(@_, "-B") }
  1080. sub SWAP_CHILDREN () { 1 }
  1081. sub ASSIGN () { 2 } # has OP= variant
  1082. my(%left, %right);
  1083. sub assoc_class {
  1084. my $op = shift;
  1085. my $name = $op->name;
  1086. if ($name eq "concat" and $op->first->name eq "concat") {
  1087. # avoid spurious `=' -- see comment in pp_concat
  1088. return "concat";
  1089. }
  1090. if ($name eq "null" and class($op) eq "UNOP"
  1091. and $op->first->name =~ /^(and|x?or)$/
  1092. and null $op->first->sibling)
  1093. {
  1094. # Like all conditional constructs, OP_ANDs and OP_ORs are topped
  1095. # with a null that's used as the common end point of the two
  1096. # flows of control. For precedence purposes, ignore it.
  1097. # (COND_EXPRs have these too, but we don't bother with
  1098. # their associativity).
  1099. return assoc_class($op->first);
  1100. }
  1101. return $name . ($op->flags & OPf_STACKED ? "=" : "");
  1102. }
  1103. # Left associative operators, like `+', for which
  1104. # $a + $b + $c is equivalent to ($a + $b) + $c
  1105. BEGIN {
  1106. %left = ('multiply' => 19, 'i_multiply' => 19,
  1107. 'divide' => 19, 'i_divide' => 19,
  1108. 'modulo' => 19, 'i_modulo' => 19,
  1109. 'repeat' => 19,
  1110. 'add' => 18, 'i_add' => 18,
  1111. 'subtract' => 18, 'i_subtract' => 18,
  1112. 'concat' => 18,
  1113. 'left_shift' => 17, 'right_shift' => 17,
  1114. 'bit_and' => 13,
  1115. 'bit_or' => 12, 'bit_xor' => 12,
  1116. 'and' => 3,
  1117. 'or' => 2, 'xor' => 2,
  1118. );
  1119. }
  1120. sub deparse_binop_left {
  1121. my $self = shift;
  1122. my($op, $left, $prec) = @_;
  1123. if ($left{assoc_class($op)} && $left{assoc_class($left)}
  1124. and $left{assoc_class($op)} == $left{assoc_class($left)})
  1125. {
  1126. return $self->deparse($left, $prec - .00001);
  1127. } else {
  1128. return $self->deparse($left, $prec);
  1129. }
  1130. }
  1131. # Right associative operators, like `=', for which
  1132. # $a = $b = $c is equivalent to $a = ($b = $c)
  1133. BEGIN {
  1134. %right = ('pow' => 22,
  1135. 'sassign=' => 7, 'aassign=' => 7,
  1136. 'multiply=' => 7, 'i_multiply=' => 7,
  1137. 'divide=' => 7, 'i_divide=' => 7,
  1138. 'modulo=' => 7, 'i_modulo=' => 7,
  1139. 'repeat=' => 7,
  1140. 'add=' => 7, 'i_add=' => 7,
  1141. 'subtract=' => 7, 'i_subtract=' => 7,
  1142. 'concat=' => 7,
  1143. 'left_shift=' => 7, 'right_shift=' => 7,
  1144. 'bit_and=' => 7,
  1145. 'bit_or=' => 7, 'bit_xor=' => 7,
  1146. 'andassign' => 7,
  1147. 'orassign' => 7,
  1148. );
  1149. }
  1150. sub deparse_binop_right {
  1151. my $self = shift;
  1152. my($op, $right, $prec) = @_;
  1153. if ($right{assoc_class($op)} && $right{assoc_class($right)}
  1154. and $right{assoc_class($op)} == $right{assoc_class($right)})
  1155. {
  1156. return $self->deparse($right, $prec - .00001);
  1157. } else {
  1158. return $self->deparse($right, $prec);
  1159. }
  1160. }
  1161. sub binop {
  1162. my $self = shift;
  1163. my ($op, $cx, $opname, $prec, $flags) = (@_, 0);
  1164. my $left = $op->first;
  1165. my $right = $op->last;
  1166. my $eq = "";
  1167. if ($op->flags & OPf_STACKED && $flags & ASSIGN) {
  1168. $eq = "=";
  1169. $prec = 7;
  1170. }
  1171. if ($flags & SWAP_CHILDREN) {
  1172. ($left, $right) = ($right, $left);
  1173. }
  1174. $left = $self->deparse_binop_left($op, $left, $prec);
  1175. $right = $self->deparse_binop_right($op, $right, $prec);
  1176. return $self->maybe_parens("$left $opname$eq $right", $cx, $prec);
  1177. }
  1178. sub pp_add { maybe_targmy(@_, \&binop, "+", 18, ASSIGN) }
  1179. sub pp_multiply { maybe_targmy(@_, \&binop, "*", 19, ASSIGN) }
  1180. sub pp_subtract { maybe_targmy(@_, \&binop, "-",18, ASSIGN) }
  1181. sub pp_divide { maybe_targmy(@_, \&binop, "/", 19, ASSIGN) }
  1182. sub pp_modulo { maybe_targmy(@_, \&binop, "%", 19, ASSIGN) }
  1183. sub pp_i_add { maybe_targmy(@_, \&binop, "+", 18, ASSIGN) }
  1184. sub pp_i_multiply { maybe_targmy(@_, \&binop, "*", 19, ASSIGN) }
  1185. sub pp_i_subtract { maybe_targmy(@_, \&binop, "-", 18, ASSIGN) }
  1186. sub pp_i_divide { maybe_targmy(@_, \&binop, "/", 19, ASSIGN) }
  1187. sub pp_i_modulo { maybe_targmy(@_, \&binop, "%", 19, ASSIGN) }
  1188. sub pp_pow { maybe_targmy(@_, \&binop, "**", 22, ASSIGN) }
  1189. sub pp_left_shift { maybe_targmy(@_, \&binop, "<<", 17, ASSIGN) }
  1190. sub pp_right_shift { maybe_targmy(@_, \&binop, ">>", 17, ASSIGN) }
  1191. sub pp_bit_and { maybe_targmy(@_, \&binop, "&", 13, ASSIGN) }
  1192. sub pp_bit_or { maybe_targmy(@_, \&binop, "|", 12, ASSIGN) }
  1193. sub pp_bit_xor { maybe_targmy(@_, \&binop, "^", 12, ASSIGN) }
  1194. sub pp_eq { binop(@_, "==", 14) }
  1195. sub pp_ne { binop(@_, "!=", 14) }
  1196. sub pp_lt { binop(@_, "<", 15) }
  1197. sub pp_gt { binop(@_, ">", 15) }
  1198. sub pp_ge { binop(@_, ">=", 15) }
  1199. sub pp_le { binop(@_, "<=", 15) }
  1200. sub pp_ncmp { binop(@_, "<=>", 14) }
  1201. sub pp_i_eq { binop(@_, "==", 14) }
  1202. sub pp_i_ne { binop(@_, "!=", 14) }
  1203. sub pp_i_lt { binop(@_, "<", 15) }
  1204. sub pp_i_gt { binop(@_, ">", 15) }
  1205. sub pp_i_ge { binop(@_, ">=", 15) }
  1206. sub pp_i_le { binop(@_, "<=", 15) }
  1207. sub pp_i_ncmp { binop(@_, "<=>", 14) }
  1208. sub pp_seq { binop(@_, "eq", 14) }
  1209. sub pp_sne { binop(@_, "ne", 14) }
  1210. sub pp_slt { binop(@_, "lt", 15) }
  1211. sub pp_sgt { binop(@_, "gt", 15) }
  1212. sub pp_sge { binop(@_, "ge", 15) }
  1213. sub pp_sle { binop(@_, "le", 15) }
  1214. sub pp_scmp { binop(@_, "cmp", 14) }
  1215. sub pp_sassign { binop(@_, "=", 7, SWAP_CHILDREN) }
  1216. sub pp_aassign { binop(@_, "=", 7, SWAP_CHILDREN) }
  1217. # `.' is special because concats-of-concats are optimized to save copying
  1218. # by making all but the first concat stacked. The effect is as if the
  1219. # programmer had written `($a . $b) .= $c', except legal.
  1220. sub pp_concat { maybe_targmy(@_, \&real_concat) }
  1221. sub real_concat {
  1222. my $self = shift;
  1223. my($op, $cx) = @_;
  1224. my $left = $op->first;
  1225. my $right = $op->last;
  1226. my $eq = "";
  1227. my $prec = 18;
  1228. if ($op->flags & OPf_STACKED and $op->first->name ne "concat") {
  1229. $eq = "=";
  1230. $prec = 7;
  1231. }
  1232. $left = $self->deparse_binop_left($op, $left, $prec);
  1233. $right = $self->deparse_binop_right($op, $right, $prec);
  1234. return $self->maybe_parens("$left .$eq $right", $cx, $prec);
  1235. }
  1236. # `x' is weird when the left arg is a list
  1237. sub pp_repeat {
  1238. my $self = shift;
  1239. my($op, $cx) = @_;
  1240. my $left = $op->first;
  1241. my $right = $op->last;
  1242. my $eq = "";
  1243. my $prec = 19;
  1244. if ($op->flags & OPf_STACKED) {
  1245. $eq = "=";
  1246. $prec = 7;
  1247. }
  1248. if (null($right)) { # list repeat; count is inside left-side ex-list
  1249. my $kid = $left->first->sibling; # skip pushmark
  1250. my @exprs;
  1251. for (; !null($kid->sibling); $kid = $kid->sibling) {
  1252. push @exprs, $self->deparse($kid, 6);
  1253. }
  1254. $right = $kid;
  1255. $left = "(" . join(", ", @exprs). ")";
  1256. } else {
  1257. $left = $self->deparse_binop_left($op, $left, $prec);
  1258. }
  1259. $right = $self->deparse_binop_right($op, $right, $prec);
  1260. return $self->maybe_parens("$left x$eq $right", $cx, $prec);
  1261. }
  1262. sub range {
  1263. my $self = shift;
  1264. my ($op, $cx, $type) = @_;
  1265. my $left = $op->first;
  1266. my $right = $left->sibling;
  1267. $left = $self->deparse($left, 9);
  1268. $right = $self->deparse($right, 9);
  1269. return $self->maybe_parens("$left $type $right", $cx, 9);
  1270. }
  1271. sub pp_flop {
  1272. my $self = shift;
  1273. my($op, $cx) = @_;
  1274. my $flip = $op->first;
  1275. my $type = ($flip->flags & OPf_SPECIAL) ? "..." : "..";
  1276. return $self->range($flip->first, $cx, $type);
  1277. }
  1278. # one-line while/until is handled in pp_leave
  1279. sub logop {
  1280. my $self = shift;
  1281. my ($op, $cx, $lowop, $lowprec, $highop, $highprec, $blockname) = @_;
  1282. my $left = $op->first;
  1283. my $right = $op->first->sibling;
  1284. if ($cx == 0 and is_scope($right) and $blockname
  1285. and $self->{'expand'} < 7)
  1286. { # if ($a) {$b}
  1287. $left = $self->deparse($left, 1);
  1288. $right = $self->deparse($right, 0);
  1289. return "$blockname ($left) {\n\t$right\n\b}\cK";
  1290. } elsif ($cx == 0 and $blockname and not $self->{'parens'}
  1291. and $self->{'expand'} < 7) { # $b if $a
  1292. $right = $self->deparse($right, 1);
  1293. $left = $self->deparse($left, 1);
  1294. return "$right $blockname $left";
  1295. } elsif ($cx > $lowprec and $highop) { # $a && $b
  1296. $left = $self->deparse_binop_left($op, $left, $highprec);
  1297. $right = $self->deparse_binop_right($op, $right, $highprec);
  1298. return $self->maybe_parens("$left $highop $right", $cx, $highprec);
  1299. } else { # $a and $b
  1300. $left = $self->deparse_binop_left($op, $left, $lowprec);
  1301. $right = $self->deparse_binop_right($op, $right, $lowprec);
  1302. return $self->maybe_parens("$left $lowop $right", $cx, $lowprec);
  1303. }
  1304. }
  1305. sub pp_and { logop(@_, "and", 3, "&&", 11, "if") }
  1306. sub pp_or { logop(@_, "or", 2, "||", 10, "unless") }
  1307. # xor is syntactically a logop, but it's really a binop (contrary to
  1308. # old versions of opcode.pl). Syntax is what matters here.
  1309. sub pp_xor { logop(@_, "xor", 2, "", 0, "") }
  1310. sub logassignop {
  1311. my $self = shift;
  1312. my ($op, $cx, $opname) = @_;
  1313. my $left = $op->first;
  1314. my $right = $op->first->sibling->first; # skip sassign
  1315. $left = $self->deparse($left, 7);
  1316. $right = $self->deparse($right, 7);
  1317. return $self->maybe_parens("$left $opname $right", $cx, 7);
  1318. }
  1319. sub pp_andassign { logassignop(@_, "&&=") }
  1320. sub pp_orassign { logassignop(@_, "||=") }
  1321. sub listop {
  1322. my $self = shift;
  1323. my($op, $cx, $name) = @_;
  1324. my(@exprs);
  1325. my $parens = ($cx >= 5) || $self->{'parens'};
  1326. my $kid = $op->first->sibling;
  1327. return $name if null $kid;
  1328. my $first = $self->deparse($kid, 6);
  1329. $first = "+$first" if not $parens and substr($first, 0, 1) eq "(";
  1330. push @exprs, $first;
  1331. $kid = $kid->sibling;
  1332. for (; !null($kid); $kid = $kid->sibling) {
  1333. push @exprs, $self->deparse($kid, 6);
  1334. }
  1335. if ($parens) {
  1336. return "$name(" . join(", ", @exprs) . ")";
  1337. } else {
  1338. return "$name " . join(", ", @exprs);
  1339. }
  1340. }
  1341. sub pp_bless { listop(@_, "bless") }
  1342. sub pp_atan2 { maybe_targmy(@_, \&listop, "atan2") }
  1343. sub pp_substr { maybe_local(@_, listop(@_, "substr")) }
  1344. sub pp_vec { maybe_local(@_, listop(@_, "vec")) }
  1345. sub pp_index { maybe_targmy(@_, \&listop, "index") }
  1346. sub pp_rindex { maybe_targmy(@_, \&listop, "rindex") }
  1347. sub pp_sprintf { maybe_targmy(@_, \&listop, "sprintf") }
  1348. sub pp_formline { listop(@_, "formline") } # see also deparse_format
  1349. sub pp_crypt { maybe_targmy(@_, \&listop, "crypt") }
  1350. sub pp_unpack { listop(@_, "unpack") }
  1351. sub pp_pack { listop(@_, "pack") }
  1352. sub pp_join { maybe_targmy(@_, \&listop, "join") }
  1353. sub pp_splice { listop(@_, "splice") }
  1354. sub pp_push { maybe_targmy(@_, \&listop, "push") }
  1355. sub pp_unshift { maybe_targmy(@_, \&listop, "unshift") }
  1356. sub pp_reverse { listop(@_, "reverse") }
  1357. sub pp_warn { listop(@_, "warn") }
  1358. sub pp_die { listop(@_, "die") }
  1359. # Actually, return is exempt from the LLAFR (see examples in this very
  1360. # module!), but for consistency's sake, ignore that fact
  1361. sub pp_return { listop(@_, "return") }
  1362. sub pp_open { listop(@_, "open") }
  1363. sub pp_pipe_op { listop(@_, "pipe") }
  1364. sub pp_tie { listop(@_, "tie") }
  1365. sub pp_binmode { listop(@_, "binmode") }
  1366. sub pp_dbmopen { listop(@_, "dbmopen") }
  1367. sub pp_sselect { listop(@_, "select") }
  1368. sub pp_select { listop(@_, "select") }
  1369. sub pp_read { listop(@_, "read") }
  1370. sub pp_sysopen { listop(@_, "sysopen") }
  1371. sub pp_sysseek { listop(@_, "sysseek") }
  1372. sub pp_sysread { listop(@_, "sysread") }
  1373. sub pp_syswrite { listop(@_, "syswrite") }
  1374. sub pp_send { listop(@_, "send") }
  1375. sub pp_recv { listop(@_, "recv") }
  1376. sub pp_seek { listop(@_, "seek") }
  1377. sub pp_fcntl { listop(@_, "fcntl") }
  1378. sub pp_ioctl { listop(@_, "ioctl") }
  1379. sub pp_flock { maybe_targmy(@_, \&listop, "flock") }
  1380. sub pp_socket { listop(@_, "socket") }
  1381. sub pp_sockpair { listop(@_, "sockpair") }
  1382. sub pp_bind { listop(@_, "bind") }
  1383. sub pp_connect { listop(@_, "connect") }
  1384. sub pp_listen { listop(@_, "listen") }
  1385. sub pp_accept { listop(@_, "accept") }
  1386. sub pp_shutdown { listop(@_, "shutdown") }
  1387. sub pp_gsockopt { listop(@_, "getsockopt") }
  1388. sub pp_ssockopt { listop(@_, "setsockopt") }
  1389. sub pp_chown { maybe_targmy(@_, \&listop, "chown") }
  1390. sub pp_unlink { maybe_targmy(@_, \&listop, "unlink") }
  1391. sub pp_chmod { maybe_targmy(@_, \&listop, "chmod") }
  1392. sub pp_utime { maybe_targmy(@_, \&listop, "utime") }
  1393. sub pp_rename { maybe_targmy(@_, \&listop, "rename") }
  1394. sub pp_link { maybe_targmy(@_, \&listop, "link") }
  1395. sub pp_symlink { maybe_targmy(@_, \&listop, "symlink") }
  1396. sub pp_mkdir { maybe_targmy(@_, \&listop, "mkdir") }
  1397. sub pp_open_dir { listop(@_, "opendir") }
  1398. sub pp_seekdir { listop(@_, "seekdir") }
  1399. sub pp_waitpid { maybe_targmy(@_, \&listop, "waitpid") }
  1400. sub pp_system { maybe_targmy(@_, \&listop, "system") }
  1401. sub pp_exec { maybe_targmy(@_, \&listop, "exec") }
  1402. sub pp_kill { maybe_targmy(@_, \&listop, "kill") }
  1403. sub pp_setpgrp { maybe_targmy(@_, \&listop, "setpgrp") }
  1404. sub pp_getpriority { maybe_targmy(@_, \&listop, "getpriority") }
  1405. sub pp_setpriority { maybe_targmy(@_, \&listop, "setpriority") }
  1406. sub pp_shmget { listop(@_, "shmget") }
  1407. sub pp_shmctl { listop(@_, "shmctl") }
  1408. sub pp_shmread { listop(@_, "shmread") }
  1409. sub pp_shmwrite { listop(@_, "shmwrite") }
  1410. sub pp_msgget { listop(@_, "msgget") }
  1411. sub pp_msgctl { listop(@_, "msgctl") }
  1412. sub pp_msgsnd { listop(@_, "msgsnd") }
  1413. sub pp_msgrcv { listop(@_, "msgrcv") }
  1414. sub pp_semget { listop(@_, "semget") }
  1415. sub pp_semctl { listop(@_, "semctl") }
  1416. sub pp_semop { listop(@_, "semop") }
  1417. sub pp_ghbyaddr { listop(@_, "gethostbyaddr") }
  1418. sub pp_gnbyaddr { listop(@_, "getnetbyaddr") }
  1419. sub pp_gpbynumber { listop(@_, "getprotobynumber") }
  1420. sub pp_gsbyname { listop(@_, "getservbyname") }
  1421. sub pp_gsbyport { listop(@_, "getservbyport") }
  1422. sub pp_syscall { listop(@_, "syscall") }
  1423. sub pp_glob {
  1424. my $self = shift;
  1425. my($op, $cx) = @_;
  1426. my $text = $self->dq($op->first->sibling); # skip pushmark
  1427. if ($text =~ /^\$?(\w|::|\`)+$/ # could look like a readline
  1428. or $text =~ /[<>]/) {
  1429. return 'glob(' . single_delim('qq', '"', $text) . ')';
  1430. } else {
  1431. return '<' . $text . '>';
  1432. }
  1433. }
  1434. # Truncate is special because OPf_SPECIAL makes a bareword first arg
  1435. # be a filehandle. This could probably be better fixed in the core
  1436. # by moving the GV lookup into ck_truc.
  1437. sub pp_truncate {
  1438. my $self = shift;
  1439. my($op, $cx) = @_;
  1440. my(@exprs);
  1441. my $parens = ($cx >= 5) || $self->{'parens'};
  1442. my $kid = $op->first->sibling;
  1443. my $fh;
  1444. if ($op->flags & OPf_SPECIAL) {
  1445. # $kid is an OP_CONST
  1446. $fh = $self->const_sv($kid)->PV;
  1447. } else {
  1448. $fh = $self->deparse($kid, 6);
  1449. $fh = "+$fh" if not $parens and substr($fh, 0, 1) eq "(";
  1450. }
  1451. my $len = $self->deparse($kid->sibling, 6);
  1452. if ($parens) {
  1453. return "truncate($fh, $len)";
  1454. } else {
  1455. return "truncate $fh, $len";
  1456. }
  1457. }
  1458. sub indirop {
  1459. my $self = shift;
  1460. my($op, $cx, $name) = @_;
  1461. my($expr, @exprs);
  1462. my $kid = $op->first->sibling;
  1463. my $indir = "";
  1464. if ($op->flags & OPf_STACKED) {
  1465. $indir = $kid;
  1466. $indir = $indir->first; # skip rv2gv
  1467. if (is_scope($indir)) {
  1468. $indir = "{" . $self->deparse($indir, 0) . "}";
  1469. } else {
  1470. $indir = $self->deparse($indir, 24);
  1471. }
  1472. $indir = $indir . " ";
  1473. $kid = $kid->sibling;
  1474. }
  1475. for (; !null($kid); $kid = $kid->sibling) {
  1476. $expr = $self->deparse($kid, 6);
  1477. push @exprs, $expr;
  1478. }
  1479. return $self->maybe_parens_func($name, $indir . join(", ", @exprs),
  1480. $cx, 5);
  1481. }
  1482. sub pp_prtf { indirop(@_, "printf") }
  1483. sub pp_print { indirop(@_, "print") }
  1484. sub pp_sort { indirop(@_, "sort") }
  1485. sub mapop {
  1486. my $self = shift;
  1487. my($op, $cx, $name) = @_;
  1488. my($expr, @exprs);
  1489. my $kid = $op->first; # this is the (map|grep)start
  1490. $kid = $kid->first->sibling; # skip a pushmark
  1491. my $code = $kid->first; # skip a null
  1492. if (is_scope $code) {
  1493. $code = "{" . $self->deparse($code, 0) . "} ";
  1494. } else {
  1495. $code = $self->deparse($code, 24) . ", ";
  1496. }
  1497. $kid = $kid->sibling;
  1498. for (; !null($kid); $kid = $kid->sibling) {
  1499. $expr = $self->deparse($kid, 6);
  1500. push @exprs, $expr if $expr;
  1501. }
  1502. return $self->maybe_parens_func($name, $code . join(", ", @exprs), $cx, 5);
  1503. }
  1504. sub pp_mapwhile { mapop(@_, "map") }
  1505. sub pp_grepwhile { mapop(@_, "grep") }
  1506. sub pp_list {
  1507. my $self = shift;
  1508. my($op, $cx) = @_;
  1509. my($expr, @exprs);
  1510. my $kid = $op->first->sibling; # skip pushmark
  1511. my $lop;
  1512. my $local = "either"; # could be local(...) or my(...)
  1513. for ($lop = $kid; !null($lop); $lop = $lop->sibling) {
  1514. # This assumes that no other private flags equal 128, and that
  1515. # OPs that store things other than flags in their op_private,
  1516. # like OP_AELEMFAST, won't be immediate children of a list.
  1517. unless ($lop->private & OPpLVAL_INTRO or $lop->name eq "undef")
  1518. {
  1519. $local = ""; # or not
  1520. last;
  1521. }
  1522. if ($lop->name =~ /^pad[ash]v$/) { # my()
  1523. ($local = "", last) if $local eq "local";
  1524. $local = "my";
  1525. } elsif ($lop->name ne "undef") { # local()
  1526. ($local = "", last) if $local eq "my";
  1527. $local = "local";
  1528. }
  1529. }
  1530. $local = "" if $local eq "either"; # no point if it's all undefs
  1531. return $self->deparse($kid, $cx) if null $kid->sibling and not $local;
  1532. for (; !null($kid); $kid = $kid->sibling) {
  1533. if ($local) {
  1534. if (class($kid) eq "UNOP" and $kid->first->name eq "gvsv") {
  1535. $lop = $kid->first;
  1536. } else {
  1537. $lop = $kid;
  1538. }
  1539. $self->{'avoid_local'}{$$lop}++;
  1540. $expr = $self->deparse($kid, 6);
  1541. delete $self->{'avoid_local'}{$$lop};
  1542. } else {
  1543. $expr = $self->deparse($kid, 6);
  1544. }
  1545. push @exprs, $expr;
  1546. }
  1547. if ($local) {
  1548. return "$local(" . join(", ", @exprs) . ")";
  1549. } else {
  1550. return $self->maybe_parens( join(", ", @exprs), $cx, 6);
  1551. }
  1552. }
  1553. sub is_ifelse_cont {
  1554. my $op = shift;
  1555. return ($op->name eq "null" and class($op) eq "UNOP"
  1556. and $op->first->name =~ /^(and|cond_expr)$/
  1557. and is_scope($op->first->first->sibling));
  1558. }
  1559. sub pp_cond_expr {
  1560. my $self = shift;
  1561. my($op, $cx) = @_;
  1562. my $cond = $op->first;
  1563. my $true = $cond->sibling;
  1564. my $false = $true->sibling;
  1565. my $cuddle = $self->{'cuddle'};
  1566. unless ($cx == 0 and (is_scope($true) and $true->name ne "null") and
  1567. (is_scope($false) || is_ifelse_cont($false))
  1568. and $self->{'expand'} < 7) {
  1569. $cond = $self->deparse($cond, 8);
  1570. $true = $self->deparse($true, 8);
  1571. $false = $self->deparse($false, 8);
  1572. return $self->maybe_parens("$cond ? $true : $false", $cx, 8);
  1573. }
  1574. $cond = $self->deparse($cond, 1);
  1575. $true = $self->deparse($true, 0);
  1576. my $head = "if ($cond) {\n\t$true\n\b}";
  1577. my @elsifs;
  1578. while (!null($false) and is_ifelse_cont($false)) {
  1579. my $newop = $false->first;
  1580. my $newcond = $newop->first;
  1581. my $newtrue = $newcond->sibling;
  1582. $false = $newtrue->sibling; # last in chain is OP_AND => no else
  1583. $newcond = $self->deparse($newcond, 1);
  1584. $newtrue = $self->deparse($newtrue, 0);
  1585. push @elsifs, "elsif ($newcond) {\n\t$newtrue\n\b}";
  1586. }
  1587. if (!null($false)) {
  1588. $false = $cuddle . "else {\n\t" .
  1589. $self->deparse($false, 0) . "\n\b}\cK";
  1590. } else {
  1591. $false = "\cK";
  1592. }
  1593. return $head . join($cuddle, "", @elsifs) . $false;
  1594. }
  1595. sub loop_common {
  1596. my $self = shift;
  1597. my($op, $cx, $init) = @_;
  1598. my $enter = $op->first;
  1599. my $kid = $enter->sibling;
  1600. local($self->{'curstash'}) = $self->{'curstash'};
  1601. my $head = "";
  1602. my $bare = 0;
  1603. my $body;
  1604. my $cond = undef;
  1605. if ($kid->name eq "lineseq") { # bare or infinite loop
  1606. if (is_state $kid->last) { # infinite
  1607. $head = "for (;;) "; # shorter than while (1)
  1608. $cond = "";
  1609. } else {
  1610. $bare = 1;
  1611. }
  1612. $body = $kid;
  1613. } elsif ($enter->name eq "enteriter") { # foreach
  1614. my $ary = $enter->first->sibling; # first was pushmark
  1615. my $var = $ary->sibling;
  1616. if ($enter->flags & OPf_STACKED
  1617. and not null $ary->first->sibling->sibling)
  1618. {
  1619. $ary = $self->deparse($ary->first->sibling, 9) . " .. " .
  1620. $self->deparse($ary->first->sibling->sibling, 9);
  1621. } else {
  1622. $ary = $self->deparse($ary, 1);
  1623. }
  1624. if (null $var) {
  1625. if ($enter->flags & OPf_SPECIAL) { # thread special var
  1626. $var = $self->pp_threadsv($enter, 1);
  1627. } else { # regular my() variable
  1628. $var = $self->pp_padsv($enter, 1);
  1629. if ($self->padname_sv($enter->targ)->IVX ==
  1630. $kid->first->first->sibling->last->cop_seq)
  1631. {
  1632. # If the scope of this variable closes at the last
  1633. # statement of the loop, it must have been
  1634. # declared here.
  1635. $var = "my " . $var;
  1636. }
  1637. }
  1638. } elsif ($var->name eq "rv2gv") {
  1639. $var = $self->pp_rv2sv($var, 1);
  1640. } elsif ($var->name eq "gv") {
  1641. $var = "\$" . $self->deparse($var, 1);
  1642. }
  1643. $head = "foreach $var ($ary) ";
  1644. $body = $kid->first->first->sibling; # skip OP_AND and OP_ITER
  1645. } elsif ($kid->name eq "null") { # while/until
  1646. $kid = $kid->first;
  1647. my $name = {"and" => "while", "or" => "until"}->{$kid->name};
  1648. $cond = $self->deparse($kid->first, 1);
  1649. $head = "$name ($cond) ";
  1650. $body = $kid->first->sibling;
  1651. } elsif ($kid->name eq "stub") { # bare and empty
  1652. return "{;}"; # {} could be a hashref
  1653. }
  1654. # If there isn't a continue block, then the next pointer for the loop
  1655. # will point to the unstack, which is kid's penultimate child, except
  1656. # in a bare loop, when it will point to the leaveloop. When neither of
  1657. # these conditions hold, then the third-to-last child in the continue
  1658. # block (or the last in a bare loop).
  1659. my $cont_start = $enter->nextop;
  1660. my $cont;
  1661. if ($$cont_start != $$op and $ {$cont_start->sibling} != $ {$body->last}) {
  1662. if ($bare) {
  1663. $cont = $body->last;
  1664. } else {
  1665. $cont = $body->first;
  1666. while (!null($cont->sibling->sibling->sibling)) {
  1667. $cont = $cont->sibling;
  1668. }
  1669. }
  1670. my $state = $body->first;
  1671. my $cuddle = $self->{'cuddle'};
  1672. my @states;
  1673. for (; $$state != $$cont; $state = $state->sibling) {
  1674. push @states, $state;
  1675. }
  1676. $body = $self->lineseq(@states);
  1677. if (defined $cond and not is_scope $cont and $self->{'expand'} < 3) {
  1678. $head = "for ($init; $cond; " . $self->deparse($cont, 1) .") ";
  1679. $cont = "\cK";
  1680. } else {
  1681. $cont = $cuddle . "continue {\n\t" .
  1682. $self->deparse($cont, 0) . "\n\b}\cK";
  1683. }
  1684. } else {
  1685. $cont = "\cK";
  1686. $body = $self->deparse($body, 0);
  1687. }
  1688. return $head . "{\n\t" . $body . "\n\b}" . $cont;
  1689. }
  1690. sub pp_leaveloop { loop_common(@_, "") }
  1691. sub for_loop {
  1692. my $self = shift;
  1693. my($op, $cx) = @_;
  1694. my $init = $self->deparse($op, 1);
  1695. return $self->loop_common($op->sibling, $cx, $init);
  1696. }
  1697. sub pp_leavetry {
  1698. my $self = shift;
  1699. return "eval {\n\t" . $self->pp_leave(@_) . "\n\b}";
  1700. }
  1701. BEGIN { eval "sub OP_CONST () {" . opnumber("const") . "}" }
  1702. BEGIN { eval "sub OP_STRINGIFY () {" . opnumber("stringify") . "}" }
  1703. sub pp_null {
  1704. my $self = shift;
  1705. my($op, $cx) = @_;
  1706. if (class($op) eq "OP") {
  1707. # old value is lost
  1708. return $self->{'ex_const'} if $op->targ == OP_CONST;
  1709. } elsif ($op->first->name eq "pushmark") {
  1710. return $self->pp_list($op, $cx);
  1711. } elsif ($op->first->name eq "enter") {
  1712. return $self->pp_leave($op, $cx);
  1713. } elsif ($op->targ == OP_STRINGIFY) {
  1714. return $self->dquote($op, $cx);
  1715. } elsif (!null($op->first->sibling) and
  1716. $op->first->sibling->name eq "readline" and
  1717. $op->first->sibling->flags & OPf_STACKED) {
  1718. return $self->maybe_parens($self->deparse($op->first, 7) . " = "
  1719. . $self->deparse($op->first->sibling, 7),
  1720. $cx, 7);
  1721. } elsif (!null($op->first->sibling) and
  1722. $op->first->sibling->name eq "trans" and
  1723. $op->first->sibling->flags & OPf_STACKED) {
  1724. return $self->maybe_parens($self->deparse($op->first, 20) . " =~ "
  1725. . $self->deparse($op->first->sibling, 20),
  1726. $cx, 20);
  1727. } else {
  1728. return $self->deparse($op->first, $cx);
  1729. }
  1730. }
  1731. sub padname {
  1732. my $self = shift;
  1733. my $targ = shift;
  1734. return $self->padname_sv($targ)->PVX;
  1735. }
  1736. sub padany {
  1737. my $self = shift;
  1738. my $op = shift;
  1739. return substr($self->padname($op->targ), 1); # skip $/@/%
  1740. }
  1741. sub pp_padsv {
  1742. my $self = shift;
  1743. my($op, $cx) = @_;
  1744. return $self->maybe_my($op, $cx, $self->padname($op->targ));
  1745. }
  1746. sub pp_padav { pp_padsv(@_) }
  1747. sub pp_padhv { pp_padsv(@_) }
  1748. my @threadsv_names;
  1749. BEGIN {
  1750. @threadsv_names = ("_", "1", "2", "3", "4", "5", "6", "7", "8", "9",
  1751. "&", "`", "'", "+", "/", ".", ",", "\\", '"', ";",
  1752. "^", "-", "%", "=", "|", "~", ":", "^A", "^E",
  1753. "!", "@");
  1754. }
  1755. sub pp_threadsv {
  1756. my $self = shift;
  1757. my($op, $cx) = @_;
  1758. return $self->maybe_local($op, $cx, "\$" . $threadsv_names[$op->targ]);
  1759. }
  1760. sub gv_or_padgv {
  1761. my $self = shift;
  1762. my $op = shift;
  1763. if (class($op) eq "PADOP") {
  1764. return $self->padval($op->padix);
  1765. } else { # class($op) eq "SVOP"
  1766. return $op->gv;
  1767. }
  1768. }
  1769. sub pp_gvsv {
  1770. my $self = shift;
  1771. my($op, $cx) = @_;
  1772. my $gv = $self->gv_or_padgv($op);
  1773. return $self->maybe_local($op, $cx, "\$" . $self->gv_name($gv));
  1774. }
  1775. sub pp_gv {
  1776. my $self = shift;
  1777. my($op, $cx) = @_;
  1778. my $gv = $self->gv_or_padgv($op);
  1779. return $self->gv_name($gv);
  1780. }
  1781. sub pp_aelemfast {
  1782. my $self = shift;
  1783. my($op, $cx) = @_;
  1784. my $gv = $self->gv_or_padgv($op);
  1785. return "\$" . $self->gv_name($gv) . "[" . $op->private . "]";
  1786. }
  1787. sub rv2x {
  1788. my $self = shift;
  1789. my($op, $cx, $type) = @_;
  1790. my $kid = $op->first;
  1791. my $str = $self->deparse($kid, 0);
  1792. return $type . (is_scalar($kid) ? $str : "{$str}");
  1793. }
  1794. sub pp_rv2sv { maybe_local(@_, rv2x(@_, "\$")) }
  1795. sub pp_rv2hv { maybe_local(@_, rv2x(@_, "%")) }
  1796. sub pp_rv2gv { maybe_local(@_, rv2x(@_, "*")) }
  1797. # skip rv2av
  1798. sub pp_av2arylen {
  1799. my $self = shift;
  1800. my($op, $cx) = @_;
  1801. if ($op->first->name eq "padav") {
  1802. return $self->maybe_local($op, $cx, '$#' . $self->padany($op->first));
  1803. } else {
  1804. return $self->maybe_local($op, $cx,
  1805. $self->rv2x($op->first, $cx, '$#'));
  1806. }
  1807. }
  1808. # skip down to the old, ex-rv2cv
  1809. sub pp_rv2cv { $_[0]->rv2x($_[1]->first->first->sibling, $_[2], "&") }
  1810. sub pp_rv2av {
  1811. my $self = shift;
  1812. my($op, $cx) = @_;
  1813. my $kid = $op->first;
  1814. if ($kid->name eq "const") { # constant list
  1815. my $av = $self->const_sv($kid);
  1816. return "(" . join(", ", map(const($_), $av->ARRAY)) . ")";
  1817. } else {
  1818. return $self->maybe_local($op, $cx, $self->rv2x($op, $cx, "\@"));
  1819. }
  1820. }
  1821. sub is_subscriptable {
  1822. my $op = shift;
  1823. if ($op->name =~ /^[ahg]elem/) {
  1824. return 1;
  1825. } elsif ($op->name eq "entersub") {
  1826. my $kid = $op->first;
  1827. return 0 unless null $kid->sibling;
  1828. $kid = $kid->first;
  1829. $kid = $kid->sibling until null $kid->sibling;
  1830. return 0 if is_scope($kid);
  1831. $kid = $kid->first;
  1832. return 0 if $kid->name eq "gv";
  1833. return 0 if is_scalar($kid);
  1834. return is_subscriptable($kid);
  1835. } else {
  1836. return 0;
  1837. }
  1838. }
  1839. sub elem {
  1840. my $self = shift;
  1841. my ($op, $cx, $left, $right, $padname) = @_;
  1842. my($array, $idx) = ($op->first, $op->first->sibling);
  1843. unless ($array->name eq $padname) { # Maybe this has been fixed
  1844. $array = $array->first; # skip rv2av (or ex-rv2av in _53+)
  1845. }
  1846. if ($array->name eq $padname) {
  1847. $array = $self->padany($array);
  1848. } elsif (is_scope($array)) { # ${expr}[0]
  1849. $array = "{" . $self->deparse($array, 0) . "}";
  1850. } elsif (is_scalar $array) { # $x[0], $$x[0], ...
  1851. $array = $self->deparse($array, 24);
  1852. } else {
  1853. # $x[20][3]{hi} or expr->[20]
  1854. my $arrow = is_subscriptable($array) ? "" : "->";
  1855. return $self->deparse($array, 24) . $arrow .
  1856. $left . $self->deparse($idx, 1) . $right;
  1857. }
  1858. $idx = $self->deparse($idx, 1);
  1859. return "\$" . $array . $left . $idx . $right;
  1860. }
  1861. sub pp_aelem { maybe_local(@_, elem(@_, "[", "]", "padav")) }
  1862. sub pp_helem { maybe_local(@_, elem(@_, "{", "}", "padhv")) }
  1863. sub pp_gelem {
  1864. my $self = shift;
  1865. my($op, $cx) = @_;
  1866. my($glob, $part) = ($op->first, $op->last);
  1867. $glob = $glob->first; # skip rv2gv
  1868. $glob = $glob->first if $glob->name eq "rv2gv"; # this one's a bug
  1869. my $scope = is_scope($glob);
  1870. $glob = $self->deparse($glob, 0);
  1871. $part = $self->deparse($part, 1);
  1872. return "*" . ($scope ? "{$glob}" : $glob) . "{$part}";
  1873. }
  1874. sub slice {
  1875. my $self = shift;
  1876. my ($op, $cx, $left, $right, $regname, $padname) = @_;
  1877. my $last;
  1878. my(@elems, $kid, $array, $list);
  1879. if (class($op) eq "LISTOP") {
  1880. $last = $op->last;
  1881. } else { # ex-hslice inside delete()
  1882. for ($kid = $op->first; !null $kid->sibling; $kid = $kid->sibling) {}
  1883. $last = $kid;
  1884. }
  1885. $array = $last;
  1886. $array = $array->first
  1887. if $array->name eq $regname or $array->name eq "null";
  1888. if (is_scope($array)) {
  1889. $array = "{" . $self->deparse($array, 0) . "}";
  1890. } elsif ($array->name eq $padname) {
  1891. $array = $self->padany($array);
  1892. } else {
  1893. $array = $self->deparse($array, 24);
  1894. }
  1895. $kid = $op->first->sibling; # skip pushmark
  1896. if ($kid->name eq "list") {
  1897. $kid = $kid->first->sibling; # skip list, pushmark
  1898. for (; !null $kid; $kid = $kid->sibling) {
  1899. push @elems, $self->deparse($kid, 6);
  1900. }
  1901. $list = join(", ", @elems);
  1902. } else {
  1903. $list = $self->deparse($kid, 1);
  1904. }
  1905. return "\@" . $array . $left . $list . $right;
  1906. }
  1907. sub pp_aslice { maybe_local(@_, slice(@_, "[", "]", "rv2av", "padav")) }
  1908. sub pp_hslice { maybe_local(@_, slice(@_, "{", "}", "rv2hv", "padhv")) }
  1909. sub pp_lslice {
  1910. my $self = shift;
  1911. my($op, $cx) = @_;
  1912. my $idx = $op->first;
  1913. my $list = $op->last;
  1914. my(@elems, $kid);
  1915. $list = $self->deparse($list, 1);
  1916. $idx = $self->deparse($idx, 1);
  1917. return "($list)" . "[$idx]";
  1918. }
  1919. sub want_scalar {
  1920. my $op = shift;
  1921. return ($op->flags & OPf_WANT) == OPf_WANT_SCALAR;
  1922. }
  1923. sub want_list {
  1924. my $op = shift;
  1925. return ($op->flags & OPf_WANT) == OPf_WANT_LIST;
  1926. }
  1927. sub method {
  1928. my $self = shift;
  1929. my($op, $cx) = @_;
  1930. my $kid = $op->first->sibling; # skip pushmark
  1931. my($meth, $obj, @exprs);
  1932. if ($kid->name eq "list" and want_list $kid) {
  1933. # When an indirect object isn't a bareword but the args are in
  1934. # parens, the parens aren't part of the method syntax (the LLAFR
  1935. # doesn't apply), but they make a list with OPf_PARENS set that
  1936. # doesn't get flattened by the append_elem that adds the method,
  1937. # making a (object, arg1, arg2, ...) list where the object
  1938. # usually is. This can be distinguished from
  1939. # `($obj, $arg1, $arg2)->meth()' (which is legal if $arg2 is an
  1940. # object) because in the later the list is in scalar context
  1941. # as the left side of -> always is, while in the former
  1942. # the list is in list context as method arguments always are.
  1943. # (Good thing there aren't method prototypes!)
  1944. $meth = $kid->sibling;
  1945. $kid = $kid->first->sibling; # skip pushmark
  1946. $obj = $kid;
  1947. $kid = $kid->sibling;
  1948. for (; not null $kid; $kid = $kid->sibling) {
  1949. push @exprs, $self->deparse($kid, 6);
  1950. }
  1951. } else {
  1952. $obj = $kid;
  1953. $kid = $kid->sibling;
  1954. for (; not null $kid->sibling; $kid = $kid->sibling) {
  1955. push @exprs, $self->deparse($kid, 6);
  1956. }
  1957. $meth = $kid;
  1958. }
  1959. $obj = $self->deparse($obj, 24);
  1960. if ($meth->name eq "method_named") {
  1961. $meth = $self->const_sv($meth)->PV;
  1962. } else {
  1963. $meth = $meth->first;
  1964. if ($meth->name eq "const") {
  1965. # As of 5.005_58, this case is probably obsoleted by the
  1966. # method_named case above
  1967. $meth = $self->const_sv($meth)->PV; # needs to be bare
  1968. } else {
  1969. $meth = $self->deparse($meth, 1);
  1970. }
  1971. }
  1972. my $args = join(", ", @exprs);
  1973. $kid = $obj . "->" . $meth;
  1974. if ($args) {
  1975. return $kid . "(" . $args . ")"; # parens mandatory
  1976. } else {
  1977. return $kid;
  1978. }
  1979. }
  1980. # returns "&" if the prototype doesn't match the args,
  1981. # or ("", $args_after_prototype_demunging) if it does.
  1982. sub check_proto {
  1983. my $self = shift;
  1984. my($proto, @args) = @_;
  1985. my($arg, $real);
  1986. my $doneok = 0;
  1987. my @reals;
  1988. # An unbackslashed @ or % gobbles up the rest of the args
  1989. $proto =~ s/([^\\]|^)([@%])(.*)$/$1$2/;
  1990. while ($proto) {
  1991. $proto =~ s/^ *([\\]?[\$\@&%*]|;)//;
  1992. my $chr = $1;
  1993. if ($chr eq "") {
  1994. return "&" if @args;
  1995. } elsif ($chr eq ";") {
  1996. $doneok = 1;
  1997. } elsif ($chr eq "@" or $chr eq "%") {
  1998. push @reals, map($self->deparse($_, 6), @args);
  1999. @args = ();
  2000. } else {
  2001. $arg = shift @args;
  2002. last unless $arg;
  2003. if ($chr eq "\$") {
  2004. if (want_scalar $arg) {
  2005. push @reals, $self->deparse($arg, 6);
  2006. } else {
  2007. return "&";
  2008. }
  2009. } elsif ($chr eq "&") {
  2010. if ($arg->name =~ /^(s?refgen|undef)$/) {
  2011. push @reals, $self->deparse($arg, 6);
  2012. } else {
  2013. return "&";
  2014. }
  2015. } elsif ($chr eq "*") {
  2016. if ($arg->name =~ /^s?refgen$/
  2017. and $arg->first->first->name eq "rv2gv")
  2018. {
  2019. $real = $arg->first->first; # skip refgen, null
  2020. if ($real->first->name eq "gv") {
  2021. push @reals, $self->deparse($real, 6);
  2022. } else {
  2023. push @reals, $self->deparse($real->first, 6);
  2024. }
  2025. } else {
  2026. return "&";
  2027. }
  2028. } elsif (substr($chr, 0, 1) eq "\\") {
  2029. $chr = substr($chr, 1);
  2030. if ($arg->name =~ /^s?refgen$/ and
  2031. !null($real = $arg->first) and
  2032. ($chr eq "\$" && is_scalar($real->first)
  2033. or ($chr eq "\@"
  2034. && $real->first->sibling->name
  2035. =~ /^(rv2|pad)av$/)
  2036. or ($chr eq "%"
  2037. && $real->first->sibling->name
  2038. =~ /^(rv2|pad)hv$/)
  2039. #or ($chr eq "&" # This doesn't work
  2040. # && $real->first->name eq "rv2cv")
  2041. or ($chr eq "*"
  2042. && $real->first->name eq "rv2gv")))
  2043. {
  2044. push @reals, $self->deparse($real, 6);
  2045. } else {
  2046. return "&";
  2047. }
  2048. }
  2049. }
  2050. }
  2051. return "&" if $proto and !$doneok; # too few args and no `;'
  2052. return "&" if @args; # too many args
  2053. return ("", join ", ", @reals);
  2054. }
  2055. sub pp_entersub {
  2056. my $self = shift;
  2057. my($op, $cx) = @_;
  2058. return $self->method($op, $cx) unless null $op->first->sibling;
  2059. my $prefix = "";
  2060. my $amper = "";
  2061. my($kid, @exprs);
  2062. if ($op->flags & OPf_SPECIAL) {
  2063. $prefix = "do ";
  2064. } elsif ($op->private & OPpENTERSUB_AMPER) {
  2065. $amper = "&";
  2066. }
  2067. $kid = $op->first;
  2068. $kid = $kid->first->sibling; # skip ex-list, pushmark
  2069. for (; not null $kid->sibling; $kid = $kid->sibling) {
  2070. push @exprs, $kid;
  2071. }
  2072. my $simple = 0;
  2073. my $proto = undef;
  2074. if (is_scope($kid)) {
  2075. $amper = "&";
  2076. $kid = "{" . $self->deparse($kid, 0) . "}";
  2077. } elsif ($kid->first->name eq "gv") {
  2078. my $gv = $self->gv_or_padgv($kid->first);
  2079. if (class($gv->CV) ne "SPECIAL") {
  2080. $proto = $gv->CV->PV if $gv->CV->FLAGS & SVf_POK;
  2081. }
  2082. $simple = 1; # only calls of named functions can be prototyped
  2083. $kid = $self->deparse($kid, 24);
  2084. } elsif (is_scalar $kid->first) {
  2085. $amper = "&";
  2086. $kid = $self->deparse($kid, 24);
  2087. } else {
  2088. $prefix = "";
  2089. my $arrow = is_subscriptable($kid->first) ? "" : "->";
  2090. $kid = $self->deparse($kid, 24) . $arrow;
  2091. }
  2092. my $args;
  2093. if (defined $proto and not $amper) {
  2094. ($amper, $args) = $self->check_proto($proto, @exprs);
  2095. if ($amper eq "&") {
  2096. $args = join(", ", map($self->deparse($_, 6), @exprs));
  2097. }
  2098. } else {
  2099. $args = join(", ", map($self->deparse($_, 6), @exprs));
  2100. }
  2101. if ($prefix or $amper) {
  2102. if ($op->flags & OPf_STACKED) {
  2103. return $prefix . $amper . $kid . "(" . $args . ")";
  2104. } else {
  2105. return $prefix . $amper. $kid;
  2106. }
  2107. } else {
  2108. if (defined $proto and $proto eq "") {
  2109. return $kid;
  2110. } elsif (defined $proto and $proto eq "\$") {
  2111. return $self->maybe_parens_func($kid, $args, $cx, 16);
  2112. } elsif (defined($proto) && $proto or $simple) {
  2113. return $self->maybe_parens_func($kid, $args, $cx, 5);
  2114. } else {
  2115. return "$kid(" . $args . ")";
  2116. }
  2117. }
  2118. }
  2119. sub pp_enterwrite { unop(@_, "write") }
  2120. # escape things that cause interpolation in double quotes,
  2121. # but not character escapes
  2122. sub uninterp {
  2123. my($str) = @_;
  2124. $str =~ s/(^|[^\\])([\$\@]|\\[uUlLQE])/$1\\$2/g;
  2125. return $str;
  2126. }
  2127. # the same, but treat $|, $), and $ at the end of the string differently
  2128. sub re_uninterp {
  2129. my($str) = @_;
  2130. $str =~ s/(^|[^\\])(\@|\\[uUlLQE])/$1\\$2/g;
  2131. $str =~ s/(^|[^\\])(\$[^)|])/$1\\$2/g;
  2132. return $str;
  2133. }
  2134. # character escapes, but not delimiters that might need to be escaped
  2135. sub escape_str { # ASCII
  2136. my($str) = @_;
  2137. $str =~ s/\a/\\a/g;
  2138. # $str =~ s/\cH/\\b/g; # \b means someting different in a regex
  2139. $str =~ s/\t/\\t/g;
  2140. $str =~ s/\n/\\n/g;
  2141. $str =~ s/\e/\\e/g;
  2142. $str =~ s/\f/\\f/g;
  2143. $str =~ s/\r/\\r/g;
  2144. $str =~ s/([\cA-\cZ])/'\\c' . chr(ord('@') + ord($1))/ge;
  2145. $str =~ s/([\0\033-\037\177-\377])/'\\' . sprintf("%03o", ord($1))/ge;
  2146. return $str;
  2147. }
  2148. # Don't do this for regexen
  2149. sub unback {
  2150. my($str) = @_;
  2151. $str =~ s/\\/\\\\/g;
  2152. return $str;
  2153. }
  2154. sub balanced_delim {
  2155. my($str) = @_;
  2156. my @str = split //, $str;
  2157. my($ar, $open, $close, $fail, $c, $cnt);
  2158. for $ar (['[',']'], ['(',')'], ['<','>'], ['{','}']) {
  2159. ($open, $close) = @$ar;
  2160. $fail = 0; $cnt = 0;
  2161. for $c (@str) {
  2162. if ($c eq $open) {
  2163. $cnt++;
  2164. } elsif ($c eq $close) {
  2165. $cnt--;
  2166. if ($cnt < 0) {
  2167. # qq()() isn't ")("
  2168. $fail = 1;
  2169. last;
  2170. }
  2171. }
  2172. }
  2173. $fail = 1 if $cnt != 0;
  2174. return ($open, "$open$str$close") if not $fail;
  2175. }
  2176. return ("", $str);
  2177. }
  2178. sub single_delim {
  2179. my($q, $default, $str) = @_;
  2180. return "$default$str$default" if $default and index($str, $default) == -1;
  2181. my($succeed, $delim);
  2182. ($succeed, $str) = balanced_delim($str);
  2183. return "$q$str" if $succeed;
  2184. for $delim ('/', '"', '#') {
  2185. return "$q$delim" . $str . $delim if index($str, $delim) == -1;
  2186. }
  2187. if ($default) {
  2188. $str =~ s/$default/\\$default/g;
  2189. return "$default$str$default";
  2190. } else {
  2191. $str =~ s[/][\\/]g;
  2192. return "$q/$str/";
  2193. }
  2194. }
  2195. sub const {
  2196. my $sv = shift;
  2197. if (class($sv) eq "SPECIAL") {
  2198. return ('undef', '1', '0')[$$sv-1]; # sv_undef, sv_yes, sv_no
  2199. } elsif ($sv->FLAGS & SVf_IOK) {
  2200. return $sv->int_value;
  2201. } elsif ($sv->FLAGS & SVf_NOK) {
  2202. return $sv->NV;
  2203. } elsif ($sv->FLAGS & SVf_ROK) {
  2204. return "\\(" . const($sv->RV) . ")"; # constant folded
  2205. } else {
  2206. my $str = $sv->PV;
  2207. if ($str =~ /[^ -~]/) { # ASCII for non-printing
  2208. return single_delim("qq", '"', uninterp escape_str unback $str);
  2209. } else {
  2210. return single_delim("q", "'", unback $str);
  2211. }
  2212. }
  2213. }
  2214. sub const_sv {
  2215. my $self = shift;
  2216. my $op = shift;
  2217. my $sv = $op->sv;
  2218. # the constant could be in the pad (under useithreads)
  2219. $sv = $self->padval($op->targ) unless $$sv;
  2220. return $sv;
  2221. }
  2222. sub pp_const {
  2223. my $self = shift;
  2224. my($op, $cx) = @_;
  2225. # if ($op->private & OPpCONST_BARE) { # trouble with `=>' autoquoting
  2226. # return $self->const_sv($op)->PV;
  2227. # }
  2228. my $sv = $self->const_sv($op);
  2229. # return const($sv);
  2230. my $c = const $sv;
  2231. return $c =~ /^-\d/ ? $self->maybe_parens($c, $cx, 21) : $c;
  2232. }
  2233. sub dq {
  2234. my $self = shift;
  2235. my $op = shift;
  2236. my $type = $op->name;
  2237. if ($type eq "const") {
  2238. return uninterp(escape_str(unback($self->const_sv($op)->PV)));
  2239. } elsif ($type eq "concat") {
  2240. my $first = $self->dq($op->first);
  2241. my $last = $self->dq($op->last);
  2242. # Disambiguate "${foo}bar", "${foo}{bar}", "${foo}[1]"
  2243. if ($last =~ /^[{\[\w]/) {
  2244. $first =~ s/([%\$@])([A-Za-z_]\w*)$/${1}{$2}/;
  2245. }
  2246. return $first . $last;
  2247. } elsif ($type eq "uc") {
  2248. return '\U' . $self->dq($op->first->sibling) . '\E';
  2249. } elsif ($type eq "lc") {
  2250. return '\L' . $self->dq($op->first->sibling) . '\E';
  2251. } elsif ($type eq "ucfirst") {
  2252. return '\u' . $self->dq($op->first->sibling);
  2253. } elsif ($type eq "lcfirst") {
  2254. return '\l' . $self->dq($op->first->sibling);
  2255. } elsif ($type eq "quotemeta") {
  2256. return '\Q' . $self->dq($op->first->sibling) . '\E';
  2257. } elsif ($type eq "join") {
  2258. return $self->deparse($op->last, 26); # was join($", @ary)
  2259. } else {
  2260. return $self->deparse($op, 26);
  2261. }
  2262. }
  2263. sub pp_backtick {
  2264. my $self = shift;
  2265. my($op, $cx) = @_;
  2266. # skip pushmark
  2267. return single_delim("qx", '`', $self->dq($op->first->sibling));
  2268. }
  2269. sub dquote {
  2270. my $self = shift;
  2271. my($op, $cx) = @_;
  2272. my $kid = $op->first->sibling; # skip ex-stringify, pushmark
  2273. return $self->deparse($kid, $cx) if $self->{'unquote'};
  2274. $self->maybe_targmy($kid, $cx,
  2275. sub {single_delim("qq", '"', $self->dq($_[1]))});
  2276. }
  2277. # OP_STRINGIFY is a listop, but it only ever has one arg
  2278. sub pp_stringify { maybe_targmy(@_, \&dquote) }
  2279. # tr/// and s/// (and tr[][], tr[]//, tr###, etc)
  2280. # note that tr(from)/to/ is OK, but not tr/from/(to)
  2281. sub double_delim {
  2282. my($from, $to) = @_;
  2283. my($succeed, $delim);
  2284. if ($from !~ m[/] and $to !~ m[/]) {
  2285. return "/$from/$to/";
  2286. } elsif (($succeed, $from) = balanced_delim($from) and $succeed) {
  2287. if (($succeed, $to) = balanced_delim($to) and $succeed) {
  2288. return "$from$to";
  2289. } else {
  2290. for $delim ('/', '"', '#') { # note no `'' -- s''' is special
  2291. return "$from$delim$to$delim" if index($to, $delim) == -1;
  2292. }
  2293. $to =~ s[/][\\/]g;
  2294. return "$from/$to/";
  2295. }
  2296. } else {
  2297. for $delim ('/', '"', '#') { # note no '
  2298. return "$delim$from$delim$to$delim"
  2299. if index($to . $from, $delim) == -1;
  2300. }
  2301. $from =~ s[/][\\/]g;
  2302. $to =~ s[/][\\/]g;
  2303. return "/$from/$to/";
  2304. }
  2305. }
  2306. sub pchr { # ASCII
  2307. my($n) = @_;
  2308. if ($n == ord '\\') {
  2309. return '\\\\';
  2310. } elsif ($n >= ord(' ') and $n <= ord('~')) {
  2311. return chr($n);
  2312. } elsif ($n == ord "\a") {
  2313. return '\\a';
  2314. } elsif ($n == ord "\b") {
  2315. return '\\b';
  2316. } elsif ($n == ord "\t") {
  2317. return '\\t';
  2318. } elsif ($n == ord "\n") {
  2319. return '\\n';
  2320. } elsif ($n == ord "\e") {
  2321. return '\\e';
  2322. } elsif ($n == ord "\f") {
  2323. return '\\f';
  2324. } elsif ($n == ord "\r") {
  2325. return '\\r';
  2326. } elsif ($n >= ord("\cA") and $n <= ord("\cZ")) {
  2327. return '\\c' . chr(ord("@") + $n);
  2328. } else {
  2329. # return '\x' . sprintf("%02x", $n);
  2330. return '\\' . sprintf("%03o", $n);
  2331. }
  2332. }
  2333. sub collapse {
  2334. my(@chars) = @_;
  2335. my($str, $c, $tr) = ("");
  2336. for ($c = 0; $c < @chars; $c++) {
  2337. $tr = $chars[$c];
  2338. $str .= pchr($tr);
  2339. if ($c <= $#chars - 2 and $chars[$c + 1] == $tr + 1 and
  2340. $chars[$c + 2] == $tr + 2)
  2341. {
  2342. for (; $c <= $#chars-1 and $chars[$c + 1] == $chars[$c] + 1; $c++)
  2343. {}
  2344. $str .= "-";
  2345. $str .= pchr($chars[$c]);
  2346. }
  2347. }
  2348. return $str;
  2349. }
  2350. # XXX This has trouble with hyphens in the replacement (tr/bac/-AC/),
  2351. # and backslashes.
  2352. sub tr_decode_byte {
  2353. my($table, $flags) = @_;
  2354. my(@table) = unpack("s256", $table);
  2355. my($c, $tr, @from, @to, @delfrom, $delhyphen);
  2356. if ($table[ord "-"] != -1 and
  2357. $table[ord("-") - 1] == -1 || $table[ord("-") + 1] == -1)
  2358. {
  2359. $tr = $table[ord "-"];
  2360. $table[ord "-"] = -1;
  2361. if ($tr >= 0) {
  2362. @from = ord("-");
  2363. @to = $tr;
  2364. } else { # -2 ==> delete
  2365. $delhyphen = 1;
  2366. }
  2367. }
  2368. for ($c = 0; $c < 256; $c++) {
  2369. $tr = $table[$c];
  2370. if ($tr >= 0) {
  2371. push @from, $c; push @to, $tr;
  2372. } elsif ($tr == -2) {
  2373. push @delfrom, $c;
  2374. }
  2375. }
  2376. @from = (@from, @delfrom);
  2377. if ($flags & OPpTRANS_COMPLEMENT) {
  2378. my @newfrom = ();
  2379. my %from;
  2380. @from{@from} = (1) x @from;
  2381. for ($c = 0; $c < 256; $c++) {
  2382. push @newfrom, $c unless $from{$c};
  2383. }
  2384. @from = @newfrom;
  2385. }
  2386. unless ($flags & OPpTRANS_DELETE || !@to) {
  2387. pop @to while $#to and $to[$#to] == $to[$#to -1];
  2388. }
  2389. my($from, $to);
  2390. $from = collapse(@from);
  2391. $to = collapse(@to);
  2392. $from .= "-" if $delhyphen;
  2393. return ($from, $to);
  2394. }
  2395. sub tr_chr {
  2396. my $x = shift;
  2397. if ($x == ord "-") {
  2398. return "\\-";
  2399. } else {
  2400. return chr $x;
  2401. }
  2402. }
  2403. # XXX This doesn't yet handle all cases correctly either
  2404. sub tr_decode_utf8 {
  2405. my($swash_hv, $flags) = @_;
  2406. my %swash = $swash_hv->ARRAY;
  2407. my $final = undef;
  2408. $final = $swash{'FINAL'}->IV if exists $swash{'FINAL'};
  2409. my $none = $swash{"NONE"}->IV;
  2410. my $extra = $none + 1;
  2411. my(@from, @delfrom, @to);
  2412. my $line;
  2413. foreach $line (split /\n/, $swash{'LIST'}->PV) {
  2414. my($min, $max, $result) = split(/\t/, $line);
  2415. $min = hex $min;
  2416. if (length $max) {
  2417. $max = hex $max;
  2418. } else {
  2419. $max = $min;
  2420. }
  2421. $result = hex $result;
  2422. if ($result == $extra) {
  2423. push @delfrom, [$min, $max];
  2424. } else {
  2425. push @from, [$min, $max];
  2426. push @to, [$result, $result + $max - $min];
  2427. }
  2428. }
  2429. for my $i (0 .. $#from) {
  2430. if ($from[$i][0] == ord '-') {
  2431. unshift @from, splice(@from, $i, 1);
  2432. unshift @to, splice(@to, $i, 1);
  2433. last;
  2434. } elsif ($from[$i][1] == ord '-') {
  2435. $from[$i][1]--;
  2436. $to[$i][1]--;
  2437. unshift @from, ord '-';
  2438. unshift @to, ord '-';
  2439. last;
  2440. }
  2441. }
  2442. for my $i (0 .. $#delfrom) {
  2443. if ($delfrom[$i][0] == ord '-') {
  2444. push @delfrom, splice(@delfrom, $i, 1);
  2445. last;
  2446. } elsif ($delfrom[$i][1] == ord '-') {
  2447. $delfrom[$i][1]--;
  2448. push @delfrom, ord '-';
  2449. last;
  2450. }
  2451. }
  2452. if (defined $final and $to[$#to][1] != $final) {
  2453. push @to, [$final, $final];
  2454. }
  2455. push @from, @delfrom;
  2456. if ($flags & OPpTRANS_COMPLEMENT) {
  2457. my @newfrom;
  2458. my $next = 0;
  2459. for my $i (0 .. $#from) {
  2460. push @newfrom, [$next, $from[$i][0] - 1];
  2461. $next = $from[$i][1] + 1;
  2462. }
  2463. @from = ();
  2464. for my $range (@newfrom) {
  2465. if ($range->[0] <= $range->[1]) {
  2466. push @from, $range;
  2467. }
  2468. }
  2469. }
  2470. my($from, $to, $diff);
  2471. for my $chunk (@from) {
  2472. $diff = $chunk->[1] - $chunk->[0];
  2473. if ($diff > 1) {
  2474. $from .= tr_chr($chunk->[0]) . "-" . tr_chr($chunk->[1]);
  2475. } elsif ($diff == 1) {
  2476. $from .= tr_chr($chunk->[0]) . tr_chr($chunk->[1]);
  2477. } else {
  2478. $from .= tr_chr($chunk->[0]);
  2479. }
  2480. }
  2481. for my $chunk (@to) {
  2482. $diff = $chunk->[1] - $chunk->[0];
  2483. if ($diff > 1) {
  2484. $to .= tr_chr($chunk->[0]) . "-" . tr_chr($chunk->[1]);
  2485. } elsif ($diff == 1) {
  2486. $to .= tr_chr($chunk->[0]) . tr_chr($chunk->[1]);
  2487. } else {
  2488. $to .= tr_chr($chunk->[0]);
  2489. }
  2490. }
  2491. #$final = sprintf("%04x", $final) if defined $final;
  2492. #$none = sprintf("%04x", $none) if defined $none;
  2493. #$extra = sprintf("%04x", $extra) if defined $extra;
  2494. #print STDERR "final: $final\n none: $none\nextra: $extra\n";
  2495. #print STDERR $swash{'LIST'}->PV;
  2496. return (escape_str($from), escape_str($to));
  2497. }
  2498. sub pp_trans {
  2499. my $self = shift;
  2500. my($op, $cx) = @_;
  2501. my($from, $to);
  2502. if (class($op) eq "PVOP") {
  2503. ($from, $to) = tr_decode_byte($op->pv, $op->private);
  2504. } else { # class($op) eq "SVOP"
  2505. ($from, $to) = tr_decode_utf8($op->sv->RV, $op->private);
  2506. }
  2507. my $flags = "";
  2508. $flags .= "c" if $op->private & OPpTRANS_COMPLEMENT;
  2509. $flags .= "d" if $op->private & OPpTRANS_DELETE;
  2510. $to = "" if $from eq $to and $flags eq "";
  2511. $flags .= "s" if $op->private & OPpTRANS_SQUASH;
  2512. return "tr" . double_delim($from, $to) . $flags;
  2513. }
  2514. # Like dq(), but different
  2515. sub re_dq {
  2516. my $self = shift;
  2517. my $op = shift;
  2518. my $type = $op->name;
  2519. if ($type eq "const") {
  2520. return re_uninterp($self->const_sv($op)->PV);
  2521. } elsif ($type eq "concat") {
  2522. my $first = $self->re_dq($op->first);
  2523. my $last = $self->re_dq($op->last);
  2524. # Disambiguate "${foo}bar", "${foo}{bar}", "${foo}[1]"
  2525. if ($last =~ /^[{\[\w]/) {
  2526. $first =~ s/([%\$@])([A-Za-z_]\w*)$/${1}{$2}/;
  2527. }
  2528. return $first . $last;
  2529. } elsif ($type eq "uc") {
  2530. return '\U' . $self->re_dq($op->first->sibling) . '\E';
  2531. } elsif ($type eq "lc") {
  2532. return '\L' . $self->re_dq($op->first->sibling) . '\E';
  2533. } elsif ($type eq "ucfirst") {
  2534. return '\u' . $self->re_dq($op->first->sibling);
  2535. } elsif ($type eq "lcfirst") {
  2536. return '\l' . $self->re_dq($op->first->sibling);
  2537. } elsif ($type eq "quotemeta") {
  2538. return '\Q' . $self->re_dq($op->first->sibling) . '\E';
  2539. } elsif ($type eq "join") {
  2540. return $self->deparse($op->last, 26); # was join($", @ary)
  2541. } else {
  2542. return $self->deparse($op, 26);
  2543. }
  2544. }
  2545. sub pp_regcomp {
  2546. my $self = shift;
  2547. my($op, $cx) = @_;
  2548. my $kid = $op->first;
  2549. $kid = $kid->first if $kid->name eq "regcmaybe";
  2550. $kid = $kid->first if $kid->name eq "regcreset";
  2551. return $self->re_dq($kid);
  2552. }
  2553. # osmic acid -- see osmium tetroxide
  2554. my %matchwords;
  2555. map($matchwords{join "", sort split //, $_} = $_, 'cig', 'cog', 'cos', 'cogs',
  2556. 'cox', 'go', 'is', 'ism', 'iso', 'mig', 'mix', 'osmic', 'ox', 'sic',
  2557. 'sig', 'six', 'smog', 'so', 'soc', 'sog', 'xi');
  2558. sub matchop {
  2559. my $self = shift;
  2560. my($op, $cx, $name, $delim) = @_;
  2561. my $kid = $op->first;
  2562. my ($binop, $var, $re) = ("", "", "");
  2563. if ($op->flags & OPf_STACKED) {
  2564. $binop = 1;
  2565. $var = $self->deparse($kid, 20);
  2566. $kid = $kid->sibling;
  2567. }
  2568. if (null $kid) {
  2569. $re = re_uninterp(escape_str($op->precomp));
  2570. } else {
  2571. $re = $self->deparse($kid, 1);
  2572. }
  2573. my $flags = "";
  2574. $flags .= "c" if $op->pmflags & PMf_CONTINUE;
  2575. $flags .= "g" if $op->pmflags & PMf_GLOBAL;
  2576. $flags .= "i" if $op->pmflags & PMf_FOLD;
  2577. $flags .= "m" if $op->pmflags & PMf_MULTILINE;
  2578. $flags .= "o" if $op->pmflags & PMf_KEEP;
  2579. $flags .= "s" if $op->pmflags & PMf_SINGLELINE;
  2580. $flags .= "x" if $op->pmflags & PMf_EXTENDED;
  2581. $flags = $matchwords{$flags} if $matchwords{$flags};
  2582. if ($op->pmflags & PMf_ONCE) { # only one kind of delimiter works here
  2583. $re =~ s/\?/\\?/g;
  2584. $re = "?$re?";
  2585. } else {
  2586. $re = single_delim($name, $delim, $re);
  2587. }
  2588. $re = $re . $flags;
  2589. if ($binop) {
  2590. return $self->maybe_parens("$var =~ $re", $cx, 20);
  2591. } else {
  2592. return $re;
  2593. }
  2594. }
  2595. sub pp_match { matchop(@_, "m", "/") }
  2596. sub pp_pushre { matchop(@_, "m", "/") }
  2597. sub pp_qr { matchop(@_, "qr", "") }
  2598. sub pp_split {
  2599. my $self = shift;
  2600. my($op, $cx) = @_;
  2601. my($kid, @exprs, $ary, $expr);
  2602. $kid = $op->first;
  2603. if ($ {$kid->pmreplroot}) {
  2604. $ary = '@' . $self->gv_name($kid->pmreplroot);
  2605. }
  2606. for (; !null($kid); $kid = $kid->sibling) {
  2607. push @exprs, $self->deparse($kid, 6);
  2608. }
  2609. $expr = "split(" . join(", ", @exprs) . ")";
  2610. if ($ary) {
  2611. return $self->maybe_parens("$ary = $expr", $cx, 7);
  2612. } else {
  2613. return $expr;
  2614. }
  2615. }
  2616. # oxime -- any of various compounds obtained chiefly by the action of
  2617. # hydroxylamine on aldehydes and ketones and characterized by the
  2618. # bivalent grouping C=NOH [Webster's Tenth]
  2619. my %substwords;
  2620. map($substwords{join "", sort split //, $_} = $_, 'ego', 'egoism', 'em',
  2621. 'es', 'ex', 'exes', 'gee', 'go', 'goes', 'ie', 'ism', 'iso', 'me',
  2622. 'meese', 'meso', 'mig', 'mix', 'os', 'ox', 'oxime', 'see', 'seem',
  2623. 'seg', 'sex', 'sig', 'six', 'smog', 'sog', 'some', 'xi');
  2624. sub pp_subst {
  2625. my $self = shift;
  2626. my($op, $cx) = @_;
  2627. my $kid = $op->first;
  2628. my($binop, $var, $re, $repl) = ("", "", "", "");
  2629. if ($op->flags & OPf_STACKED) {
  2630. $binop = 1;
  2631. $var = $self->deparse($kid, 20);
  2632. $kid = $kid->sibling;
  2633. }
  2634. my $flags = "";
  2635. if (null($op->pmreplroot)) {
  2636. $repl = $self->dq($kid);
  2637. $kid = $kid->sibling;
  2638. } else {
  2639. $repl = $op->pmreplroot->first; # skip substcont
  2640. while ($repl->name eq "entereval") {
  2641. $repl = $repl->first;
  2642. $flags .= "e";
  2643. }
  2644. if ($op->pmflags & PMf_EVAL) {
  2645. $repl = $self->deparse($repl, 0);
  2646. } else {
  2647. $repl = $self->dq($repl);
  2648. }
  2649. }
  2650. if (null $kid) {
  2651. $re = re_uninterp(escape_str($op->precomp));
  2652. } else {
  2653. $re = $self->deparse($kid, 1);
  2654. }
  2655. $flags .= "e" if $op->pmflags & PMf_EVAL;
  2656. $flags .= "g" if $op->pmflags & PMf_GLOBAL;
  2657. $flags .= "i" if $op->pmflags & PMf_FOLD;
  2658. $flags .= "m" if $op->pmflags & PMf_MULTILINE;
  2659. $flags .= "o" if $op->pmflags & PMf_KEEP;
  2660. $flags .= "s" if $op->pmflags & PMf_SINGLELINE;
  2661. $flags .= "x" if $op->pmflags & PMf_EXTENDED;
  2662. $flags = $substwords{$flags} if $substwords{$flags};
  2663. if ($binop) {
  2664. return $self->maybe_parens("$var =~ s"
  2665. . double_delim($re, $repl) . $flags,
  2666. $cx, 20);
  2667. } else {
  2668. return "s". double_delim($re, $repl) . $flags;
  2669. }
  2670. }
  2671. 1;
  2672. __END__
  2673. =head1 NAME
  2674. B::Deparse - Perl compiler backend to produce perl code
  2675. =head1 SYNOPSIS
  2676. B<perl> B<-MO=Deparse>[B<,-u>I<PACKAGE>][B<,-p>][B<,-q>][B<,-l>]
  2677. [B<,-s>I<LETTERS>][B<,-x>I<LEVEL>] I<prog.pl>
  2678. =head1 DESCRIPTION
  2679. B::Deparse is a backend module for the Perl compiler that generates
  2680. perl source code, based on the internal compiled structure that perl
  2681. itself creates after parsing a program. The output of B::Deparse won't
  2682. be exactly the same as the original source, since perl doesn't keep
  2683. track of comments or whitespace, and there isn't a one-to-one
  2684. correspondence between perl's syntactical constructions and their
  2685. compiled form, but it will often be close. When you use the B<-p>
  2686. option, the output also includes parentheses even when they are not
  2687. required by precedence, which can make it easy to see if perl is
  2688. parsing your expressions the way you intended.
  2689. Please note that this module is mainly new and untested code and is
  2690. still under development, so it may change in the future.
  2691. =head1 OPTIONS
  2692. As with all compiler backend options, these must follow directly after
  2693. the '-MO=Deparse', separated by a comma but not any white space.
  2694. =over 4
  2695. =item B<-l>
  2696. Add '#line' declarations to the output based on the line and file
  2697. locations of the original code.
  2698. =item B<-p>
  2699. Print extra parentheses. Without this option, B::Deparse includes
  2700. parentheses in its output only when they are needed, based on the
  2701. structure of your program. With B<-p>, it uses parentheses (almost)
  2702. whenever they would be legal. This can be useful if you are used to
  2703. LISP, or if you want to see how perl parses your input. If you say
  2704. if ($var & 0x7f == 65) {print "Gimme an A!"}
  2705. print ($which ? $a : $b), "\n";
  2706. $name = $ENV{USER} or "Bob";
  2707. C<B::Deparse,-p> will print
  2708. if (($var & 0)) {
  2709. print('Gimme an A!')
  2710. };
  2711. (print(($which ? $a : $b)), '???');
  2712. (($name = $ENV{'USER'}) or '???')
  2713. which probably isn't what you intended (the C<'???'> is a sign that
  2714. perl optimized away a constant value).
  2715. =item B<-q>
  2716. Expand double-quoted strings into the corresponding combinations of
  2717. concatenation, uc, ucfirst, lc, lcfirst, quotemeta, and join. For
  2718. instance, print
  2719. print "Hello, $world, @ladies, \u$gentlemen\E, \u\L$me!";
  2720. as
  2721. print 'Hello, ' . $world . ', ' . join($", @ladies) . ', '
  2722. . ucfirst($gentlemen) . ', ' . ucfirst(lc $me . '!');
  2723. Note that the expanded form represents the way perl handles such
  2724. constructions internally -- this option actually turns off the reverse
  2725. translation that B::Deparse usually does. On the other hand, note that
  2726. C<$x = "$y"> is not the same as C<$x = $y>: the former makes the value
  2727. of $y into a string before doing the assignment.
  2728. =item B<-u>I<PACKAGE>
  2729. Normally, B::Deparse deparses the main code of a program, all the subs
  2730. called by the main program (and all the subs called by them,
  2731. recursively), and any other subs in the main:: package. To include
  2732. subs in other packages that aren't called directly, such as AUTOLOAD,
  2733. DESTROY, other subs called automatically by perl, and methods (which
  2734. aren't resolved to subs until runtime), use the B<-u> option. The
  2735. argument to B<-u> is the name of a package, and should follow directly
  2736. after the 'u'. Multiple B<-u> options may be given, separated by
  2737. commas. Note that unlike some other backends, B::Deparse doesn't
  2738. (yet) try to guess automatically when B<-u> is needed -- you must
  2739. invoke it yourself.
  2740. =item B<-s>I<LETTERS>
  2741. Tweak the style of B::Deparse's output. The letters should follow
  2742. directly after the 's', with no space or punctuation. The following
  2743. options are available:
  2744. =over 4
  2745. =item B<C>
  2746. Cuddle C<elsif>, C<else>, and C<continue> blocks. For example, print
  2747. if (...) {
  2748. ...
  2749. } else {
  2750. ...
  2751. }
  2752. instead of
  2753. if (...) {
  2754. ...
  2755. }
  2756. else {
  2757. ...
  2758. }
  2759. The default is not to cuddle.
  2760. =item B<i>I<NUMBER>
  2761. Indent lines by multiples of I<NUMBER> columns. The default is 4 columns.
  2762. =item B<T>
  2763. Use tabs for each 8 columns of indent. The default is to use only spaces.
  2764. For instance, if the style options are B<-si4T>, a line that's indented
  2765. 3 times will be preceded by one tab and four spaces; if the options were
  2766. B<-si8T>, the same line would be preceded by three tabs.
  2767. =item B<v>I<STRING>B<.>
  2768. Print I<STRING> for the value of a constant that can't be determined
  2769. because it was optimized away (mnemonic: this happens when a constant
  2770. is used in B<v>oid context). The end of the string is marked by a period.
  2771. The string should be a valid perl expression, generally a constant.
  2772. Note that unless it's a number, it probably needs to be quoted, and on
  2773. a command line quotes need to be protected from the shell. Some
  2774. conventional values include 0, 1, 42, '', 'foo', and
  2775. 'Useless use of constant omitted' (which may need to be
  2776. B<-sv"'Useless use of constant omitted'.">
  2777. or something similar depending on your shell). The default is '???'.
  2778. If you're using B::Deparse on a module or other file that's require'd,
  2779. you shouldn't use a value that evaluates to false, since the customary
  2780. true constant at the end of a module will be in void context when the
  2781. file is compiled as a main program.
  2782. =back
  2783. =item B<-x>I<LEVEL>
  2784. Expand conventional syntax constructions into equivalent ones that expose
  2785. their internal operation. I<LEVEL> should be a digit, with higher values
  2786. meaning more expansion. As with B<-q>, this actually involves turning off
  2787. special cases in B::Deparse's normal operations.
  2788. If I<LEVEL> is at least 3, for loops will be translated into equivalent
  2789. while loops with continue blocks; for instance
  2790. for ($i = 0; $i < 10; ++$i) {
  2791. print $i;
  2792. }
  2793. turns into
  2794. $i = 0;
  2795. while ($i < 10) {
  2796. print $i;
  2797. } continue {
  2798. ++$i
  2799. }
  2800. Note that in a few cases this translation can't be perfectly carried back
  2801. into the source code -- if the loop's initializer declares a my variable,
  2802. for instance, it won't have the correct scope outside of the loop.
  2803. If I<LEVEL> is at least 7, if statements will be translated into equivalent
  2804. expressions using C<&&>, C<?:> and C<do {}>; for instance
  2805. print 'hi' if $nice;
  2806. if ($nice) {
  2807. print 'hi';
  2808. }
  2809. if ($nice) {
  2810. print 'hi';
  2811. } else {
  2812. print 'bye';
  2813. }
  2814. turns into
  2815. $nice and print 'hi';
  2816. $nice and do { print 'hi' };
  2817. $nice ? do { print 'hi' } : do { print 'bye' };
  2818. Long sequences of elsifs will turn into nested ternary operators, which
  2819. B::Deparse doesn't know how to indent nicely.
  2820. =back
  2821. =head1 USING B::Deparse AS A MODULE
  2822. =head2 Synopsis
  2823. use B::Deparse;
  2824. $deparse = B::Deparse->new("-p", "-sC");
  2825. $body = $deparse->coderef2text(\&func);
  2826. eval "sub func $body"; # the inverse operation
  2827. =head2 Description
  2828. B::Deparse can also be used on a sub-by-sub basis from other perl
  2829. programs.
  2830. =head2 new
  2831. $deparse = B::Deparse->new(OPTIONS)
  2832. Create an object to store the state of a deparsing operation and any
  2833. options. The options are the same as those that can be given on the
  2834. command line (see L</OPTIONS>); options that are separated by commas
  2835. after B<-MO=Deparse> should be given as separate strings. Some
  2836. options, like B<-u>, don't make sense for a single subroutine, so
  2837. don't pass them.
  2838. =head2 coderef2text
  2839. $body = $deparse->coderef2text(\&func)
  2840. $body = $deparse->coderef2text(sub ($$) { ... })
  2841. Return source code for the body of a subroutine (a block, optionally
  2842. preceded by a prototype in parens), given a reference to the
  2843. sub. Because a subroutine can have no names, or more than one name,
  2844. this method doesn't return a complete subroutine definition -- if you
  2845. want to eval the result, you should prepend "sub subname ", or "sub "
  2846. for an anonymous function constructor. Unless the sub was defined in
  2847. the main:: package, the code will include a package declaration.
  2848. =head1 BUGS
  2849. See the 'to do' list at the beginning of the module file.
  2850. =head1 AUTHOR
  2851. Stephen McCamant <[email protected]>, based on an earlier
  2852. version by Malcolm Beattie <[email protected]>, with
  2853. contributions from Gisle Aas, James Duncan, Albert Dvornik, Hugo van
  2854. der Sanden, Gurusamy Sarathy, and Nick Ing-Simmons.
  2855. =cut