Install Perl 5.8.8
[msysgit.git] / mingw / html / lib / B / Deparse.html
blob4c4140ffc5650381bc4eb453d7b8c6f66191a5e0
1 <?xml version="1.0" ?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3 <html xmlns="http://www.w3.org/1999/xhtml">
4 <head>
5 <title>B::Deparse - Perl compiler backend to produce perl code</title>
6 <meta http-equiv="content-type" content="text/html; charset=utf-8" />
7 <link rev="made" href="mailto:" />
8 </head>
10 <body style="background-color: white">
11 <table border="0" width="100%" cellspacing="0" cellpadding="3">
12 <tr><td class="block" style="background-color: #cccccc" valign="middle">
13 <big><strong><span class="block">&nbsp;B::Deparse - Perl compiler backend to produce perl code</span></strong></big>
14 </td></tr>
15 </table>
17 <p><a name="__index__"></a></p>
18 <!-- INDEX BEGIN -->
20 <ul>
22 <li><a href="#name">NAME</a></li>
23 <li><a href="#synopsis">SYNOPSIS</a></li>
24 <li><a href="#description">DESCRIPTION</a></li>
25 <li><a href="#options">OPTIONS</a></li>
26 <li><a href="#using_b__deparse_as_a_module">USING B::Deparse AS A MODULE</a></li>
27 <ul>
29 <li><a href="#synopsis">Synopsis</a></li>
30 <li><a href="#description">Description</a></li>
31 <li><a href="#new">new</a></li>
32 <li><a href="#ambient_pragmas">ambient_pragmas</a></li>
33 <li><a href="#coderef2text">coderef2text</a></li>
34 </ul>
36 <li><a href="#bugs">BUGS</a></li>
37 <li><a href="#author">AUTHOR</a></li>
38 </ul>
39 <!-- INDEX END -->
41 <hr />
42 <p>
43 </p>
44 <h1><a name="name">NAME</a></h1>
45 <p>B::Deparse - Perl compiler backend to produce perl code</p>
46 <p>
47 </p>
48 <hr />
49 <h1><a name="synopsis">SYNOPSIS</a></h1>
50 <p><strong>perl</strong> <strong>-MO=Deparse</strong>[<strong>,-d</strong>][<strong>,-f</strong><em>FILE</em>][<strong>,-p</strong>][<strong>,-q</strong>][<strong>,-l</strong>]
51 [<strong>,-s</strong><em>LETTERS</em>][<strong>,-x</strong><em>LEVEL</em>] <em>prog.pl</em></p>
52 <p>
53 </p>
54 <hr />
55 <h1><a name="description">DESCRIPTION</a></h1>
56 <p>B::Deparse is a backend module for the Perl compiler that generates
57 perl source code, based on the internal compiled structure that perl
58 itself creates after parsing a program. The output of B::Deparse won't
59 be exactly the same as the original source, since perl doesn't keep
60 track of comments or whitespace, and there isn't a one-to-one
61 correspondence between perl's syntactical constructions and their
62 compiled form, but it will often be close. When you use the <strong>-p</strong>
63 option, the output also includes parentheses even when they are not
64 required by precedence, which can make it easy to see if perl is
65 parsing your expressions the way you intended.</p>
66 <p>While B::Deparse goes to some lengths to try to figure out what your
67 original program was doing, some parts of the language can still trip
68 it up; it still fails even on some parts of Perl's own test suite. If
69 you encounter a failure other than the most common ones described in
70 the BUGS section below, you can help contribute to B::Deparse's
71 ongoing development by submitting a bug report with a small
72 example.</p>
73 <p>
74 </p>
75 <hr />
76 <h1><a name="options">OPTIONS</a></h1>
77 <p>As with all compiler backend options, these must follow directly after
78 the '-MO=Deparse', separated by a comma but not any white space.</p>
79 <dl>
80 <dt><strong><a name="item__2dd"><strong>-d</strong></a></strong>
82 <dd>
83 <p>Output data values (when they appear as constants) using Data::Dumper.
84 Without this option, B::Deparse will use some simple routines of its
85 own for the same purpose. Currently, Data::Dumper is better for some
86 kinds of data (such as complex structures with sharing and
87 self-reference) while the built-in routines are better for others
88 (such as odd floating-point values).</p>
89 </dd>
90 </li>
91 <dt><strong><a name="item__2dffile"><strong>-f</strong><em>FILE</em></a></strong>
93 <dd>
94 <p>Normally, B::Deparse deparses the main code of a program, and all the subs
95 defined in the same file. To include subs defined in other files, pass the
96 <strong>-f</strong> option with the filename. You can pass the <strong>-f</strong> option several times, to
97 include more than one secondary file. (Most of the time you don't want to
98 use it at all.) You can also use this option to include subs which are
99 defined in the scope of a <strong>#line</strong> directive with two parameters.</p>
100 </dd>
101 </li>
102 <dt><strong><a name="item__2dl"><strong>-l</strong></a></strong>
104 <dd>
105 <p>Add '#line' declarations to the output based on the line and file
106 locations of the original code.</p>
107 </dd>
108 </li>
109 <dt><strong><a name="item__2dp"><strong>-p</strong></a></strong>
111 <dd>
112 <p>Print extra parentheses. Without this option, B::Deparse includes
113 parentheses in its output only when they are needed, based on the
114 structure of your program. With <strong>-p</strong>, it uses parentheses (almost)
115 whenever they would be legal. This can be useful if you are used to
116 LISP, or if you want to see how perl parses your input. If you say</p>
117 </dd>
118 <dd>
119 <pre>
120 if ($var &amp; 0x7f == 65) {print &quot;Gimme an A!&quot;}
121 print ($which ? $a : $b), &quot;\n&quot;;
122 $name = $ENV{USER} or &quot;Bob&quot;;</pre>
123 </dd>
124 <dd>
125 <p><code>B::Deparse,-p</code> will print</p>
126 </dd>
127 <dd>
128 <pre>
129 if (($var &amp; 0)) {
130 print('Gimme an A!')
132 (print(($which ? $a : $b)), '???');
133 (($name = $ENV{'USER'}) or '???')</pre>
134 </dd>
135 <dd>
136 <p>which probably isn't what you intended (the <code>'???'</code> is a sign that
137 perl optimized away a constant value).</p>
138 </dd>
139 </li>
140 <dt><strong><a name="item__2dp"><strong>-P</strong></a></strong>
142 <dd>
143 <p>Disable prototype checking. With this option, all function calls are
144 deparsed as if no prototype was defined for them. In other words,</p>
145 </dd>
146 <dd>
147 <pre>
148 perl -MO=Deparse,-P -e 'sub foo (\@) { 1 } foo @x'</pre>
149 </dd>
150 <dd>
151 <p>will print</p>
152 </dd>
153 <dd>
154 <pre>
155 sub foo (\@) {
158 &amp;foo(\@x);</pre>
159 </dd>
160 <dd>
161 <p>making clear how the parameters are actually passed to <code>foo</code>.</p>
162 </dd>
163 </li>
164 <dt><strong><a name="item__2dq"><strong>-q</strong></a></strong>
166 <dd>
167 <p>Expand double-quoted strings into the corresponding combinations of
168 concatenation, uc, ucfirst, lc, lcfirst, quotemeta, and join. For
169 instance, print</p>
170 </dd>
171 <dd>
172 <pre>
173 print &quot;Hello, $world, @ladies, \u$gentlemen\E, \u\L$me!&quot;;</pre>
174 </dd>
175 <dd>
176 <p>as</p>
177 </dd>
178 <dd>
179 <pre>
180 print 'Hello, ' . $world . ', ' . join($&quot;, @ladies) . ', '
181 . ucfirst($gentlemen) . ', ' . ucfirst(lc $me . '!');</pre>
182 </dd>
183 <dd>
184 <p>Note that the expanded form represents the way perl handles such
185 constructions internally -- this option actually turns off the reverse
186 translation that B::Deparse usually does. On the other hand, note that
187 <code>$x = &quot;$y&quot;</code> is not the same as <code>$x = $y</code>: the former makes the value
188 of $y into a string before doing the assignment.</p>
189 </dd>
190 </li>
191 <dt><strong><a name="item__2dsletters"><strong>-s</strong><em>LETTERS</em></a></strong>
193 <dd>
194 <p>Tweak the style of B::Deparse's output. The letters should follow
195 directly after the 's', with no space or punctuation. The following
196 options are available:</p>
197 </dd>
198 <dl>
199 <dt><strong><a name="item_c"><strong>C</strong></a></strong>
201 <dd>
202 <p>Cuddle <code>elsif</code>, <code>else</code>, and <code>continue</code> blocks. For example, print</p>
203 </dd>
204 <dd>
205 <pre>
206 if (...) {
208 } else {
210 }</pre>
211 </dd>
212 <dd>
213 <p>instead of</p>
214 </dd>
215 <dd>
216 <pre>
217 if (...) {
220 else {
222 }</pre>
223 </dd>
224 <dd>
225 <p>The default is not to cuddle.</p>
226 </dd>
227 </li>
228 <dt><strong><a name="item_inumber"><strong>i</strong><em>NUMBER</em></a></strong>
230 <dd>
231 <p>Indent lines by multiples of <em>NUMBER</em> columns. The default is 4 columns.</p>
232 </dd>
233 </li>
234 <dt><strong><a name="item_t"><strong>T</strong></a></strong>
236 <dd>
237 <p>Use tabs for each 8 columns of indent. The default is to use only spaces.
238 For instance, if the style options are <strong>-si4T</strong>, a line that's indented
239 3 times will be preceded by one tab and four spaces; if the options were
240 <strong>-si8T</strong>, the same line would be preceded by three tabs.</p>
241 </dd>
242 </li>
243 <dt><strong><a name="item_vstring_2e"><strong>v</strong><em>STRING</em><strong>.</strong></a></strong>
245 <dd>
246 <p>Print <em>STRING</em> for the value of a constant that can't be determined
247 because it was optimized away (mnemonic: this happens when a constant
248 is used in <strong>v</strong>oid context). The end of the string is marked by a period.
249 The string should be a valid perl expression, generally a constant.
250 Note that unless it's a number, it probably needs to be quoted, and on
251 a command line quotes need to be protected from the shell. Some
252 conventional values include 0, 1, 42, '', 'foo', and
253 'Useless use of constant omitted' (which may need to be
254 <strong>-sv``'Useless use of constant omitted'.''</strong>
255 or something similar depending on your shell). The default is '???'.
256 If you're using B::Deparse on a module or other file that's require'd,
257 you shouldn't use a value that evaluates to false, since the customary
258 true constant at the end of a module will be in void context when the
259 file is compiled as a main program.</p>
260 </dd>
261 </li>
262 </dl>
263 <dt><strong><a name="item__2dxlevel"><strong>-x</strong><em>LEVEL</em></a></strong>
265 <dd>
266 <p>Expand conventional syntax constructions into equivalent ones that expose
267 their internal operation. <em>LEVEL</em> should be a digit, with higher values
268 meaning more expansion. As with <strong>-q</strong>, this actually involves turning off
269 special cases in B::Deparse's normal operations.</p>
270 </dd>
271 <dd>
272 <p>If <em>LEVEL</em> is at least 3, <code>for</code> loops will be translated into equivalent
273 while loops with continue blocks; for instance</p>
274 </dd>
275 <dd>
276 <pre>
277 for ($i = 0; $i &lt; 10; ++$i) {
278 print $i;
279 }</pre>
280 </dd>
281 <dd>
282 <p>turns into</p>
283 </dd>
284 <dd>
285 <pre>
286 $i = 0;
287 while ($i &lt; 10) {
288 print $i;
289 } continue {
290 ++$i
291 }</pre>
292 </dd>
293 <dd>
294 <p>Note that in a few cases this translation can't be perfectly carried back
295 into the source code -- if the loop's initializer declares a my variable,
296 for instance, it won't have the correct scope outside of the loop.</p>
297 </dd>
298 <dd>
299 <p>If <em>LEVEL</em> is at least 5, <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_use"><code>use</code></a> declarations will be translated into
300 <code>BEGIN</code> blocks containing calls to <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_require"><code>require</code></a> and <code>import</code>; for
301 instance,</p>
302 </dd>
303 <dd>
304 <pre>
305 use strict 'refs';</pre>
306 </dd>
307 <dd>
308 <p>turns into</p>
309 </dd>
310 <dd>
311 <pre>
312 sub BEGIN {
313 require strict;
314 do {
315 'strict'-&gt;import('refs')
317 }</pre>
318 </dd>
319 <dd>
320 <p>If <em>LEVEL</em> is at least 7, <code>if</code> statements will be translated into
321 equivalent expressions using <code>&amp;&amp;</code>, <code>?:</code> and <code>do {}</code>; for instance</p>
322 </dd>
323 <dd>
324 <pre>
325 print 'hi' if $nice;
326 if ($nice) {
327 print 'hi';
329 if ($nice) {
330 print 'hi';
331 } else {
332 print 'bye';
333 }</pre>
334 </dd>
335 <dd>
336 <p>turns into</p>
337 </dd>
338 <dd>
339 <pre>
340 $nice and print 'hi';
341 $nice and do { print 'hi' };
342 $nice ? do { print 'hi' } : do { print 'bye' };</pre>
343 </dd>
344 <dd>
345 <p>Long sequences of elsifs will turn into nested ternary operators, which
346 B::Deparse doesn't know how to indent nicely.</p>
347 </dd>
348 </li>
349 </dl>
351 </p>
352 <hr />
353 <h1><a name="using_b__deparse_as_a_module">USING B::Deparse AS A MODULE</a></h1>
355 </p>
356 <h2><a name="synopsis">Synopsis</a></h2>
357 <pre>
358 use B::Deparse;
359 $deparse = B::Deparse-&gt;new(&quot;-p&quot;, &quot;-sC&quot;);
360 $body = $deparse-&gt;coderef2text(\&amp;func);
361 eval &quot;sub func $body&quot;; # the inverse operation</pre>
363 </p>
364 <h2><a name="description">Description</a></h2>
365 <p>B::Deparse can also be used on a sub-by-sub basis from other perl
366 programs.</p>
368 </p>
369 <h2><a name="new">new</a></h2>
370 <pre>
371 $deparse = B::Deparse-&gt;new(OPTIONS)</pre>
372 <p>Create an object to store the state of a deparsing operation and any
373 options. The options are the same as those that can be given on the
374 command line (see <a href="#options">OPTIONS</a>); options that are separated by commas
375 after <strong>-MO=Deparse</strong> should be given as separate strings. Some
376 options, like <strong>-u</strong>, don't make sense for a single subroutine, so
377 don't pass them.</p>
379 </p>
380 <h2><a name="ambient_pragmas">ambient_pragmas</a></h2>
381 <pre>
382 $deparse-&gt;ambient_pragmas(strict =&gt; 'all', '$[' =&gt; $[);</pre>
383 <p>The compilation of a subroutine can be affected by a few compiler
384 directives, <strong>pragmas</strong>. These are:</p>
385 <ul>
386 <li>
387 <p>use strict;</p>
388 </li>
389 <li>
390 <p>use warnings;</p>
391 </li>
392 <li>
393 <p>Assigning to the special variable $[</p>
394 </li>
395 <li>
396 <p>use integer;</p>
397 </li>
398 <li>
399 <p>use bytes;</p>
400 </li>
401 <li>
402 <p>use utf8;</p>
403 </li>
404 <li>
405 <p>use re;</p>
406 </li>
407 </ul>
408 <p>Ordinarily, if you use B::Deparse on a subroutine which has
409 been compiled in the presence of one or more of these pragmas,
410 the output will include statements to turn on the appropriate
411 directives. So if you then compile the code returned by coderef2text,
412 it will behave the same way as the subroutine which you deparsed.</p>
413 <p>However, you may know that you intend to use the results in a
414 particular context, where some pragmas are already in scope. In
415 this case, you use the <strong>ambient_pragmas</strong> method to describe the
416 assumptions you wish to make.</p>
417 <p>Not all of the options currently have any useful effect. See
418 <a href="#bugs">BUGS</a> for more details.</p>
419 <p>The parameters it accepts are:</p>
420 <dl>
421 <dt><strong><a name="item_strict">strict</a></strong>
423 <dd>
424 <p>Takes a string, possibly containing several values separated
425 by whitespace. The special values ``all'' and ``none'' mean what you'd
426 expect.</p>
427 </dd>
428 <dd>
429 <pre>
430 $deparse-&gt;ambient_pragmas(strict =&gt; 'subs refs');</pre>
431 </dd>
432 </li>
433 <dt><strong><a name="item___">$[</a></strong>
435 <dd>
436 <p>Takes a number, the value of the array base $[.</p>
437 </dd>
438 </li>
439 <dt><strong><a name="item_bytes">bytes</a></strong>
441 <dt><strong><a name="item_utf8">utf8</a></strong>
443 <dt><strong><a name="item_integer">integer</a></strong>
445 <dd>
446 <p>If the value is true, then the appropriate pragma is assumed to
447 be in the ambient scope, otherwise not.</p>
448 </dd>
449 </li>
450 <dt><strong><a name="item_re">re</a></strong>
452 <dd>
453 <p>Takes a string, possibly containing a whitespace-separated list of
454 values. The values ``all'' and ``none'' are special. It's also permissible
455 to pass an array reference here.</p>
456 </dd>
457 <dd>
458 <pre>
459 $deparser-&gt;ambient_pragmas(re =&gt; 'eval');</pre>
460 </dd>
461 </li>
462 <dt><strong><a name="item_warnings">warnings</a></strong>
464 <dd>
465 <p>Takes a string, possibly containing a whitespace-separated list of
466 values. The values ``all'' and ``none'' are special, again. It's also
467 permissible to pass an array reference here.</p>
468 </dd>
469 <dd>
470 <pre>
471 $deparser-&gt;ambient_pragmas(warnings =&gt; [qw[void io]]);</pre>
472 </dd>
473 <dd>
474 <p>If one of the values is the string ``FATAL'', then all the warnings
475 in that list will be considered fatal, just as with the <strong>warnings</strong>
476 pragma itself. Should you need to specify that some warnings are
477 fatal, and others are merely enabled, you can pass the <strong>warnings</strong>
478 parameter twice:</p>
479 </dd>
480 <dd>
481 <pre>
482 $deparser-&gt;ambient_pragmas(
483 warnings =&gt; 'all',
484 warnings =&gt; [FATAL =&gt; qw/void io/],
485 );</pre>
486 </dd>
487 <dd>
488 <p>See <a href="file://C|\msysgit\mingw\html/pod/perllexwarn.html">the perllexwarn manpage</a> for more information about lexical warnings.</p>
489 </dd>
490 </li>
491 <dt><strong><a name="item_hint_bits">hint_bits</a></strong>
493 <dt><strong><a name="item_warning_bits">warning_bits</a></strong>
495 <dd>
496 <p>These two parameters are used to specify the ambient pragmas in
497 the format used by the special variables $^H and ${^WARNING_BITS}.</p>
498 </dd>
499 <dd>
500 <p>They exist principally so that you can write code like:</p>
501 </dd>
502 <dd>
503 <pre>
504 { my ($hint_bits, $warning_bits);
505 BEGIN {($hint_bits, $warning_bits) = ($^H, ${^WARNING_BITS})}
506 $deparser-&gt;ambient_pragmas (
507 hint_bits =&gt; $hint_bits,
508 warning_bits =&gt; $warning_bits,
509 '$[' =&gt; 0 + $[
510 ); }</pre>
511 </dd>
512 <dd>
513 <p>which specifies that the ambient pragmas are exactly those which
514 are in scope at the point of calling.</p>
515 </dd>
516 </li>
517 </dl>
519 </p>
520 <h2><a name="coderef2text">coderef2text</a></h2>
521 <pre>
522 $body = $deparse-&gt;coderef2text(\&amp;func)
523 $body = $deparse-&gt;coderef2text(sub ($$) { ... })</pre>
524 <p>Return source code for the body of a subroutine (a block, optionally
525 preceded by a prototype in parens), given a reference to the
526 sub. Because a subroutine can have no names, or more than one name,
527 this method doesn't return a complete subroutine definition -- if you
528 want to eval the result, you should prepend ``sub subname '', or ``sub ''
529 for an anonymous function constructor. Unless the sub was defined in
530 the main:: package, the code will include a package declaration.</p>
532 </p>
533 <hr />
534 <h1><a name="bugs">BUGS</a></h1>
535 <ul>
536 <li>
537 <p>The only pragmas to be completely supported are: <code>use warnings</code>,
538 <code>use strict 'refs'</code>, <code>use bytes</code>, and <code>use integer</code>. (<a href="#item___"><code>$[</code></a>, which
539 behaves like a pragma, is also supported.)</p>
540 <p>Excepting those listed above, we're currently unable to guarantee that
541 B::Deparse will produce a pragma at the correct point in the program.
542 (Specifically, pragmas at the beginning of a block often appear right
543 before the start of the block instead.)
544 Since the effects of pragmas are often lexically scoped, this can mean
545 that the pragma holds sway over a different portion of the program
546 than in the input file.</p>
547 </li>
548 <li>
549 <p>In fact, the above is a specific instance of a more general problem:
550 we can't guarantee to produce BEGIN blocks or <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_use"><code>use</code></a> declarations in
551 exactly the right place. So if you use a module which affects compilation
552 (such as by over-riding keywords, overloading constants or whatever)
553 then the output code might not work as intended.</p>
554 <p>This is the most serious outstanding problem, and will require some help
555 from the Perl core to fix.</p>
556 </li>
557 <li>
558 <p>If a keyword is over-ridden, and your program explicitly calls
559 the built-in version by using CORE::keyword, the output of B::Deparse
560 will not reflect this. If you run the resulting code, it will call
561 the over-ridden version rather than the built-in one. (Maybe there
562 should be an option to <strong>always</strong> print keyword calls as <code>CORE::name</code>.)</p>
563 </li>
564 <li>
565 <p>Some constants don't print correctly either with or without <strong>-d</strong>.
566 For instance, neither B::Deparse nor Data::Dumper know how to print
567 dual-valued scalars correctly, as in:</p>
568 <pre>
569 use constant E2BIG =&gt; ($!=7); $y = E2BIG; print $y, 0+$y;</pre>
570 </li>
571 <li>
572 <p>An input file that uses source filtering probably won't be deparsed into
573 runnable code, because it will still include the <strong>use</strong> declaration
574 for the source filtering module, even though the code that is
575 produced is already ordinary Perl which shouldn't be filtered again.</p>
576 </li>
577 <li>
578 <p>Optimised away statements are rendered as '???'. This includes statements that
579 have a compile-time side-effect, such as the obscure</p>
580 <pre>
581 my $x if 0;</pre>
582 <p>which is not, consequently, deparsed correctly.</p>
583 </li>
584 <li>
585 <p>There are probably many more bugs on non-ASCII platforms (EBCDIC).</p>
586 </li>
587 </ul>
589 </p>
590 <hr />
591 <h1><a name="author">AUTHOR</a></h1>
592 <p>Stephen McCamant &lt;<a href="mailto:smcc@CSUA.Berkeley.EDU">smcc@CSUA.Berkeley.EDU</a>&gt;, based on an earlier version
593 by Malcolm Beattie &lt;<a href="mailto:mbeattie@sable.ox.ac.uk">mbeattie@sable.ox.ac.uk</a>&gt;, with contributions from
594 Gisle Aas, James Duncan, Albert Dvornik, Robin Houston, Dave Mitchell,
595 Hugo van der Sanden, Gurusamy Sarathy, Nick Ing-Simmons, and Rafael
596 Garcia-Suarez.</p>
597 <table border="0" width="100%" cellspacing="0" cellpadding="3">
598 <tr><td class="block" style="background-color: #cccccc" valign="middle">
599 <big><strong><span class="block">&nbsp;B::Deparse - Perl compiler backend to produce perl code</span></strong></big>
600 </td></tr>
601 </table>
603 </body>
605 </html>