| 1 | # B::Deparse.pm |
| 2 | # Copyright (c) 1998-2000, 2002, 2003, 2004, 2005, 2006 Stephen McCamant. |
| 3 | # All rights reserved. |
| 4 | # This module is free software; you can redistribute and/or modify |
| 5 | # it under the same terms as Perl itself. |
| 6 | |
| 7 | # This is based on the module of the same name by Malcolm Beattie, |
| 8 | # but essentially none of his code remains. |
| 9 | |
| 10 | package B::Deparse; |
| 11 | use Carp; |
| 12 | use B qw(class main_root main_start main_cv svref_2object opnumber perlstring |
| 13 | OPf_WANT OPf_WANT_VOID OPf_WANT_SCALAR OPf_WANT_LIST |
| 14 | OPf_KIDS OPf_REF OPf_STACKED OPf_SPECIAL OPf_MOD |
| 15 | OPpLVAL_INTRO OPpOUR_INTRO OPpENTERSUB_AMPER OPpSLICE OPpCONST_BARE |
| 16 | OPpTRANS_SQUASH OPpTRANS_DELETE OPpTRANS_COMPLEMENT OPpTARGET_MY |
| 17 | OPpEXISTS_SUB OPpSORT_NUMERIC OPpSORT_INTEGER |
| 18 | OPpSORT_REVERSE |
| 19 | SVf_IOK SVf_NOK SVf_ROK SVf_POK SVpad_OUR SVf_FAKE SVs_RMG SVs_SMG |
| 20 | CVf_METHOD CVf_LVALUE |
| 21 | PMf_KEEP PMf_GLOBAL PMf_CONTINUE PMf_EVAL PMf_ONCE |
| 22 | PMf_MULTILINE PMf_SINGLELINE PMf_FOLD PMf_EXTENDED); |
| 23 | $VERSION = "1.11"; |
| 24 | use strict; |
| 25 | use vars qw/$AUTOLOAD/; |
| 26 | use warnings (); |
| 27 | |
| 28 | BEGIN { |
| 29 | # List version-specific constants here. |
| 30 | # Easiest way to keep this code portable between version looks to |
| 31 | # be to fake up a dummy constant that will never actually be true. |
| 32 | foreach (qw(OPpSORT_INPLACE OPpSORT_DESCEND OPpITER_REVERSED OPpCONST_NOVER |
| 33 | OPpPAD_STATE PMf_SKIPWHITE RXf_SKIPWHITE |
| 34 | RXf_PMf_CHARSET RXf_PMf_KEEPCOPY |
| 35 | CVf_LOCKED OPpREVERSE_INPLACE OPpSUBSTR_REPL_FIRST |
| 36 | PMf_NONDESTRUCT OPpCONST_ARYBASE OPpEVAL_BYTES)) { |
| 37 | eval { import B $_ }; |
| 38 | no strict 'refs'; |
| 39 | *{$_} = sub () {0} unless *{$_}{CODE}; |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | # Changes between 0.50 and 0.51: |
| 44 | # - fixed nulled leave with live enter in sort { } |
| 45 | # - fixed reference constants (\"str") |
| 46 | # - handle empty programs gracefully |
| 47 | # - handle infinite loops (for (;;) {}, while (1) {}) |
| 48 | # - differentiate between 'for my $x ...' and 'my $x; for $x ...' |
| 49 | # - various minor cleanups |
| 50 | # - moved globals into an object |
| 51 | # - added '-u', like B::C |
| 52 | # - package declarations using cop_stash |
| 53 | # - subs, formats and code sorted by cop_seq |
| 54 | # Changes between 0.51 and 0.52: |
| 55 | # - added pp_threadsv (special variables under USE_5005THREADS) |
| 56 | # - added documentation |
| 57 | # Changes between 0.52 and 0.53: |
| 58 | # - many changes adding precedence contexts and associativity |
| 59 | # - added '-p' and '-s' output style options |
| 60 | # - various other minor fixes |
| 61 | # Changes between 0.53 and 0.54: |
| 62 | # - added support for new 'for (1..100)' optimization, |
| 63 | # thanks to Gisle Aas |
| 64 | # Changes between 0.54 and 0.55: |
| 65 | # - added support for new qr// construct |
| 66 | # - added support for new pp_regcreset OP |
| 67 | # Changes between 0.55 and 0.56: |
| 68 | # - tested on base/*.t, cmd/*.t, comp/*.t, io/*.t |
| 69 | # - fixed $# on non-lexicals broken in last big rewrite |
| 70 | # - added temporary fix for change in opcode of OP_STRINGIFY |
| 71 | # - fixed problem in 0.54's for() patch in 'for (@ary)' |
| 72 | # - fixed precedence in conditional of ?: |
| 73 | # - tweaked list paren elimination in 'my($x) = @_' |
| 74 | # - made continue-block detection trickier wrt. null ops |
| 75 | # - fixed various prototype problems in pp_entersub |
| 76 | # - added support for sub prototypes that never get GVs |
| 77 | # - added unquoting for special filehandle first arg in truncate |
| 78 | # - print doubled rv2gv (a bug) as '*{*GV}' instead of illegal '**GV' |
| 79 | # - added semicolons at the ends of blocks |
| 80 | # - added -l '#line' declaration option -- fixes cmd/subval.t 27,28 |
| 81 | # Changes between 0.56 and 0.561: |
| 82 | # - fixed multiply-declared my var in pp_truncate (thanks to Sarathy) |
| 83 | # - used new B.pm symbolic constants (done by Nick Ing-Simmons) |
| 84 | # Changes between 0.561 and 0.57: |
| 85 | # - stylistic changes to symbolic constant stuff |
| 86 | # - handled scope in s///e replacement code |
| 87 | # - added unquote option for expanding "" into concats, etc. |
| 88 | # - split method and proto parts of pp_entersub into separate functions |
| 89 | # - various minor cleanups |
| 90 | # Changes after 0.57: |
| 91 | # - added parens in \&foo (patch by Albert Dvornik) |
| 92 | # Changes between 0.57 and 0.58: |
| 93 | # - fixed '0' statements that weren't being printed |
| 94 | # - added methods for use from other programs |
| 95 | # (based on patches from James Duncan and Hugo van der Sanden) |
| 96 | # - added -si and -sT to control indenting (also based on a patch from Hugo) |
| 97 | # - added -sv to print something else instead of '???' |
| 98 | # - preliminary version of utf8 tr/// handling |
| 99 | # Changes after 0.58: |
| 100 | # - uses of $op->ppaddr changed to new $op->name (done by Sarathy) |
| 101 | # - added support for Hugo's new OP_SETSTATE (like nextstate) |
| 102 | # Changes between 0.58 and 0.59 |
| 103 | # - added support for Chip's OP_METHOD_NAMED |
| 104 | # - added support for Ilya's OPpTARGET_MY optimization |
| 105 | # - elided arrows before '()' subscripts when possible |
| 106 | # Changes between 0.59 and 0.60 |
| 107 | # - support for method attributes was added |
| 108 | # - some warnings fixed |
| 109 | # - separate recognition of constant subs |
| 110 | # - rewrote continue block handling, now recognizing for loops |
| 111 | # - added more control of expanding control structures |
| 112 | # Changes between 0.60 and 0.61 (mostly by Robin Houston) |
| 113 | # - many bug-fixes |
| 114 | # - support for pragmas and 'use' |
| 115 | # - support for the little-used $[ variable |
| 116 | # - support for __DATA__ sections |
| 117 | # - UTF8 support |
| 118 | # - BEGIN, CHECK, INIT and END blocks |
| 119 | # - scoping of subroutine declarations fixed |
| 120 | # - compile-time output from the input program can be suppressed, so that the |
| 121 | # output is just the deparsed code. (a change to O.pm in fact) |
| 122 | # - our() declarations |
| 123 | # - *all* the known bugs are now listed in the BUGS section |
| 124 | # - comprehensive test mechanism (TEST -deparse) |
| 125 | # Changes between 0.62 and 0.63 (mostly by Rafael Garcia-Suarez) |
| 126 | # - bug-fixes |
| 127 | # - new switch -P |
| 128 | # - support for command-line switches (-l, -0, etc.) |
| 129 | # Changes between 0.63 and 0.64 |
| 130 | # - support for //, CHECK blocks, and assertions |
| 131 | # - improved handling of foreach loops and lexicals |
| 132 | # - option to use Data::Dumper for constants |
| 133 | # - more bug fixes |
| 134 | # - discovered lots more bugs not yet fixed |
| 135 | # |
| 136 | # ... |
| 137 | # |
| 138 | # Changes between 0.72 and 0.73 |
| 139 | # - support new switch constructs |
| 140 | |
| 141 | # Todo: |
| 142 | # (See also BUGS section at the end of this file) |
| 143 | # |
| 144 | # - finish tr/// changes |
| 145 | # - add option for even more parens (generalize \&foo change) |
| 146 | # - left/right context |
| 147 | # - copy comments (look at real text with $^P?) |
| 148 | # - avoid semis in one-statement blocks |
| 149 | # - associativity of &&=, ||=, ?: |
| 150 | # - ',' => '=>' (auto-unquote?) |
| 151 | # - break long lines ("\r" as discretionary break?) |
| 152 | # - configurable syntax highlighting: ANSI color, HTML, TeX, etc. |
| 153 | # - more style options: brace style, hex vs. octal, quotes, ... |
| 154 | # - print big ints as hex/octal instead of decimal (heuristic?) |
| 155 | # - handle 'my $x if 0'? |
| 156 | # - version using op_next instead of op_first/sibling? |
| 157 | # - avoid string copies (pass arrays, one big join?) |
| 158 | # - here-docs? |
| 159 | |
| 160 | # Current test.deparse failures |
| 161 | # comp/hints 6 - location of BEGIN blocks wrt. block openings |
| 162 | # run/switchI 1 - missing -I switches entirely |
| 163 | # perl -Ifoo -e 'print @INC' |
| 164 | # op/caller 2 - warning mask propagates backwards before warnings::register |
| 165 | # 'use warnings; BEGIN {${^WARNING_BITS} eq "U"x12;} use warnings::register' |
| 166 | # op/getpid 2 - can't assign to shared my() declaration (threads only) |
| 167 | # 'my $x : shared = 5' |
| 168 | # op/override 7 - parens on overridden require change v-string interpretation |
| 169 | # 'BEGIN{*CORE::GLOBAL::require=sub {}} require v5.6' |
| 170 | # c.f. 'BEGIN { *f = sub {0} }; f 2' |
| 171 | # op/pat 774 - losing Unicode-ness of Latin1-only strings |
| 172 | # 'use charnames ":short"; $x="\N{latin:a with acute}"' |
| 173 | # op/recurse 12 - missing parens on recursive call makes it look like method |
| 174 | # 'sub f { f($x) }' |
| 175 | # op/subst 90 - inconsistent handling of utf8 under "use utf8" |
| 176 | # op/taint 29 - "use re 'taint'" deparsed in the wrong place wrt. block open |
| 177 | # op/tiehandle compile - "use strict" deparsed in the wrong place |
| 178 | # uni/tr_ several |
| 179 | # ext/B/t/xref 11 - line numbers when we add newlines to one-line subs |
| 180 | # ext/Data/Dumper/t/dumper compile |
| 181 | # ext/DB_file/several |
| 182 | # ext/Encode/several |
| 183 | # ext/Ernno/Errno warnings |
| 184 | # ext/IO/lib/IO/t/io_sel 23 |
| 185 | # ext/PerlIO/t/encoding compile |
| 186 | # ext/POSIX/t/posix 6 |
| 187 | # ext/Socket/Socket 8 |
| 188 | # ext/Storable/t/croak compile |
| 189 | # lib/Attribute/Handlers/t/multi compile |
| 190 | # lib/bignum/ several |
| 191 | # lib/charnames 35 |
| 192 | # lib/constant 32 |
| 193 | # lib/English 40 |
| 194 | # lib/ExtUtils/t/bytes 4 |
| 195 | # lib/File/DosGlob compile |
| 196 | # lib/Filter/Simple/t/data 1 |
| 197 | # lib/Math/BigInt/t/constant 1 |
| 198 | # lib/Net/t/config Deparse-warning |
| 199 | # lib/overload compile |
| 200 | # lib/Switch/ several |
| 201 | # lib/Symbol 4 |
| 202 | # lib/Test/Simple several |
| 203 | # lib/Term/Complete |
| 204 | # lib/Tie/File/t/29_downcopy 5 |
| 205 | # lib/vars 22 |
| 206 | |
| 207 | # Object fields (were globals): |
| 208 | # |
| 209 | # avoid_local: |
| 210 | # (local($a), local($b)) and local($a, $b) have the same internal |
| 211 | # representation but the short form looks better. We notice we can |
| 212 | # use a large-scale local when checking the list, but need to prevent |
| 213 | # individual locals too. This hash holds the addresses of OPs that |
| 214 | # have already had their local-ness accounted for. The same thing |
| 215 | # is done with my(). |
| 216 | # |
| 217 | # curcv: |
| 218 | # CV for current sub (or main program) being deparsed |
| 219 | # |
| 220 | # curcvlex: |
| 221 | # Cached hash of lexical variables for curcv: keys are names, |
| 222 | # each value is an array of pairs, indicating the cop_seq of scopes |
| 223 | # in which a var of that name is valid. |
| 224 | # |
| 225 | # curcop: |
| 226 | # COP for statement being deparsed |
| 227 | # |
| 228 | # curstash: |
| 229 | # name of the current package for deparsed code |
| 230 | # |
| 231 | # subs_todo: |
| 232 | # array of [cop_seq, CV, is_format?] for subs and formats we still |
| 233 | # want to deparse |
| 234 | # |
| 235 | # protos_todo: |
| 236 | # as above, but [name, prototype] for subs that never got a GV |
| 237 | # |
| 238 | # subs_done, forms_done: |
| 239 | # keys are addresses of GVs for subs and formats we've already |
| 240 | # deparsed (or at least put into subs_todo) |
| 241 | # |
| 242 | # subs_declared |
| 243 | # keys are names of subs for which we've printed declarations. |
| 244 | # That means we can omit parentheses from the arguments. It also means we |
| 245 | # need to put CORE:: on core functions of the same name. |
| 246 | # |
| 247 | # subs_deparsed |
| 248 | # Keeps track of fully qualified names of all deparsed subs. |
| 249 | # |
| 250 | # parens: -p |
| 251 | # linenums: -l |
| 252 | # unquote: -q |
| 253 | # cuddle: ' ' or '\n', depending on -sC |
| 254 | # indent_size: -si |
| 255 | # use_tabs: -sT |
| 256 | # ex_const: -sv |
| 257 | |
| 258 | # A little explanation of how precedence contexts and associativity |
| 259 | # work: |
| 260 | # |
| 261 | # deparse() calls each per-op subroutine with an argument $cx (short |
| 262 | # for context, but not the same as the cx* in the perl core), which is |
| 263 | # a number describing the op's parents in terms of precedence, whether |
| 264 | # they're inside an expression or at statement level, etc. (see |
| 265 | # chart below). When ops with children call deparse on them, they pass |
| 266 | # along their precedence. Fractional values are used to implement |
| 267 | # associativity ('($x + $y) + $z' => '$x + $y + $y') and related |
| 268 | # parentheses hacks. The major disadvantage of this scheme is that |
| 269 | # it doesn't know about right sides and left sides, so say if you |
| 270 | # assign a listop to a variable, it can't tell it's allowed to leave |
| 271 | # the parens off the listop. |
| 272 | |
| 273 | # Precedences: |
| 274 | # 26 [TODO] inside interpolation context ("") |
| 275 | # 25 left terms and list operators (leftward) |
| 276 | # 24 left -> |
| 277 | # 23 nonassoc ++ -- |
| 278 | # 22 right ** |
| 279 | # 21 right ! ~ \ and unary + and - |
| 280 | # 20 left =~ !~ |
| 281 | # 19 left * / % x |
| 282 | # 18 left + - . |
| 283 | # 17 left << >> |
| 284 | # 16 nonassoc named unary operators |
| 285 | # 15 nonassoc < > <= >= lt gt le ge |
| 286 | # 14 nonassoc == != <=> eq ne cmp |
| 287 | # 13 left & |
| 288 | # 12 left | ^ |
| 289 | # 11 left && |
| 290 | # 10 left || |
| 291 | # 9 nonassoc .. ... |
| 292 | # 8 right ?: |
| 293 | # 7 right = += -= *= etc. |
| 294 | # 6 left , => |
| 295 | # 5 nonassoc list operators (rightward) |
| 296 | # 4 right not |
| 297 | # 3 left and |
| 298 | # 2 left or xor |
| 299 | # 1 statement modifiers |
| 300 | # 0.5 statements, but still print scopes as do { ... } |
| 301 | # 0 statement level |
| 302 | |
| 303 | # Nonprinting characters with special meaning: |
| 304 | # \cS - steal parens (see maybe_parens_unop) |
| 305 | # \n - newline and indent |
| 306 | # \t - increase indent |
| 307 | # \b - decrease indent ('outdent') |
| 308 | # \f - flush left (no indent) |
| 309 | # \cK - kill following semicolon, if any |
| 310 | |
| 311 | sub null { |
| 312 | my $op = shift; |
| 313 | return class($op) eq "NULL"; |
| 314 | } |
| 315 | |
| 316 | sub todo { |
| 317 | my $self = shift; |
| 318 | my($cv, $is_form) = @_; |
| 319 | return unless ($cv->FILE eq $0 || exists $self->{files}{$cv->FILE}); |
| 320 | my $seq; |
| 321 | if ($cv->OUTSIDE_SEQ) { |
| 322 | $seq = $cv->OUTSIDE_SEQ; |
| 323 | } elsif (!null($cv->START) and is_state($cv->START)) { |
| 324 | $seq = $cv->START->cop_seq; |
| 325 | } else { |
| 326 | $seq = 0; |
| 327 | } |
| 328 | push @{$self->{'subs_todo'}}, [$seq, $cv, $is_form]; |
| 329 | unless ($is_form || class($cv->STASH) eq 'SPECIAL') { |
| 330 | $self->{'subs_deparsed'}{$cv->STASH->NAME."::".$cv->GV->NAME} = 1; |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | sub next_todo { |
| 335 | my $self = shift; |
| 336 | my $ent = shift @{$self->{'subs_todo'}}; |
| 337 | my $cv = $ent->[1]; |
| 338 | my $gv = $cv->GV; |
| 339 | my $name = $self->gv_name($gv); |
| 340 | if ($ent->[2]) { |
| 341 | return "format $name =\n" |
| 342 | . $self->deparse_format($ent->[1]). "\n"; |
| 343 | } else { |
| 344 | $self->{'subs_declared'}{$name} = 1; |
| 345 | if ($name eq "BEGIN") { |
| 346 | my $use_dec = $self->begin_is_use($cv); |
| 347 | if (defined ($use_dec) and $self->{'expand'} < 5) { |
| 348 | return () if 0 == length($use_dec); |
| 349 | return $use_dec; |
| 350 | } |
| 351 | } |
| 352 | my $l = ''; |
| 353 | if ($self->{'linenums'}) { |
| 354 | my $line = $gv->LINE; |
| 355 | my $file = $gv->FILE; |
| 356 | $l = "\n\f#line $line \"$file\"\n"; |
| 357 | } |
| 358 | my $p = ''; |
| 359 | if (class($cv->STASH) ne "SPECIAL") { |
| 360 | my $stash = $cv->STASH->NAME; |
| 361 | if ($stash ne $self->{'curstash'}) { |
| 362 | $p = "package $stash;\n"; |
| 363 | $name = "$self->{'curstash'}::$name" unless $name =~ /::/; |
| 364 | $self->{'curstash'} = $stash; |
| 365 | } |
| 366 | $name =~ s/^\Q$stash\E::(?!\z|.*::)//; |
| 367 | } |
| 368 | return "${p}${l}sub $name " . $self->deparse_sub($cv); |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | # Return a "use" declaration for this BEGIN block, if appropriate |
| 373 | sub begin_is_use { |
| 374 | my ($self, $cv) = @_; |
| 375 | my $root = $cv->ROOT; |
| 376 | local @$self{qw'curcv curcvlex'} = ($cv); |
| 377 | #require B::Debug; |
| 378 | #B::walkoptree($cv->ROOT, "debug"); |
| 379 | my $lineseq = $root->first; |
| 380 | return if $lineseq->name ne "lineseq"; |
| 381 | |
| 382 | my $req_op = $lineseq->first->sibling; |
| 383 | return if $req_op->name ne "require"; |
| 384 | |
| 385 | my $module; |
| 386 | if ($req_op->first->private & OPpCONST_BARE) { |
| 387 | # Actually it should always be a bareword |
| 388 | $module = $self->const_sv($req_op->first)->PV; |
| 389 | $module =~ s[/][::]g; |
| 390 | $module =~ s/.pm$//; |
| 391 | } |
| 392 | else { |
| 393 | $module = $self->const($self->const_sv($req_op->first), 6); |
| 394 | } |
| 395 | |
| 396 | my $version; |
| 397 | my $version_op = $req_op->sibling; |
| 398 | return if class($version_op) eq "NULL"; |
| 399 | if ($version_op->name eq "lineseq") { |
| 400 | # We have a version parameter; skip nextstate & pushmark |
| 401 | my $constop = $version_op->first->next->next; |
| 402 | |
| 403 | return unless $self->const_sv($constop)->PV eq $module; |
| 404 | $constop = $constop->sibling; |
| 405 | $version = $self->const_sv($constop); |
| 406 | if (class($version) eq "IV") { |
| 407 | $version = $version->int_value; |
| 408 | } elsif (class($version) eq "NV") { |
| 409 | $version = $version->NV; |
| 410 | } elsif (class($version) ne "PVMG") { |
| 411 | # Includes PVIV and PVNV |
| 412 | $version = $version->PV; |
| 413 | } else { |
| 414 | # version specified as a v-string |
| 415 | $version = 'v'.join '.', map ord, split //, $version->PV; |
| 416 | } |
| 417 | $constop = $constop->sibling; |
| 418 | return if $constop->name ne "method_named"; |
| 419 | return if $self->const_sv($constop)->PV ne "VERSION"; |
| 420 | } |
| 421 | |
| 422 | $lineseq = $version_op->sibling; |
| 423 | return if $lineseq->name ne "lineseq"; |
| 424 | my $entersub = $lineseq->first->sibling; |
| 425 | if ($entersub->name eq "stub") { |
| 426 | return "use $module $version ();\n" if defined $version; |
| 427 | return "use $module ();\n"; |
| 428 | } |
| 429 | return if $entersub->name ne "entersub"; |
| 430 | |
| 431 | # See if there are import arguments |
| 432 | my $args = ''; |
| 433 | |
| 434 | my $svop = $entersub->first->sibling; # Skip over pushmark |
| 435 | return unless $self->const_sv($svop)->PV eq $module; |
| 436 | |
| 437 | # Pull out the arguments |
| 438 | for ($svop=$svop->sibling; $svop->name ne "method_named"; |
| 439 | $svop = $svop->sibling) { |
| 440 | $args .= ", " if length($args); |
| 441 | $args .= $self->deparse($svop, 6); |
| 442 | } |
| 443 | |
| 444 | my $use = 'use'; |
| 445 | my $method_named = $svop; |
| 446 | return if $method_named->name ne "method_named"; |
| 447 | my $method_name = $self->const_sv($method_named)->PV; |
| 448 | |
| 449 | if ($method_name eq "unimport") { |
| 450 | $use = 'no'; |
| 451 | } |
| 452 | |
| 453 | # Certain pragmas are dealt with using hint bits, |
| 454 | # so we ignore them here |
| 455 | if ($module eq 'strict' || $module eq 'integer' |
| 456 | || $module eq 'bytes' || $module eq 'warnings' |
| 457 | || $module eq 'feature') { |
| 458 | return ""; |
| 459 | } |
| 460 | |
| 461 | if (defined $version && length $args) { |
| 462 | return "$use $module $version ($args);\n"; |
| 463 | } elsif (defined $version) { |
| 464 | return "$use $module $version;\n"; |
| 465 | } elsif (length $args) { |
| 466 | return "$use $module ($args);\n"; |
| 467 | } else { |
| 468 | return "$use $module;\n"; |
| 469 | } |
| 470 | } |
| 471 | |
| 472 | sub stash_subs { |
| 473 | my ($self, $pack, $seen) = @_; |
| 474 | my (@ret, $stash); |
| 475 | if (!defined $pack) { |
| 476 | $pack = ''; |
| 477 | $stash = \%::; |
| 478 | } |
| 479 | else { |
| 480 | $pack =~ s/(::)?$/::/; |
| 481 | no strict 'refs'; |
| 482 | $stash = \%{"main::$pack"}; |
| 483 | } |
| 484 | return |
| 485 | if ($seen ||= {})->{ |
| 486 | $INC{"overload.pm"} ? overload::StrVal($stash) : $stash |
| 487 | }++; |
| 488 | my %stash = svref_2object($stash)->ARRAY; |
| 489 | while (my ($key, $val) = each %stash) { |
| 490 | my $class = class($val); |
| 491 | if ($class eq "PV") { |
| 492 | # Just a prototype. As an ugly but fairly effective way |
| 493 | # to find out if it belongs here is to see if the AUTOLOAD |
| 494 | # (if any) for the stash was defined in one of our files. |
| 495 | my $A = $stash{"AUTOLOAD"}; |
| 496 | if (defined ($A) && class($A) eq "GV" && defined($A->CV) |
| 497 | && class($A->CV) eq "CV") { |
| 498 | my $AF = $A->FILE; |
| 499 | next unless $AF eq $0 || exists $self->{'files'}{$AF}; |
| 500 | } |
| 501 | push @{$self->{'protos_todo'}}, [$pack . $key, $val->PV]; |
| 502 | } elsif ($class eq "IV" && !($val->FLAGS & SVf_ROK)) { |
| 503 | # Just a name. As above. |
| 504 | # But skip proxy constant subroutines, as some form of perl-space |
| 505 | # visible code must have created them, be it a use statement, or |
| 506 | # some direct symbol-table manipulation code that we will Deparse |
| 507 | my $A = $stash{"AUTOLOAD"}; |
| 508 | if (defined ($A) && class($A) eq "GV" && defined($A->CV) |
| 509 | && class($A->CV) eq "CV") { |
| 510 | my $AF = $A->FILE; |
| 511 | next unless $AF eq $0 || exists $self->{'files'}{$AF}; |
| 512 | } |
| 513 | push @{$self->{'protos_todo'}}, [$pack . $key, undef]; |
| 514 | } elsif ($class eq "GV") { |
| 515 | if (class(my $cv = $val->CV) ne "SPECIAL") { |
| 516 | next if $self->{'subs_done'}{$$val}++; |
| 517 | next if $$val != ${$cv->GV}; # Ignore imposters |
| 518 | $self->todo($cv, 0); |
| 519 | } |
| 520 | if (class(my $cv = $val->FORM) ne "SPECIAL") { |
| 521 | next if $self->{'forms_done'}{$$val}++; |
| 522 | next if $$val != ${$cv->GV}; # Ignore imposters |
| 523 | $self->todo($cv, 1); |
| 524 | } |
| 525 | if (class($val->HV) ne "SPECIAL" && $key =~ /::$/) { |
| 526 | $self->stash_subs($pack . $key, $seen); |
| 527 | } |
| 528 | } |
| 529 | } |
| 530 | } |
| 531 | |
| 532 | sub print_protos { |
| 533 | my $self = shift; |
| 534 | my $ar; |
| 535 | my @ret; |
| 536 | foreach $ar (@{$self->{'protos_todo'}}) { |
| 537 | my $proto = (defined $ar->[1] ? " (". $ar->[1] . ")" : ""); |
| 538 | push @ret, "sub " . $ar->[0] . "$proto;\n"; |
| 539 | } |
| 540 | delete $self->{'protos_todo'}; |
| 541 | return @ret; |
| 542 | } |
| 543 | |
| 544 | sub style_opts { |
| 545 | my $self = shift; |
| 546 | my $opts = shift; |
| 547 | my $opt; |
| 548 | while (length($opt = substr($opts, 0, 1))) { |
| 549 | if ($opt eq "C") { |
| 550 | $self->{'cuddle'} = " "; |
| 551 | $opts = substr($opts, 1); |
| 552 | } elsif ($opt eq "i") { |
| 553 | $opts =~ s/^i(\d+)//; |
| 554 | $self->{'indent_size'} = $1; |
| 555 | } elsif ($opt eq "T") { |
| 556 | $self->{'use_tabs'} = 1; |
| 557 | $opts = substr($opts, 1); |
| 558 | } elsif ($opt eq "v") { |
| 559 | $opts =~ s/^v([^.]*)(.|$)//; |
| 560 | $self->{'ex_const'} = $1; |
| 561 | } |
| 562 | } |
| 563 | } |
| 564 | |
| 565 | sub new { |
| 566 | my $class = shift; |
| 567 | my $self = bless {}, $class; |
| 568 | $self->{'cuddle'} = "\n"; |
| 569 | $self->{'curcop'} = undef; |
| 570 | $self->{'curstash'} = "main"; |
| 571 | $self->{'ex_const'} = "'???'"; |
| 572 | $self->{'expand'} = 0; |
| 573 | $self->{'files'} = {}; |
| 574 | $self->{'indent_size'} = 4; |
| 575 | $self->{'linenums'} = 0; |
| 576 | $self->{'parens'} = 0; |
| 577 | $self->{'subs_todo'} = []; |
| 578 | $self->{'unquote'} = 0; |
| 579 | $self->{'use_dumper'} = 0; |
| 580 | $self->{'use_tabs'} = 0; |
| 581 | |
| 582 | $self->{'ambient_arybase'} = 0; |
| 583 | $self->{'ambient_warnings'} = undef; # Assume no lexical warnings |
| 584 | $self->{'ambient_hints'} = 0; |
| 585 | $self->{'ambient_hinthash'} = undef; |
| 586 | $self->init(); |
| 587 | |
| 588 | while (my $arg = shift @_) { |
| 589 | if ($arg eq "-d") { |
| 590 | $self->{'use_dumper'} = 1; |
| 591 | require Data::Dumper; |
| 592 | } elsif ($arg =~ /^-f(.*)/) { |
| 593 | $self->{'files'}{$1} = 1; |
| 594 | } elsif ($arg eq "-l") { |
| 595 | $self->{'linenums'} = 1; |
| 596 | } elsif ($arg eq "-p") { |
| 597 | $self->{'parens'} = 1; |
| 598 | } elsif ($arg eq "-P") { |
| 599 | $self->{'noproto'} = 1; |
| 600 | } elsif ($arg eq "-q") { |
| 601 | $self->{'unquote'} = 1; |
| 602 | } elsif (substr($arg, 0, 2) eq "-s") { |
| 603 | $self->style_opts(substr $arg, 2); |
| 604 | } elsif ($arg =~ /^-x(\d)$/) { |
| 605 | $self->{'expand'} = $1; |
| 606 | } |
| 607 | } |
| 608 | return $self; |
| 609 | } |
| 610 | |
| 611 | { |
| 612 | # Mask out the bits that L<warnings::register> uses |
| 613 | my $WARN_MASK; |
| 614 | BEGIN { |
| 615 | $WARN_MASK = $warnings::Bits{all} | $warnings::DeadBits{all}; |
| 616 | } |
| 617 | sub WARN_MASK () { |
| 618 | return $WARN_MASK; |
| 619 | } |
| 620 | } |
| 621 | |
| 622 | # Initialise the contextual information, either from |
| 623 | # defaults provided with the ambient_pragmas method, |
| 624 | # or from perl's own defaults otherwise. |
| 625 | sub init { |
| 626 | my $self = shift; |
| 627 | |
| 628 | $self->{'arybase'} = $self->{'ambient_arybase'}; |
| 629 | $self->{'warnings'} = defined ($self->{'ambient_warnings'}) |
| 630 | ? $self->{'ambient_warnings'} & WARN_MASK |
| 631 | : undef; |
| 632 | $self->{'hints'} = $self->{'ambient_hints'}; |
| 633 | $self->{'hints'} &= 0xFF if $] < 5.009; |
| 634 | $self->{'hinthash'} = $self->{'ambient_hinthash'}; |
| 635 | |
| 636 | # also a convenient place to clear out subs_declared |
| 637 | delete $self->{'subs_declared'}; |
| 638 | } |
| 639 | |
| 640 | sub compile { |
| 641 | my(@args) = @_; |
| 642 | return sub { |
| 643 | my $self = B::Deparse->new(@args); |
| 644 | # First deparse command-line args |
| 645 | if (defined $^I) { # deparse -i |
| 646 | print q(BEGIN { $^I = ).perlstring($^I).qq(; }\n); |
| 647 | } |
| 648 | if ($^W) { # deparse -w |
| 649 | print qq(BEGIN { \$^W = $^W; }\n); |
| 650 | } |
| 651 | if ($/ ne "\n" or defined $O::savebackslash) { # deparse -l and -0 |
| 652 | my $fs = perlstring($/) || 'undef'; |
| 653 | my $bs = perlstring($O::savebackslash) || 'undef'; |
| 654 | print qq(BEGIN { \$/ = $fs; \$\\ = $bs; }\n); |
| 655 | } |
| 656 | my @BEGINs = B::begin_av->isa("B::AV") ? B::begin_av->ARRAY : (); |
| 657 | my @UNITCHECKs = B::unitcheck_av->isa("B::AV") |
| 658 | ? B::unitcheck_av->ARRAY |
| 659 | : (); |
| 660 | my @CHECKs = B::check_av->isa("B::AV") ? B::check_av->ARRAY : (); |
| 661 | my @INITs = B::init_av->isa("B::AV") ? B::init_av->ARRAY : (); |
| 662 | my @ENDs = B::end_av->isa("B::AV") ? B::end_av->ARRAY : (); |
| 663 | for my $block (@BEGINs, @UNITCHECKs, @CHECKs, @INITs, @ENDs) { |
| 664 | $self->todo($block, 0); |
| 665 | } |
| 666 | $self->stash_subs(); |
| 667 | local($SIG{"__DIE__"}) = |
| 668 | sub { |
| 669 | if ($self->{'curcop'}) { |
| 670 | my $cop = $self->{'curcop'}; |
| 671 | my($line, $file) = ($cop->line, $cop->file); |
| 672 | print STDERR "While deparsing $file near line $line,\n"; |
| 673 | } |
| 674 | }; |
| 675 | $self->{'curcv'} = main_cv; |
| 676 | $self->{'curcvlex'} = undef; |
| 677 | print $self->print_protos; |
| 678 | @{$self->{'subs_todo'}} = |
| 679 | sort {$a->[0] <=> $b->[0]} @{$self->{'subs_todo'}}; |
| 680 | print $self->indent($self->deparse_root(main_root)), "\n" |
| 681 | unless null main_root; |
| 682 | my @text; |
| 683 | while (scalar(@{$self->{'subs_todo'}})) { |
| 684 | push @text, $self->next_todo; |
| 685 | } |
| 686 | print $self->indent(join("", @text)), "\n" if @text; |
| 687 | |
| 688 | # Print __DATA__ section, if necessary |
| 689 | no strict 'refs'; |
| 690 | my $laststash = defined $self->{'curcop'} |
| 691 | ? $self->{'curcop'}->stash->NAME : $self->{'curstash'}; |
| 692 | if (defined *{$laststash."::DATA"}{IO}) { |
| 693 | print "package $laststash;\n" |
| 694 | unless $laststash eq $self->{'curstash'}; |
| 695 | print "__DATA__\n"; |
| 696 | print readline(*{$laststash."::DATA"}); |
| 697 | } |
| 698 | } |
| 699 | } |
| 700 | |
| 701 | sub coderef2text { |
| 702 | my $self = shift; |
| 703 | my $sub = shift; |
| 704 | croak "Usage: ->coderef2text(CODEREF)" unless UNIVERSAL::isa($sub, "CODE"); |
| 705 | |
| 706 | $self->init(); |
| 707 | return $self->indent($self->deparse_sub(svref_2object($sub))); |
| 708 | } |
| 709 | |
| 710 | sub ambient_pragmas { |
| 711 | my $self = shift; |
| 712 | my ($arybase, $hint_bits, $warning_bits, $hinthash) = (0, 0); |
| 713 | |
| 714 | while (@_ > 1) { |
| 715 | my $name = shift(); |
| 716 | my $val = shift(); |
| 717 | |
| 718 | if ($name eq 'strict') { |
| 719 | require strict; |
| 720 | |
| 721 | if ($val eq 'none') { |
| 722 | $hint_bits &= ~strict::bits(qw/refs subs vars/); |
| 723 | next(); |
| 724 | } |
| 725 | |
| 726 | my @names; |
| 727 | if ($val eq "all") { |
| 728 | @names = qw/refs subs vars/; |
| 729 | } |
| 730 | elsif (ref $val) { |
| 731 | @names = @$val; |
| 732 | } |
| 733 | else { |
| 734 | @names = split' ', $val; |
| 735 | } |
| 736 | $hint_bits |= strict::bits(@names); |
| 737 | } |
| 738 | |
| 739 | elsif ($name eq '$[') { |
| 740 | if (OPpCONST_ARYBASE) { |
| 741 | $arybase = $val; |
| 742 | } else { |
| 743 | croak "\$[ can't be non-zero on this perl" unless $val == 0; |
| 744 | } |
| 745 | } |
| 746 | |
| 747 | elsif ($name eq 'integer' |
| 748 | || $name eq 'bytes' |
| 749 | || $name eq 'utf8') { |
| 750 | require "$name.pm"; |
| 751 | if ($val) { |
| 752 | $hint_bits |= ${$::{"${name}::"}{"hint_bits"}}; |
| 753 | } |
| 754 | else { |
| 755 | $hint_bits &= ~${$::{"${name}::"}{"hint_bits"}}; |
| 756 | } |
| 757 | } |
| 758 | |
| 759 | elsif ($name eq 're') { |
| 760 | require re; |
| 761 | if ($val eq 'none') { |
| 762 | $hint_bits &= ~re::bits(qw/taint eval/); |
| 763 | next(); |
| 764 | } |
| 765 | |
| 766 | my @names; |
| 767 | if ($val eq 'all') { |
| 768 | @names = qw/taint eval/; |
| 769 | } |
| 770 | elsif (ref $val) { |
| 771 | @names = @$val; |
| 772 | } |
| 773 | else { |
| 774 | @names = split' ',$val; |
| 775 | } |
| 776 | $hint_bits |= re::bits(@names); |
| 777 | } |
| 778 | |
| 779 | elsif ($name eq 'warnings') { |
| 780 | if ($val eq 'none') { |
| 781 | $warning_bits = $warnings::NONE; |
| 782 | next(); |
| 783 | } |
| 784 | |
| 785 | my @names; |
| 786 | if (ref $val) { |
| 787 | @names = @$val; |
| 788 | } |
| 789 | else { |
| 790 | @names = split/\s+/, $val; |
| 791 | } |
| 792 | |
| 793 | $warning_bits = $warnings::NONE if !defined ($warning_bits); |
| 794 | $warning_bits |= warnings::bits(@names); |
| 795 | } |
| 796 | |
| 797 | elsif ($name eq 'warning_bits') { |
| 798 | $warning_bits = $val; |
| 799 | } |
| 800 | |
| 801 | elsif ($name eq 'hint_bits') { |
| 802 | $hint_bits = $val; |
| 803 | } |
| 804 | |
| 805 | elsif ($name eq '%^H') { |
| 806 | $hinthash = $val; |
| 807 | } |
| 808 | |
| 809 | else { |
| 810 | croak "Unknown pragma type: $name"; |
| 811 | } |
| 812 | } |
| 813 | if (@_) { |
| 814 | croak "The ambient_pragmas method expects an even number of args"; |
| 815 | } |
| 816 | |
| 817 | $self->{'ambient_arybase'} = $arybase; |
| 818 | $self->{'ambient_warnings'} = $warning_bits; |
| 819 | $self->{'ambient_hints'} = $hint_bits; |
| 820 | $self->{'ambient_hinthash'} = $hinthash; |
| 821 | } |
| 822 | |
| 823 | # This method is the inner loop, so try to keep it simple |
| 824 | sub deparse { |
| 825 | my $self = shift; |
| 826 | my($op, $cx) = @_; |
| 827 | |
| 828 | Carp::confess("Null op in deparse") if !defined($op) |
| 829 | || class($op) eq "NULL"; |
| 830 | my $meth = "pp_" . $op->name; |
| 831 | return $self->$meth($op, $cx); |
| 832 | } |
| 833 | |
| 834 | sub indent { |
| 835 | my $self = shift; |
| 836 | my $txt = shift; |
| 837 | my @lines = split(/\n/, $txt); |
| 838 | my $leader = ""; |
| 839 | my $level = 0; |
| 840 | my $line; |
| 841 | for $line (@lines) { |
| 842 | my $cmd = substr($line, 0, 1); |
| 843 | if ($cmd eq "\t" or $cmd eq "\b") { |
| 844 | $level += ($cmd eq "\t" ? 1 : -1) * $self->{'indent_size'}; |
| 845 | if ($self->{'use_tabs'}) { |
| 846 | $leader = "\t" x ($level / 8) . " " x ($level % 8); |
| 847 | } else { |
| 848 | $leader = " " x $level; |
| 849 | } |
| 850 | $line = substr($line, 1); |
| 851 | } |
| 852 | if (substr($line, 0, 1) eq "\f") { |
| 853 | $line = substr($line, 1); # no indent |
| 854 | } else { |
| 855 | $line = $leader . $line; |
| 856 | } |
| 857 | $line =~ s/\cK;?//g; |
| 858 | } |
| 859 | return join("\n", @lines); |
| 860 | } |
| 861 | |
| 862 | sub deparse_sub { |
| 863 | my $self = shift; |
| 864 | my $cv = shift; |
| 865 | my $proto = ""; |
| 866 | Carp::confess("NULL in deparse_sub") if !defined($cv) || $cv->isa("B::NULL"); |
| 867 | Carp::confess("SPECIAL in deparse_sub") if $cv->isa("B::SPECIAL"); |
| 868 | local $self->{'curcop'} = $self->{'curcop'}; |
| 869 | if ($cv->FLAGS & SVf_POK) { |
| 870 | $proto = "(". $cv->PV . ") "; |
| 871 | } |
| 872 | if ($cv->CvFLAGS & (CVf_METHOD|CVf_LOCKED|CVf_LVALUE)) { |
| 873 | $proto .= ": "; |
| 874 | $proto .= "lvalue " if $cv->CvFLAGS & CVf_LVALUE; |
| 875 | $proto .= "locked " if $cv->CvFLAGS & CVf_LOCKED; |
| 876 | $proto .= "method " if $cv->CvFLAGS & CVf_METHOD; |
| 877 | } |
| 878 | |
| 879 | local($self->{'curcv'}) = $cv; |
| 880 | local($self->{'curcvlex'}); |
| 881 | local(@$self{qw'curstash warnings hints hinthash'}) |
| 882 | = @$self{qw'curstash warnings hints hinthash'}; |
| 883 | my $body; |
| 884 | if (not null $cv->ROOT) { |
| 885 | my $lineseq = $cv->ROOT->first; |
| 886 | if ($lineseq->name eq "lineseq") { |
| 887 | my @ops; |
| 888 | for(my$o=$lineseq->first; $$o; $o=$o->sibling) { |
| 889 | push @ops, $o; |
| 890 | } |
| 891 | $body = $self->lineseq(undef, @ops).";"; |
| 892 | my $scope_en = $self->find_scope_en($lineseq); |
| 893 | if (defined $scope_en) { |
| 894 | my $subs = join"", $self->seq_subs($scope_en); |
| 895 | $body .= ";\n$subs" if length($subs); |
| 896 | } |
| 897 | } |
| 898 | else { |
| 899 | $body = $self->deparse($cv->ROOT->first, 0); |
| 900 | } |
| 901 | } |
| 902 | else { |
| 903 | my $sv = $cv->const_sv; |
| 904 | if ($$sv) { |
| 905 | # uh-oh. inlinable sub... format it differently |
| 906 | return $proto . "{ " . $self->const($sv, 0) . " }\n"; |
| 907 | } else { # XSUB? (or just a declaration) |
| 908 | return "$proto;\n"; |
| 909 | } |
| 910 | } |
| 911 | return $proto ."{\n\t$body\n\b}" ."\n"; |
| 912 | } |
| 913 | |
| 914 | sub deparse_format { |
| 915 | my $self = shift; |
| 916 | my $form = shift; |
| 917 | my @text; |
| 918 | local($self->{'curcv'}) = $form; |
| 919 | local($self->{'curcvlex'}); |
| 920 | local($self->{'in_format'}) = 1; |
| 921 | local(@$self{qw'curstash warnings hints hinthash'}) |
| 922 | = @$self{qw'curstash warnings hints hinthash'}; |
| 923 | my $op = $form->ROOT; |
| 924 | my $kid; |
| 925 | return "\f." if $op->first->name eq 'stub' |
| 926 | || $op->first->name eq 'nextstate'; |
| 927 | $op = $op->first->first; # skip leavewrite, lineseq |
| 928 | while (not null $op) { |
| 929 | $op = $op->sibling; # skip nextstate |
| 930 | my @exprs; |
| 931 | $kid = $op->first->sibling; # skip pushmark |
| 932 | push @text, "\f".$self->const_sv($kid)->PV; |
| 933 | $kid = $kid->sibling; |
| 934 | for (; not null $kid; $kid = $kid->sibling) { |
| 935 | push @exprs, $self->deparse($kid, 0); |
| 936 | } |
| 937 | push @text, "\f".join(", ", @exprs)."\n" if @exprs; |
| 938 | $op = $op->sibling; |
| 939 | } |
| 940 | return join("", @text) . "\f."; |
| 941 | } |
| 942 | |
| 943 | sub is_scope { |
| 944 | my $op = shift; |
| 945 | return $op->name eq "leave" || $op->name eq "scope" |
| 946 | || $op->name eq "lineseq" |
| 947 | || ($op->name eq "null" && class($op) eq "UNOP" |
| 948 | && (is_scope($op->first) || $op->first->name eq "enter")); |
| 949 | } |
| 950 | |
| 951 | sub is_state { |
| 952 | my $name = $_[0]->name; |
| 953 | return $name eq "nextstate" || $name eq "dbstate" || $name eq "setstate"; |
| 954 | } |
| 955 | |
| 956 | sub is_miniwhile { # check for one-line loop ('foo() while $y--') |
| 957 | my $op = shift; |
| 958 | return (!null($op) and null($op->sibling) |
| 959 | and $op->name eq "null" and class($op) eq "UNOP" |
| 960 | and (($op->first->name =~ /^(and|or)$/ |
| 961 | and $op->first->first->sibling->name eq "lineseq") |
| 962 | or ($op->first->name eq "lineseq" |
| 963 | and not null $op->first->first->sibling |
| 964 | and $op->first->first->sibling->name eq "unstack") |
| 965 | )); |
| 966 | } |
| 967 | |
| 968 | # Check if the op and its sibling are the initialization and the rest of a |
| 969 | # for (..;..;..) { ... } loop |
| 970 | sub is_for_loop { |
| 971 | my $op = shift; |
| 972 | # This OP might be almost anything, though it won't be a |
| 973 | # nextstate. (It's the initialization, so in the canonical case it |
| 974 | # will be an sassign.) The sibling is (old style) a lineseq whose |
| 975 | # first child is a nextstate and whose second is a leaveloop, or |
| 976 | # (new style) an unstack whose sibling is a leaveloop. |
| 977 | my $lseq = $op->sibling; |
| 978 | return 0 unless !is_state($op) and !null($lseq); |
| 979 | if ($lseq->name eq "lineseq") { |
| 980 | if ($lseq->first && !null($lseq->first) && is_state($lseq->first) |
| 981 | && (my $sib = $lseq->first->sibling)) { |
| 982 | return (!null($sib) && $sib->name eq "leaveloop"); |
| 983 | } |
| 984 | } elsif ($lseq->name eq "unstack" && ($lseq->flags & OPf_SPECIAL)) { |
| 985 | my $sib = $lseq->sibling; |
| 986 | return $sib && !null($sib) && $sib->name eq "leaveloop"; |
| 987 | } |
| 988 | return 0; |
| 989 | } |
| 990 | |
| 991 | sub is_scalar { |
| 992 | my $op = shift; |
| 993 | return ($op->name eq "rv2sv" or |
| 994 | $op->name eq "padsv" or |
| 995 | $op->name eq "gv" or # only in array/hash constructs |
| 996 | $op->flags & OPf_KIDS && !null($op->first) |
| 997 | && $op->first->name eq "gvsv"); |
| 998 | } |
| 999 | |
| 1000 | sub maybe_parens { |
| 1001 | my $self = shift; |
| 1002 | my($text, $cx, $prec) = @_; |
| 1003 | if ($prec < $cx # unary ops nest just fine |
| 1004 | or $prec == $cx and $cx != 4 and $cx != 16 and $cx != 21 |
| 1005 | or $self->{'parens'}) |
| 1006 | { |
| 1007 | $text = "($text)"; |
| 1008 | # In a unop, let parent reuse our parens; see maybe_parens_unop |
| 1009 | $text = "\cS" . $text if $cx == 16; |
| 1010 | return $text; |
| 1011 | } else { |
| 1012 | return $text; |
| 1013 | } |
| 1014 | } |
| 1015 | |
| 1016 | # same as above, but get around the 'if it looks like a function' rule |
| 1017 | sub maybe_parens_unop { |
| 1018 | my $self = shift; |
| 1019 | my($name, $kid, $cx) = @_; |
| 1020 | if ($cx > 16 or $self->{'parens'}) { |
| 1021 | $kid = $self->deparse($kid, 1); |
| 1022 | if ($name eq "umask" && $kid =~ /^\d+$/) { |
| 1023 | $kid = sprintf("%#o", $kid); |
| 1024 | } |
| 1025 | return $self->keyword($name) . "($kid)"; |
| 1026 | } else { |
| 1027 | $kid = $self->deparse($kid, 16); |
| 1028 | if ($name eq "umask" && $kid =~ /^\d+$/) { |
| 1029 | $kid = sprintf("%#o", $kid); |
| 1030 | } |
| 1031 | $name = $self->keyword($name); |
| 1032 | if (substr($kid, 0, 1) eq "\cS") { |
| 1033 | # use kid's parens |
| 1034 | return $name . substr($kid, 1); |
| 1035 | } elsif (substr($kid, 0, 1) eq "(") { |
| 1036 | # avoid looks-like-a-function trap with extra parens |
| 1037 | # ('+' can lead to ambiguities) |
| 1038 | return "$name(" . $kid . ")"; |
| 1039 | } else { |
| 1040 | return "$name $kid"; |
| 1041 | } |
| 1042 | } |
| 1043 | } |
| 1044 | |
| 1045 | sub maybe_parens_func { |
| 1046 | my $self = shift; |
| 1047 | my($func, $text, $cx, $prec) = @_; |
| 1048 | if ($prec <= $cx or substr($text, 0, 1) eq "(" or $self->{'parens'}) { |
| 1049 | return "$func($text)"; |
| 1050 | } else { |
| 1051 | return "$func $text"; |
| 1052 | } |
| 1053 | } |
| 1054 | |
| 1055 | sub maybe_local { |
| 1056 | my $self = shift; |
| 1057 | my($op, $cx, $text) = @_; |
| 1058 | my $our_intro = ($op->name =~ /^(gv|rv2)[ash]v$/) ? OPpOUR_INTRO : 0; |
| 1059 | if ($op->private & (OPpLVAL_INTRO|$our_intro) |
| 1060 | and not $self->{'avoid_local'}{$$op}) { |
| 1061 | my $our_local = ($op->private & OPpLVAL_INTRO) ? "local" : "our"; |
| 1062 | if( $our_local eq 'our' ) { |
| 1063 | if ( $text !~ /^\W(\w+::)*\w+\z/ |
| 1064 | and !utf8::decode($text) || $text !~ /^\W(\w+::)*\w+\z/ |
| 1065 | ) { |
| 1066 | die "Unexpected our($text)\n"; |
| 1067 | } |
| 1068 | $text =~ s/(\w+::)+//; |
| 1069 | } |
| 1070 | if (want_scalar($op)) { |
| 1071 | return "$our_local $text"; |
| 1072 | } else { |
| 1073 | return $self->maybe_parens_func("$our_local", $text, $cx, 16); |
| 1074 | } |
| 1075 | } else { |
| 1076 | return $text; |
| 1077 | } |
| 1078 | } |
| 1079 | |
| 1080 | sub maybe_targmy { |
| 1081 | my $self = shift; |
| 1082 | my($op, $cx, $func, @args) = @_; |
| 1083 | if ($op->private & OPpTARGET_MY) { |
| 1084 | my $var = $self->padname($op->targ); |
| 1085 | my $val = $func->($self, $op, 7, @args); |
| 1086 | return $self->maybe_parens("$var = $val", $cx, 7); |
| 1087 | } else { |
| 1088 | return $func->($self, $op, $cx, @args); |
| 1089 | } |
| 1090 | } |
| 1091 | |
| 1092 | sub padname_sv { |
| 1093 | my $self = shift; |
| 1094 | my $targ = shift; |
| 1095 | return $self->{'curcv'}->PADLIST->ARRAYelt(0)->ARRAYelt($targ); |
| 1096 | } |
| 1097 | |
| 1098 | sub maybe_my { |
| 1099 | my $self = shift; |
| 1100 | my($op, $cx, $text) = @_; |
| 1101 | if ($op->private & OPpLVAL_INTRO and not $self->{'avoid_local'}{$$op}) { |
| 1102 | my $my = $op->private & OPpPAD_STATE |
| 1103 | ? $self->keyword("state") |
| 1104 | : "my"; |
| 1105 | if (want_scalar($op)) { |
| 1106 | return "$my $text"; |
| 1107 | } else { |
| 1108 | return $self->maybe_parens_func($my, $text, $cx, 16); |
| 1109 | } |
| 1110 | } else { |
| 1111 | return $text; |
| 1112 | } |
| 1113 | } |
| 1114 | |
| 1115 | # The following OPs don't have functions: |
| 1116 | |
| 1117 | # pp_padany -- does not exist after parsing |
| 1118 | |
| 1119 | sub AUTOLOAD { |
| 1120 | if ($AUTOLOAD =~ s/^.*::pp_//) { |
| 1121 | warn "unexpected OP_".uc $AUTOLOAD; |
| 1122 | return "XXX"; |
| 1123 | } else { |
| 1124 | die "Undefined subroutine $AUTOLOAD called"; |
| 1125 | } |
| 1126 | } |
| 1127 | |
| 1128 | sub DESTROY {} # Do not AUTOLOAD |
| 1129 | |
| 1130 | # $root should be the op which represents the root of whatever |
| 1131 | # we're sequencing here. If it's undefined, then we don't append |
| 1132 | # any subroutine declarations to the deparsed ops, otherwise we |
| 1133 | # append appropriate declarations. |
| 1134 | sub lineseq { |
| 1135 | my($self, $root, @ops) = @_; |
| 1136 | my($expr, @exprs); |
| 1137 | |
| 1138 | my $out_cop = $self->{'curcop'}; |
| 1139 | my $out_seq = defined($out_cop) ? $out_cop->cop_seq : undef; |
| 1140 | my $limit_seq; |
| 1141 | if (defined $root) { |
| 1142 | $limit_seq = $out_seq; |
| 1143 | my $nseq; |
| 1144 | $nseq = $self->find_scope_st($root->sibling) if ${$root->sibling}; |
| 1145 | $limit_seq = $nseq if !defined($limit_seq) |
| 1146 | or defined($nseq) && $nseq < $limit_seq; |
| 1147 | } |
| 1148 | $limit_seq = $self->{'limit_seq'} |
| 1149 | if defined($self->{'limit_seq'}) |
| 1150 | && (!defined($limit_seq) || $self->{'limit_seq'} < $limit_seq); |
| 1151 | local $self->{'limit_seq'} = $limit_seq; |
| 1152 | |
| 1153 | $self->walk_lineseq($root, \@ops, |
| 1154 | sub { push @exprs, $_[0]} ); |
| 1155 | |
| 1156 | my $body = join(";\n", grep {length} @exprs); |
| 1157 | my $subs = ""; |
| 1158 | if (defined $root && defined $limit_seq && !$self->{'in_format'}) { |
| 1159 | $subs = join "\n", $self->seq_subs($limit_seq); |
| 1160 | } |
| 1161 | return join(";\n", grep {length} $body, $subs); |
| 1162 | } |
| 1163 | |
| 1164 | sub scopeop { |
| 1165 | my($real_block, $self, $op, $cx) = @_; |
| 1166 | my $kid; |
| 1167 | my @kids; |
| 1168 | |
| 1169 | local(@$self{qw'curstash warnings hints hinthash'}) |
| 1170 | = @$self{qw'curstash warnings hints hinthash'} if $real_block; |
| 1171 | if ($real_block) { |
| 1172 | $kid = $op->first->sibling; # skip enter |
| 1173 | if (is_miniwhile($kid)) { |
| 1174 | my $top = $kid->first; |
| 1175 | my $name = $top->name; |
| 1176 | if ($name eq "and") { |
| 1177 | $name = "while"; |
| 1178 | } elsif ($name eq "or") { |
| 1179 | $name = "until"; |
| 1180 | } else { # no conditional -> while 1 or until 0 |
| 1181 | return $self->deparse($top->first, 1) . " while 1"; |
| 1182 | } |
| 1183 | my $cond = $top->first; |
| 1184 | my $body = $cond->sibling->first; # skip lineseq |
| 1185 | $cond = $self->deparse($cond, 1); |
| 1186 | $body = $self->deparse($body, 1); |
| 1187 | return "$body $name $cond"; |
| 1188 | } |
| 1189 | } else { |
| 1190 | $kid = $op->first; |
| 1191 | } |
| 1192 | for (; !null($kid); $kid = $kid->sibling) { |
| 1193 | push @kids, $kid; |
| 1194 | } |
| 1195 | if ($cx > 0) { # inside an expression, (a do {} while for lineseq) |
| 1196 | return "do {\n\t" . $self->lineseq($op, @kids) . "\n\b}"; |
| 1197 | } else { |
| 1198 | my $lineseq = $self->lineseq($op, @kids); |
| 1199 | return (length ($lineseq) ? "$lineseq;" : ""); |
| 1200 | } |
| 1201 | } |
| 1202 | |
| 1203 | sub pp_scope { scopeop(0, @_); } |
| 1204 | sub pp_lineseq { scopeop(0, @_); } |
| 1205 | sub pp_leave { scopeop(1, @_); } |
| 1206 | |
| 1207 | # This is a special case of scopeop and lineseq, for the case of the |
| 1208 | # main_root. The difference is that we print the output statements as |
| 1209 | # soon as we get them, for the sake of impatient users. |
| 1210 | sub deparse_root { |
| 1211 | my $self = shift; |
| 1212 | my($op) = @_; |
| 1213 | local(@$self{qw'curstash warnings hints hinthash'}) |
| 1214 | = @$self{qw'curstash warnings hints hinthash'}; |
| 1215 | my @kids; |
| 1216 | return if null $op->first; # Can happen, e.g., for Bytecode without -k |
| 1217 | for (my $kid = $op->first->sibling; !null($kid); $kid = $kid->sibling) { |
| 1218 | push @kids, $kid; |
| 1219 | } |
| 1220 | $self->walk_lineseq($op, \@kids, |
| 1221 | sub { print $self->indent($_[0].';'); |
| 1222 | print "\n" unless $_[1] == $#kids; |
| 1223 | }); |
| 1224 | } |
| 1225 | |
| 1226 | sub walk_lineseq { |
| 1227 | my ($self, $op, $kids, $callback) = @_; |
| 1228 | my @kids = @$kids; |
| 1229 | for (my $i = 0; $i < @kids; $i++) { |
| 1230 | my $expr = ""; |
| 1231 | if (is_state $kids[$i]) { |
| 1232 | $expr = $self->deparse($kids[$i++], 0); |
| 1233 | if ($i > $#kids) { |
| 1234 | $callback->($expr, $i); |
| 1235 | last; |
| 1236 | } |
| 1237 | } |
| 1238 | if (is_for_loop($kids[$i])) { |
| 1239 | $callback->($expr . $self->for_loop($kids[$i], 0), |
| 1240 | $i += $kids[$i]->sibling->name eq "unstack" ? 2 : 1); |
| 1241 | next; |
| 1242 | } |
| 1243 | $expr .= $self->deparse($kids[$i], (@kids != 1)/2); |
| 1244 | $expr =~ s/;\n?\z//; |
| 1245 | $callback->($expr, $i); |
| 1246 | } |
| 1247 | } |
| 1248 | |
| 1249 | # The BEGIN {} is used here because otherwise this code isn't executed |
| 1250 | # when you run B::Deparse on itself. |
| 1251 | my %globalnames; |
| 1252 | BEGIN { map($globalnames{$_}++, "SIG", "STDIN", "STDOUT", "STDERR", "INC", |
| 1253 | "ENV", "ARGV", "ARGVOUT", "_"); } |
| 1254 | |
| 1255 | sub gv_name { |
| 1256 | my $self = shift; |
| 1257 | my $gv = shift; |
| 1258 | my $raw = shift; |
| 1259 | Carp::confess() unless ref($gv) eq "B::GV"; |
| 1260 | my $stash = $gv->STASH->NAME; |
| 1261 | my $name = $raw ? $gv->NAME : $gv->SAFENAME; |
| 1262 | if ($stash eq 'main' && $name =~ /^::/) { |
| 1263 | $stash = '::'; |
| 1264 | } |
| 1265 | elsif (($stash eq 'main' |
| 1266 | && ($globalnames{$name} || $name =~ /^[^A-Za-z_:]/)) |
| 1267 | or ($stash eq $self->{'curstash'} && !$globalnames{$name} |
| 1268 | && ($stash eq 'main' || $name !~ /::/)) |
| 1269 | ) |
| 1270 | { |
| 1271 | $stash = ""; |
| 1272 | } else { |
| 1273 | $stash = $stash . "::"; |
| 1274 | } |
| 1275 | if (!$raw and $name =~ /^(\^..|{)/) { |
| 1276 | $name = "{$name}"; # ${^WARNING_BITS}, etc and ${ |
| 1277 | } |
| 1278 | return $stash . $name; |
| 1279 | } |
| 1280 | |
| 1281 | # Return the name to use for a stash variable. |
| 1282 | # If a lexical with the same name is in scope, it may need to be |
| 1283 | # fully-qualified. |
| 1284 | sub stash_variable { |
| 1285 | my ($self, $prefix, $name, $cx) = @_; |
| 1286 | |
| 1287 | return "$prefix$name" if $name =~ /::/; |
| 1288 | |
| 1289 | unless ($prefix eq '$' || $prefix eq '@' || #' |
| 1290 | $prefix eq '%' || $prefix eq '$#') { |
| 1291 | return "$prefix$name"; |
| 1292 | } |
| 1293 | |
| 1294 | if ($name =~ /^[^\w+-]$/) { |
| 1295 | if (defined $cx && $cx == 26) { |
| 1296 | if ($prefix eq '@') { |
| 1297 | return "$prefix\{$name}"; |
| 1298 | } |
| 1299 | elsif ($name eq '#') { return '${#}' } # "${#}a" vs "$#a" |
| 1300 | } |
| 1301 | if ($prefix eq '$#') { |
| 1302 | return "\$#{$name}"; |
| 1303 | } |
| 1304 | } |
| 1305 | |
| 1306 | my $v = ($prefix eq '$#' ? '@' : $prefix) . $name; |
| 1307 | return $prefix .$self->{'curstash'}.'::'. $name if $self->lex_in_scope($v); |
| 1308 | return "$prefix$name"; |
| 1309 | } |
| 1310 | |
| 1311 | # Return just the name, without the prefix. It may be returned as a quoted |
| 1312 | # string. The second return value is a boolean indicating that. |
| 1313 | sub stash_variable_name { |
| 1314 | my($self, $prefix, $gv) = @_; |
| 1315 | my $name = $self->gv_name($gv, 1); |
| 1316 | $name = $self->{'curstash'}.'::'. $name |
| 1317 | if $prefix and $self->lex_in_scope("$prefix$name"); |
| 1318 | if ($name =~ /^(?:\S|(?!\d)[\ca-\cz]?(?:\w|::)*|\d+)\z/) { |
| 1319 | $name =~ s/^([\ca-\cz])/'^'.($1|'@')/e; |
| 1320 | $name =~ /^(\^..|{)/ and $name = "{$name}"; |
| 1321 | return $name, 0; # not quoted |
| 1322 | } |
| 1323 | else { |
| 1324 | single_delim("q", "'", $name), 1; |
| 1325 | } |
| 1326 | } |
| 1327 | |
| 1328 | sub lex_in_scope { |
| 1329 | my ($self, $name) = @_; |
| 1330 | $self->populate_curcvlex() if !defined $self->{'curcvlex'}; |
| 1331 | |
| 1332 | return 0 if !defined($self->{'curcop'}); |
| 1333 | my $seq = $self->{'curcop'}->cop_seq; |
| 1334 | return 0 if !exists $self->{'curcvlex'}{$name}; |
| 1335 | for my $a (@{$self->{'curcvlex'}{$name}}) { |
| 1336 | my ($st, $en) = @$a; |
| 1337 | return 1 if $seq > $st && $seq <= $en; |
| 1338 | } |
| 1339 | return 0; |
| 1340 | } |
| 1341 | |
| 1342 | sub populate_curcvlex { |
| 1343 | my $self = shift; |
| 1344 | for (my $cv = $self->{'curcv'}; class($cv) eq "CV"; $cv = $cv->OUTSIDE) { |
| 1345 | my $padlist = $cv->PADLIST; |
| 1346 | # an undef CV still in lexical chain |
| 1347 | next if class($padlist) eq "SPECIAL"; |
| 1348 | my @padlist = $padlist->ARRAY; |
| 1349 | my @ns = $padlist[0]->ARRAY; |
| 1350 | |
| 1351 | for (my $i=0; $i<@ns; ++$i) { |
| 1352 | next if class($ns[$i]) eq "SPECIAL"; |
| 1353 | next if $ns[$i]->FLAGS & SVpad_OUR; # Skip "our" vars |
| 1354 | if (class($ns[$i]) eq "PV") { |
| 1355 | # Probably that pesky lexical @_ |
| 1356 | next; |
| 1357 | } |
| 1358 | my $name = $ns[$i]->PVX; |
| 1359 | my ($seq_st, $seq_en) = |
| 1360 | ($ns[$i]->FLAGS & SVf_FAKE) |
| 1361 | ? (0, 999999) |
| 1362 | : ($ns[$i]->COP_SEQ_RANGE_LOW, $ns[$i]->COP_SEQ_RANGE_HIGH); |
| 1363 | |
| 1364 | push @{$self->{'curcvlex'}{$name}}, [$seq_st, $seq_en]; |
| 1365 | } |
| 1366 | } |
| 1367 | } |
| 1368 | |
| 1369 | sub find_scope_st { ((find_scope(@_))[0]); } |
| 1370 | sub find_scope_en { ((find_scope(@_))[1]); } |
| 1371 | |
| 1372 | # Recurses down the tree, looking for pad variable introductions and COPs |
| 1373 | sub find_scope { |
| 1374 | my ($self, $op, $scope_st, $scope_en) = @_; |
| 1375 | carp("Undefined op in find_scope") if !defined $op; |
| 1376 | return ($scope_st, $scope_en) unless $op->flags & OPf_KIDS; |
| 1377 | |
| 1378 | my @queue = ($op); |
| 1379 | while(my $op = shift @queue ) { |
| 1380 | for (my $o=$op->first; $$o; $o=$o->sibling) { |
| 1381 | if ($o->name =~ /^pad.v$/ && $o->private & OPpLVAL_INTRO) { |
| 1382 | my $s = int($self->padname_sv($o->targ)->COP_SEQ_RANGE_LOW); |
| 1383 | my $e = $self->padname_sv($o->targ)->COP_SEQ_RANGE_HIGH; |
| 1384 | $scope_st = $s if !defined($scope_st) || $s < $scope_st; |
| 1385 | $scope_en = $e if !defined($scope_en) || $e > $scope_en; |
| 1386 | return ($scope_st, $scope_en); |
| 1387 | } |
| 1388 | elsif (is_state($o)) { |
| 1389 | my $c = $o->cop_seq; |
| 1390 | $scope_st = $c if !defined($scope_st) || $c < $scope_st; |
| 1391 | $scope_en = $c if !defined($scope_en) || $c > $scope_en; |
| 1392 | return ($scope_st, $scope_en); |
| 1393 | } |
| 1394 | elsif ($o->flags & OPf_KIDS) { |
| 1395 | unshift (@queue, $o); |
| 1396 | } |
| 1397 | } |
| 1398 | } |
| 1399 | |
| 1400 | return ($scope_st, $scope_en); |
| 1401 | } |
| 1402 | |
| 1403 | # Returns a list of subs which should be inserted before the COP |
| 1404 | sub cop_subs { |
| 1405 | my ($self, $op, $out_seq) = @_; |
| 1406 | my $seq = $op->cop_seq; |
| 1407 | # If we have nephews, then our sequence number indicates |
| 1408 | # the cop_seq of the end of some sort of scope. |
| 1409 | if (class($op->sibling) ne "NULL" && $op->sibling->flags & OPf_KIDS |
| 1410 | and my $nseq = $self->find_scope_st($op->sibling) ) { |
| 1411 | $seq = $nseq; |
| 1412 | } |
| 1413 | $seq = $out_seq if defined($out_seq) && $out_seq < $seq; |
| 1414 | return $self->seq_subs($seq); |
| 1415 | } |
| 1416 | |
| 1417 | sub seq_subs { |
| 1418 | my ($self, $seq) = @_; |
| 1419 | my @text; |
| 1420 | #push @text, "# ($seq)\n"; |
| 1421 | |
| 1422 | return "" if !defined $seq; |
| 1423 | while (scalar(@{$self->{'subs_todo'}}) |
| 1424 | and $seq > $self->{'subs_todo'}[0][0]) { |
| 1425 | push @text, $self->next_todo; |
| 1426 | } |
| 1427 | return @text; |
| 1428 | } |
| 1429 | |
| 1430 | # Notice how subs and formats are inserted between statements here; |
| 1431 | # also $[ assignments and pragmas. |
| 1432 | sub pp_nextstate { |
| 1433 | my $self = shift; |
| 1434 | my($op, $cx) = @_; |
| 1435 | $self->{'curcop'} = $op; |
| 1436 | my @text; |
| 1437 | push @text, $self->cop_subs($op); |
| 1438 | my $stash = $op->stashpv; |
| 1439 | if ($stash ne $self->{'curstash'}) { |
| 1440 | push @text, "package $stash;\n"; |
| 1441 | $self->{'curstash'} = $stash; |
| 1442 | } |
| 1443 | |
| 1444 | if (OPpCONST_ARYBASE && $self->{'arybase'} != $op->arybase) { |
| 1445 | push @text, '$[ = '. $op->arybase .";\n"; |
| 1446 | $self->{'arybase'} = $op->arybase; |
| 1447 | } |
| 1448 | |
| 1449 | my $warnings = $op->warnings; |
| 1450 | my $warning_bits; |
| 1451 | if ($warnings->isa("B::SPECIAL") && $$warnings == 4) { |
| 1452 | $warning_bits = $warnings::Bits{"all"} & WARN_MASK; |
| 1453 | } |
| 1454 | elsif ($warnings->isa("B::SPECIAL") && $$warnings == 5) { |
| 1455 | $warning_bits = $warnings::NONE; |
| 1456 | } |
| 1457 | elsif ($warnings->isa("B::SPECIAL")) { |
| 1458 | $warning_bits = undef; |
| 1459 | } |
| 1460 | else { |
| 1461 | $warning_bits = $warnings->PV & WARN_MASK; |
| 1462 | } |
| 1463 | |
| 1464 | if (defined ($warning_bits) and |
| 1465 | !defined($self->{warnings}) || $self->{'warnings'} ne $warning_bits) { |
| 1466 | push @text, declare_warnings($self->{'warnings'}, $warning_bits); |
| 1467 | $self->{'warnings'} = $warning_bits; |
| 1468 | } |
| 1469 | |
| 1470 | my $hints = $] < 5.008009 ? $op->private : $op->hints; |
| 1471 | if ($self->{'hints'} != $hints) { |
| 1472 | push @text, declare_hints($self->{'hints'}, $hints); |
| 1473 | $self->{'hints'} = $hints; |
| 1474 | } |
| 1475 | |
| 1476 | if ($] > 5.009 && |
| 1477 | @text != push @text, declare_hinthash( |
| 1478 | $self->{'hinthash'}, $op->hints_hash->HASH, |
| 1479 | $self->{indent_size} |
| 1480 | ) |
| 1481 | ) { |
| 1482 | $self->{'hinthash'} = $op->hints_hash->HASH; |
| 1483 | } |
| 1484 | |
| 1485 | # This should go after of any branches that add statements, to |
| 1486 | # increase the chances that it refers to the same line it did in |
| 1487 | # the original program. |
| 1488 | if ($self->{'linenums'}) { |
| 1489 | push @text, "\f#line " . $op->line . |
| 1490 | ' "' . $op->file, qq'"\n'; |
| 1491 | } |
| 1492 | |
| 1493 | push @text, $op->label . ": " if $op->label; |
| 1494 | |
| 1495 | return join("", @text); |
| 1496 | } |
| 1497 | |
| 1498 | sub declare_warnings { |
| 1499 | my ($from, $to) = @_; |
| 1500 | if (($to & WARN_MASK) eq (warnings::bits("all") & WARN_MASK)) { |
| 1501 | return "use warnings;\n"; |
| 1502 | } |
| 1503 | elsif (($to & WARN_MASK) eq ("\0"x length($to) & WARN_MASK)) { |
| 1504 | return "no warnings;\n"; |
| 1505 | } |
| 1506 | return "BEGIN {\${^WARNING_BITS} = ".perlstring($to)."}\n"; |
| 1507 | } |
| 1508 | |
| 1509 | sub declare_hints { |
| 1510 | my ($from, $to) = @_; |
| 1511 | my $use = $to & ~$from; |
| 1512 | my $no = $from & ~$to; |
| 1513 | my $decls = ""; |
| 1514 | for my $pragma (hint_pragmas($use)) { |
| 1515 | $decls .= "use $pragma;\n"; |
| 1516 | } |
| 1517 | for my $pragma (hint_pragmas($no)) { |
| 1518 | $decls .= "no $pragma;\n"; |
| 1519 | } |
| 1520 | return $decls; |
| 1521 | } |
| 1522 | |
| 1523 | # Internal implementation hints that the core sets automatically, so don't need |
| 1524 | # (or want) to be passed back to the user |
| 1525 | my %ignored_hints = ( |
| 1526 | 'open<' => 1, |
| 1527 | 'open>' => 1, |
| 1528 | ':' => 1, |
| 1529 | 'strict/refs' => 1, |
| 1530 | 'strict/subs' => 1, |
| 1531 | 'strict/vars' => 1, |
| 1532 | ); |
| 1533 | |
| 1534 | sub declare_hinthash { |
| 1535 | my ($from, $to, $indent) = @_; |
| 1536 | my @decls; |
| 1537 | for my $key (keys %$to) { |
| 1538 | next if $ignored_hints{$key}; |
| 1539 | if (!exists $from->{$key} or $from->{$key} ne $to->{$key}) { |
| 1540 | push @decls, |
| 1541 | qq(\$^H{) . single_delim("q", "'", $key) . qq(} = ) |
| 1542 | . ( |
| 1543 | defined $to->{$key} |
| 1544 | ? single_delim("q", "'", $to->{$key}) |
| 1545 | : 'undef' |
| 1546 | ) |
| 1547 | . qq(;); |
| 1548 | } |
| 1549 | } |
| 1550 | for my $key (keys %$from) { |
| 1551 | next if $ignored_hints{$key}; |
| 1552 | if (!exists $to->{$key}) { |
| 1553 | push @decls, qq(delete \$^H{'$key'};); |
| 1554 | } |
| 1555 | } |
| 1556 | @decls or return; |
| 1557 | return join("\n" . (" " x $indent), "BEGIN {", @decls) . "\n}\n"; |
| 1558 | } |
| 1559 | |
| 1560 | sub hint_pragmas { |
| 1561 | my ($bits) = @_; |
| 1562 | my @pragmas; |
| 1563 | push @pragmas, "integer" if $bits & 0x1; |
| 1564 | push @pragmas, "strict 'refs'" if $bits & 0x2; |
| 1565 | push @pragmas, "bytes" if $bits & 0x8; |
| 1566 | return @pragmas; |
| 1567 | } |
| 1568 | |
| 1569 | sub pp_dbstate { pp_nextstate(@_) } |
| 1570 | sub pp_setstate { pp_nextstate(@_) } |
| 1571 | |
| 1572 | sub pp_unstack { return "" } # see also leaveloop |
| 1573 | |
| 1574 | my %feature_keywords = ( |
| 1575 | # keyword => 'feature', |
| 1576 | state => 'state', |
| 1577 | say => 'say', |
| 1578 | given => 'switch', |
| 1579 | when => 'switch', |
| 1580 | default => 'switch', |
| 1581 | break => 'switch', |
| 1582 | evalbytes=>'evalbytes', |
| 1583 | __SUB__ => '__SUB__', |
| 1584 | ); |
| 1585 | |
| 1586 | my $feature_bundle_mask = 0x1c000000; |
| 1587 | |
| 1588 | sub keyword { |
| 1589 | my $self = shift; |
| 1590 | my $name = shift; |
| 1591 | return $name if $name =~ /^CORE::/; # just in case |
| 1592 | if (exists $feature_keywords{$name}) { |
| 1593 | my $hh; |
| 1594 | my $hints = $self->{hints} & $feature_bundle_mask; |
| 1595 | if ($hints && $hints != $feature_bundle_mask) { |
| 1596 | require feature; |
| 1597 | local $^H = $self->{hints}; |
| 1598 | # Shh! Keep quite about this function. It is not to be |
| 1599 | # relied upon. |
| 1600 | $hh = { map +($_ => 1), feature::current_bundle() }; |
| 1601 | } |
| 1602 | elsif ($hints) { $hh = $self->{'hinthash'} } |
| 1603 | return "CORE::$name" |
| 1604 | if !$hh |
| 1605 | || !$hh->{"feature_$feature_keywords{$name}"} |
| 1606 | } |
| 1607 | if ( |
| 1608 | $name !~ /^(?:chom?p|do|exec|glob|s(?:elect|ystem))\z/ |
| 1609 | && !defined eval{prototype "CORE::$name"} |
| 1610 | ) { return $name } |
| 1611 | if ( |
| 1612 | exists $self->{subs_declared}{$name} |
| 1613 | or |
| 1614 | exists &{"$self->{curstash}::$name"} |
| 1615 | ) { |
| 1616 | return "CORE::$name" |
| 1617 | } |
| 1618 | return $name; |
| 1619 | } |
| 1620 | |
| 1621 | sub baseop { |
| 1622 | my $self = shift; |
| 1623 | my($op, $cx, $name) = @_; |
| 1624 | return $self->keyword($name); |
| 1625 | } |
| 1626 | |
| 1627 | sub pp_stub { |
| 1628 | my $self = shift; |
| 1629 | my($op, $cx, $name) = @_; |
| 1630 | if ($cx >= 1) { |
| 1631 | return "()"; |
| 1632 | } |
| 1633 | else { |
| 1634 | return "();"; |
| 1635 | } |
| 1636 | } |
| 1637 | sub pp_wantarray { baseop(@_, "wantarray") } |
| 1638 | sub pp_fork { baseop(@_, "fork") } |
| 1639 | sub pp_wait { maybe_targmy(@_, \&baseop, "wait") } |
| 1640 | sub pp_getppid { maybe_targmy(@_, \&baseop, "getppid") } |
| 1641 | sub pp_time { maybe_targmy(@_, \&baseop, "time") } |
| 1642 | sub pp_tms { baseop(@_, "times") } |
| 1643 | sub pp_ghostent { baseop(@_, "gethostent") } |
| 1644 | sub pp_gnetent { baseop(@_, "getnetent") } |
| 1645 | sub pp_gprotoent { baseop(@_, "getprotoent") } |
| 1646 | sub pp_gservent { baseop(@_, "getservent") } |
| 1647 | sub pp_ehostent { baseop(@_, "endhostent") } |
| 1648 | sub pp_enetent { baseop(@_, "endnetent") } |
| 1649 | sub pp_eprotoent { baseop(@_, "endprotoent") } |
| 1650 | sub pp_eservent { baseop(@_, "endservent") } |
| 1651 | sub pp_gpwent { baseop(@_, "getpwent") } |
| 1652 | sub pp_spwent { baseop(@_, "setpwent") } |
| 1653 | sub pp_epwent { baseop(@_, "endpwent") } |
| 1654 | sub pp_ggrent { baseop(@_, "getgrent") } |
| 1655 | sub pp_sgrent { baseop(@_, "setgrent") } |
| 1656 | sub pp_egrent { baseop(@_, "endgrent") } |
| 1657 | sub pp_getlogin { baseop(@_, "getlogin") } |
| 1658 | |
| 1659 | sub POSTFIX () { 1 } |
| 1660 | |
| 1661 | # I couldn't think of a good short name, but this is the category of |
| 1662 | # symbolic unary operators with interesting precedence |
| 1663 | |
| 1664 | sub pfixop { |
| 1665 | my $self = shift; |
| 1666 | my($op, $cx, $name, $prec, $flags) = (@_, 0); |
| 1667 | my $kid = $op->first; |
| 1668 | $kid = $self->deparse($kid, $prec); |
| 1669 | return $self->maybe_parens(($flags & POSTFIX) |
| 1670 | ? "$kid$name" |
| 1671 | # avoid confusion with filetests |
| 1672 | : $name eq '-' |
| 1673 | && $kid =~ /^[a-zA-Z](?!\w)/ |
| 1674 | ? "$name($kid)" |
| 1675 | : "$name$kid", |
| 1676 | $cx, $prec); |
| 1677 | } |
| 1678 | |
| 1679 | sub pp_preinc { pfixop(@_, "++", 23) } |
| 1680 | sub pp_predec { pfixop(@_, "--", 23) } |
| 1681 | sub pp_postinc { maybe_targmy(@_, \&pfixop, "++", 23, POSTFIX) } |
| 1682 | sub pp_postdec { maybe_targmy(@_, \&pfixop, "--", 23, POSTFIX) } |
| 1683 | sub pp_i_preinc { pfixop(@_, "++", 23) } |
| 1684 | sub pp_i_predec { pfixop(@_, "--", 23) } |
| 1685 | sub pp_i_postinc { maybe_targmy(@_, \&pfixop, "++", 23, POSTFIX) } |
| 1686 | sub pp_i_postdec { maybe_targmy(@_, \&pfixop, "--", 23, POSTFIX) } |
| 1687 | sub pp_complement { maybe_targmy(@_, \&pfixop, "~", 21) } |
| 1688 | |
| 1689 | sub pp_negate { maybe_targmy(@_, \&real_negate) } |
| 1690 | sub real_negate { |
| 1691 | my $self = shift; |
| 1692 | my($op, $cx) = @_; |
| 1693 | if ($op->first->name =~ /^(i_)?negate$/) { |
| 1694 | # avoid --$x |
| 1695 | $self->pfixop($op, $cx, "-", 21.5); |
| 1696 | } else { |
| 1697 | $self->pfixop($op, $cx, "-", 21); |
| 1698 | } |
| 1699 | } |
| 1700 | sub pp_i_negate { pp_negate(@_) } |
| 1701 | |
| 1702 | sub pp_not { |
| 1703 | my $self = shift; |
| 1704 | my($op, $cx) = @_; |
| 1705 | if ($cx <= 4) { |
| 1706 | $self->listop($op, $cx, "not", $op->first); |
| 1707 | } else { |
| 1708 | $self->pfixop($op, $cx, "!", 21); |
| 1709 | } |
| 1710 | } |
| 1711 | |
| 1712 | sub unop { |
| 1713 | my $self = shift; |
| 1714 | my($op, $cx, $name, $nollafr) = @_; |
| 1715 | my $kid; |
| 1716 | if ($op->flags & OPf_KIDS) { |
| 1717 | $kid = $op->first; |
| 1718 | if (not $name) { |
| 1719 | # this deals with 'boolkeys' right now |
| 1720 | return $self->deparse($kid,$cx); |
| 1721 | } |
| 1722 | my $builtinname = $name; |
| 1723 | $builtinname =~ /^CORE::/ or $builtinname = "CORE::$name"; |
| 1724 | if (defined prototype($builtinname) |
| 1725 | && prototype($builtinname) =~ /^;?\*/ |
| 1726 | && $kid->name eq "rv2gv") { |
| 1727 | $kid = $kid->first; |
| 1728 | } |
| 1729 | |
| 1730 | if ($nollafr) { |
| 1731 | ($kid = $self->deparse($kid, 16)) =~ s/^\cS//; |
| 1732 | return $self->maybe_parens( |
| 1733 | $self->keyword($name) . " $kid", $cx, 16 |
| 1734 | ); |
| 1735 | } |
| 1736 | return $self->maybe_parens_unop($name, $kid, $cx); |
| 1737 | } else { |
| 1738 | return $self->maybe_parens( |
| 1739 | $self->keyword($name) . ($op->flags & OPf_SPECIAL ? "()" : ""), |
| 1740 | $cx, 16, |
| 1741 | ); |
| 1742 | } |
| 1743 | } |
| 1744 | |
| 1745 | sub pp_chop { maybe_targmy(@_, \&unop, "chop") } |
| 1746 | sub pp_chomp { maybe_targmy(@_, \&unop, "chomp") } |
| 1747 | sub pp_schop { maybe_targmy(@_, \&unop, "chop") } |
| 1748 | sub pp_schomp { maybe_targmy(@_, \&unop, "chomp") } |
| 1749 | sub pp_defined { unop(@_, "defined") } |
| 1750 | sub pp_undef { unop(@_, "undef") } |
| 1751 | sub pp_study { unop(@_, "study") } |
| 1752 | sub pp_ref { unop(@_, "ref") } |
| 1753 | sub pp_pos { maybe_local(@_, unop(@_, "pos")) } |
| 1754 | |
| 1755 | sub pp_sin { maybe_targmy(@_, \&unop, "sin") } |
| 1756 | sub pp_cos { maybe_targmy(@_, \&unop, "cos") } |
| 1757 | sub pp_rand { maybe_targmy(@_, \&unop, "rand") } |
| 1758 | sub pp_srand { unop(@_, "srand") } |
| 1759 | sub pp_exp { maybe_targmy(@_, \&unop, "exp") } |
| 1760 | sub pp_log { maybe_targmy(@_, \&unop, "log") } |
| 1761 | sub pp_sqrt { maybe_targmy(@_, \&unop, "sqrt") } |
| 1762 | sub pp_int { maybe_targmy(@_, \&unop, "int") } |
| 1763 | sub pp_hex { maybe_targmy(@_, \&unop, "hex") } |
| 1764 | sub pp_oct { maybe_targmy(@_, \&unop, "oct") } |
| 1765 | sub pp_abs { maybe_targmy(@_, \&unop, "abs") } |
| 1766 | |
| 1767 | sub pp_length { maybe_targmy(@_, \&unop, "length") } |
| 1768 | sub pp_ord { maybe_targmy(@_, \&unop, "ord") } |
| 1769 | sub pp_chr { maybe_targmy(@_, \&unop, "chr") } |
| 1770 | |
| 1771 | sub pp_each { unop(@_, "each") } |
| 1772 | sub pp_values { unop(@_, "values") } |
| 1773 | sub pp_keys { unop(@_, "keys") } |
| 1774 | { no strict 'refs'; *{"pp_r$_"} = *{"pp_$_"} for qw< keys each values >; } |
| 1775 | sub pp_boolkeys { |
| 1776 | # no name because its an optimisation op that has no keyword |
| 1777 | unop(@_,""); |
| 1778 | } |
| 1779 | sub pp_aeach { unop(@_, "each") } |
| 1780 | sub pp_avalues { unop(@_, "values") } |
| 1781 | sub pp_akeys { unop(@_, "keys") } |
| 1782 | sub pp_pop { unop(@_, "pop") } |
| 1783 | sub pp_shift { unop(@_, "shift") } |
| 1784 | |
| 1785 | sub pp_caller { unop(@_, "caller") } |
| 1786 | sub pp_reset { unop(@_, "reset") } |
| 1787 | sub pp_exit { unop(@_, "exit") } |
| 1788 | sub pp_prototype { unop(@_, "prototype") } |
| 1789 | |
| 1790 | sub pp_close { unop(@_, "close") } |
| 1791 | sub pp_fileno { unop(@_, "fileno") } |
| 1792 | sub pp_umask { unop(@_, "umask") } |
| 1793 | sub pp_untie { unop(@_, "untie") } |
| 1794 | sub pp_tied { unop(@_, "tied") } |
| 1795 | sub pp_dbmclose { unop(@_, "dbmclose") } |
| 1796 | sub pp_getc { unop(@_, "getc") } |
| 1797 | sub pp_eof { unop(@_, "eof") } |
| 1798 | sub pp_tell { unop(@_, "tell") } |
| 1799 | sub pp_getsockname { unop(@_, "getsockname") } |
| 1800 | sub pp_getpeername { unop(@_, "getpeername") } |
| 1801 | |
| 1802 | sub pp_chdir { maybe_targmy(@_, \&unop, "chdir") } |
| 1803 | sub pp_chroot { maybe_targmy(@_, \&unop, "chroot") } |
| 1804 | sub pp_readlink { unop(@_, "readlink") } |
| 1805 | sub pp_rmdir { maybe_targmy(@_, \&unop, "rmdir") } |
| 1806 | sub pp_readdir { unop(@_, "readdir") } |
| 1807 | sub pp_telldir { unop(@_, "telldir") } |
| 1808 | sub pp_rewinddir { unop(@_, "rewinddir") } |
| 1809 | sub pp_closedir { unop(@_, "closedir") } |
| 1810 | sub pp_getpgrp { maybe_targmy(@_, \&unop, "getpgrp") } |
| 1811 | sub pp_localtime { unop(@_, "localtime") } |
| 1812 | sub pp_gmtime { unop(@_, "gmtime") } |
| 1813 | sub pp_alarm { unop(@_, "alarm") } |
| 1814 | sub pp_sleep { maybe_targmy(@_, \&unop, "sleep") } |
| 1815 | |
| 1816 | sub pp_dofile { |
| 1817 | my $code = unop(@_, "do", 1); # llafr does not apply |
| 1818 | if ($code =~ s/^((?:CORE::)?do) \{/$1({/) { $code .= ')' } |
| 1819 | $code; |
| 1820 | } |
| 1821 | sub pp_entereval { |
| 1822 | unop( |
| 1823 | @_, |
| 1824 | $_[1]->private & OPpEVAL_BYTES ? $_[0]->keyword('evalbytes') : "eval" |
| 1825 | ) |
| 1826 | } |
| 1827 | |
| 1828 | sub pp_ghbyname { unop(@_, "gethostbyname") } |
| 1829 | sub pp_gnbyname { unop(@_, "getnetbyname") } |
| 1830 | sub pp_gpbyname { unop(@_, "getprotobyname") } |
| 1831 | sub pp_shostent { unop(@_, "sethostent") } |
| 1832 | sub pp_snetent { unop(@_, "setnetent") } |
| 1833 | sub pp_sprotoent { unop(@_, "setprotoent") } |
| 1834 | sub pp_sservent { unop(@_, "setservent") } |
| 1835 | sub pp_gpwnam { unop(@_, "getpwnam") } |
| 1836 | sub pp_gpwuid { unop(@_, "getpwuid") } |
| 1837 | sub pp_ggrnam { unop(@_, "getgrnam") } |
| 1838 | sub pp_ggrgid { unop(@_, "getgrgid") } |
| 1839 | |
| 1840 | sub pp_lock { unop(@_, "lock") } |
| 1841 | |
| 1842 | sub pp_continue { unop(@_, "continue"); } |
| 1843 | sub pp_break { unop(@_, "break"); } |
| 1844 | |
| 1845 | sub givwhen { |
| 1846 | my $self = shift; |
| 1847 | my($op, $cx, $givwhen) = @_; |
| 1848 | |
| 1849 | my $enterop = $op->first; |
| 1850 | my ($head, $block); |
| 1851 | if ($enterop->flags & OPf_SPECIAL) { |
| 1852 | $head = $self->keyword("default"); |
| 1853 | $block = $self->deparse($enterop->first, 0); |
| 1854 | } |
| 1855 | else { |
| 1856 | my $cond = $enterop->first; |
| 1857 | my $cond_str = $self->deparse($cond, 1); |
| 1858 | $head = "$givwhen ($cond_str)"; |
| 1859 | $block = $self->deparse($cond->sibling, 0); |
| 1860 | } |
| 1861 | |
| 1862 | return "$head {\n". |
| 1863 | "\t$block\n". |
| 1864 | "\b}\cK"; |
| 1865 | } |
| 1866 | |
| 1867 | sub pp_leavegiven { givwhen(@_, $_[0]->keyword("given")); } |
| 1868 | sub pp_leavewhen { givwhen(@_, $_[0]->keyword("when")); } |
| 1869 | |
| 1870 | sub pp_exists { |
| 1871 | my $self = shift; |
| 1872 | my($op, $cx) = @_; |
| 1873 | my $arg; |
| 1874 | if ($op->private & OPpEXISTS_SUB) { |
| 1875 | # Checking for the existence of a subroutine |
| 1876 | return $self->maybe_parens_func("exists", |
| 1877 | $self->pp_rv2cv($op->first, 16), $cx, 16); |
| 1878 | } |
| 1879 | if ($op->flags & OPf_SPECIAL) { |
| 1880 | # Array element, not hash element |
| 1881 | return $self->maybe_parens_func("exists", |
| 1882 | $self->pp_aelem($op->first, 16), $cx, 16); |
| 1883 | } |
| 1884 | return $self->maybe_parens_func("exists", $self->pp_helem($op->first, 16), |
| 1885 | $cx, 16); |
| 1886 | } |
| 1887 | |
| 1888 | sub pp_delete { |
| 1889 | my $self = shift; |
| 1890 | my($op, $cx) = @_; |
| 1891 | my $arg; |
| 1892 | if ($op->private & OPpSLICE) { |
| 1893 | if ($op->flags & OPf_SPECIAL) { |
| 1894 | # Deleting from an array, not a hash |
| 1895 | return $self->maybe_parens_func("delete", |
| 1896 | $self->pp_aslice($op->first, 16), |
| 1897 | $cx, 16); |
| 1898 | } |
| 1899 | return $self->maybe_parens_func("delete", |
| 1900 | $self->pp_hslice($op->first, 16), |
| 1901 | $cx, 16); |
| 1902 | } else { |
| 1903 | if ($op->flags & OPf_SPECIAL) { |
| 1904 | # Deleting from an array, not a hash |
| 1905 | return $self->maybe_parens_func("delete", |
| 1906 | $self->pp_aelem($op->first, 16), |
| 1907 | $cx, 16); |
| 1908 | } |
| 1909 | return $self->maybe_parens_func("delete", |
| 1910 | $self->pp_helem($op->first, 16), |
| 1911 | $cx, 16); |
| 1912 | } |
| 1913 | } |
| 1914 | |
| 1915 | sub pp_require { |
| 1916 | my $self = shift; |
| 1917 | my($op, $cx) = @_; |
| 1918 | my $opname = $op->flags & OPf_SPECIAL ? 'CORE::require' : 'require'; |
| 1919 | if (class($op) eq "UNOP" and $op->first->name eq "const" |
| 1920 | and $op->first->private & OPpCONST_BARE) |
| 1921 | { |
| 1922 | my $name = $self->const_sv($op->first)->PV; |
| 1923 | $name =~ s[/][::]g; |
| 1924 | $name =~ s/\.pm//g; |
| 1925 | return $self->maybe_parens("$opname $name", $cx, 16); |
| 1926 | } else { |
| 1927 | $self->unop( |
| 1928 | $op, $cx, |
| 1929 | $op->first->name eq 'const' |
| 1930 | && $op->first->private & OPpCONST_NOVER |
| 1931 | ? "no" |
| 1932 | : $opname, |
| 1933 | 1, # llafr does not apply |
| 1934 | ); |
| 1935 | } |
| 1936 | } |
| 1937 | |
| 1938 | sub pp_scalar { |
| 1939 | my $self = shift; |
| 1940 | my($op, $cx) = @_; |
| 1941 | my $kid = $op->first; |
| 1942 | if (not null $kid->sibling) { |
| 1943 | # XXX Was a here-doc |
| 1944 | return $self->dquote($op); |
| 1945 | } |
| 1946 | $self->unop(@_, "scalar"); |
| 1947 | } |
| 1948 | |
| 1949 | |
| 1950 | sub padval { |
| 1951 | my $self = shift; |
| 1952 | my $targ = shift; |
| 1953 | return $self->{'curcv'}->PADLIST->ARRAYelt(1)->ARRAYelt($targ); |
| 1954 | } |
| 1955 | |
| 1956 | sub anon_hash_or_list { |
| 1957 | my $self = shift; |
| 1958 | my($op, $cx) = @_; |
| 1959 | |
| 1960 | my($pre, $post) = @{{"anonlist" => ["[","]"], |
| 1961 | "anonhash" => ["{","}"]}->{$op->name}}; |
| 1962 | my($expr, @exprs); |
| 1963 | $op = $op->first->sibling; # skip pushmark |
| 1964 | for (; !null($op); $op = $op->sibling) { |
| 1965 | $expr = $self->deparse($op, 6); |
| 1966 | push @exprs, $expr; |
| 1967 | } |
| 1968 | if ($pre eq "{" and $cx < 1) { |
| 1969 | # Disambiguate that it's not a block |
| 1970 | $pre = "+{"; |
| 1971 | } |
| 1972 | return $pre . join(", ", @exprs) . $post; |
| 1973 | } |
| 1974 | |
| 1975 | sub pp_anonlist { |
| 1976 | my $self = shift; |
| 1977 | my ($op, $cx) = @_; |
| 1978 | if ($op->flags & OPf_SPECIAL) { |
| 1979 | return $self->anon_hash_or_list($op, $cx); |
| 1980 | } |
| 1981 | warn "Unexpected op pp_" . $op->name() . " without OPf_SPECIAL"; |
| 1982 | return 'XXX'; |
| 1983 | } |
| 1984 | |
| 1985 | *pp_anonhash = \&pp_anonlist; |
| 1986 | |
| 1987 | sub pp_refgen { |
| 1988 | my $self = shift; |
| 1989 | my($op, $cx) = @_; |
| 1990 | my $kid = $op->first; |
| 1991 | if ($kid->name eq "null") { |
| 1992 | $kid = $kid->first; |
| 1993 | if (!null($kid->sibling) and |
| 1994 | $kid->sibling->name eq "anoncode") { |
| 1995 | return $self->e_anoncode({ code => $self->padval($kid->sibling->targ) }); |
| 1996 | } elsif ($kid->name eq "pushmark") { |
| 1997 | my $sib_name = $kid->sibling->name; |
| 1998 | if ($sib_name =~ /^(pad|rv2)[ah]v$/ |
| 1999 | and not $kid->sibling->flags & OPf_REF) |
| 2000 | { |
| 2001 | # The @a in \(@a) isn't in ref context, but only when the |
| 2002 | # parens are there. |
| 2003 | return "\\(" . $self->pp_list($op->first) . ")"; |
| 2004 | } elsif ($sib_name eq 'entersub') { |
| 2005 | my $text = $self->deparse($kid->sibling, 1); |
| 2006 | # Always show parens for \(&func()), but only with -p otherwise |
| 2007 | $text = "($text)" if $self->{'parens'} |
| 2008 | or $kid->sibling->private & OPpENTERSUB_AMPER; |
| 2009 | return "\\$text"; |
| 2010 | } |
| 2011 | } |
| 2012 | } |
| 2013 | $self->pfixop($op, $cx, "\\", 20); |
| 2014 | } |
| 2015 | |
| 2016 | sub e_anoncode { |
| 2017 | my ($self, $info) = @_; |
| 2018 | my $text = $self->deparse_sub($info->{code}); |
| 2019 | return "sub " . $text; |
| 2020 | } |
| 2021 | |
| 2022 | sub pp_srefgen { pp_refgen(@_) } |
| 2023 | |
| 2024 | sub pp_readline { |
| 2025 | my $self = shift; |
| 2026 | my($op, $cx) = @_; |
| 2027 | my $kid = $op->first; |
| 2028 | $kid = $kid->first if $kid->name eq "rv2gv"; # <$fh> |
| 2029 | return "<" . $self->deparse($kid, 1) . ">" if is_scalar($kid); |
| 2030 | return $self->unop($op, $cx, "readline"); |
| 2031 | } |
| 2032 | |
| 2033 | sub pp_rcatline { |
| 2034 | my $self = shift; |
| 2035 | my($op) = @_; |
| 2036 | return "<" . $self->gv_name($self->gv_or_padgv($op)) . ">"; |
| 2037 | } |
| 2038 | |
| 2039 | # Unary operators that can occur as pseudo-listops inside double quotes |
| 2040 | sub dq_unop { |
| 2041 | my $self = shift; |
| 2042 | my($op, $cx, $name, $prec, $flags) = (@_, 0, 0); |
| 2043 | my $kid; |
| 2044 | if ($op->flags & OPf_KIDS) { |
| 2045 | $kid = $op->first; |
| 2046 | # If there's more than one kid, the first is an ex-pushmark. |
| 2047 | $kid = $kid->sibling if not null $kid->sibling; |
| 2048 | return $self->maybe_parens_unop($name, $kid, $cx); |
| 2049 | } else { |
| 2050 | return $name . ($op->flags & OPf_SPECIAL ? "()" : ""); |
| 2051 | } |
| 2052 | } |
| 2053 | |
| 2054 | sub pp_ucfirst { dq_unop(@_, "ucfirst") } |
| 2055 | sub pp_lcfirst { dq_unop(@_, "lcfirst") } |
| 2056 | sub pp_uc { dq_unop(@_, "uc") } |
| 2057 | sub pp_lc { dq_unop(@_, "lc") } |
| 2058 | sub pp_quotemeta { maybe_targmy(@_, \&dq_unop, "quotemeta") } |
| 2059 | |
| 2060 | sub loopex { |
| 2061 | my $self = shift; |
| 2062 | my ($op, $cx, $name) = @_; |
| 2063 | if (class($op) eq "PVOP") { |
| 2064 | $name .= " " . $op->pv; |
| 2065 | } elsif (class($op) eq "OP") { |
| 2066 | # no-op |
| 2067 | } elsif (class($op) eq "UNOP") { |
| 2068 | (my $kid = $self->deparse($op->first, 16)) =~ s/^\cS//; |
| 2069 | $name .= " $kid"; |
| 2070 | } |
| 2071 | return $self->maybe_parens($name, $cx, 16); |
| 2072 | } |
| 2073 | |
| 2074 | sub pp_last { loopex(@_, "last") } |
| 2075 | sub pp_next { loopex(@_, "next") } |
| 2076 | sub pp_redo { loopex(@_, "redo") } |
| 2077 | sub pp_goto { loopex(@_, "goto") } |
| 2078 | sub pp_dump { loopex(@_, "CORE::dump") } |
| 2079 | |
| 2080 | sub ftst { |
| 2081 | my $self = shift; |
| 2082 | my($op, $cx, $name) = @_; |
| 2083 | if (class($op) eq "UNOP") { |
| 2084 | # Genuine '-X' filetests are exempt from the LLAFR, but not |
| 2085 | # l?stat() |
| 2086 | if ($name =~ /^-/) { |
| 2087 | (my $kid = $self->deparse($op->first, 16)) =~ s/^\cS//; |
| 2088 | return $self->maybe_parens("$name $kid", $cx, 16); |
| 2089 | } |
| 2090 | return $self->maybe_parens_unop($name, $op->first, $cx); |
| 2091 | } elsif (class($op) =~ /^(SV|PAD)OP$/) { |
| 2092 | return $self->maybe_parens_func($name, $self->pp_gv($op, 1), $cx, 16); |
| 2093 | } else { # I don't think baseop filetests ever survive ck_ftst, but... |
| 2094 | return $name; |
| 2095 | } |
| 2096 | } |
| 2097 | |
| 2098 | sub pp_lstat { ftst(@_, "lstat") } |
| 2099 | sub pp_stat { ftst(@_, "stat") } |
| 2100 | sub pp_ftrread { ftst(@_, "-R") } |
| 2101 | sub pp_ftrwrite { ftst(@_, "-W") } |
| 2102 | sub pp_ftrexec { ftst(@_, "-X") } |
| 2103 | sub pp_fteread { ftst(@_, "-r") } |
| 2104 | sub pp_ftewrite { ftst(@_, "-w") } |
| 2105 | sub pp_fteexec { ftst(@_, "-x") } |
| 2106 | sub pp_ftis { ftst(@_, "-e") } |
| 2107 | sub pp_fteowned { ftst(@_, "-O") } |
| 2108 | sub pp_ftrowned { ftst(@_, "-o") } |
| 2109 | sub pp_ftzero { ftst(@_, "-z") } |
| 2110 | sub pp_ftsize { ftst(@_, "-s") } |
| 2111 | sub pp_ftmtime { ftst(@_, "-M") } |
| 2112 | sub pp_ftatime { ftst(@_, "-A") } |
| 2113 | sub pp_ftctime { ftst(@_, "-C") } |
| 2114 | sub pp_ftsock { ftst(@_, "-S") } |
| 2115 | sub pp_ftchr { ftst(@_, "-c") } |
| 2116 | sub pp_ftblk { ftst(@_, "-b") } |
| 2117 | sub pp_ftfile { ftst(@_, "-f") } |
| 2118 | sub pp_ftdir { ftst(@_, "-d") } |
| 2119 | sub pp_ftpipe { ftst(@_, "-p") } |
| 2120 | sub pp_ftlink { ftst(@_, "-l") } |
| 2121 | sub pp_ftsuid { ftst(@_, "-u") } |
| 2122 | sub pp_ftsgid { ftst(@_, "-g") } |
| 2123 | sub pp_ftsvtx { ftst(@_, "-k") } |
| 2124 | sub pp_fttty { ftst(@_, "-t") } |
| 2125 | sub pp_fttext { ftst(@_, "-T") } |
| 2126 | sub pp_ftbinary { ftst(@_, "-B") } |
| 2127 | |
| 2128 | sub SWAP_CHILDREN () { 1 } |
| 2129 | sub ASSIGN () { 2 } # has OP= variant |
| 2130 | sub LIST_CONTEXT () { 4 } # Assignment is in list context |
| 2131 | |
| 2132 | my(%left, %right); |
| 2133 | |
| 2134 | sub assoc_class { |
| 2135 | my $op = shift; |
| 2136 | my $name = $op->name; |
| 2137 | if ($name eq "concat" and $op->first->name eq "concat") { |
| 2138 | # avoid spurious '=' -- see comment in pp_concat |
| 2139 | return "concat"; |
| 2140 | } |
| 2141 | if ($name eq "null" and class($op) eq "UNOP" |
| 2142 | and $op->first->name =~ /^(and|x?or)$/ |
| 2143 | and null $op->first->sibling) |
| 2144 | { |
| 2145 | # Like all conditional constructs, OP_ANDs and OP_ORs are topped |
| 2146 | # with a null that's used as the common end point of the two |
| 2147 | # flows of control. For precedence purposes, ignore it. |
| 2148 | # (COND_EXPRs have these too, but we don't bother with |
| 2149 | # their associativity). |
| 2150 | return assoc_class($op->first); |
| 2151 | } |
| 2152 | return $name . ($op->flags & OPf_STACKED ? "=" : ""); |
| 2153 | } |
| 2154 | |
| 2155 | # Left associative operators, like '+', for which |
| 2156 | # $a + $b + $c is equivalent to ($a + $b) + $c |
| 2157 | |
| 2158 | BEGIN { |
| 2159 | %left = ('multiply' => 19, 'i_multiply' => 19, |
| 2160 | 'divide' => 19, 'i_divide' => 19, |
| 2161 | 'modulo' => 19, 'i_modulo' => 19, |
| 2162 | 'repeat' => 19, |
| 2163 | 'add' => 18, 'i_add' => 18, |
| 2164 | 'subtract' => 18, 'i_subtract' => 18, |
| 2165 | 'concat' => 18, |
| 2166 | 'left_shift' => 17, 'right_shift' => 17, |
| 2167 | 'bit_and' => 13, |
| 2168 | 'bit_or' => 12, 'bit_xor' => 12, |
| 2169 | 'and' => 3, |
| 2170 | 'or' => 2, 'xor' => 2, |
| 2171 | ); |
| 2172 | } |
| 2173 | |
| 2174 | sub deparse_binop_left { |
| 2175 | my $self = shift; |
| 2176 | my($op, $left, $prec) = @_; |
| 2177 | if ($left{assoc_class($op)} && $left{assoc_class($left)} |
| 2178 | and $left{assoc_class($op)} == $left{assoc_class($left)}) |
| 2179 | { |
| 2180 | return $self->deparse($left, $prec - .00001); |
| 2181 | } else { |
| 2182 | return $self->deparse($left, $prec); |
| 2183 | } |
| 2184 | } |
| 2185 | |
| 2186 | # Right associative operators, like '=', for which |
| 2187 | # $a = $b = $c is equivalent to $a = ($b = $c) |
| 2188 | |
| 2189 | BEGIN { |
| 2190 | %right = ('pow' => 22, |
| 2191 | 'sassign=' => 7, 'aassign=' => 7, |
| 2192 | 'multiply=' => 7, 'i_multiply=' => 7, |
| 2193 | 'divide=' => 7, 'i_divide=' => 7, |
| 2194 | 'modulo=' => 7, 'i_modulo=' => 7, |
| 2195 | 'repeat=' => 7, |
| 2196 | 'add=' => 7, 'i_add=' => 7, |
| 2197 | 'subtract=' => 7, 'i_subtract=' => 7, |
| 2198 | 'concat=' => 7, |
| 2199 | 'left_shift=' => 7, 'right_shift=' => 7, |
| 2200 | 'bit_and=' => 7, |
| 2201 | 'bit_or=' => 7, 'bit_xor=' => 7, |
| 2202 | 'andassign' => 7, |
| 2203 | 'orassign' => 7, |
| 2204 | ); |
| 2205 | } |
| 2206 | |
| 2207 | sub deparse_binop_right { |
| 2208 | my $self = shift; |
| 2209 | my($op, $right, $prec) = @_; |
| 2210 | if ($right{assoc_class($op)} && $right{assoc_class($right)} |
| 2211 | and $right{assoc_class($op)} == $right{assoc_class($right)}) |
| 2212 | { |
| 2213 | return $self->deparse($right, $prec - .00001); |
| 2214 | } else { |
| 2215 | return $self->deparse($right, $prec); |
| 2216 | } |
| 2217 | } |
| 2218 | |
| 2219 | sub binop { |
| 2220 | my $self = shift; |
| 2221 | my ($op, $cx, $opname, $prec, $flags) = (@_, 0); |
| 2222 | my $left = $op->first; |
| 2223 | my $right = $op->last; |
| 2224 | my $eq = ""; |
| 2225 | if ($op->flags & OPf_STACKED && $flags & ASSIGN) { |
| 2226 | $eq = "="; |
| 2227 | $prec = 7; |
| 2228 | } |
| 2229 | if ($flags & SWAP_CHILDREN) { |
| 2230 | ($left, $right) = ($right, $left); |
| 2231 | } |
| 2232 | $left = $self->deparse_binop_left($op, $left, $prec); |
| 2233 | $left = "($left)" if $flags & LIST_CONTEXT |
| 2234 | && $left !~ /^(my|our|local|)[\@\(]/; |
| 2235 | $right = $self->deparse_binop_right($op, $right, $prec); |
| 2236 | return $self->maybe_parens("$left $opname$eq $right", $cx, $prec); |
| 2237 | } |
| 2238 | |
| 2239 | sub pp_add { maybe_targmy(@_, \&binop, "+", 18, ASSIGN) } |
| 2240 | sub pp_multiply { maybe_targmy(@_, \&binop, "*", 19, ASSIGN) } |
| 2241 | sub pp_subtract { maybe_targmy(@_, \&binop, "-",18, ASSIGN) } |
| 2242 | sub pp_divide { maybe_targmy(@_, \&binop, "/", 19, ASSIGN) } |
| 2243 | sub pp_modulo { maybe_targmy(@_, \&binop, "%", 19, ASSIGN) } |
| 2244 | sub pp_i_add { maybe_targmy(@_, \&binop, "+", 18, ASSIGN) } |
| 2245 | sub pp_i_multiply { maybe_targmy(@_, \&binop, "*", 19, ASSIGN) } |
| 2246 | sub pp_i_subtract { maybe_targmy(@_, \&binop, "-", 18, ASSIGN) } |
| 2247 | sub pp_i_divide { maybe_targmy(@_, \&binop, "/", 19, ASSIGN) } |
| 2248 | sub pp_i_modulo { maybe_targmy(@_, \&binop, "%", 19, ASSIGN) } |
| 2249 | sub pp_pow { maybe_targmy(@_, \&binop, "**", 22, ASSIGN) } |
| 2250 | |
| 2251 | sub pp_left_shift { maybe_targmy(@_, \&binop, "<<", 17, ASSIGN) } |
| 2252 | sub pp_right_shift { maybe_targmy(@_, \&binop, ">>", 17, ASSIGN) } |
| 2253 | sub pp_bit_and { maybe_targmy(@_, \&binop, "&", 13, ASSIGN) } |
| 2254 | sub pp_bit_or { maybe_targmy(@_, \&binop, "|", 12, ASSIGN) } |
| 2255 | sub pp_bit_xor { maybe_targmy(@_, \&binop, "^", 12, ASSIGN) } |
| 2256 | |
| 2257 | sub pp_eq { binop(@_, "==", 14) } |
| 2258 | sub pp_ne { binop(@_, "!=", 14) } |
| 2259 | sub pp_lt { binop(@_, "<", 15) } |
| 2260 | sub pp_gt { binop(@_, ">", 15) } |
| 2261 | sub pp_ge { binop(@_, ">=", 15) } |
| 2262 | sub pp_le { binop(@_, "<=", 15) } |
| 2263 | sub pp_ncmp { binop(@_, "<=>", 14) } |
| 2264 | sub pp_i_eq { binop(@_, "==", 14) } |
| 2265 | sub pp_i_ne { binop(@_, "!=", 14) } |
| 2266 | sub pp_i_lt { binop(@_, "<", 15) } |
| 2267 | sub pp_i_gt { binop(@_, ">", 15) } |
| 2268 | sub pp_i_ge { binop(@_, ">=", 15) } |
| 2269 | sub pp_i_le { binop(@_, "<=", 15) } |
| 2270 | sub pp_i_ncmp { binop(@_, "<=>", 14) } |
| 2271 | |
| 2272 | sub pp_seq { binop(@_, "eq", 14) } |
| 2273 | sub pp_sne { binop(@_, "ne", 14) } |
| 2274 | sub pp_slt { binop(@_, "lt", 15) } |
| 2275 | sub pp_sgt { binop(@_, "gt", 15) } |
| 2276 | sub pp_sge { binop(@_, "ge", 15) } |
| 2277 | sub pp_sle { binop(@_, "le", 15) } |
| 2278 | sub pp_scmp { binop(@_, "cmp", 14) } |
| 2279 | |
| 2280 | sub pp_sassign { binop(@_, "=", 7, SWAP_CHILDREN) } |
| 2281 | sub pp_aassign { binop(@_, "=", 7, SWAP_CHILDREN | LIST_CONTEXT) } |
| 2282 | |
| 2283 | sub pp_smartmatch { |
| 2284 | my ($self, $op, $cx) = @_; |
| 2285 | if ($op->flags & OPf_SPECIAL) { |
| 2286 | return $self->deparse($op->last, $cx); |
| 2287 | } |
| 2288 | else { |
| 2289 | binop(@_, "~~", 14); |
| 2290 | } |
| 2291 | } |
| 2292 | |
| 2293 | # '.' is special because concats-of-concats are optimized to save copying |
| 2294 | # by making all but the first concat stacked. The effect is as if the |
| 2295 | # programmer had written '($a . $b) .= $c', except legal. |
| 2296 | sub pp_concat { maybe_targmy(@_, \&real_concat) } |
| 2297 | sub real_concat { |
| 2298 | my $self = shift; |
| 2299 | my($op, $cx) = @_; |
| 2300 | my $left = $op->first; |
| 2301 | my $right = $op->last; |
| 2302 | my $eq = ""; |
| 2303 | my $prec = 18; |
| 2304 | if ($op->flags & OPf_STACKED and $op->first->name ne "concat") { |
| 2305 | $eq = "="; |
| 2306 | $prec = 7; |
| 2307 | } |
| 2308 | $left = $self->deparse_binop_left($op, $left, $prec); |
| 2309 | $right = $self->deparse_binop_right($op, $right, $prec); |
| 2310 | return $self->maybe_parens("$left .$eq $right", $cx, $prec); |
| 2311 | } |
| 2312 | |
| 2313 | # 'x' is weird when the left arg is a list |
| 2314 | sub pp_repeat { |
| 2315 | my $self = shift; |
| 2316 | my($op, $cx) = @_; |
| 2317 | my $left = $op->first; |
| 2318 | my $right = $op->last; |
| 2319 | my $eq = ""; |
| 2320 | my $prec = 19; |
| 2321 | if ($op->flags & OPf_STACKED) { |
| 2322 | $eq = "="; |
| 2323 | $prec = 7; |
| 2324 | } |
| 2325 | if (null($right)) { # list repeat; count is inside left-side ex-list |
| 2326 | my $kid = $left->first->sibling; # skip pushmark |
| 2327 | my @exprs; |
| 2328 | for (; !null($kid->sibling); $kid = $kid->sibling) { |
| 2329 | push @exprs, $self->deparse($kid, 6); |
| 2330 | } |
| 2331 | $right = $kid; |
| 2332 | $left = "(" . join(", ", @exprs). ")"; |
| 2333 | } else { |
| 2334 | $left = $self->deparse_binop_left($op, $left, $prec); |
| 2335 | } |
| 2336 | $right = $self->deparse_binop_right($op, $right, $prec); |
| 2337 | return $self->maybe_parens("$left x$eq $right", $cx, $prec); |
| 2338 | } |
| 2339 | |
| 2340 | sub range { |
| 2341 | my $self = shift; |
| 2342 | my ($op, $cx, $type) = @_; |
| 2343 | my $left = $op->first; |
| 2344 | my $right = $left->sibling; |
| 2345 | $left = $self->deparse($left, 9); |
| 2346 | $right = $self->deparse($right, 9); |
| 2347 | return $self->maybe_parens("$left $type $right", $cx, 9); |
| 2348 | } |
| 2349 | |
| 2350 | sub pp_flop { |
| 2351 | my $self = shift; |
| 2352 | my($op, $cx) = @_; |
| 2353 | my $flip = $op->first; |
| 2354 | my $type = ($flip->flags & OPf_SPECIAL) ? "..." : ".."; |
| 2355 | return $self->range($flip->first, $cx, $type); |
| 2356 | } |
| 2357 | |
| 2358 | # one-line while/until is handled in pp_leave |
| 2359 | |
| 2360 | sub logop { |
| 2361 | my $self = shift; |
| 2362 | my ($op, $cx, $lowop, $lowprec, $highop, $highprec, $blockname) = @_; |
| 2363 | my $left = $op->first; |
| 2364 | my $right = $op->first->sibling; |
| 2365 | if ($cx < 1 and is_scope($right) and $blockname |
| 2366 | and $self->{'expand'} < 7) |
| 2367 | { # if ($a) {$b} |
| 2368 | $left = $self->deparse($left, 1); |
| 2369 | $right = $self->deparse($right, 0); |
| 2370 | return "$blockname ($left) {\n\t$right\n\b}\cK"; |
| 2371 | } elsif ($cx < 1 and $blockname and not $self->{'parens'} |
| 2372 | and $self->{'expand'} < 7) { # $b if $a |
| 2373 | $right = $self->deparse($right, 1); |
| 2374 | $left = $self->deparse($left, 1); |
| 2375 | return "$right $blockname $left"; |
| 2376 | } elsif ($cx > $lowprec and $highop) { # $a && $b |
| 2377 | $left = $self->deparse_binop_left($op, $left, $highprec); |
| 2378 | $right = $self->deparse_binop_right($op, $right, $highprec); |
| 2379 | return $self->maybe_parens("$left $highop $right", $cx, $highprec); |
| 2380 | } else { # $a and $b |
| 2381 | $left = $self->deparse_binop_left($op, $left, $lowprec); |
| 2382 | $right = $self->deparse_binop_right($op, $right, $lowprec); |
| 2383 | return $self->maybe_parens("$left $lowop $right", $cx, $lowprec); |
| 2384 | } |
| 2385 | } |
| 2386 | |
| 2387 | sub pp_and { logop(@_, "and", 3, "&&", 11, "if") } |
| 2388 | sub pp_or { logop(@_, "or", 2, "||", 10, "unless") } |
| 2389 | sub pp_dor { logop(@_, "//", 10) } |
| 2390 | |
| 2391 | # xor is syntactically a logop, but it's really a binop (contrary to |
| 2392 | # old versions of opcode.pl). Syntax is what matters here. |
| 2393 | sub pp_xor { logop(@_, "xor", 2, "", 0, "") } |
| 2394 | |
| 2395 | sub logassignop { |
| 2396 | my $self = shift; |
| 2397 | my ($op, $cx, $opname) = @_; |
| 2398 | my $left = $op->first; |
| 2399 | my $right = $op->first->sibling->first; # skip sassign |
| 2400 | $left = $self->deparse($left, 7); |
| 2401 | $right = $self->deparse($right, 7); |
| 2402 | return $self->maybe_parens("$left $opname $right", $cx, 7); |
| 2403 | } |
| 2404 | |
| 2405 | sub pp_andassign { logassignop(@_, "&&=") } |
| 2406 | sub pp_orassign { logassignop(@_, "||=") } |
| 2407 | sub pp_dorassign { logassignop(@_, "//=") } |
| 2408 | |
| 2409 | sub rv2gv_or_string { |
| 2410 | my($self,$op) = @_; |
| 2411 | if ($op->name eq "gv") { # could be open("open") or open("###") |
| 2412 | my($name,$quoted) = |
| 2413 | $self->stash_variable_name(undef,$self->gv_or_padgv($op)); |
| 2414 | $quoted ? $name : "*$name"; |
| 2415 | } |
| 2416 | else { |
| 2417 | $self->deparse($op, 6); |
| 2418 | } |
| 2419 | } |
| 2420 | |
| 2421 | sub listop { |
| 2422 | my $self = shift; |
| 2423 | my($op, $cx, $name, $kid, $nollafr) = @_; |
| 2424 | my(@exprs); |
| 2425 | my $parens = ($cx >= 5) || $self->{'parens'}; |
| 2426 | $kid ||= $op->first->sibling; |
| 2427 | # If there are no arguments, add final parentheses (or parenthesize the |
| 2428 | # whole thing if the llafr does not apply) to account for cases like |
| 2429 | # (return)+1 or setpgrp()+1. When the llafr does not apply, we use a |
| 2430 | # precedence of 6 (< comma), as "return, 1" does not need parentheses. |
| 2431 | if (null $kid) { |
| 2432 | return $nollafr |
| 2433 | ? $self->maybe_parens($self->keyword($name), $cx, 7) |
| 2434 | : $self->keyword($name) . '()' x (7 < $cx); |
| 2435 | } |
| 2436 | my $first; |
| 2437 | $name = "socketpair" if $name eq "sockpair"; |
| 2438 | my $fullname = $self->keyword($name); |
| 2439 | my $proto = prototype("CORE::$name"); |
| 2440 | if (defined $proto |
| 2441 | && $proto =~ /^;?\*/ |
| 2442 | && $kid->name eq "rv2gv" && !($kid->private & OPpLVAL_INTRO)) { |
| 2443 | $first = $self->rv2gv_or_string($kid->first); |
| 2444 | } |
| 2445 | else { |
| 2446 | $first = $self->deparse($kid, 6); |
| 2447 | } |
| 2448 | if ($name eq "chmod" && $first =~ /^\d+$/) { |
| 2449 | $first = sprintf("%#o", $first); |
| 2450 | } |
| 2451 | $first = "+$first" |
| 2452 | if not $parens and not $nollafr and substr($first, 0, 1) eq "("; |
| 2453 | push @exprs, $first; |
| 2454 | $kid = $kid->sibling; |
| 2455 | if (defined $proto && $proto =~ /^\*\*/ && $kid->name eq "rv2gv" |
| 2456 | && !($kid->private & OPpLVAL_INTRO)) { |
| 2457 | push @exprs, $first = $self->rv2gv_or_string($kid->first); |
| 2458 | $kid = $kid->sibling; |
| 2459 | } |
| 2460 | for (; !null($kid); $kid = $kid->sibling) { |
| 2461 | push @exprs, $self->deparse($kid, 6); |
| 2462 | } |
| 2463 | if ($name eq "reverse" && ($op->private & OPpREVERSE_INPLACE)) { |
| 2464 | return "$exprs[0] = $fullname" |
| 2465 | . ($parens ? "($exprs[0])" : " $exprs[0]"); |
| 2466 | } |
| 2467 | if ($parens && $nollafr) { |
| 2468 | return "($fullname " . join(", ", @exprs) . ")"; |
| 2469 | } elsif ($parens) { |
| 2470 | return "$fullname(" . join(", ", @exprs) . ")"; |
| 2471 | } else { |
| 2472 | return "$fullname " . join(", ", @exprs); |
| 2473 | } |
| 2474 | } |
| 2475 | |
| 2476 | sub pp_bless { listop(@_, "bless") } |
| 2477 | sub pp_atan2 { maybe_targmy(@_, \&listop, "atan2") } |
| 2478 | sub pp_substr { |
| 2479 | my ($self,$op,$cx) = @_; |
| 2480 | if ($op->private & OPpSUBSTR_REPL_FIRST) { |
| 2481 | return |
| 2482 | listop($self, $op, 7, "substr", $op->first->sibling->sibling) |
| 2483 | . " = " |
| 2484 | . $self->deparse($op->first->sibling, 7); |
| 2485 | } |
| 2486 | maybe_local(@_, listop(@_, "substr")) |
| 2487 | } |
| 2488 | sub pp_vec { maybe_local(@_, listop(@_, "vec")) } |
| 2489 | sub pp_index { maybe_targmy(@_, \&listop, "index") } |
| 2490 | sub pp_rindex { maybe_targmy(@_, \&listop, "rindex") } |
| 2491 | sub pp_sprintf { maybe_targmy(@_, \&listop, "sprintf") } |
| 2492 | sub pp_formline { listop(@_, "formline") } # see also deparse_format |
| 2493 | sub pp_crypt { maybe_targmy(@_, \&listop, "crypt") } |
| 2494 | sub pp_unpack { listop(@_, "unpack") } |
| 2495 | sub pp_pack { listop(@_, "pack") } |
| 2496 | sub pp_join { maybe_targmy(@_, \&listop, "join") } |
| 2497 | sub pp_splice { listop(@_, "splice") } |
| 2498 | sub pp_push { maybe_targmy(@_, \&listop, "push") } |
| 2499 | sub pp_unshift { maybe_targmy(@_, \&listop, "unshift") } |
| 2500 | sub pp_reverse { listop(@_, "reverse") } |
| 2501 | sub pp_warn { listop(@_, "warn") } |
| 2502 | sub pp_die { listop(@_, "die") } |
| 2503 | sub pp_return { listop(@_, "return", undef, 1) } # llafr does not apply |
| 2504 | sub pp_open { listop(@_, "open") } |
| 2505 | sub pp_pipe_op { listop(@_, "pipe") } |
| 2506 | sub pp_tie { listop(@_, "tie") } |
| 2507 | sub pp_binmode { listop(@_, "binmode") } |
| 2508 | sub pp_dbmopen { listop(@_, "dbmopen") } |
| 2509 | sub pp_sselect { listop(@_, "select") } |
| 2510 | sub pp_select { listop(@_, "select") } |
| 2511 | sub pp_read { listop(@_, "read") } |
| 2512 | sub pp_sysopen { listop(@_, "sysopen") } |
| 2513 | sub pp_sysseek { listop(@_, "sysseek") } |
| 2514 | sub pp_sysread { listop(@_, "sysread") } |
| 2515 | sub pp_syswrite { listop(@_, "syswrite") } |
| 2516 | sub pp_send { listop(@_, "send") } |
| 2517 | sub pp_recv { listop(@_, "recv") } |
| 2518 | sub pp_seek { listop(@_, "seek") } |
| 2519 | sub pp_fcntl { listop(@_, "fcntl") } |
| 2520 | sub pp_ioctl { listop(@_, "ioctl") } |
| 2521 | sub pp_flock { maybe_targmy(@_, \&listop, "flock") } |
| 2522 | sub pp_socket { listop(@_, "socket") } |
| 2523 | sub pp_sockpair { listop(@_, "sockpair") } |
| 2524 | sub pp_bind { listop(@_, "bind") } |
| 2525 | sub pp_connect { listop(@_, "connect") } |
| 2526 | sub pp_listen { listop(@_, "listen") } |
| 2527 | sub pp_accept { listop(@_, "accept") } |
| 2528 | sub pp_shutdown { listop(@_, "shutdown") } |
| 2529 | sub pp_gsockopt { listop(@_, "getsockopt") } |
| 2530 | sub pp_ssockopt { listop(@_, "setsockopt") } |
| 2531 | sub pp_chown { maybe_targmy(@_, \&listop, "chown") } |
| 2532 | sub pp_unlink { maybe_targmy(@_, \&listop, "unlink") } |
| 2533 | sub pp_chmod { maybe_targmy(@_, \&listop, "chmod") } |
| 2534 | sub pp_utime { maybe_targmy(@_, \&listop, "utime") } |
| 2535 | sub pp_rename { maybe_targmy(@_, \&listop, "rename") } |
| 2536 | sub pp_link { maybe_targmy(@_, \&listop, "link") } |
| 2537 | sub pp_symlink { maybe_targmy(@_, \&listop, "symlink") } |
| 2538 | sub pp_mkdir { maybe_targmy(@_, \&listop, "mkdir") } |
| 2539 | sub pp_open_dir { listop(@_, "opendir") } |
| 2540 | sub pp_seekdir { listop(@_, "seekdir") } |
| 2541 | sub pp_waitpid { maybe_targmy(@_, \&listop, "waitpid") } |
| 2542 | sub pp_system { maybe_targmy(@_, \&listop, "system") } |
| 2543 | sub pp_exec { maybe_targmy(@_, \&listop, "exec") } |
| 2544 | sub pp_kill { maybe_targmy(@_, \&listop, "kill") } |
| 2545 | sub pp_setpgrp { maybe_targmy(@_, \&listop, "setpgrp") } |
| 2546 | sub pp_getpriority { maybe_targmy(@_, \&listop, "getpriority") } |
| 2547 | sub pp_setpriority { maybe_targmy(@_, \&listop, "setpriority") } |
| 2548 | sub pp_shmget { listop(@_, "shmget") } |
| 2549 | sub pp_shmctl { listop(@_, "shmctl") } |
| 2550 | sub pp_shmread { listop(@_, "shmread") } |
| 2551 | sub pp_shmwrite { listop(@_, "shmwrite") } |
| 2552 | sub pp_msgget { listop(@_, "msgget") } |
| 2553 | sub pp_msgctl { listop(@_, "msgctl") } |
| 2554 | sub pp_msgsnd { listop(@_, "msgsnd") } |
| 2555 | sub pp_msgrcv { listop(@_, "msgrcv") } |
| 2556 | sub pp_semget { listop(@_, "semget") } |
| 2557 | sub pp_semctl { listop(@_, "semctl") } |
| 2558 | sub pp_semop { listop(@_, "semop") } |
| 2559 | sub pp_ghbyaddr { listop(@_, "gethostbyaddr") } |
| 2560 | sub pp_gnbyaddr { listop(@_, "getnetbyaddr") } |
| 2561 | sub pp_gpbynumber { listop(@_, "getprotobynumber") } |
| 2562 | sub pp_gsbyname { listop(@_, "getservbyname") } |
| 2563 | sub pp_gsbyport { listop(@_, "getservbyport") } |
| 2564 | sub pp_syscall { listop(@_, "syscall") } |
| 2565 | |
| 2566 | sub pp_glob { |
| 2567 | my $self = shift; |
| 2568 | my($op, $cx) = @_; |
| 2569 | my $text = $self->dq($op->first->sibling); # skip pushmark |
| 2570 | my $keyword = |
| 2571 | $op->flags & OPf_SPECIAL ? 'glob' : $self->keyword('glob'); |
| 2572 | if ($text =~ /^\$?(\w|::|\`)+$/ # could look like a readline |
| 2573 | or $keyword =~ /^CORE::/ |
| 2574 | or $text =~ /[<>]/) { |
| 2575 | return "$keyword(" . single_delim('qq', '"', $text) . ')'; |
| 2576 | } else { |
| 2577 | return '<' . $text . '>'; |
| 2578 | } |
| 2579 | } |
| 2580 | |
| 2581 | # Truncate is special because OPf_SPECIAL makes a bareword first arg |
| 2582 | # be a filehandle. This could probably be better fixed in the core |
| 2583 | # by moving the GV lookup into ck_truc. |
| 2584 | |
| 2585 | sub pp_truncate { |
| 2586 | my $self = shift; |
| 2587 | my($op, $cx) = @_; |
| 2588 | my(@exprs); |
| 2589 | my $parens = ($cx >= 5) || $self->{'parens'}; |
| 2590 | my $kid = $op->first->sibling; |
| 2591 | my $fh; |
| 2592 | if ($op->flags & OPf_SPECIAL) { |
| 2593 | # $kid is an OP_CONST |
| 2594 | $fh = $self->const_sv($kid)->PV; |
| 2595 | } else { |
| 2596 | $fh = $self->deparse($kid, 6); |
| 2597 | $fh = "+$fh" if not $parens and substr($fh, 0, 1) eq "("; |
| 2598 | } |
| 2599 | my $len = $self->deparse($kid->sibling, 6); |
| 2600 | my $name = $self->keyword('truncate'); |
| 2601 | if ($parens) { |
| 2602 | return "$name($fh, $len)"; |
| 2603 | } else { |
| 2604 | return "$name $fh, $len"; |
| 2605 | } |
| 2606 | } |
| 2607 | |
| 2608 | sub indirop { |
| 2609 | my $self = shift; |
| 2610 | my($op, $cx, $name) = @_; |
| 2611 | my($expr, @exprs); |
| 2612 | my $firstkid = my $kid = $op->first->sibling; |
| 2613 | my $indir = ""; |
| 2614 | if ($op->flags & OPf_STACKED) { |
| 2615 | $indir = $kid; |
| 2616 | $indir = $indir->first; # skip rv2gv |
| 2617 | if (is_scope($indir)) { |
| 2618 | $indir = "{" . $self->deparse($indir, 0) . "}"; |
| 2619 | $indir = "{;}" if $indir eq "{}"; |
| 2620 | } elsif ($indir->name eq "const" && $indir->private & OPpCONST_BARE) { |
| 2621 | $indir = $self->const_sv($indir)->PV; |
| 2622 | } else { |
| 2623 | $indir = $self->deparse($indir, 24); |
| 2624 | } |
| 2625 | $indir = $indir . " "; |
| 2626 | $kid = $kid->sibling; |
| 2627 | } |
| 2628 | if ($name eq "sort" && $op->private & (OPpSORT_NUMERIC | OPpSORT_INTEGER)) { |
| 2629 | $indir = ($op->private & OPpSORT_DESCEND) ? '{$b <=> $a} ' |
| 2630 | : '{$a <=> $b} '; |
| 2631 | } |
| 2632 | elsif ($name eq "sort" && $op->private & OPpSORT_DESCEND) { |
| 2633 | $indir = '{$b cmp $a} '; |
| 2634 | } |
| 2635 | for (; !null($kid); $kid = $kid->sibling) { |
| 2636 | $expr = $self->deparse($kid, !$indir && $kid == $firstkid && $name eq "sort" && $firstkid->name eq "entersub" ? 16 : 6); |
| 2637 | push @exprs, $expr; |
| 2638 | } |
| 2639 | my $name2; |
| 2640 | if ($name eq "sort" && $op->private & OPpSORT_REVERSE) { |
| 2641 | $name2 = $self->keyword('reverse') . ' ' . $self->keyword('sort'); |
| 2642 | } |
| 2643 | else { $name2 = $self->keyword($name) } |
| 2644 | if ($name eq "sort" && ($op->private & OPpSORT_INPLACE)) { |
| 2645 | return "$exprs[0] = $name2 $indir $exprs[0]"; |
| 2646 | } |
| 2647 | |
| 2648 | my $args = $indir . join(", ", @exprs); |
| 2649 | if ($indir ne "" && $name eq "sort") { |
| 2650 | # We don't want to say "sort(f 1, 2, 3)", since perl -w will |
| 2651 | # give bareword warnings in that case. Therefore if context |
| 2652 | # requires, we'll put parens around the outside "(sort f 1, 2, |
| 2653 | # 3)". Unfortunately, we'll currently think the parens are |
| 2654 | # necessary more often that they really are, because we don't |
| 2655 | # distinguish which side of an assignment we're on. |
| 2656 | if ($cx >= 5) { |
| 2657 | return "($name2 $args)"; |
| 2658 | } else { |
| 2659 | return "$name2 $args"; |
| 2660 | } |
| 2661 | } elsif ( |
| 2662 | !$indir && $name eq "sort" |
| 2663 | && $op->first->sibling->name eq 'entersub' |
| 2664 | ) { |
| 2665 | # We cannot say sort foo(bar), as foo will be interpreted as a |
| 2666 | # comparison routine. We have to say sort(...) in that case. |
| 2667 | return "$name2($args)"; |
| 2668 | } else { |
| 2669 | return $self->maybe_parens_func($name2, $args, $cx, 5); |
| 2670 | } |
| 2671 | |
| 2672 | } |
| 2673 | |
| 2674 | sub pp_prtf { indirop(@_, "printf") } |
| 2675 | sub pp_print { indirop(@_, "print") } |
| 2676 | sub pp_say { indirop(@_, "say") } |
| 2677 | sub pp_sort { indirop(@_, "sort") } |
| 2678 | |
| 2679 | sub mapop { |
| 2680 | my $self = shift; |
| 2681 | my($op, $cx, $name) = @_; |
| 2682 | my($expr, @exprs); |
| 2683 | my $kid = $op->first; # this is the (map|grep)start |
| 2684 | $kid = $kid->first->sibling; # skip a pushmark |
| 2685 | my $code = $kid->first; # skip a null |
| 2686 | if (is_scope $code) { |
| 2687 | $code = "{" . $self->deparse($code, 0) . "} "; |
| 2688 | } else { |
| 2689 | $code = $self->deparse($code, 24) . ", "; |
| 2690 | } |
| 2691 | $kid = $kid->sibling; |
| 2692 | for (; !null($kid); $kid = $kid->sibling) { |
| 2693 | $expr = $self->deparse($kid, 6); |
| 2694 | push @exprs, $expr if defined $expr; |
| 2695 | } |
| 2696 | return $self->maybe_parens_func($name, $code . join(", ", @exprs), $cx, 5); |
| 2697 | } |
| 2698 | |
| 2699 | sub pp_mapwhile { mapop(@_, "map") } |
| 2700 | sub pp_grepwhile { mapop(@_, "grep") } |
| 2701 | sub pp_mapstart { baseop(@_, "map") } |
| 2702 | sub pp_grepstart { baseop(@_, "grep") } |
| 2703 | |
| 2704 | sub pp_list { |
| 2705 | my $self = shift; |
| 2706 | my($op, $cx) = @_; |
| 2707 | my($expr, @exprs); |
| 2708 | my $kid = $op->first->sibling; # skip pushmark |
| 2709 | return '' if class($kid) eq 'NULL'; |
| 2710 | my $lop; |
| 2711 | my $local = "either"; # could be local(...), my(...), state(...) or our(...) |
| 2712 | for ($lop = $kid; !null($lop); $lop = $lop->sibling) { |
| 2713 | # This assumes that no other private flags equal 128, and that |
| 2714 | # OPs that store things other than flags in their op_private, |
| 2715 | # like OP_AELEMFAST, won't be immediate children of a list. |
| 2716 | # |
| 2717 | # OP_ENTERSUB can break this logic, so check for it. |
| 2718 | # I suspect that open and exit can too. |
| 2719 | |
| 2720 | if (!($lop->private & (OPpLVAL_INTRO|OPpOUR_INTRO) |
| 2721 | or $lop->name eq "undef") |
| 2722 | or $lop->name eq "entersub" |
| 2723 | or $lop->name eq "exit" |
| 2724 | or $lop->name eq "open") |
| 2725 | { |
| 2726 | $local = ""; # or not |
| 2727 | last; |
| 2728 | } |
| 2729 | if ($lop->name =~ /^pad[ash]v$/) { |
| 2730 | if ($lop->private & OPpPAD_STATE) { # state() |
| 2731 | ($local = "", last) if $local =~ /^(?:local|our|my)$/; |
| 2732 | $local = "state"; |
| 2733 | } else { # my() |
| 2734 | ($local = "", last) if $local =~ /^(?:local|our|state)$/; |
| 2735 | $local = "my"; |
| 2736 | } |
| 2737 | } elsif ($lop->name =~ /^(gv|rv2)[ash]v$/ |
| 2738 | && $lop->private & OPpOUR_INTRO |
| 2739 | or $lop->name eq "null" && $lop->first->name eq "gvsv" |
| 2740 | && $lop->first->private & OPpOUR_INTRO) { # our() |
| 2741 | ($local = "", last) if $local =~ /^(?:my|local|state)$/; |
| 2742 | $local = "our"; |
| 2743 | } elsif ($lop->name ne "undef" |
| 2744 | # specifically avoid the "reverse sort" optimisation, |
| 2745 | # where "reverse" is nullified |
| 2746 | && !($lop->name eq 'sort' && ($lop->flags & OPpSORT_REVERSE))) |
| 2747 | { |
| 2748 | # local() |
| 2749 | ($local = "", last) if $local =~ /^(?:my|our|state)$/; |
| 2750 | $local = "local"; |
| 2751 | } |
| 2752 | } |
| 2753 | $local = "" if $local eq "either"; # no point if it's all undefs |
| 2754 | return $self->deparse($kid, $cx) if null $kid->sibling and not $local; |
| 2755 | for (; !null($kid); $kid = $kid->sibling) { |
| 2756 | if ($local) { |
| 2757 | if (class($kid) eq "UNOP" and $kid->first->name eq "gvsv") { |
| 2758 | $lop = $kid->first; |
| 2759 | } else { |
| 2760 | $lop = $kid; |
| 2761 | } |
| 2762 | $self->{'avoid_local'}{$$lop}++; |
| 2763 | $expr = $self->deparse($kid, 6); |
| 2764 | delete $self->{'avoid_local'}{$$lop}; |
| 2765 | } else { |
| 2766 | $expr = $self->deparse($kid, 6); |
| 2767 | } |
| 2768 | push @exprs, $expr; |
| 2769 | } |
| 2770 | if ($local) { |
| 2771 | return "$local(" . join(", ", @exprs) . ")"; |
| 2772 | } else { |
| 2773 | return $self->maybe_parens( join(", ", @exprs), $cx, 6); |
| 2774 | } |
| 2775 | } |
| 2776 | |
| 2777 | sub is_ifelse_cont { |
| 2778 | my $op = shift; |
| 2779 | return ($op->name eq "null" and class($op) eq "UNOP" |
| 2780 | and $op->first->name =~ /^(and|cond_expr)$/ |
| 2781 | and is_scope($op->first->first->sibling)); |
| 2782 | } |
| 2783 | |
| 2784 | sub pp_cond_expr { |
| 2785 | my $self = shift; |
| 2786 | my($op, $cx) = @_; |
| 2787 | my $cond = $op->first; |
| 2788 | my $true = $cond->sibling; |
| 2789 | my $false = $true->sibling; |
| 2790 | my $cuddle = $self->{'cuddle'}; |
| 2791 | unless ($cx < 1 and (is_scope($true) and $true->name ne "null") and |
| 2792 | (is_scope($false) || is_ifelse_cont($false)) |
| 2793 | and $self->{'expand'} < 7) { |
| 2794 | $cond = $self->deparse($cond, 8); |
| 2795 | $true = $self->deparse($true, 6); |
| 2796 | $false = $self->deparse($false, 8); |
| 2797 | return $self->maybe_parens("$cond ? $true : $false", $cx, 8); |
| 2798 | } |
| 2799 | |
| 2800 | $cond = $self->deparse($cond, 1); |
| 2801 | $true = $self->deparse($true, 0); |
| 2802 | my $head = "if ($cond) {\n\t$true\n\b}"; |
| 2803 | my @elsifs; |
| 2804 | while (!null($false) and is_ifelse_cont($false)) { |
| 2805 | my $newop = $false->first; |
| 2806 | my $newcond = $newop->first; |
| 2807 | my $newtrue = $newcond->sibling; |
| 2808 | $false = $newtrue->sibling; # last in chain is OP_AND => no else |
| 2809 | if ($newcond->name eq "lineseq") |
| 2810 | { |
| 2811 | # lineseq to ensure correct line numbers in elsif() |
| 2812 | # Bug #37302 fixed by change #33710. |
| 2813 | $newcond = $newcond->first->sibling; |
| 2814 | } |
| 2815 | $newcond = $self->deparse($newcond, 1); |
| 2816 | $newtrue = $self->deparse($newtrue, 0); |
| 2817 | push @elsifs, "elsif ($newcond) {\n\t$newtrue\n\b}"; |
| 2818 | } |
| 2819 | if (!null($false)) { |
| 2820 | $false = $cuddle . "else {\n\t" . |
| 2821 | $self->deparse($false, 0) . "\n\b}\cK"; |
| 2822 | } else { |
| 2823 | $false = "\cK"; |
| 2824 | } |
| 2825 | return $head . join($cuddle, "", @elsifs) . $false; |
| 2826 | } |
| 2827 | |
| 2828 | sub pp_once { |
| 2829 | my ($self, $op, $cx) = @_; |
| 2830 | my $cond = $op->first; |
| 2831 | my $true = $cond->sibling; |
| 2832 | |
| 2833 | return $self->deparse($true, $cx); |
| 2834 | } |
| 2835 | |
| 2836 | sub loop_common { |
| 2837 | my $self = shift; |
| 2838 | my($op, $cx, $init) = @_; |
| 2839 | my $enter = $op->first; |
| 2840 | my $kid = $enter->sibling; |
| 2841 | local(@$self{qw'curstash warnings hints hinthash'}) |
| 2842 | = @$self{qw'curstash warnings hints hinthash'}; |
| 2843 | my $head = ""; |
| 2844 | my $bare = 0; |
| 2845 | my $body; |
| 2846 | my $cond = undef; |
| 2847 | if ($kid->name eq "lineseq") { # bare or infinite loop |
| 2848 | if ($kid->last->name eq "unstack") { # infinite |
| 2849 | $head = "while (1) "; # Can't use for(;;) if there's a continue |
| 2850 | $cond = ""; |
| 2851 | } else { |
| 2852 | $bare = 1; |
| 2853 | } |
| 2854 | $body = $kid; |
| 2855 | } elsif ($enter->name eq "enteriter") { # foreach |
| 2856 | my $ary = $enter->first->sibling; # first was pushmark |
| 2857 | my $var = $ary->sibling; |
| 2858 | if ($ary->name eq 'null' and $enter->private & OPpITER_REVERSED) { |
| 2859 | # "reverse" was optimised away |
| 2860 | $ary = listop($self, $ary->first->sibling, 1, 'reverse'); |
| 2861 | } elsif ($enter->flags & OPf_STACKED |
| 2862 | and not null $ary->first->sibling->sibling) |
| 2863 | { |
| 2864 | $ary = $self->deparse($ary->first->sibling, 9) . " .. " . |
| 2865 | $self->deparse($ary->first->sibling->sibling, 9); |
| 2866 | } else { |
| 2867 | $ary = $self->deparse($ary, 1); |
| 2868 | } |
| 2869 | if (null $var) { |
| 2870 | if (($enter->flags & OPf_SPECIAL) && ($] < 5.009)) { |
| 2871 | # thread special var, under 5005threads |
| 2872 | $var = $self->pp_threadsv($enter, 1); |
| 2873 | } else { # regular my() variable |
| 2874 | $var = $self->pp_padsv($enter, 1); |
| 2875 | } |
| 2876 | } elsif ($var->name eq "rv2gv") { |
| 2877 | $var = $self->pp_rv2sv($var, 1); |
| 2878 | if ($enter->private & OPpOUR_INTRO) { |
| 2879 | # our declarations don't have package names |
| 2880 | $var =~ s/^(.).*::/$1/; |
| 2881 | $var = "our $var"; |
| 2882 | } |
| 2883 | } elsif ($var->name eq "gv") { |
| 2884 | $var = "\$" . $self->deparse($var, 1); |
| 2885 | } |
| 2886 | $body = $kid->first->first->sibling; # skip OP_AND and OP_ITER |
| 2887 | if (!is_state $body->first and $body->first->name ne "stub") { |
| 2888 | confess unless $var eq '$_'; |
| 2889 | $body = $body->first; |
| 2890 | return $self->deparse($body, 2) . " foreach ($ary)"; |
| 2891 | } |
| 2892 | $head = "foreach $var ($ary) "; |
| 2893 | } elsif ($kid->name eq "null") { # while/until |
| 2894 | $kid = $kid->first; |
| 2895 | my $name = {"and" => "while", "or" => "until"}->{$kid->name}; |
| 2896 | $cond = $self->deparse($kid->first, 1); |
| 2897 | $head = "$name ($cond) "; |
| 2898 | $body = $kid->first->sibling; |
| 2899 | } elsif ($kid->name eq "stub") { # bare and empty |
| 2900 | return "{;}"; # {} could be a hashref |
| 2901 | } |
| 2902 | # If there isn't a continue block, then the next pointer for the loop |
| 2903 | # will point to the unstack, which is kid's last child, except |
| 2904 | # in a bare loop, when it will point to the leaveloop. When neither of |
| 2905 | # these conditions hold, then the second-to-last child is the continue |
| 2906 | # block (or the last in a bare loop). |
| 2907 | my $cont_start = $enter->nextop; |
| 2908 | my $cont; |
| 2909 | if ($$cont_start != $$op && ${$cont_start} != ${$body->last}) { |
| 2910 | if ($bare) { |
| 2911 | $cont = $body->last; |
| 2912 | } else { |
| 2913 | $cont = $body->first; |
| 2914 | while (!null($cont->sibling->sibling)) { |
| 2915 | $cont = $cont->sibling; |
| 2916 | } |
| 2917 | } |
| 2918 | my $state = $body->first; |
| 2919 | my $cuddle = $self->{'cuddle'}; |
| 2920 | my @states; |
| 2921 | for (; $$state != $$cont; $state = $state->sibling) { |
| 2922 | push @states, $state; |
| 2923 | } |
| 2924 | $body = $self->lineseq(undef, @states); |
| 2925 | if (defined $cond and not is_scope $cont and $self->{'expand'} < 3) { |
| 2926 | $head = "for ($init; $cond; " . $self->deparse($cont, 1) .") "; |
| 2927 | $cont = "\cK"; |
| 2928 | } else { |
| 2929 | $cont = $cuddle . "continue {\n\t" . |
| 2930 | $self->deparse($cont, 0) . "\n\b}\cK"; |
| 2931 | } |
| 2932 | } else { |
| 2933 | return "" if !defined $body; |
| 2934 | if (length $init) { |
| 2935 | $head = "for ($init; $cond;) "; |
| 2936 | } |
| 2937 | $cont = "\cK"; |
| 2938 | $body = $self->deparse($body, 0); |
| 2939 | } |
| 2940 | $body =~ s/;?$/;\n/; |
| 2941 | |
| 2942 | return $head . "{\n\t" . $body . "\b}" . $cont; |
| 2943 | } |
| 2944 | |
| 2945 | sub pp_leaveloop { shift->loop_common(@_, "") } |
| 2946 | |
| 2947 | sub for_loop { |
| 2948 | my $self = shift; |
| 2949 | my($op, $cx) = @_; |
| 2950 | my $init = $self->deparse($op, 1); |
| 2951 | my $s = $op->sibling; |
| 2952 | my $ll = $s->name eq "unstack" ? $s->sibling : $s->first->sibling; |
| 2953 | return $self->loop_common($ll, $cx, $init); |
| 2954 | } |
| 2955 | |
| 2956 | sub pp_leavetry { |
| 2957 | my $self = shift; |
| 2958 | return "eval {\n\t" . $self->pp_leave(@_) . "\n\b}"; |
| 2959 | } |
| 2960 | |
| 2961 | BEGIN { for (qw[ const stringify rv2sv list glob ]) { |
| 2962 | eval "sub OP_\U$_ () { " . opnumber($_) . "}" |
| 2963 | }} |
| 2964 | |
| 2965 | sub pp_null { |
| 2966 | my $self = shift; |
| 2967 | my($op, $cx) = @_; |
| 2968 | if (class($op) eq "OP") { |
| 2969 | # old value is lost |
| 2970 | return $self->{'ex_const'} if $op->targ == OP_CONST; |
| 2971 | } elsif ($op->first->name eq "pushmark") { |
| 2972 | return $self->pp_list($op, $cx); |
| 2973 | } elsif ($op->first->name eq "enter") { |
| 2974 | return $self->pp_leave($op, $cx); |
| 2975 | } elsif ($op->first->name eq "leave") { |
| 2976 | return $self->pp_leave($op->first, $cx); |
| 2977 | } elsif ($op->first->name eq "scope") { |
| 2978 | return $self->pp_scope($op->first, $cx); |
| 2979 | } elsif ($op->targ == OP_STRINGIFY) { |
| 2980 | return $self->dquote($op, $cx); |
| 2981 | } elsif ($op->targ == OP_GLOB) { |
| 2982 | return $self->pp_glob( |
| 2983 | $op->first # entersub |
| 2984 | ->first # ex-list |
| 2985 | ->first # pushmark |
| 2986 | ->sibling, # glob |
| 2987 | $cx |
| 2988 | ); |
| 2989 | } elsif (!null($op->first->sibling) and |
| 2990 | $op->first->sibling->name eq "readline" and |
| 2991 | $op->first->sibling->flags & OPf_STACKED) { |
| 2992 | return $self->maybe_parens($self->deparse($op->first, 7) . " = " |
| 2993 | . $self->deparse($op->first->sibling, 7), |
| 2994 | $cx, 7); |
| 2995 | } elsif (!null($op->first->sibling) and |
| 2996 | $op->first->sibling->name eq "trans" and |
| 2997 | $op->first->sibling->flags & OPf_STACKED) { |
| 2998 | return $self->maybe_parens($self->deparse($op->first, 20) . " =~ " |
| 2999 | . $self->deparse($op->first->sibling, 20), |
| 3000 | $cx, 20); |
| 3001 | } elsif ($op->flags & OPf_SPECIAL && $cx < 1 && !$op->targ) { |
| 3002 | return "do {\n\t". $self->deparse($op->first, $cx) ."\n\b};"; |
| 3003 | } elsif (!null($op->first->sibling) and |
| 3004 | $op->first->sibling->name eq "null" and |
| 3005 | class($op->first->sibling) eq "UNOP" and |
| 3006 | $op->first->sibling->first->flags & OPf_STACKED and |
| 3007 | $op->first->sibling->first->name eq "rcatline") { |
| 3008 | return $self->maybe_parens($self->deparse($op->first, 18) . " .= " |
| 3009 | . $self->deparse($op->first->sibling, 18), |
| 3010 | $cx, 18); |
| 3011 | } else { |
| 3012 | return $self->deparse($op->first, $cx); |
| 3013 | } |
| 3014 | } |
| 3015 | |
| 3016 | sub padname { |
| 3017 | my $self = shift; |
| 3018 | my $targ = shift; |
| 3019 | return $self->padname_sv($targ)->PVX; |
| 3020 | } |
| 3021 | |
| 3022 | sub padany { |
| 3023 | my $self = shift; |
| 3024 | my $op = shift; |
| 3025 | return substr($self->padname($op->targ), 1); # skip $/@/% |
| 3026 | } |
| 3027 | |
| 3028 | sub pp_padsv { |
| 3029 | my $self = shift; |
| 3030 | my($op, $cx) = @_; |
| 3031 | return $self->maybe_my($op, $cx, $self->padname($op->targ)); |
| 3032 | } |
| 3033 | |
| 3034 | sub pp_padav { pp_padsv(@_) } |
| 3035 | sub pp_padhv { pp_padsv(@_) } |
| 3036 | |
| 3037 | my @threadsv_names = B::threadsv_names; |
| 3038 | sub pp_threadsv { |
| 3039 | my $self = shift; |
| 3040 | my($op, $cx) = @_; |
| 3041 | return $self->maybe_local($op, $cx, "\$" . $threadsv_names[$op->targ]); |
| 3042 | } |
| 3043 | |
| 3044 | sub gv_or_padgv { |
| 3045 | my $self = shift; |
| 3046 | my $op = shift; |
| 3047 | if (class($op) eq "PADOP") { |
| 3048 | return $self->padval($op->padix); |
| 3049 | } else { # class($op) eq "SVOP" |
| 3050 | return $op->gv; |
| 3051 | } |
| 3052 | } |
| 3053 | |
| 3054 | sub pp_gvsv { |
| 3055 | my $self = shift; |
| 3056 | my($op, $cx) = @_; |
| 3057 | my $gv = $self->gv_or_padgv($op); |
| 3058 | return $self->maybe_local($op, $cx, $self->stash_variable("\$", |
| 3059 | $self->gv_name($gv), $cx)); |
| 3060 | } |
| 3061 | |
| 3062 | sub pp_gv { |
| 3063 | my $self = shift; |
| 3064 | my($op, $cx) = @_; |
| 3065 | my $gv = $self->gv_or_padgv($op); |
| 3066 | return $self->gv_name($gv); |
| 3067 | } |
| 3068 | |
| 3069 | sub pp_aelemfast_lex { |
| 3070 | my $self = shift; |
| 3071 | my($op, $cx) = @_; |
| 3072 | my $name = $self->padname($op->targ); |
| 3073 | $name =~ s/^@/\$/; |
| 3074 | return $name . "[" . ($op->private + $self->{'arybase'}) . "]"; |
| 3075 | } |
| 3076 | |
| 3077 | sub pp_aelemfast { |
| 3078 | my $self = shift; |
| 3079 | my($op, $cx) = @_; |
| 3080 | # optimised PADAV, pre 5.15 |
| 3081 | return $self->pp_aelemfast_lex(@_) if ($op->flags & OPf_SPECIAL); |
| 3082 | |
| 3083 | my $gv = $self->gv_or_padgv($op); |
| 3084 | my($name,$quoted) = $self->stash_variable_name('@',$gv); |
| 3085 | $name = $quoted ? "$name->" : '$' . $name; |
| 3086 | return $name . "[" . ($op->private + $self->{'arybase'}) . "]"; |
| 3087 | } |
| 3088 | |
| 3089 | sub rv2x { |
| 3090 | my $self = shift; |
| 3091 | my($op, $cx, $type) = @_; |
| 3092 | |
| 3093 | if (class($op) eq 'NULL' || !$op->can("first")) { |
| 3094 | carp("Unexpected op in pp_rv2x"); |
| 3095 | return 'XXX'; |
| 3096 | } |
| 3097 | my $kid = $op->first; |
| 3098 | if ($kid->name eq "gv") { |
| 3099 | return $self->stash_variable($type, $self->deparse($kid, 0), $cx); |
| 3100 | } elsif (is_scalar $kid) { |
| 3101 | my $str = $self->deparse($kid, 0); |
| 3102 | if ($str =~ /^\$([^\w\d])\z/) { |
| 3103 | # "$$+" isn't a legal way to write the scalar dereference |
| 3104 | # of $+, since the lexer can't tell you aren't trying to |
| 3105 | # do something like "$$ + 1" to get one more than your |
| 3106 | # PID. Either "${$+}" or "$${+}" are workable |
| 3107 | # disambiguations, but if the programmer did the former, |
| 3108 | # they'd be in the "else" clause below rather than here. |
| 3109 | # It's not clear if this should somehow be unified with |
| 3110 | # the code in dq and re_dq that also adds lexer |
| 3111 | # disambiguation braces. |
| 3112 | $str = '$' . "{$1}"; #' |
| 3113 | } |
| 3114 | return $type . $str; |
| 3115 | } else { |
| 3116 | return $type . "{" . $self->deparse($kid, 0) . "}"; |
| 3117 | } |
| 3118 | } |
| 3119 | |
| 3120 | sub pp_rv2sv { maybe_local(@_, rv2x(@_, "\$")) } |
| 3121 | sub pp_rv2hv { maybe_local(@_, rv2x(@_, "%")) } |
| 3122 | sub pp_rv2gv { maybe_local(@_, rv2x(@_, "*")) } |
| 3123 | |
| 3124 | # skip rv2av |
| 3125 | sub pp_av2arylen { |
| 3126 | my $self = shift; |
| 3127 | my($op, $cx) = @_; |
| 3128 | if ($op->first->name eq "padav") { |
| 3129 | return $self->maybe_local($op, $cx, '$#' . $self->padany($op->first)); |
| 3130 | } else { |
| 3131 | return $self->maybe_local($op, $cx, |
| 3132 | $self->rv2x($op->first, $cx, '$#')); |
| 3133 | } |
| 3134 | } |
| 3135 | |
| 3136 | # skip down to the old, ex-rv2cv |
| 3137 | sub pp_rv2cv { |
| 3138 | my ($self, $op, $cx) = @_; |
| 3139 | if (!null($op->first) && $op->first->name eq 'null' && |
| 3140 | $op->first->targ eq OP_LIST) |
| 3141 | { |
| 3142 | return $self->rv2x($op->first->first->sibling, $cx, "&") |
| 3143 | } |
| 3144 | else { |
| 3145 | return $self->rv2x($op, $cx, "") |
| 3146 | } |
| 3147 | } |
| 3148 | |
| 3149 | sub list_const { |
| 3150 | my $self = shift; |
| 3151 | my($cx, @list) = @_; |
| 3152 | my @a = map $self->const($_, 6), @list; |
| 3153 | if (@a == 0) { |
| 3154 | return "()"; |
| 3155 | } elsif (@a == 1) { |
| 3156 | return $a[0]; |
| 3157 | } elsif ( @a > 2 and !grep(!/^-?\d+$/, @a)) { |
| 3158 | # collapse (-1,0,1,2) into (-1..2) |
| 3159 | my ($s, $e) = @a[0,-1]; |
| 3160 | my $i = $s; |
| 3161 | return $self->maybe_parens("$s..$e", $cx, 9) |
| 3162 | unless grep $i++ != $_, @a; |
| 3163 | } |
| 3164 | return $self->maybe_parens(join(", ", @a), $cx, 6); |
| 3165 | } |
| 3166 | |
| 3167 | sub pp_rv2av { |
| 3168 | my $self = shift; |
| 3169 | my($op, $cx) = @_; |
| 3170 | my $kid = $op->first; |
| 3171 | if ($kid->name eq "const") { # constant list |
| 3172 | my $av = $self->const_sv($kid); |
| 3173 | return $self->list_const($cx, $av->ARRAY); |
| 3174 | } else { |
| 3175 | return $self->maybe_local($op, $cx, $self->rv2x($op, $cx, "\@")); |
| 3176 | } |
| 3177 | } |
| 3178 | |
| 3179 | sub is_subscriptable { |
| 3180 | my $op = shift; |
| 3181 | if ($op->name =~ /^[ahg]elem/) { |
| 3182 | return 1; |
| 3183 | } elsif ($op->name eq "entersub") { |
| 3184 | my $kid = $op->first; |
| 3185 | return 0 unless null $kid->sibling; |
| 3186 | $kid = $kid->first; |
| 3187 | $kid = $kid->sibling until null $kid->sibling; |
| 3188 | return 0 if is_scope($kid); |
| 3189 | $kid = $kid->first; |
| 3190 | return 0 if $kid->name eq "gv"; |
| 3191 | return 0 if is_scalar($kid); |
| 3192 | return is_subscriptable($kid); |
| 3193 | } else { |
| 3194 | return 0; |
| 3195 | } |
| 3196 | } |
| 3197 | |
| 3198 | sub elem_or_slice_array_name |
| 3199 | { |
| 3200 | my $self = shift; |
| 3201 | my ($array, $left, $padname, $allow_arrow) = @_; |
| 3202 | |
| 3203 | if ($array->name eq $padname) { |
| 3204 | return $self->padany($array); |
| 3205 | } elsif (is_scope($array)) { # ${expr}[0] |
| 3206 | return "{" . $self->deparse($array, 0) . "}"; |
| 3207 | } elsif ($array->name eq "gv") { |
| 3208 | ($array, my $quoted) = |
| 3209 | $self->stash_variable_name( |
| 3210 | $left eq '[' ? '@' : '%', $self->gv_or_padgv($array) |
| 3211 | ); |
| 3212 | if (!$allow_arrow && $quoted) { |
| 3213 | # This cannot happen. |
| 3214 | die "Invalid variable name $array for slice"; |
| 3215 | } |
| 3216 | return $quoted ? "$array->" : $array; |
| 3217 | } elsif (!$allow_arrow || is_scalar $array) { # $x[0], $$x[0], ... |
| 3218 | return $self->deparse($array, 24); |
| 3219 | } else { |
| 3220 | return undef; |
| 3221 | } |
| 3222 | } |
| 3223 | |
| 3224 | sub elem_or_slice_single_index |
| 3225 | { |
| 3226 | my $self = shift; |
| 3227 | my ($idx) = @_; |
| 3228 | |
| 3229 | $idx = $self->deparse($idx, 1); |
| 3230 | |
| 3231 | # Outer parens in an array index will confuse perl |
| 3232 | # if we're interpolating in a regular expression, i.e. |
| 3233 | # /$x$foo[(-1)]/ is *not* the same as /$x$foo[-1]/ |
| 3234 | # |
| 3235 | # If $self->{parens}, then an initial '(' will |
| 3236 | # definitely be paired with a final ')'. If |
| 3237 | # !$self->{parens}, the misleading parens won't |
| 3238 | # have been added in the first place. |
| 3239 | # |
| 3240 | # [You might think that we could get "(...)...(...)" |
| 3241 | # where the initial and final parens do not match |
| 3242 | # each other. But we can't, because the above would |
| 3243 | # only happen if there's an infix binop between the |
| 3244 | # two pairs of parens, and *that* means that the whole |
| 3245 | # expression would be parenthesized as well.] |
| 3246 | # |
| 3247 | $idx =~ s/^\((.*)\)$/$1/ if $self->{'parens'}; |
| 3248 | |
| 3249 | # Hash-element braces will autoquote a bareword inside themselves. |
| 3250 | # We need to make sure that C<$hash{warn()}> doesn't come out as |
| 3251 | # C<$hash{warn}>, which has a quite different meaning. Currently |
| 3252 | # B::Deparse will always quote strings, even if the string was a |
| 3253 | # bareword in the original (i.e. the OPpCONST_BARE flag is ignored |
| 3254 | # for constant strings.) So we can cheat slightly here - if we see |
| 3255 | # a bareword, we know that it is supposed to be a function call. |
| 3256 | # |
| 3257 | $idx =~ s/^([A-Za-z_]\w*)$/$1()/; |
| 3258 | |
| 3259 | return $idx; |
| 3260 | } |
| 3261 | |
| 3262 | sub elem { |
| 3263 | my $self = shift; |
| 3264 | my ($op, $cx, $left, $right, $padname) = @_; |
| 3265 | my($array, $idx) = ($op->first, $op->first->sibling); |
| 3266 | |
| 3267 | $idx = $self->elem_or_slice_single_index($idx); |
| 3268 | |
| 3269 | unless ($array->name eq $padname) { # Maybe this has been fixed |
| 3270 | $array = $array->first; # skip rv2av (or ex-rv2av in _53+) |
| 3271 | } |
| 3272 | if (my $array_name=$self->elem_or_slice_array_name |
| 3273 | ($array, $left, $padname, 1)) { |
| 3274 | return ($array_name =~ /->\z/ ? $array_name : "\$" . $array_name) |
| 3275 | . $left . $idx . $right; |
| 3276 | } else { |
| 3277 | # $x[20][3]{hi} or expr->[20] |
| 3278 | my $arrow = is_subscriptable($array) ? "" : "->"; |
| 3279 | return $self->deparse($array, 24) . $arrow . $left . $idx . $right; |
| 3280 | } |
| 3281 | |
| 3282 | } |
| 3283 | |
| 3284 | sub pp_aelem { maybe_local(@_, elem(@_, "[", "]", "padav")) } |
| 3285 | sub pp_helem { maybe_local(@_, elem(@_, "{", "}", "padhv")) } |
| 3286 | |
| 3287 | sub pp_gelem { |
| 3288 | my $self = shift; |
| 3289 | my($op, $cx) = @_; |
| 3290 | my($glob, $part) = ($op->first, $op->last); |
| 3291 | $glob = $glob->first; # skip rv2gv |
| 3292 | $glob = $glob->first if $glob->name eq "rv2gv"; # this one's a bug |
| 3293 | my $scope = is_scope($glob); |
| 3294 | $glob = $self->deparse($glob, 0); |
| 3295 | $part = $self->deparse($part, 1); |
| 3296 | return "*" . ($scope ? "{$glob}" : $glob) . "{$part}"; |
| 3297 | } |
| 3298 | |
| 3299 | sub slice { |
| 3300 | my $self = shift; |
| 3301 | my ($op, $cx, $left, $right, $regname, $padname) = @_; |
| 3302 | my $last; |
| 3303 | my(@elems, $kid, $array, $list); |
| 3304 | if (class($op) eq "LISTOP") { |
| 3305 | $last = $op->last; |
| 3306 | } else { # ex-hslice inside delete() |
| 3307 | for ($kid = $op->first; !null $kid->sibling; $kid = $kid->sibling) {} |
| 3308 | $last = $kid; |
| 3309 | } |
| 3310 | $array = $last; |
| 3311 | $array = $array->first |
| 3312 | if $array->name eq $regname or $array->name eq "null"; |
| 3313 | $array = $self->elem_or_slice_array_name($array,$left,$padname,0); |
| 3314 | $kid = $op->first->sibling; # skip pushmark |
| 3315 | if ($kid->name eq "list") { |
| 3316 | $kid = $kid->first->sibling; # skip list, pushmark |
| 3317 | for (; !null $kid; $kid = $kid->sibling) { |
| 3318 | push @elems, $self->deparse($kid, 6); |
| 3319 | } |
| 3320 | $list = join(", ", @elems); |
| 3321 | } else { |
| 3322 | $list = $self->elem_or_slice_single_index($kid); |
| 3323 | } |
| 3324 | return "\@" . $array . $left . $list . $right; |
| 3325 | } |
| 3326 | |
| 3327 | sub pp_aslice { maybe_local(@_, slice(@_, "[", "]", "rv2av", "padav")) } |
| 3328 | sub pp_hslice { maybe_local(@_, slice(@_, "{", "}", "rv2hv", "padhv")) } |
| 3329 | |
| 3330 | sub pp_lslice { |
| 3331 | my $self = shift; |
| 3332 | my($op, $cx) = @_; |
| 3333 | my $idx = $op->first; |
| 3334 | my $list = $op->last; |
| 3335 | my(@elems, $kid); |
| 3336 | $list = $self->deparse($list, 1); |
| 3337 | $idx = $self->deparse($idx, 1); |
| 3338 | return "($list)" . "[$idx]"; |
| 3339 | } |
| 3340 | |
| 3341 | sub want_scalar { |
| 3342 | my $op = shift; |
| 3343 | return ($op->flags & OPf_WANT) == OPf_WANT_SCALAR; |
| 3344 | } |
| 3345 | |
| 3346 | sub want_list { |
| 3347 | my $op = shift; |
| 3348 | return ($op->flags & OPf_WANT) == OPf_WANT_LIST; |
| 3349 | } |
| 3350 | |
| 3351 | sub _method { |
| 3352 | my $self = shift; |
| 3353 | my($op, $cx) = @_; |
| 3354 | my $kid = $op->first->sibling; # skip pushmark |
| 3355 | my($meth, $obj, @exprs); |
| 3356 | if ($kid->name eq "list" and want_list $kid) { |
| 3357 | # When an indirect object isn't a bareword but the args are in |
| 3358 | # parens, the parens aren't part of the method syntax (the LLAFR |
| 3359 | # doesn't apply), but they make a list with OPf_PARENS set that |
| 3360 | # doesn't get flattened by the append_elem that adds the method, |
| 3361 | # making a (object, arg1, arg2, ...) list where the object |
| 3362 | # usually is. This can be distinguished from |
| 3363 | # '($obj, $arg1, $arg2)->meth()' (which is legal if $arg2 is an |
| 3364 | # object) because in the later the list is in scalar context |
| 3365 | # as the left side of -> always is, while in the former |
| 3366 | # the list is in list context as method arguments always are. |
| 3367 | # (Good thing there aren't method prototypes!) |
| 3368 | $meth = $kid->sibling; |
| 3369 | $kid = $kid->first->sibling; # skip pushmark |
| 3370 | $obj = $kid; |
| 3371 | $kid = $kid->sibling; |
| 3372 | for (; not null $kid; $kid = $kid->sibling) { |
| 3373 | push @exprs, $kid; |
| 3374 | } |
| 3375 | } else { |
| 3376 | $obj = $kid; |
| 3377 | $kid = $kid->sibling; |
| 3378 | for (; !null ($kid->sibling) && $kid->name!~/^method(?:_named)?\z/; |
| 3379 | $kid = $kid->sibling) { |
| 3380 | push @exprs, $kid |
| 3381 | } |
| 3382 | $meth = $kid; |
| 3383 | } |
| 3384 | |
| 3385 | if ($meth->name eq "method_named") { |
| 3386 | $meth = $self->const_sv($meth)->PV; |
| 3387 | } else { |
| 3388 | $meth = $meth->first; |
| 3389 | if ($meth->name eq "const") { |
| 3390 | # As of 5.005_58, this case is probably obsoleted by the |
| 3391 | # method_named case above |
| 3392 | $meth = $self->const_sv($meth)->PV; # needs to be bare |
| 3393 | } |
| 3394 | } |
| 3395 | |
| 3396 | return { method => $meth, variable_method => ref($meth), |
| 3397 | object => $obj, args => \@exprs }, |
| 3398 | $cx; |
| 3399 | } |
| 3400 | |
| 3401 | # compat function only |
| 3402 | sub method { |
| 3403 | my $self = shift; |
| 3404 | my $info = $self->_method(@_); |
| 3405 | return $self->e_method( $self->_method(@_) ); |
| 3406 | } |
| 3407 | |
| 3408 | sub e_method { |
| 3409 | my ($self, $info, $cx) = @_; |
| 3410 | my $obj = $self->deparse($info->{object}, 24); |
| 3411 | |
| 3412 | my $meth = $info->{method}; |
| 3413 | $meth = $self->deparse($meth, 1) if $info->{variable_method}; |
| 3414 | my $args = join(", ", map { $self->deparse($_, 6) } @{$info->{args}} ); |
| 3415 | if ($info->{object}->name eq 'scope' && want_list $info->{object}) { |
| 3416 | # method { $object } |
| 3417 | # This must be deparsed this way to preserve list context |
| 3418 | # of $object. |
| 3419 | my $need_paren = $cx >= 6; |
| 3420 | return '(' x $need_paren |
| 3421 | . $meth . substr($obj,2) # chop off the "do" |
| 3422 | . " $args" |
| 3423 | . ')' x $need_paren; |
| 3424 | } |
| 3425 | my $kid = $obj . "->" . $meth; |
| 3426 | if (length $args) { |
| 3427 | return $kid . "(" . $args . ")"; # parens mandatory |
| 3428 | } else { |
| 3429 | return $kid; |
| 3430 | } |
| 3431 | } |
| 3432 | |
| 3433 | # returns "&" if the prototype doesn't match the args, |
| 3434 | # or ("", $args_after_prototype_demunging) if it does. |
| 3435 | sub check_proto { |
| 3436 | my $self = shift; |
| 3437 | return "&" if $self->{'noproto'}; |
| 3438 | my($proto, @args) = @_; |
| 3439 | my($arg, $real); |
| 3440 | my $doneok = 0; |
| 3441 | my @reals; |
| 3442 | # An unbackslashed @ or % gobbles up the rest of the args |
| 3443 | 1 while $proto =~ s/(?<!\\)([@%])[^\]]+$/$1/; |
| 3444 | while ($proto) { |
| 3445 | $proto =~ s/^(\\?[\$\@&%*_]|\\\[[\$\@&%*]+\]|;)//; |
| 3446 | my $chr = $1; |
| 3447 | if ($chr eq "") { |
| 3448 | return "&" if @args; |
| 3449 | } elsif ($chr eq ";") { |
| 3450 | $doneok = 1; |
| 3451 | } elsif ($chr eq "@" or $chr eq "%") { |
| 3452 | push @reals, map($self->deparse($_, 6), @args); |
| 3453 | @args = (); |
| 3454 | } else { |
| 3455 | $arg = shift @args; |
| 3456 | last unless $arg; |
| 3457 | if ($chr eq "\$" || $chr eq "_") { |
| 3458 | if (want_scalar $arg) { |
| 3459 | push @reals, $self->deparse($arg, 6); |
| 3460 | } else { |
| 3461 | return "&"; |
| 3462 | } |
| 3463 | } elsif ($chr eq "&") { |
| 3464 | if ($arg->name =~ /^(s?refgen|undef)$/) { |
| 3465 | push @reals, $self->deparse($arg, 6); |
| 3466 | } else { |
| 3467 | return "&"; |
| 3468 | } |
| 3469 | } elsif ($chr eq "*") { |
| 3470 | if ($arg->name =~ /^s?refgen$/ |
| 3471 | and $arg->first->first->name eq "rv2gv") |
| 3472 | { |
| 3473 | $real = $arg->first->first; # skip refgen, null |
| 3474 | if ($real->first->name eq "gv") { |
| 3475 | push @reals, $self->deparse($real, 6); |
| 3476 | } else { |
| 3477 | push @reals, $self->deparse($real->first, 6); |
| 3478 | } |
| 3479 | } else { |
| 3480 | return "&"; |
| 3481 | } |
| 3482 | } elsif (substr($chr, 0, 1) eq "\\") { |
| 3483 | $chr =~ tr/\\[]//d; |
| 3484 | if ($arg->name =~ /^s?refgen$/ and |
| 3485 | !null($real = $arg->first) and |
| 3486 | ($chr =~ /\$/ && is_scalar($real->first) |
| 3487 | or ($chr =~ /@/ |
| 3488 | && class($real->first->sibling) ne 'NULL' |
| 3489 | && $real->first->sibling->name |
| 3490 | =~ /^(rv2|pad)av$/) |
| 3491 | or ($chr =~ /%/ |
| 3492 | && class($real->first->sibling) ne 'NULL' |
| 3493 | && $real->first->sibling->name |
| 3494 | =~ /^(rv2|pad)hv$/) |
| 3495 | #or ($chr =~ /&/ # This doesn't work |
| 3496 | # && $real->first->name eq "rv2cv") |
| 3497 | or ($chr =~ /\*/ |
| 3498 | && $real->first->name eq "rv2gv"))) |
| 3499 | { |
| 3500 | push @reals, $self->deparse($real, 6); |
| 3501 | } else { |
| 3502 | return "&"; |
| 3503 | } |
| 3504 | } |
| 3505 | } |
| 3506 | } |
| 3507 | return "&" if $proto and !$doneok; # too few args and no ';' |
| 3508 | return "&" if @args; # too many args |
| 3509 | return ("", join ", ", @reals); |
| 3510 | } |
| 3511 | |
| 3512 | sub pp_entersub { |
| 3513 | my $self = shift; |
| 3514 | my($op, $cx) = @_; |
| 3515 | return $self->e_method($self->_method($op, $cx)) |
| 3516 | unless null $op->first->sibling; |
| 3517 | my $prefix = ""; |
| 3518 | my $amper = ""; |
| 3519 | my($kid, @exprs); |
| 3520 | if ($op->flags & OPf_SPECIAL && !($op->flags & OPf_MOD)) { |
| 3521 | $prefix = "do "; |
| 3522 | } elsif ($op->private & OPpENTERSUB_AMPER) { |
| 3523 | $amper = "&"; |
| 3524 | } |
| 3525 | $kid = $op->first; |
| 3526 | $kid = $kid->first->sibling; # skip ex-list, pushmark |
| 3527 | for (; not null $kid->sibling; $kid = $kid->sibling) { |
| 3528 | push @exprs, $kid; |
| 3529 | } |
| 3530 | my $simple = 0; |
| 3531 | my $proto = undef; |
| 3532 | if (is_scope($kid)) { |
| 3533 | $amper = "&"; |
| 3534 | $kid = "{" . $self->deparse($kid, 0) . "}"; |
| 3535 | } elsif ($kid->first->name eq "gv") { |
| 3536 | my $gv = $self->gv_or_padgv($kid->first); |
| 3537 | if (class($gv->CV) ne "SPECIAL") { |
| 3538 | $proto = $gv->CV->PV if $gv->CV->FLAGS & SVf_POK; |
| 3539 | } |
| 3540 | $simple = 1; # only calls of named functions can be prototyped |
| 3541 | $kid = $self->deparse($kid, 24); |
| 3542 | if (!$amper) { |
| 3543 | if ($kid eq 'main::') { |
| 3544 | $kid = '::'; |
| 3545 | } elsif ($kid !~ /^(?:\w|::)(?:[\w\d]|::(?!\z))*\z/) { |
| 3546 | $kid = single_delim("q", "'", $kid) . '->'; |
| 3547 | } |
| 3548 | } |
| 3549 | } elsif (is_scalar ($kid->first) && $kid->first->name ne 'rv2cv') { |
| 3550 | $amper = "&"; |
| 3551 | $kid = $self->deparse($kid, 24); |
| 3552 | } else { |
| 3553 | $prefix = ""; |
| 3554 | my $arrow = is_subscriptable($kid->first) ? "" : "->"; |
| 3555 | $kid = $self->deparse($kid, 24) . $arrow; |
| 3556 | } |
| 3557 | |
| 3558 | # Doesn't matter how many prototypes there are, if |
| 3559 | # they haven't happened yet! |
| 3560 | my $declared; |
| 3561 | { |
| 3562 | no strict 'refs'; |
| 3563 | no warnings 'uninitialized'; |
| 3564 | $declared = exists $self->{'subs_declared'}{$kid} |
| 3565 | || ( |
| 3566 | defined &{ ${$self->{'curstash'}."::"}{$kid} } |
| 3567 | && !exists |
| 3568 | $self->{'subs_deparsed'}{$self->{'curstash'}."::".$kid} |
| 3569 | && defined prototype $self->{'curstash'}."::".$kid |
| 3570 | ); |
| 3571 | if (!$declared && defined($proto)) { |
| 3572 | # Avoid "too early to check prototype" warning |
| 3573 | ($amper, $proto) = ('&'); |
| 3574 | } |
| 3575 | } |
| 3576 | |
| 3577 | my $args; |
| 3578 | if ($declared and defined $proto and not $amper) { |
| 3579 | ($amper, $args) = $self->check_proto($proto, @exprs); |
| 3580 | if ($amper eq "&") { |
| 3581 | $args = join(", ", map($self->deparse($_, 6), @exprs)); |
| 3582 | } |
| 3583 | } else { |
| 3584 | $args = join(", ", map($self->deparse($_, 6), @exprs)); |
| 3585 | } |
| 3586 | if ($prefix or $amper) { |
| 3587 | if ($kid eq '&') { $kid = "{$kid}" } # &{&} cannot be written as && |
| 3588 | if ($op->flags & OPf_STACKED) { |
| 3589 | return $prefix . $amper . $kid . "(" . $args . ")"; |
| 3590 | } else { |
| 3591 | return $prefix . $amper. $kid; |
| 3592 | } |
| 3593 | } else { |
| 3594 | # It's a syntax error to call CORE::GLOBAL::foo with a prefix, |
| 3595 | # so it must have been translated from a keyword call. Translate |
| 3596 | # it back. |
| 3597 | $kid =~ s/^CORE::GLOBAL:://; |
| 3598 | |
| 3599 | my $dproto = defined($proto) ? $proto : "undefined"; |
| 3600 | if (!$declared) { |
| 3601 | return "$kid(" . $args . ")"; |
| 3602 | } elsif ($dproto eq "") { |
| 3603 | return $kid; |
| 3604 | } elsif ($dproto eq "\$" and is_scalar($exprs[0])) { |
| 3605 | # is_scalar is an excessively conservative test here: |
| 3606 | # really, we should be comparing to the precedence of the |
| 3607 | # top operator of $exprs[0] (ala unop()), but that would |
| 3608 | # take some major code restructuring to do right. |
| 3609 | return $self->maybe_parens_func($kid, $args, $cx, 16); |
| 3610 | } elsif ($dproto ne '$' and defined($proto) || $simple) { #' |
| 3611 | return $self->maybe_parens_func($kid, $args, $cx, 5); |
| 3612 | } else { |
| 3613 | return "$kid(" . $args . ")"; |
| 3614 | } |
| 3615 | } |
| 3616 | } |
| 3617 | |
| 3618 | sub pp_enterwrite { unop(@_, "write") } |
| 3619 | |
| 3620 | # escape things that cause interpolation in double quotes, |
| 3621 | # but not character escapes |
| 3622 | sub uninterp { |
| 3623 | my($str) = @_; |
| 3624 | $str =~ s/(^|\G|[^\\])((?:\\\\)*)([\$\@]|\\[uUlLQE])/$1$2\\$3/g; |
| 3625 | return $str; |
| 3626 | } |
| 3627 | |
| 3628 | { |
| 3629 | my $bal; |
| 3630 | BEGIN { |
| 3631 | use re "eval"; |
| 3632 | # Matches any string which is balanced with respect to {braces} |
| 3633 | $bal = qr( |
| 3634 | (?: |
| 3635 | [^\\{}] |
| 3636 | | \\\\ |
| 3637 | | \\[{}] |
| 3638 | | \{(??{$bal})\} |
| 3639 | )* |
| 3640 | )x; |
| 3641 | } |
| 3642 | |
| 3643 | # the same, but treat $|, $), $( and $ at the end of the string differently |
| 3644 | sub re_uninterp { |
| 3645 | my($str) = @_; |
| 3646 | |
| 3647 | $str =~ s/ |
| 3648 | ( ^|\G # $1 |
| 3649 | | [^\\] |
| 3650 | ) |
| 3651 | |
| 3652 | ( # $2 |
| 3653 | (?:\\\\)* |
| 3654 | ) |
| 3655 | |
| 3656 | ( # $3 |
| 3657 | (\(\?\??\{$bal\}\)) # $4 |
| 3658 | | [\$\@] |
| 3659 | (?!\||\)|\(|$) |
| 3660 | | \\[uUlLQE] |
| 3661 | ) |
| 3662 | |
| 3663 | /defined($4) && length($4) ? "$1$2$4" : "$1$2\\$3"/xeg; |
| 3664 | |
| 3665 | return $str; |
| 3666 | } |
| 3667 | |
| 3668 | # This is for regular expressions with the /x modifier |
| 3669 | # We have to leave comments unmangled. |
| 3670 | sub re_uninterp_extended { |
| 3671 | my($str) = @_; |
| 3672 | |
| 3673 | $str =~ s/ |
| 3674 | ( ^|\G # $1 |
| 3675 | | [^\\] |
| 3676 | ) |
| 3677 | |
| 3678 | ( # $2 |
| 3679 | (?:\\\\)* |
| 3680 | ) |
| 3681 | |
| 3682 | ( # $3 |
| 3683 | ( \(\?\??\{$bal\}\) # $4 (skip over (?{}) and (??{}) blocks) |
| 3684 | | \#[^\n]* # (skip over comments) |
| 3685 | ) |
| 3686 | | [\$\@] |
| 3687 | (?!\||\)|\(|$|\s) |
| 3688 | | \\[uUlLQE] |
| 3689 | ) |
| 3690 | |
| 3691 | /defined($4) && length($4) ? "$1$2$4" : "$1$2\\$3"/xeg; |
| 3692 | |
| 3693 | return $str; |
| 3694 | } |
| 3695 | } |
| 3696 | |
| 3697 | my %unctrl = # portable to to EBCDIC |
| 3698 | ( |
| 3699 | "\c@" => '\c@', # unused |
| 3700 | "\cA" => '\cA', |
| 3701 | "\cB" => '\cB', |
| 3702 | "\cC" => '\cC', |
| 3703 | "\cD" => '\cD', |
| 3704 | "\cE" => '\cE', |
| 3705 | "\cF" => '\cF', |
| 3706 | "\cG" => '\cG', |
| 3707 | "\cH" => '\cH', |
| 3708 | "\cI" => '\cI', |
| 3709 | "\cJ" => '\cJ', |
| 3710 | "\cK" => '\cK', |
| 3711 | "\cL" => '\cL', |
| 3712 | "\cM" => '\cM', |
| 3713 | "\cN" => '\cN', |
| 3714 | "\cO" => '\cO', |
| 3715 | "\cP" => '\cP', |
| 3716 | "\cQ" => '\cQ', |
| 3717 | "\cR" => '\cR', |
| 3718 | "\cS" => '\cS', |
| 3719 | "\cT" => '\cT', |
| 3720 | "\cU" => '\cU', |
| 3721 | "\cV" => '\cV', |
| 3722 | "\cW" => '\cW', |
| 3723 | "\cX" => '\cX', |
| 3724 | "\cY" => '\cY', |
| 3725 | "\cZ" => '\cZ', |
| 3726 | "\c[" => '\c[', # unused |
| 3727 | "\c\\" => '\c\\', # unused |
| 3728 | "\c]" => '\c]', # unused |
| 3729 | "\c_" => '\c_', # unused |
| 3730 | ); |
| 3731 | |
| 3732 | # character escapes, but not delimiters that might need to be escaped |
| 3733 | sub escape_str { # ASCII, UTF8 |
| 3734 | my($str) = @_; |
| 3735 | $str =~ s/(.)/ord($1) > 255 ? sprintf("\\x{%x}", ord($1)) : $1/eg; |
| 3736 | $str =~ s/\a/\\a/g; |
| 3737 | # $str =~ s/\cH/\\b/g; # \b means something different in a regex |
| 3738 | $str =~ s/\t/\\t/g; |
| 3739 | $str =~ s/\n/\\n/g; |
| 3740 | $str =~ s/\e/\\e/g; |
| 3741 | $str =~ s/\f/\\f/g; |
| 3742 | $str =~ s/\r/\\r/g; |
| 3743 | $str =~ s/([\cA-\cZ])/$unctrl{$1}/ge; |
| 3744 | $str =~ s/([[:^print:]])/sprintf("\\%03o", ord($1))/ge; |
| 3745 | return $str; |
| 3746 | } |
| 3747 | |
| 3748 | # For regexes with the /x modifier. |
| 3749 | # Leave whitespace unmangled. |
| 3750 | sub escape_extended_re { |
| 3751 | my($str) = @_; |
| 3752 | $str =~ s/(.)/ord($1) > 255 ? sprintf("\\x{%x}", ord($1)) : $1/eg; |
| 3753 | $str =~ s/([[:^print:]])/ |
| 3754 | ($1 =~ y! \t\n!!) ? $1 : sprintf("\\%03o", ord($1))/ge; |
| 3755 | $str =~ s/\n/\n\f/g; |
| 3756 | return $str; |
| 3757 | } |
| 3758 | |
| 3759 | # Don't do this for regexen |
| 3760 | sub unback { |
| 3761 | my($str) = @_; |
| 3762 | $str =~ s/\\/\\\\/g; |
| 3763 | return $str; |
| 3764 | } |
| 3765 | |
| 3766 | # Remove backslashes which precede literal control characters, |
| 3767 | # to avoid creating ambiguity when we escape the latter. |
| 3768 | sub re_unback { |
| 3769 | my($str) = @_; |
| 3770 | |
| 3771 | # the insane complexity here is due to the behaviour of "\c\" |
| 3772 | $str =~ s/(^|[^\\]|\\c\\)(?<!\\c)\\(\\\\)*(?=[[:^print:]])/$1$2/g; |
| 3773 | return $str; |
| 3774 | } |
| 3775 | |
| 3776 | sub balanced_delim { |
| 3777 | my($str) = @_; |
| 3778 | my @str = split //, $str; |
| 3779 | my($ar, $open, $close, $fail, $c, $cnt, $last_bs); |
| 3780 | for $ar (['[',']'], ['(',')'], ['<','>'], ['{','}']) { |
| 3781 | ($open, $close) = @$ar; |
| 3782 | $fail = 0; $cnt = 0; $last_bs = 0; |
| 3783 | for $c (@str) { |
| 3784 | if ($c eq $open) { |
| 3785 | $fail = 1 if $last_bs; |
| 3786 | $cnt++; |
| 3787 | } elsif ($c eq $close) { |
| 3788 | $fail = 1 if $last_bs; |
| 3789 | $cnt--; |
| 3790 | if ($cnt < 0) { |
| 3791 | # qq()() isn't ")(" |
| 3792 | $fail = 1; |
| 3793 | last; |
| 3794 | } |
| 3795 | } |
| 3796 | $last_bs = $c eq '\\'; |
| 3797 | } |
| 3798 | $fail = 1 if $cnt != 0; |
| 3799 | return ($open, "$open$str$close") if not $fail; |
| 3800 | } |
| 3801 | return ("", $str); |
| 3802 | } |
| 3803 | |
| 3804 | sub single_delim { |
| 3805 | my($q, $default, $str) = @_; |
| 3806 | return "$default$str$default" if $default and index($str, $default) == -1; |
| 3807 | if ($q ne 'qr') { |
| 3808 | (my $succeed, $str) = balanced_delim($str); |
| 3809 | return "$q$str" if $succeed; |
| 3810 | } |
| 3811 | for my $delim ('/', '"', '#') { |
| 3812 | return "$q$delim" . $str . $delim if index($str, $delim) == -1; |
| 3813 | } |
| 3814 | if ($default) { |
| 3815 | $str =~ s/$default/\\$default/g; |
| 3816 | return "$default$str$default"; |
| 3817 | } else { |
| 3818 | $str =~ s[/][\\/]g; |
| 3819 | return "$q/$str/"; |
| 3820 | } |
| 3821 | } |
| 3822 | |
| 3823 | my $max_prec; |
| 3824 | BEGIN { $max_prec = int(0.999 + 8*length(pack("F", 42))*log(2)/log(10)); } |
| 3825 | |
| 3826 | # Split a floating point number into an integer mantissa and a binary |
| 3827 | # exponent. Assumes you've already made sure the number isn't zero or |
| 3828 | # some weird infinity or NaN. |
| 3829 | sub split_float { |
| 3830 | my($f) = @_; |
| 3831 | my $exponent = 0; |
| 3832 | if ($f == int($f)) { |
| 3833 | while ($f % 2 == 0) { |
| 3834 | $f /= 2; |
| 3835 | $exponent++; |
| 3836 | } |
| 3837 | } else { |
| 3838 | while ($f != int($f)) { |
| 3839 | $f *= 2; |
| 3840 | $exponent--; |
| 3841 | } |
| 3842 | } |
| 3843 | my $mantissa = sprintf("%.0f", $f); |
| 3844 | return ($mantissa, $exponent); |
| 3845 | } |
| 3846 | |
| 3847 | sub const { |
| 3848 | my $self = shift; |
| 3849 | my($sv, $cx) = @_; |
| 3850 | if ($self->{'use_dumper'}) { |
| 3851 | return $self->const_dumper($sv, $cx); |
| 3852 | } |
| 3853 | if (class($sv) eq "SPECIAL") { |
| 3854 | # sv_undef, sv_yes, sv_no |
| 3855 | return ('undef', '1', $self->maybe_parens("!1", $cx, 21))[$$sv-1]; |
| 3856 | } |
| 3857 | if (class($sv) eq "NULL") { |
| 3858 | return 'undef'; |
| 3859 | } |
| 3860 | # convert a version object into the "v1.2.3" string in its V magic |
| 3861 | if ($sv->FLAGS & SVs_RMG) { |
| 3862 | for (my $mg = $sv->MAGIC; $mg; $mg = $mg->MOREMAGIC) { |
| 3863 | return $mg->PTR if $mg->TYPE eq 'V'; |
| 3864 | } |
| 3865 | } |
| 3866 | |
| 3867 | if ($sv->FLAGS & SVf_IOK) { |
| 3868 | my $str = $sv->int_value; |
| 3869 | $str = $self->maybe_parens($str, $cx, 21) if $str < 0; |
| 3870 | return $str; |
| 3871 | } elsif ($sv->FLAGS & SVf_NOK) { |
| 3872 | my $nv = $sv->NV; |
| 3873 | if ($nv == 0) { |
| 3874 | if (pack("F", $nv) eq pack("F", 0)) { |
| 3875 | # positive zero |
| 3876 | return "0"; |
| 3877 | } else { |
| 3878 | # negative zero |
| 3879 | return $self->maybe_parens("-.0", $cx, 21); |
| 3880 | } |
| 3881 | } elsif (1/$nv == 0) { |
| 3882 | if ($nv > 0) { |
| 3883 | # positive infinity |
| 3884 | return $self->maybe_parens("9**9**9", $cx, 22); |
| 3885 | } else { |
| 3886 | # negative infinity |
| 3887 | return $self->maybe_parens("-9**9**9", $cx, 21); |
| 3888 | } |
| 3889 | } elsif ($nv != $nv) { |
| 3890 | # NaN |
| 3891 | if (pack("F", $nv) eq pack("F", sin(9**9**9))) { |
| 3892 | # the normal kind |
| 3893 | return "sin(9**9**9)"; |
| 3894 | } elsif (pack("F", $nv) eq pack("F", -sin(9**9**9))) { |
| 3895 | # the inverted kind |
| 3896 | return $self->maybe_parens("-sin(9**9**9)", $cx, 21); |
| 3897 | } else { |
| 3898 | # some other kind |
| 3899 | my $hex = unpack("h*", pack("F", $nv)); |
| 3900 | return qq'unpack("F", pack("h*", "$hex"))'; |
| 3901 | } |
| 3902 | } |
| 3903 | # first, try the default stringification |
| 3904 | my $str = "$nv"; |
| 3905 | if ($str != $nv) { |
| 3906 | # failing that, try using more precision |
| 3907 | $str = sprintf("%.${max_prec}g", $nv); |
| 3908 | # if (pack("F", $str) ne pack("F", $nv)) { |
| 3909 | if ($str != $nv) { |
| 3910 | # not representable in decimal with whatever sprintf() |
| 3911 | # and atof() Perl is using here. |
| 3912 | my($mant, $exp) = split_float($nv); |
| 3913 | return $self->maybe_parens("$mant * 2**$exp", $cx, 19); |
| 3914 | } |
| 3915 | } |
| 3916 | $str = $self->maybe_parens($str, $cx, 21) if $nv < 0; |
| 3917 | return $str; |
| 3918 | } elsif ($sv->FLAGS & SVf_ROK && $sv->can("RV")) { |
| 3919 | my $ref = $sv->RV; |
| 3920 | if (class($ref) eq "AV") { |
| 3921 | return "[" . $self->list_const(2, $ref->ARRAY) . "]"; |
| 3922 | } elsif (class($ref) eq "HV") { |
| 3923 | my %hash = $ref->ARRAY; |
| 3924 | my @elts; |
| 3925 | for my $k (sort keys %hash) { |
| 3926 | push @elts, "$k => " . $self->const($hash{$k}, 6); |
| 3927 | } |
| 3928 | return "{" . join(", ", @elts) . "}"; |
| 3929 | } elsif (class($ref) eq "CV") { |
| 3930 | BEGIN { |
| 3931 | if ($] > 5.0150051) { |
| 3932 | require overloading; |
| 3933 | unimport overloading; |
| 3934 | } |
| 3935 | } |
| 3936 | if ($] > 5.0150051 && $self->{curcv} && |
| 3937 | $self->{curcv}->object_2svref == $ref->object_2svref) { |
| 3938 | return $self->keyword("__SUB__"); |
| 3939 | } |
| 3940 | return "sub " . $self->deparse_sub($ref); |
| 3941 | } |
| 3942 | if ($ref->FLAGS & SVs_SMG) { |
| 3943 | for (my $mg = $ref->MAGIC; $mg; $mg = $mg->MOREMAGIC) { |
| 3944 | if ($mg->TYPE eq 'r') { |
| 3945 | my $re = re_uninterp(escape_str(re_unback($mg->precomp))); |
| 3946 | return single_delim("qr", "", $re); |
| 3947 | } |
| 3948 | } |
| 3949 | } |
| 3950 | |
| 3951 | return $self->maybe_parens("\\" . $self->const($ref, 20), $cx, 20); |
| 3952 | } elsif ($sv->FLAGS & SVf_POK) { |
| 3953 | my $str = $sv->PV; |
| 3954 | if ($str =~ /[[:^print:]]/) { |
| 3955 | return single_delim("qq", '"', uninterp escape_str unback $str); |
| 3956 | } else { |
| 3957 | return single_delim("q", "'", unback $str); |
| 3958 | } |
| 3959 | } else { |
| 3960 | return "undef"; |
| 3961 | } |
| 3962 | } |
| 3963 | |
| 3964 | sub const_dumper { |
| 3965 | my $self = shift; |
| 3966 | my($sv, $cx) = @_; |
| 3967 | my $ref = $sv->object_2svref(); |
| 3968 | my $dumper = Data::Dumper->new([$$ref], ['$v']); |
| 3969 | $dumper->Purity(1)->Terse(1)->Deparse(1)->Indent(0)->Useqq(1)->Sortkeys(1); |
| 3970 | my $str = $dumper->Dump(); |
| 3971 | if ($str =~ /^\$v/) { |
| 3972 | return '${my ' . $str . ' \$v}'; |
| 3973 | } else { |
| 3974 | return $str; |
| 3975 | } |
| 3976 | } |
| 3977 | |
| 3978 | sub const_sv { |
| 3979 | my $self = shift; |
| 3980 | my $op = shift; |
| 3981 | my $sv = $op->sv; |
| 3982 | # the constant could be in the pad (under useithreads) |
| 3983 | $sv = $self->padval($op->targ) unless $$sv; |
| 3984 | return $sv; |
| 3985 | } |
| 3986 | |
| 3987 | sub pp_const { |
| 3988 | my $self = shift; |
| 3989 | my($op, $cx) = @_; |
| 3990 | if ($op->private & OPpCONST_ARYBASE) { |
| 3991 | return '$['; |
| 3992 | } |
| 3993 | # if ($op->private & OPpCONST_BARE) { # trouble with '=>' autoquoting |
| 3994 | # return $self->const_sv($op)->PV; |
| 3995 | # } |
| 3996 | my $sv = $self->const_sv($op); |
| 3997 | return $self->const($sv, $cx); |
| 3998 | } |
| 3999 | |
| 4000 | sub dq { |
| 4001 | my $self = shift; |
| 4002 | my $op = shift; |
| 4003 | my $type = $op->name; |
| 4004 | if ($type eq "const") { |
| 4005 | return '$[' if $op->private & OPpCONST_ARYBASE; |
| 4006 | return uninterp(escape_str(unback($self->const_sv($op)->as_string))); |
| 4007 | } elsif ($type eq "concat") { |
| 4008 | my $first = $self->dq($op->first); |
| 4009 | my $last = $self->dq($op->last); |
| 4010 | |
| 4011 | # Disambiguate "${foo}bar", "${foo}{bar}", "${foo}[1]", "$foo\::bar" |
| 4012 | ($last =~ /^[A-Z\\\^\[\]_?]/ && |
| 4013 | $first =~ s/([\$@])\^$/${1}{^}/) # "${^}W" etc |
| 4014 | || ($last =~ /^[:'{\[\w_]/ && #' |
| 4015 | $first =~ s/([\$@])([A-Za-z_]\w*)$/${1}{$2}/); |
| 4016 | |
| 4017 | return $first . $last; |
| 4018 | } elsif ($type eq "uc") { |
| 4019 | return '\U' . $self->dq($op->first->sibling) . '\E'; |
| 4020 | } elsif ($type eq "lc") { |
| 4021 | return '\L' . $self->dq($op->first->sibling) . '\E'; |
| 4022 | } elsif ($type eq "ucfirst") { |
| 4023 | return '\u' . $self->dq($op->first->sibling); |
| 4024 | } elsif ($type eq "lcfirst") { |
| 4025 | return '\l' . $self->dq($op->first->sibling); |
| 4026 | } elsif ($type eq "quotemeta") { |
| 4027 | return '\Q' . $self->dq($op->first->sibling) . '\E'; |
| 4028 | } elsif ($type eq "join") { |
| 4029 | return $self->deparse($op->last, 26); # was join($", @ary) |
| 4030 | } else { |
| 4031 | return $self->deparse($op, 26); |
| 4032 | } |
| 4033 | } |
| 4034 | |
| 4035 | sub pp_backtick { |
| 4036 | my $self = shift; |
| 4037 | my($op, $cx) = @_; |
| 4038 | # skip pushmark if it exists (readpipe() vs ``) |
| 4039 | my $child = $op->first->sibling->isa('B::NULL') |
| 4040 | ? $op->first : $op->first->sibling; |
| 4041 | if ($self->pure_string($child)) { |
| 4042 | return single_delim("qx", '`', $self->dq($child, 1)); |
| 4043 | } |
| 4044 | unop($self, @_, "readpipe"); |
| 4045 | } |
| 4046 | |
| 4047 | sub dquote { |
| 4048 | my $self = shift; |
| 4049 | my($op, $cx) = @_; |
| 4050 | my $kid = $op->first->sibling; # skip ex-stringify, pushmark |
| 4051 | return $self->deparse($kid, $cx) if $self->{'unquote'}; |
| 4052 | $self->maybe_targmy($kid, $cx, |
| 4053 | sub {single_delim("qq", '"', $self->dq($_[1]))}); |
| 4054 | } |
| 4055 | |
| 4056 | # OP_STRINGIFY is a listop, but it only ever has one arg |
| 4057 | sub pp_stringify { maybe_targmy(@_, \&dquote) } |
| 4058 | |
| 4059 | # tr/// and s/// (and tr[][], tr[]//, tr###, etc) |
| 4060 | # note that tr(from)/to/ is OK, but not tr/from/(to) |
| 4061 | sub double_delim { |
| 4062 | my($from, $to) = @_; |
| 4063 | my($succeed, $delim); |
| 4064 | if ($from !~ m[/] and $to !~ m[/]) { |
| 4065 | return "/$from/$to/"; |
| 4066 | } elsif (($succeed, $from) = balanced_delim($from) and $succeed) { |
| 4067 | if (($succeed, $to) = balanced_delim($to) and $succeed) { |
| 4068 | return "$from$to"; |
| 4069 | } else { |
| 4070 | for $delim ('/', '"', '#') { # note no "'" -- s''' is special |
| 4071 | return "$from$delim$to$delim" if index($to, $delim) == -1; |
| 4072 | } |
| 4073 | $to =~ s[/][\\/]g; |
| 4074 | return "$from/$to/"; |
| 4075 | } |
| 4076 | } else { |
| 4077 | for $delim ('/', '"', '#') { # note no ' |
| 4078 | return "$delim$from$delim$to$delim" |
| 4079 | if index($to . $from, $delim) == -1; |
| 4080 | } |
| 4081 | $from =~ s[/][\\/]g; |
| 4082 | $to =~ s[/][\\/]g; |
| 4083 | return "/$from/$to/"; |
| 4084 | } |
| 4085 | } |
| 4086 | |
| 4087 | # Only used by tr///, so backslashes hyphens |
| 4088 | sub pchr { # ASCII |
| 4089 | my($n) = @_; |
| 4090 | if ($n == ord '\\') { |
| 4091 | return '\\\\'; |
| 4092 | } elsif ($n == ord "-") { |
| 4093 | return "\\-"; |
| 4094 | } elsif ($n >= ord(' ') and $n <= ord('~')) { |
| 4095 | return chr($n); |
| 4096 | } elsif ($n == ord "\a") { |
| 4097 | return '\\a'; |
| 4098 | } elsif ($n == ord "\b") { |
| 4099 | return '\\b'; |
| 4100 | } elsif ($n == ord "\t") { |
| 4101 | return '\\t'; |
| 4102 | } elsif ($n == ord "\n") { |
| 4103 | return '\\n'; |
| 4104 | } elsif ($n == ord "\e") { |
| 4105 | return '\\e'; |
| 4106 | } elsif ($n == ord "\f") { |
| 4107 | return '\\f'; |
| 4108 | } elsif ($n == ord "\r") { |
| 4109 | return '\\r'; |
| 4110 | } elsif ($n >= ord("\cA") and $n <= ord("\cZ")) { |
| 4111 | return '\\c' . chr(ord("@") + $n); |
| 4112 | } else { |
| 4113 | # return '\x' . sprintf("%02x", $n); |
| 4114 | return '\\' . sprintf("%03o", $n); |
| 4115 | } |
| 4116 | } |
| 4117 | |
| 4118 | sub collapse { |
| 4119 | my(@chars) = @_; |
| 4120 | my($str, $c, $tr) = (""); |
| 4121 | for ($c = 0; $c < @chars; $c++) { |
| 4122 | $tr = $chars[$c]; |
| 4123 | $str .= pchr($tr); |
| 4124 | if ($c <= $#chars - 2 and $chars[$c + 1] == $tr + 1 and |
| 4125 | $chars[$c + 2] == $tr + 2) |
| 4126 | { |
| 4127 | for (; $c <= $#chars-1 and $chars[$c + 1] == $chars[$c] + 1; $c++) |
| 4128 | {} |
| 4129 | $str .= "-"; |
| 4130 | $str .= pchr($chars[$c]); |
| 4131 | } |
| 4132 | } |
| 4133 | return $str; |
| 4134 | } |
| 4135 | |
| 4136 | sub tr_decode_byte { |
| 4137 | my($table, $flags) = @_; |
| 4138 | my(@table) = unpack("s*", $table); |
| 4139 | splice @table, 0x100, 1; # Number of subsequent elements |
| 4140 | my($c, $tr, @from, @to, @delfrom, $delhyphen); |
| 4141 | if ($table[ord "-"] != -1 and |
| 4142 | $table[ord("-") - 1] == -1 || $table[ord("-") + 1] == -1) |
| 4143 | { |
| 4144 | $tr = $table[ord "-"]; |
| 4145 | $table[ord "-"] = -1; |
| 4146 | if ($tr >= 0) { |
| 4147 | @from = ord("-"); |
| 4148 | @to = $tr; |
| 4149 | } else { # -2 ==> delete |
| 4150 | $delhyphen = 1; |
| 4151 | } |
| 4152 | } |
| 4153 | for ($c = 0; $c < @table; $c++) { |
| 4154 | $tr = $table[$c]; |
| 4155 | if ($tr >= 0) { |
| 4156 | push @from, $c; push @to, $tr; |
| 4157 | } elsif ($tr == -2) { |
| 4158 | push @delfrom, $c; |
| 4159 | } |
| 4160 | } |
| 4161 | @from = (@from, @delfrom); |
| 4162 | if ($flags & OPpTRANS_COMPLEMENT) { |
| 4163 | my @newfrom = (); |
| 4164 | my %from; |
| 4165 | @from{@from} = (1) x @from; |
| 4166 | for ($c = 0; $c < 256; $c++) { |
| 4167 | push @newfrom, $c unless $from{$c}; |
| 4168 | } |
| 4169 | @from = @newfrom; |
| 4170 | } |
| 4171 | unless ($flags & OPpTRANS_DELETE || !@to) { |
| 4172 | pop @to while $#to and $to[$#to] == $to[$#to -1]; |
| 4173 | } |
| 4174 | my($from, $to); |
| 4175 | $from = collapse(@from); |
| 4176 | $to = collapse(@to); |
| 4177 | $from .= "-" if $delhyphen; |
| 4178 | return ($from, $to); |
| 4179 | } |
| 4180 | |
| 4181 | sub tr_chr { |
| 4182 | my $x = shift; |
| 4183 | if ($x == ord "-") { |
| 4184 | return "\\-"; |
| 4185 | } elsif ($x == ord "\\") { |
| 4186 | return "\\\\"; |
| 4187 | } else { |
| 4188 | return chr $x; |
| 4189 | } |
| 4190 | } |
| 4191 | |
| 4192 | # XXX This doesn't yet handle all cases correctly either |
| 4193 | |
| 4194 | sub tr_decode_utf8 { |
| 4195 | my($swash_hv, $flags) = @_; |
| 4196 | my %swash = $swash_hv->ARRAY; |
| 4197 | my $final = undef; |
| 4198 | $final = $swash{'FINAL'}->IV if exists $swash{'FINAL'}; |
| 4199 | my $none = $swash{"NONE"}->IV; |
| 4200 | my $extra = $none + 1; |
| 4201 | my(@from, @delfrom, @to); |
| 4202 | my $line; |
| 4203 | foreach $line (split /\n/, $swash{'LIST'}->PV) { |
| 4204 | my($min, $max, $result) = split(/\t/, $line); |
| 4205 | $min = hex $min; |
| 4206 | if (length $max) { |
| 4207 | $max = hex $max; |
| 4208 | } else { |
| 4209 | $max = $min; |
| 4210 | } |
| 4211 | $result = hex $result; |
| 4212 | if ($result == $extra) { |
| 4213 | push @delfrom, [$min, $max]; |
| 4214 | } else { |
| 4215 | push @from, [$min, $max]; |
| 4216 | push @to, [$result, $result + $max - $min]; |
| 4217 | } |
| 4218 | } |
| 4219 | for my $i (0 .. $#from) { |
| 4220 | if ($from[$i][0] == ord '-') { |
| 4221 | unshift @from, splice(@from, $i, 1); |
| 4222 | unshift @to, splice(@to, $i, 1); |
| 4223 | last; |
| 4224 | } elsif ($from[$i][1] == ord '-') { |
| 4225 | $from[$i][1]--; |
| 4226 | $to[$i][1]--; |
| 4227 | unshift @from, ord '-'; |
| 4228 | unshift @to, ord '-'; |
| 4229 | last; |
| 4230 | } |
| 4231 | } |
| 4232 | for my $i (0 .. $#delfrom) { |
| 4233 | if ($delfrom[$i][0] == ord '-') { |
| 4234 | push @delfrom, splice(@delfrom, $i, 1); |
| 4235 | last; |
| 4236 | } elsif ($delfrom[$i][1] == ord '-') { |
| 4237 | $delfrom[$i][1]--; |
| 4238 | push @delfrom, ord '-'; |
| 4239 | last; |
| 4240 | } |
| 4241 | } |
| 4242 | if (defined $final and $to[$#to][1] != $final) { |
| 4243 | push @to, [$final, $final]; |
| 4244 | } |
| 4245 | push @from, @delfrom; |
| 4246 | if ($flags & OPpTRANS_COMPLEMENT) { |
| 4247 | my @newfrom; |
| 4248 | my $next = 0; |
| 4249 | for my $i (0 .. $#from) { |
| 4250 | push @newfrom, [$next, $from[$i][0] - 1]; |
| 4251 | $next = $from[$i][1] + 1; |
| 4252 | } |
| 4253 | @from = (); |
| 4254 | for my $range (@newfrom) { |
| 4255 | if ($range->[0] <= $range->[1]) { |
| 4256 | push @from, $range; |
| 4257 | } |
| 4258 | } |
| 4259 | } |
| 4260 | my($from, $to, $diff); |
| 4261 | for my $chunk (@from) { |
| 4262 | $diff = $chunk->[1] - $chunk->[0]; |
| 4263 | if ($diff > 1) { |
| 4264 | $from .= tr_chr($chunk->[0]) . "-" . tr_chr($chunk->[1]); |
| 4265 | } elsif ($diff == 1) { |
| 4266 | $from .= tr_chr($chunk->[0]) . tr_chr($chunk->[1]); |
| 4267 | } else { |
| 4268 | $from .= tr_chr($chunk->[0]); |
| 4269 | } |
| 4270 | } |
| 4271 | for my $chunk (@to) { |
| 4272 | $diff = $chunk->[1] - $chunk->[0]; |
| 4273 | if ($diff > 1) { |
| 4274 | $to .= tr_chr($chunk->[0]) . "-" . tr_chr($chunk->[1]); |
| 4275 | } elsif ($diff == 1) { |
| 4276 | $to .= tr_chr($chunk->[0]) . tr_chr($chunk->[1]); |
| 4277 | } else { |
| 4278 | $to .= tr_chr($chunk->[0]); |
| 4279 | } |
| 4280 | } |
| 4281 | #$final = sprintf("%04x", $final) if defined $final; |
| 4282 | #$none = sprintf("%04x", $none) if defined $none; |
| 4283 | #$extra = sprintf("%04x", $extra) if defined $extra; |
| 4284 | #print STDERR "final: $final\n none: $none\nextra: $extra\n"; |
| 4285 | #print STDERR $swash{'LIST'}->PV; |
| 4286 | return (escape_str($from), escape_str($to)); |
| 4287 | } |
| 4288 | |
| 4289 | sub pp_trans { |
| 4290 | my $self = shift; |
| 4291 | my($op, $cx) = @_; |
| 4292 | my($from, $to); |
| 4293 | my $class = class($op); |
| 4294 | my $priv_flags = $op->private; |
| 4295 | if ($class eq "PVOP") { |
| 4296 | ($from, $to) = tr_decode_byte($op->pv, $priv_flags); |
| 4297 | } elsif ($class eq "PADOP") { |
| 4298 | ($from, $to) |
| 4299 | = tr_decode_utf8($self->padval($op->padix)->RV, $priv_flags); |
| 4300 | } else { # class($op) eq "SVOP" |
| 4301 | ($from, $to) = tr_decode_utf8($op->sv->RV, $priv_flags); |
| 4302 | } |
| 4303 | my $flags = ""; |
| 4304 | $flags .= "c" if $priv_flags & OPpTRANS_COMPLEMENT; |
| 4305 | $flags .= "d" if $priv_flags & OPpTRANS_DELETE; |
| 4306 | $to = "" if $from eq $to and $flags eq ""; |
| 4307 | $flags .= "s" if $priv_flags & OPpTRANS_SQUASH; |
| 4308 | return "tr" . double_delim($from, $to) . $flags; |
| 4309 | } |
| 4310 | |
| 4311 | sub pp_transr { &pp_trans . 'r' } |
| 4312 | |
| 4313 | sub re_dq_disambiguate { |
| 4314 | my ($first, $last) = @_; |
| 4315 | # Disambiguate "${foo}bar", "${foo}{bar}", "${foo}[1]" |
| 4316 | ($last =~ /^[A-Z\\\^\[\]_?]/ && |
| 4317 | $first =~ s/([\$@])\^$/${1}{^}/) # "${^}W" etc |
| 4318 | || ($last =~ /^[{\[\w_]/ && |
| 4319 | $first =~ s/([\$@])([A-Za-z_]\w*)$/${1}{$2}/); |
| 4320 | return $first . $last; |
| 4321 | } |
| 4322 | |
| 4323 | # Like dq(), but different |
| 4324 | sub re_dq { |
| 4325 | my $self = shift; |
| 4326 | my ($op, $extended) = @_; |
| 4327 | |
| 4328 | my $type = $op->name; |
| 4329 | if ($type eq "const") { |
| 4330 | return '$[' if $op->private & OPpCONST_ARYBASE; |
| 4331 | my $unbacked = re_unback($self->const_sv($op)->as_string); |
| 4332 | return re_uninterp_extended(escape_extended_re($unbacked)) |
| 4333 | if $extended; |
| 4334 | return re_uninterp(escape_str($unbacked)); |
| 4335 | } elsif ($type eq "concat") { |
| 4336 | my $first = $self->re_dq($op->first, $extended); |
| 4337 | my $last = $self->re_dq($op->last, $extended); |
| 4338 | return re_dq_disambiguate($first, $last); |
| 4339 | } elsif ($type eq "uc") { |
| 4340 | return '\U' . $self->re_dq($op->first->sibling, $extended) . '\E'; |
| 4341 | } elsif ($type eq "lc") { |
| 4342 | return '\L' . $self->re_dq($op->first->sibling, $extended) . '\E'; |
| 4343 | } elsif ($type eq "ucfirst") { |
| 4344 | return '\u' . $self->re_dq($op->first->sibling, $extended); |
| 4345 | } elsif ($type eq "lcfirst") { |
| 4346 | return '\l' . $self->re_dq($op->first->sibling, $extended); |
| 4347 | } elsif ($type eq "quotemeta") { |
| 4348 | return '\Q' . $self->re_dq($op->first->sibling, $extended) . '\E'; |
| 4349 | } elsif ($type eq "join") { |
| 4350 | return $self->deparse($op->last, 26); # was join($", @ary) |
| 4351 | } else { |
| 4352 | return $self->deparse($op, 26); |
| 4353 | } |
| 4354 | } |
| 4355 | |
| 4356 | sub pure_string { |
| 4357 | my ($self, $op) = @_; |
| 4358 | return 0 if null $op; |
| 4359 | my $type = $op->name; |
| 4360 | |
| 4361 | if ($type eq 'const') { |
| 4362 | return 1; |
| 4363 | } |
| 4364 | elsif ($type =~ /^[ul]c(first)?$/ || $type eq 'quotemeta') { |
| 4365 | return $self->pure_string($op->first->sibling); |
| 4366 | } |
| 4367 | elsif ($type eq 'join') { |
| 4368 | my $join_op = $op->first->sibling; # Skip pushmark |
| 4369 | return 0 unless $join_op->name eq 'null' && $join_op->targ eq OP_RV2SV; |
| 4370 | |
| 4371 | my $gvop = $join_op->first; |
| 4372 | return 0 unless $gvop->name eq 'gvsv'; |
| 4373 | return 0 unless '"' eq $self->gv_name($self->gv_or_padgv($gvop)); |
| 4374 | |
| 4375 | return 0 unless ${$join_op->sibling} eq ${$op->last}; |
| 4376 | return 0 unless $op->last->name =~ /^(?:[ah]slice|(?:rv2|pad)av)$/; |
| 4377 | } |
| 4378 | elsif ($type eq 'concat') { |
| 4379 | return $self->pure_string($op->first) |
| 4380 | && $self->pure_string($op->last); |
| 4381 | } |
| 4382 | elsif (is_scalar($op) || $type =~ /^[ah]elem$/) { |
| 4383 | return 1; |
| 4384 | } |
| 4385 | elsif ($type eq "null" and $op->can('first') and not null $op->first and |
| 4386 | ($op->first->name eq "null" and $op->first->can('first') |
| 4387 | and not null $op->first->first and |
| 4388 | $op->first->first->name eq "aelemfast" |
| 4389 | or |
| 4390 | $op->first->name =~ /^aelemfast(?:_lex)?\z/ |
| 4391 | )) { |
| 4392 | return 1; |
| 4393 | } |
| 4394 | else { |
| 4395 | return 0; |
| 4396 | } |
| 4397 | |
| 4398 | return 1; |
| 4399 | } |
| 4400 | |
| 4401 | sub regcomp { |
| 4402 | my $self = shift; |
| 4403 | my($op, $cx, $extended) = @_; |
| 4404 | my $kid = $op->first; |
| 4405 | $kid = $kid->first if $kid->name eq "regcmaybe"; |
| 4406 | $kid = $kid->first if $kid->name eq "regcreset"; |
| 4407 | if ($kid->name eq "null" and !null($kid->first) |
| 4408 | and $kid->first->name eq 'pushmark') |
| 4409 | { |
| 4410 | my $str = ''; |
| 4411 | $kid = $kid->first->sibling; |
| 4412 | while (!null($kid)) { |
| 4413 | my $first = $str; |
| 4414 | my $last = $self->re_dq($kid, $extended); |
| 4415 | $str = re_dq_disambiguate($first, $last); |
| 4416 | $kid = $kid->sibling; |
| 4417 | } |
| 4418 | return $str, 1; |
| 4419 | } |
| 4420 | |
| 4421 | return ($self->re_dq($kid, $extended), 1) if $self->pure_string($kid); |
| 4422 | return ($self->deparse($kid, $cx), 0); |
| 4423 | } |
| 4424 | |
| 4425 | sub pp_regcomp { |
| 4426 | my ($self, $op, $cx) = @_; |
| 4427 | return (($self->regcomp($op, $cx, 0))[0]); |
| 4428 | } |
| 4429 | |
| 4430 | sub re_flags { |
| 4431 | my ($self, $op) = @_; |
| 4432 | my $flags = ''; |
| 4433 | my $pmflags = $op->pmflags; |
| 4434 | $flags .= "g" if $pmflags & PMf_GLOBAL; |
| 4435 | $flags .= "i" if $pmflags & PMf_FOLD; |
| 4436 | $flags .= "m" if $pmflags & PMf_MULTILINE; |
| 4437 | $flags .= "o" if $pmflags & PMf_KEEP; |
| 4438 | $flags .= "s" if $pmflags & PMf_SINGLELINE; |
| 4439 | $flags .= "x" if $pmflags & PMf_EXTENDED; |
| 4440 | $flags .= "p" if $pmflags & RXf_PMf_KEEPCOPY; |
| 4441 | if (my $charset = $pmflags & RXf_PMf_CHARSET) { |
| 4442 | # Hardcoding this is fragile, but B does not yet export the |
| 4443 | # constants we need. |
| 4444 | $flags .= qw(d l u a aa)[$charset >> 5] |
| 4445 | } |
| 4446 | # The /d flag is indicated by 0; only show it if necessary. |
| 4447 | elsif ($self->{hinthash} and |
| 4448 | $self->{hinthash}{reflags_charset} |
| 4449 | || $self->{hinthash}{feature_unicode}) { |
| 4450 | $flags .= 'd'; |
| 4451 | } |
| 4452 | $flags; |
| 4453 | } |
| 4454 | |
| 4455 | # osmic acid -- see osmium tetroxide |
| 4456 | |
| 4457 | my %matchwords; |
| 4458 | map($matchwords{join "", sort split //, $_} = $_, 'cig', 'cog', 'cos', 'cogs', |
| 4459 | 'cox', 'go', 'is', 'ism', 'iso', 'mig', 'mix', 'osmic', 'ox', 'sic', |
| 4460 | 'sig', 'six', 'smog', 'so', 'soc', 'sog', 'xi'); |
| 4461 | |
| 4462 | sub matchop { |
| 4463 | my $self = shift; |
| 4464 | my($op, $cx, $name, $delim) = @_; |
| 4465 | my $kid = $op->first; |
| 4466 | my ($binop, $var, $re) = ("", "", ""); |
| 4467 | if ($op->flags & OPf_STACKED) { |
| 4468 | $binop = 1; |
| 4469 | $var = $self->deparse($kid, 20); |
| 4470 | $kid = $kid->sibling; |
| 4471 | } |
| 4472 | my $quote = 1; |
| 4473 | my $pmflags = $op->pmflags; |
| 4474 | my $extended = ($pmflags & PMf_EXTENDED); |
| 4475 | my $rhs_bound_to_defsv; |
| 4476 | if (null $kid) { |
| 4477 | my $unbacked = re_unback($op->precomp); |
| 4478 | if ($extended) { |
| 4479 | $re = re_uninterp_extended(escape_extended_re($unbacked)); |
| 4480 | } else { |
| 4481 | $re = re_uninterp(escape_str(re_unback($op->precomp))); |
| 4482 | } |
| 4483 | } elsif ($kid->name ne 'regcomp') { |
| 4484 | carp("found ".$kid->name." where regcomp expected"); |
| 4485 | } else { |
| 4486 | ($re, $quote) = $self->regcomp($kid, 21, $extended); |
| 4487 | my $matchop = $kid->first->first; |
| 4488 | if ($matchop->name =~ /^(?:match|transr?|subst)\z/ |
| 4489 | && $matchop->flags & OPf_SPECIAL) { |
| 4490 | $rhs_bound_to_defsv = 1; |
| 4491 | } |
| 4492 | } |
| 4493 | my $flags = ""; |
| 4494 | $flags .= "c" if $pmflags & PMf_CONTINUE; |
| 4495 | $flags .= $self->re_flags($op); |
| 4496 | $flags = join '', sort split //, $flags; |
| 4497 | $flags = $matchwords{$flags} if $matchwords{$flags}; |
| 4498 | if ($pmflags & PMf_ONCE) { # only one kind of delimiter works here |
| 4499 | $re =~ s/\?/\\?/g; |
| 4500 | $re = "?$re?"; |
| 4501 | } elsif ($quote) { |
| 4502 | $re = single_delim($name, $delim, $re); |
| 4503 | } |
| 4504 | $re = $re . $flags if $quote; |
| 4505 | if ($binop) { |
| 4506 | return |
| 4507 | $self->maybe_parens( |
| 4508 | $rhs_bound_to_defsv |
| 4509 | ? "$var =~ (\$_ =~ $re)" |
| 4510 | : "$var =~ $re", |
| 4511 | $cx, 20 |
| 4512 | ); |
| 4513 | } else { |
| 4514 | return $re; |
| 4515 | } |
| 4516 | } |
| 4517 | |
| 4518 | sub pp_match { matchop(@_, "m", "/") } |
| 4519 | sub pp_pushre { matchop(@_, "m", "/") } |
| 4520 | sub pp_qr { matchop(@_, "qr", "") } |
| 4521 | |
| 4522 | sub pp_runcv { unop(@_, "__SUB__"); } |
| 4523 | |
| 4524 | sub pp_split { |
| 4525 | my $self = shift; |
| 4526 | my($op, $cx) = @_; |
| 4527 | my($kid, @exprs, $ary, $expr); |
| 4528 | $kid = $op->first; |
| 4529 | |
| 4530 | # For our kid (an OP_PUSHRE), pmreplroot is never actually the |
| 4531 | # root of a replacement; it's either empty, or abused to point to |
| 4532 | # the GV for an array we split into (an optimization to save |
| 4533 | # assignment overhead). Depending on whether we're using ithreads, |
| 4534 | # this OP* holds either a GV* or a PADOFFSET. Luckily, B.xs |
| 4535 | # figures out for us which it is. |
| 4536 | my $replroot = $kid->pmreplroot; |
| 4537 | my $gv = 0; |
| 4538 | if (ref($replroot) eq "B::GV") { |
| 4539 | $gv = $replroot; |
| 4540 | } elsif (!ref($replroot) and $replroot > 0) { |
| 4541 | $gv = $self->padval($replroot); |
| 4542 | } |
| 4543 | $ary = $self->stash_variable('@', $self->gv_name($gv), $cx) if $gv; |
| 4544 | |
| 4545 | for (; !null($kid); $kid = $kid->sibling) { |
| 4546 | push @exprs, $self->deparse($kid, 6); |
| 4547 | } |
| 4548 | |
| 4549 | # handle special case of split(), and split(' ') that compiles to /\s+/ |
| 4550 | # Under 5.10, the reflags may be undef if the split regexp isn't a constant |
| 4551 | $kid = $op->first; |
| 4552 | if ( $kid->flags & OPf_SPECIAL |
| 4553 | and ( $] < 5.009 ? $kid->pmflags & PMf_SKIPWHITE() |
| 4554 | : ($kid->reflags || 0) & RXf_SKIPWHITE() ) ) { |
| 4555 | $exprs[0] = "' '"; |
| 4556 | } |
| 4557 | |
| 4558 | $expr = "split(" . join(", ", @exprs) . ")"; |
| 4559 | if ($ary) { |
| 4560 | return $self->maybe_parens("$ary = $expr", $cx, 7); |
| 4561 | } else { |
| 4562 | return $expr; |
| 4563 | } |
| 4564 | } |
| 4565 | |
| 4566 | # oxime -- any of various compounds obtained chiefly by the action of |
| 4567 | # hydroxylamine on aldehydes and ketones and characterized by the |
| 4568 | # bivalent grouping C=NOH [Webster's Tenth] |
| 4569 | |
| 4570 | my %substwords; |
| 4571 | map($substwords{join "", sort split //, $_} = $_, 'ego', 'egoism', 'em', |
| 4572 | 'es', 'ex', 'exes', 'gee', 'go', 'goes', 'ie', 'ism', 'iso', 'me', |
| 4573 | 'meese', 'meso', 'mig', 'mix', 'os', 'ox', 'oxime', 'see', 'seem', |
| 4574 | 'seg', 'sex', 'sig', 'six', 'smog', 'sog', 'some', 'xi', 'rogue', |
| 4575 | 'sir', 'rise', 'smore', 'more', 'seer', 'rome', 'gore', 'grim', 'grime', |
| 4576 | 'or', 'rose', 'rosie'); |
| 4577 | |
| 4578 | sub pp_subst { |
| 4579 | my $self = shift; |
| 4580 | my($op, $cx) = @_; |
| 4581 | my $kid = $op->first; |
| 4582 | my($binop, $var, $re, $repl) = ("", "", "", ""); |
| 4583 | if ($op->flags & OPf_STACKED) { |
| 4584 | $binop = 1; |
| 4585 | $var = $self->deparse($kid, 20); |
| 4586 | $kid = $kid->sibling; |
| 4587 | } |
| 4588 | my $flags = ""; |
| 4589 | my $pmflags = $op->pmflags; |
| 4590 | if (null($op->pmreplroot)) { |
| 4591 | $repl = $self->dq($kid); |
| 4592 | $kid = $kid->sibling; |
| 4593 | } else { |
| 4594 | $repl = $op->pmreplroot->first; # skip substcont |
| 4595 | while ($repl->name eq "entereval") { |
| 4596 | $repl = $repl->first; |
| 4597 | $flags .= "e"; |
| 4598 | } |
| 4599 | if ($pmflags & PMf_EVAL) { |
| 4600 | $repl = $self->deparse($repl->first, 0); |
| 4601 | } else { |
| 4602 | $repl = $self->dq($repl); |
| 4603 | } |
| 4604 | } |
| 4605 | my $extended = ($pmflags & PMf_EXTENDED); |
| 4606 | if (null $kid) { |
| 4607 | my $unbacked = re_unback($op->precomp); |
| 4608 | if ($extended) { |
| 4609 | $re = re_uninterp_extended(escape_extended_re($unbacked)); |
| 4610 | } |
| 4611 | else { |
| 4612 | $re = re_uninterp(escape_str($unbacked)); |
| 4613 | } |
| 4614 | } else { |
| 4615 | ($re) = $self->regcomp($kid, 1, $extended); |
| 4616 | } |
| 4617 | $flags .= "r" if $pmflags & PMf_NONDESTRUCT; |
| 4618 | $flags .= "e" if $pmflags & PMf_EVAL; |
| 4619 | $flags .= $self->re_flags($op); |
| 4620 | $flags = join '', sort split //, $flags; |
| 4621 | $flags = $substwords{$flags} if $substwords{$flags}; |
| 4622 | if ($binop) { |
| 4623 | return $self->maybe_parens("$var =~ s" |
| 4624 | . double_delim($re, $repl) . $flags, |
| 4625 | $cx, 20); |
| 4626 | } else { |
| 4627 | return "s". double_delim($re, $repl) . $flags; |
| 4628 | } |
| 4629 | } |
| 4630 | |
| 4631 | 1; |
| 4632 | __END__ |
| 4633 | |
| 4634 | =head1 NAME |
| 4635 | |
| 4636 | B::Deparse - Perl compiler backend to produce perl code |
| 4637 | |
| 4638 | =head1 SYNOPSIS |
| 4639 | |
| 4640 | B<perl> B<-MO=Deparse>[B<,-d>][B<,-f>I<FILE>][B<,-p>][B<,-q>][B<,-l>] |
| 4641 | [B<,-s>I<LETTERS>][B<,-x>I<LEVEL>] I<prog.pl> |
| 4642 | |
| 4643 | =head1 DESCRIPTION |
| 4644 | |
| 4645 | B::Deparse is a backend module for the Perl compiler that generates |
| 4646 | perl source code, based on the internal compiled structure that perl |
| 4647 | itself creates after parsing a program. The output of B::Deparse won't |
| 4648 | be exactly the same as the original source, since perl doesn't keep |
| 4649 | track of comments or whitespace, and there isn't a one-to-one |
| 4650 | correspondence between perl's syntactical constructions and their |
| 4651 | compiled form, but it will often be close. When you use the B<-p> |
| 4652 | option, the output also includes parentheses even when they are not |
| 4653 | required by precedence, which can make it easy to see if perl is |
| 4654 | parsing your expressions the way you intended. |
| 4655 | |
| 4656 | While B::Deparse goes to some lengths to try to figure out what your |
| 4657 | original program was doing, some parts of the language can still trip |
| 4658 | it up; it still fails even on some parts of Perl's own test suite. If |
| 4659 | you encounter a failure other than the most common ones described in |
| 4660 | the BUGS section below, you can help contribute to B::Deparse's |
| 4661 | ongoing development by submitting a bug report with a small |
| 4662 | example. |
| 4663 | |
| 4664 | =head1 OPTIONS |
| 4665 | |
| 4666 | As with all compiler backend options, these must follow directly after |
| 4667 | the '-MO=Deparse', separated by a comma but not any white space. |
| 4668 | |
| 4669 | =over 4 |
| 4670 | |
| 4671 | =item B<-d> |
| 4672 | |
| 4673 | Output data values (when they appear as constants) using Data::Dumper. |
| 4674 | Without this option, B::Deparse will use some simple routines of its |
| 4675 | own for the same purpose. Currently, Data::Dumper is better for some |
| 4676 | kinds of data (such as complex structures with sharing and |
| 4677 | self-reference) while the built-in routines are better for others |
| 4678 | (such as odd floating-point values). |
| 4679 | |
| 4680 | =item B<-f>I<FILE> |
| 4681 | |
| 4682 | Normally, B::Deparse deparses the main code of a program, and all the subs |
| 4683 | defined in the same file. To include subs defined in |
| 4684 | other files, pass the B<-f> option with the filename. |
| 4685 | You can pass the B<-f> option several times, to |
| 4686 | include more than one secondary file. (Most of the time you don't want to |
| 4687 | use it at all.) You can also use this option to include subs which are |
| 4688 | defined in the scope of a B<#line> directive with two parameters. |
| 4689 | |
| 4690 | =item B<-l> |
| 4691 | |
| 4692 | Add '#line' declarations to the output based on the line and file |
| 4693 | locations of the original code. |
| 4694 | |
| 4695 | =item B<-p> |
| 4696 | |
| 4697 | Print extra parentheses. Without this option, B::Deparse includes |
| 4698 | parentheses in its output only when they are needed, based on the |
| 4699 | structure of your program. With B<-p>, it uses parentheses (almost) |
| 4700 | whenever they would be legal. This can be useful if you are used to |
| 4701 | LISP, or if you want to see how perl parses your input. If you say |
| 4702 | |
| 4703 | if ($var & 0x7f == 65) {print "Gimme an A!"} |
| 4704 | print ($which ? $a : $b), "\n"; |
| 4705 | $name = $ENV{USER} or "Bob"; |
| 4706 | |
| 4707 | C<B::Deparse,-p> will print |
| 4708 | |
| 4709 | if (($var & 0)) { |
| 4710 | print('Gimme an A!') |
| 4711 | }; |
| 4712 | (print(($which ? $a : $b)), '???'); |
| 4713 | (($name = $ENV{'USER'}) or '???') |
| 4714 | |
| 4715 | which probably isn't what you intended (the C<'???'> is a sign that |
| 4716 | perl optimized away a constant value). |
| 4717 | |
| 4718 | =item B<-P> |
| 4719 | |
| 4720 | Disable prototype checking. With this option, all function calls are |
| 4721 | deparsed as if no prototype was defined for them. In other words, |
| 4722 | |
| 4723 | perl -MO=Deparse,-P -e 'sub foo (\@) { 1 } foo @x' |
| 4724 | |
| 4725 | will print |
| 4726 | |
| 4727 | sub foo (\@) { |
| 4728 | 1; |
| 4729 | } |
| 4730 | &foo(\@x); |
| 4731 | |
| 4732 | making clear how the parameters are actually passed to C<foo>. |
| 4733 | |
| 4734 | =item B<-q> |
| 4735 | |
| 4736 | Expand double-quoted strings into the corresponding combinations of |
| 4737 | concatenation, uc, ucfirst, lc, lcfirst, quotemeta, and join. For |
| 4738 | instance, print |
| 4739 | |
| 4740 | print "Hello, $world, @ladies, \u$gentlemen\E, \u\L$me!"; |
| 4741 | |
| 4742 | as |
| 4743 | |
| 4744 | print 'Hello, ' . $world . ', ' . join($", @ladies) . ', ' |
| 4745 | . ucfirst($gentlemen) . ', ' . ucfirst(lc $me . '!'); |
| 4746 | |
| 4747 | Note that the expanded form represents the way perl handles such |
| 4748 | constructions internally -- this option actually turns off the reverse |
| 4749 | translation that B::Deparse usually does. On the other hand, note that |
| 4750 | C<$x = "$y"> is not the same as C<$x = $y>: the former makes the value |
| 4751 | of $y into a string before doing the assignment. |
| 4752 | |
| 4753 | =item B<-s>I<LETTERS> |
| 4754 | |
| 4755 | Tweak the style of B::Deparse's output. The letters should follow |
| 4756 | directly after the 's', with no space or punctuation. The following |
| 4757 | options are available: |
| 4758 | |
| 4759 | =over 4 |
| 4760 | |
| 4761 | =item B<C> |
| 4762 | |
| 4763 | Cuddle C<elsif>, C<else>, and C<continue> blocks. For example, print |
| 4764 | |
| 4765 | if (...) { |
| 4766 | ... |
| 4767 | } else { |
| 4768 | ... |
| 4769 | } |
| 4770 | |
| 4771 | instead of |
| 4772 | |
| 4773 | if (...) { |
| 4774 | ... |
| 4775 | } |
| 4776 | else { |
| 4777 | ... |
| 4778 | } |
| 4779 | |
| 4780 | The default is not to cuddle. |
| 4781 | |
| 4782 | =item B<i>I<NUMBER> |
| 4783 | |
| 4784 | Indent lines by multiples of I<NUMBER> columns. The default is 4 columns. |
| 4785 | |
| 4786 | =item B<T> |
| 4787 | |
| 4788 | Use tabs for each 8 columns of indent. The default is to use only spaces. |
| 4789 | For instance, if the style options are B<-si4T>, a line that's indented |
| 4790 | 3 times will be preceded by one tab and four spaces; if the options were |
| 4791 | B<-si8T>, the same line would be preceded by three tabs. |
| 4792 | |
| 4793 | =item B<v>I<STRING>B<.> |
| 4794 | |
| 4795 | Print I<STRING> for the value of a constant that can't be determined |
| 4796 | because it was optimized away (mnemonic: this happens when a constant |
| 4797 | is used in B<v>oid context). The end of the string is marked by a period. |
| 4798 | The string should be a valid perl expression, generally a constant. |
| 4799 | Note that unless it's a number, it probably needs to be quoted, and on |
| 4800 | a command line quotes need to be protected from the shell. Some |
| 4801 | conventional values include 0, 1, 42, '', 'foo', and |
| 4802 | 'Useless use of constant omitted' (which may need to be |
| 4803 | B<-sv"'Useless use of constant omitted'."> |
| 4804 | or something similar depending on your shell). The default is '???'. |
| 4805 | If you're using B::Deparse on a module or other file that's require'd, |
| 4806 | you shouldn't use a value that evaluates to false, since the customary |
| 4807 | true constant at the end of a module will be in void context when the |
| 4808 | file is compiled as a main program. |
| 4809 | |
| 4810 | =back |
| 4811 | |
| 4812 | =item B<-x>I<LEVEL> |
| 4813 | |
| 4814 | Expand conventional syntax constructions into equivalent ones that expose |
| 4815 | their internal operation. I<LEVEL> should be a digit, with higher values |
| 4816 | meaning more expansion. As with B<-q>, this actually involves turning off |
| 4817 | special cases in B::Deparse's normal operations. |
| 4818 | |
| 4819 | If I<LEVEL> is at least 3, C<for> loops will be translated into equivalent |
| 4820 | while loops with continue blocks; for instance |
| 4821 | |
| 4822 | for ($i = 0; $i < 10; ++$i) { |
| 4823 | print $i; |
| 4824 | } |
| 4825 | |
| 4826 | turns into |
| 4827 | |
| 4828 | $i = 0; |
| 4829 | while ($i < 10) { |
| 4830 | print $i; |
| 4831 | } continue { |
| 4832 | ++$i |
| 4833 | } |
| 4834 | |
| 4835 | Note that in a few cases this translation can't be perfectly carried back |
| 4836 | into the source code -- if the loop's initializer declares a my variable, |
| 4837 | for instance, it won't have the correct scope outside of the loop. |
| 4838 | |
| 4839 | If I<LEVEL> is at least 5, C<use> declarations will be translated into |
| 4840 | C<BEGIN> blocks containing calls to C<require> and C<import>; for |
| 4841 | instance, |
| 4842 | |
| 4843 | use strict 'refs'; |
| 4844 | |
| 4845 | turns into |
| 4846 | |
| 4847 | sub BEGIN { |
| 4848 | require strict; |
| 4849 | do { |
| 4850 | 'strict'->import('refs') |
| 4851 | }; |
| 4852 | } |
| 4853 | |
| 4854 | If I<LEVEL> is at least 7, C<if> statements will be translated into |
| 4855 | equivalent expressions using C<&&>, C<?:> and C<do {}>; for instance |
| 4856 | |
| 4857 | print 'hi' if $nice; |
| 4858 | if ($nice) { |
| 4859 | print 'hi'; |
| 4860 | } |
| 4861 | if ($nice) { |
| 4862 | print 'hi'; |
| 4863 | } else { |
| 4864 | print 'bye'; |
| 4865 | } |
| 4866 | |
| 4867 | turns into |
| 4868 | |
| 4869 | $nice and print 'hi'; |
| 4870 | $nice and do { print 'hi' }; |
| 4871 | $nice ? do { print 'hi' } : do { print 'bye' }; |
| 4872 | |
| 4873 | Long sequences of elsifs will turn into nested ternary operators, which |
| 4874 | B::Deparse doesn't know how to indent nicely. |
| 4875 | |
| 4876 | =back |
| 4877 | |
| 4878 | =head1 USING B::Deparse AS A MODULE |
| 4879 | |
| 4880 | =head2 Synopsis |
| 4881 | |
| 4882 | use B::Deparse; |
| 4883 | $deparse = B::Deparse->new("-p", "-sC"); |
| 4884 | $body = $deparse->coderef2text(\&func); |
| 4885 | eval "sub func $body"; # the inverse operation |
| 4886 | |
| 4887 | =head2 Description |
| 4888 | |
| 4889 | B::Deparse can also be used on a sub-by-sub basis from other perl |
| 4890 | programs. |
| 4891 | |
| 4892 | =head2 new |
| 4893 | |
| 4894 | $deparse = B::Deparse->new(OPTIONS) |
| 4895 | |
| 4896 | Create an object to store the state of a deparsing operation and any |
| 4897 | options. The options are the same as those that can be given on the |
| 4898 | command line (see L</OPTIONS>); options that are separated by commas |
| 4899 | after B<-MO=Deparse> should be given as separate strings. |
| 4900 | |
| 4901 | =head2 ambient_pragmas |
| 4902 | |
| 4903 | $deparse->ambient_pragmas(strict => 'all', '$[' => $[); |
| 4904 | |
| 4905 | The compilation of a subroutine can be affected by a few compiler |
| 4906 | directives, B<pragmas>. These are: |
| 4907 | |
| 4908 | =over 4 |
| 4909 | |
| 4910 | =item * |
| 4911 | |
| 4912 | use strict; |
| 4913 | |
| 4914 | =item * |
| 4915 | |
| 4916 | use warnings; |
| 4917 | |
| 4918 | =item * |
| 4919 | |
| 4920 | Assigning to the special variable $[ |
| 4921 | |
| 4922 | =item * |
| 4923 | |
| 4924 | use integer; |
| 4925 | |
| 4926 | =item * |
| 4927 | |
| 4928 | use bytes; |
| 4929 | |
| 4930 | =item * |
| 4931 | |
| 4932 | use utf8; |
| 4933 | |
| 4934 | =item * |
| 4935 | |
| 4936 | use re; |
| 4937 | |
| 4938 | =back |
| 4939 | |
| 4940 | Ordinarily, if you use B::Deparse on a subroutine which has |
| 4941 | been compiled in the presence of one or more of these pragmas, |
| 4942 | the output will include statements to turn on the appropriate |
| 4943 | directives. So if you then compile the code returned by coderef2text, |
| 4944 | it will behave the same way as the subroutine which you deparsed. |
| 4945 | |
| 4946 | However, you may know that you intend to use the results in a |
| 4947 | particular context, where some pragmas are already in scope. In |
| 4948 | this case, you use the B<ambient_pragmas> method to describe the |
| 4949 | assumptions you wish to make. |
| 4950 | |
| 4951 | Not all of the options currently have any useful effect. See |
| 4952 | L</BUGS> for more details. |
| 4953 | |
| 4954 | The parameters it accepts are: |
| 4955 | |
| 4956 | =over 4 |
| 4957 | |
| 4958 | =item strict |
| 4959 | |
| 4960 | Takes a string, possibly containing several values separated |
| 4961 | by whitespace. The special values "all" and "none" mean what you'd |
| 4962 | expect. |
| 4963 | |
| 4964 | $deparse->ambient_pragmas(strict => 'subs refs'); |
| 4965 | |
| 4966 | =item $[ |
| 4967 | |
| 4968 | Takes a number, the value of the array base $[. |
| 4969 | Cannot be non-zero on Perl 5.15.3 or later. |
| 4970 | |
| 4971 | =item bytes |
| 4972 | |
| 4973 | =item utf8 |
| 4974 | |
| 4975 | =item integer |
| 4976 | |
| 4977 | If the value is true, then the appropriate pragma is assumed to |
| 4978 | be in the ambient scope, otherwise not. |
| 4979 | |
| 4980 | =item re |
| 4981 | |
| 4982 | Takes a string, possibly containing a whitespace-separated list of |
| 4983 | values. The values "all" and "none" are special. It's also permissible |
| 4984 | to pass an array reference here. |
| 4985 | |
| 4986 | $deparser->ambient_pragmas(re => 'eval'); |
| 4987 | |
| 4988 | |
| 4989 | =item warnings |
| 4990 | |
| 4991 | Takes a string, possibly containing a whitespace-separated list of |
| 4992 | values. The values "all" and "none" are special, again. It's also |
| 4993 | permissible to pass an array reference here. |
| 4994 | |
| 4995 | $deparser->ambient_pragmas(warnings => [qw[void io]]); |
| 4996 | |
| 4997 | If one of the values is the string "FATAL", then all the warnings |
| 4998 | in that list will be considered fatal, just as with the B<warnings> |
| 4999 | pragma itself. Should you need to specify that some warnings are |
| 5000 | fatal, and others are merely enabled, you can pass the B<warnings> |
| 5001 | parameter twice: |
| 5002 | |
| 5003 | $deparser->ambient_pragmas( |
| 5004 | warnings => 'all', |
| 5005 | warnings => [FATAL => qw/void io/], |
| 5006 | ); |
| 5007 | |
| 5008 | See L<perllexwarn> for more information about lexical warnings. |
| 5009 | |
| 5010 | =item hint_bits |
| 5011 | |
| 5012 | =item warning_bits |
| 5013 | |
| 5014 | These two parameters are used to specify the ambient pragmas in |
| 5015 | the format used by the special variables $^H and ${^WARNING_BITS}. |
| 5016 | |
| 5017 | They exist principally so that you can write code like: |
| 5018 | |
| 5019 | { my ($hint_bits, $warning_bits); |
| 5020 | BEGIN {($hint_bits, $warning_bits) = ($^H, ${^WARNING_BITS})} |
| 5021 | $deparser->ambient_pragmas ( |
| 5022 | hint_bits => $hint_bits, |
| 5023 | warning_bits => $warning_bits, |
| 5024 | '$[' => 0 + $[ |
| 5025 | ); } |
| 5026 | |
| 5027 | which specifies that the ambient pragmas are exactly those which |
| 5028 | are in scope at the point of calling. |
| 5029 | |
| 5030 | =item %^H |
| 5031 | |
| 5032 | This parameter is used to specify the ambient pragmas which are |
| 5033 | stored in the special hash %^H. |
| 5034 | |
| 5035 | =back |
| 5036 | |
| 5037 | =head2 coderef2text |
| 5038 | |
| 5039 | $body = $deparse->coderef2text(\&func) |
| 5040 | $body = $deparse->coderef2text(sub ($$) { ... }) |
| 5041 | |
| 5042 | Return source code for the body of a subroutine (a block, optionally |
| 5043 | preceded by a prototype in parens), given a reference to the |
| 5044 | sub. Because a subroutine can have no names, or more than one name, |
| 5045 | this method doesn't return a complete subroutine definition -- if you |
| 5046 | want to eval the result, you should prepend "sub subname ", or "sub " |
| 5047 | for an anonymous function constructor. Unless the sub was defined in |
| 5048 | the main:: package, the code will include a package declaration. |
| 5049 | |
| 5050 | =head1 BUGS |
| 5051 | |
| 5052 | =over 4 |
| 5053 | |
| 5054 | =item * |
| 5055 | |
| 5056 | The only pragmas to be completely supported are: C<use warnings>, |
| 5057 | C<use strict 'refs'>, C<use bytes>, and C<use integer>. (C<$[>, which |
| 5058 | behaves like a pragma, is also supported.) |
| 5059 | |
| 5060 | Excepting those listed above, we're currently unable to guarantee that |
| 5061 | B::Deparse will produce a pragma at the correct point in the program. |
| 5062 | (Specifically, pragmas at the beginning of a block often appear right |
| 5063 | before the start of the block instead.) |
| 5064 | Since the effects of pragmas are often lexically scoped, this can mean |
| 5065 | that the pragma holds sway over a different portion of the program |
| 5066 | than in the input file. |
| 5067 | |
| 5068 | =item * |
| 5069 | |
| 5070 | In fact, the above is a specific instance of a more general problem: |
| 5071 | we can't guarantee to produce BEGIN blocks or C<use> declarations in |
| 5072 | exactly the right place. So if you use a module which affects compilation |
| 5073 | (such as by over-riding keywords, overloading constants or whatever) |
| 5074 | then the output code might not work as intended. |
| 5075 | |
| 5076 | This is the most serious outstanding problem, and will require some help |
| 5077 | from the Perl core to fix. |
| 5078 | |
| 5079 | =item * |
| 5080 | |
| 5081 | Some constants don't print correctly either with or without B<-d>. |
| 5082 | For instance, neither B::Deparse nor Data::Dumper know how to print |
| 5083 | dual-valued scalars correctly, as in: |
| 5084 | |
| 5085 | use constant E2BIG => ($!=7); $y = E2BIG; print $y, 0+$y; |
| 5086 | |
| 5087 | use constant H => { "#" => 1 }; H->{"#"}; |
| 5088 | |
| 5089 | =item * |
| 5090 | |
| 5091 | An input file that uses source filtering probably won't be deparsed into |
| 5092 | runnable code, because it will still include the B<use> declaration |
| 5093 | for the source filtering module, even though the code that is |
| 5094 | produced is already ordinary Perl which shouldn't be filtered again. |
| 5095 | |
| 5096 | =item * |
| 5097 | |
| 5098 | Optimised away statements are rendered as |
| 5099 | '???'. This includes statements that |
| 5100 | have a compile-time side-effect, such as the obscure |
| 5101 | |
| 5102 | my $x if 0; |
| 5103 | |
| 5104 | which is not, consequently, deparsed correctly. |
| 5105 | |
| 5106 | foreach my $i (@_) { 0 } |
| 5107 | => |
| 5108 | foreach my $i (@_) { '???' } |
| 5109 | |
| 5110 | =item * |
| 5111 | |
| 5112 | Lexical (my) variables declared in scopes external to a subroutine |
| 5113 | appear in code2ref output text as package variables. This is a tricky |
| 5114 | problem, as perl has no native facility for referring to a lexical variable |
| 5115 | defined within a different scope, although L<PadWalker> is a good start. |
| 5116 | |
| 5117 | =item * |
| 5118 | |
| 5119 | There are probably many more bugs on non-ASCII platforms (EBCDIC). |
| 5120 | |
| 5121 | =back |
| 5122 | |
| 5123 | =head1 AUTHOR |
| 5124 | |
| 5125 | Stephen McCamant <smcc@CSUA.Berkeley.EDU>, based on an earlier version |
| 5126 | by Malcolm Beattie <mbeattie@sable.ox.ac.uk>, with contributions from |
| 5127 | Gisle Aas, James Duncan, Albert Dvornik, Robin Houston, Dave Mitchell, |
| 5128 | Hugo van der Sanden, Gurusamy Sarathy, Nick Ing-Simmons, and Rafael |
| 5129 | Garcia-Suarez. |
| 5130 | |
| 5131 | =cut |