1B::Deparse(3pm)        Perl Programmers Reference Guide        B::Deparse(3pm)
2
3
4

NAME

6       B::Deparse - Perl compiler backend to produce perl code
7

SYNOPSIS

9       perl -MO=Deparse[,-d][,-fFILE][,-p][,-q][,-l]
10               [,-sLETTERS][,-xLEVEL] prog.pl
11

DESCRIPTION

13       B::Deparse is a backend module for the Perl compiler that generates
14       perl source code, based on the internal compiled structure that perl
15       itself creates after parsing a program. The output of B::Deparse won't
16       be exactly the same as the original source, since perl doesn't keep
17       track of comments or whitespace, and there isn't a one-to-one corre‐
18       spondence between perl's syntactical constructions and their compiled
19       form, but it will often be close. When you use the -p option, the out‐
20       put also includes parentheses even when they are not required by prece‐
21       dence, which can make it easy to see if perl is parsing your expres‐
22       sions the way you intended.
23
24       While B::Deparse goes to some lengths to try to figure out what your
25       original program was doing, some parts of the language can still trip
26       it up; it still fails even on some parts of Perl's own test suite. If
27       you encounter a failure other than the most common ones described in
28       the BUGS section below, you can help contribute to B::Deparse's ongoing
29       development by submitting a bug report with a small example.
30

OPTIONS

32       As with all compiler backend options, these must follow directly after
33       the '-MO=Deparse', separated by a comma but not any white space.
34
35       -d  Output data values (when they appear as constants) using
36           Data::Dumper.  Without this option, B::Deparse will use some simple
37           routines of its own for the same purpose. Currently, Data::Dumper
38           is better for some kinds of data (such as complex structures with
39           sharing and self-reference) while the built-in routines are better
40           for others (such as odd floating-point values).
41
42       -fFILE
43           Normally, B::Deparse deparses the main code of a program, and all
44           the subs defined in the same file. To include subs defined in other
45           files, pass the -f option with the filename. You can pass the -f
46           option several times, to include more than one secondary file.
47           (Most of the time you don't want to use it at all.)  You can also
48           use this option to include subs which are defined in the scope of a
49           #line directive with two parameters.
50
51       -l  Add '#line' declarations to the output based on the line and file
52           locations of the original code.
53
54       -p  Print extra parentheses. Without this option, B::Deparse includes
55           parentheses in its output only when they are needed, based on the
56           structure of your program. With -p, it uses parentheses (almost)
57           whenever they would be legal. This can be useful if you are used to
58           LISP, or if you want to see how perl parses your input. If you say
59
60               if ($var & 0x7f == 65) {print "Gimme an A!"}
61               print ($which ? $a : $b), "\n";
62               $name = $ENV{USER} or "Bob";
63
64           "B::Deparse,-p" will print
65
66               if (($var & 0)) {
67                   print('Gimme an A!')
68               };
69               (print(($which ? $a : $b)), '???');
70               (($name = $ENV{'USER'}) or '???')
71
72           which probably isn't what you intended (the '???' is a sign that
73           perl optimized away a constant value).
74
75       -P  Disable prototype checking. With this option, all function calls
76           are deparsed as if no prototype was defined for them. In other
77           words,
78
79               perl -MO=Deparse,-P -e 'sub foo (\@) { 1 } foo @x'
80
81           will print
82
83               sub foo (\@) {
84                   1;
85               }
86               &foo(\@x);
87
88           making clear how the parameters are actually passed to "foo".
89
90       -q  Expand double-quoted strings into the corresponding combinations of
91           concatenation, uc, ucfirst, lc, lcfirst, quotemeta, and join. For
92           instance, print
93
94               print "Hello, $world, @ladies, \u$gentlemen\E, \u\L$me!";
95
96           as
97
98               print 'Hello, ' . $world . ', ' . join($", @ladies) . ', '
99                     . ucfirst($gentlemen) . ', ' . ucfirst(lc $me . '!');
100
101           Note that the expanded form represents the way perl handles such
102           constructions internally -- this option actually turns off the
103           reverse translation that B::Deparse usually does. On the other
104           hand, note that "$x = "$y"" is not the same as "$x = $y": the for‐
105           mer makes the value of $y into a string before doing the assign‐
106           ment.
107
108       -sLETTERS
109           Tweak the style of B::Deparse's output. The letters should follow
110           directly after the 's', with no space or punctuation. The following
111           options are available:
112
113           C   Cuddle "elsif", "else", and "continue" blocks. For example,
114               print
115
116                   if (...) {
117                        ...
118                   } else {
119                        ...
120                   }
121
122               instead of
123
124                   if (...) {
125                        ...
126                   }
127                   else {
128                        ...
129                   }
130
131               The default is not to cuddle.
132
133           iNUMBER
134               Indent lines by multiples of NUMBER columns. The default is 4
135               columns.
136
137           T   Use tabs for each 8 columns of indent. The default is to use
138               only spaces.  For instance, if the style options are -si4T, a
139               line that's indented 3 times will be preceded by one tab and
140               four spaces; if the options were -si8T, the same line would be
141               preceded by three tabs.
142
143           vSTRING.
144               Print STRING for the value of a constant that can't be deter‐
145               mined because it was optimized away (mnemonic: this happens
146               when a constant is used in void context). The end of the string
147               is marked by a period.  The string should be a valid perl
148               expression, generally a constant.  Note that unless it's a num‐
149               ber, it probably needs to be quoted, and on a command line
150               quotes need to be protected from the shell. Some conventional
151               values include 0, 1, 42, '', 'foo', and 'Useless use of con‐
152               stant omitted' (which may need to be -sv"'Useless use of con‐
153               stant omitted'."  or something similar depending on your
154               shell). The default is '???'.  If you're using B::Deparse on a
155               module or other file that's require'd, you shouldn't use a
156               value that evaluates to false, since the customary true con‐
157               stant at the end of a module will be in void context when the
158               file is compiled as a main program.
159
160       -xLEVEL
161           Expand conventional syntax constructions into equivalent ones that
162           expose their internal operation. LEVEL should be a digit, with
163           higher values meaning more expansion. As with -q, this actually
164           involves turning off special cases in B::Deparse's normal opera‐
165           tions.
166
167           If LEVEL is at least 3, "for" loops will be translated into equiva‐
168           lent while loops with continue blocks; for instance
169
170               for ($i = 0; $i < 10; ++$i) {
171                   print $i;
172               }
173
174           turns into
175
176               $i = 0;
177               while ($i < 10) {
178                   print $i;
179               } continue {
180                   ++$i
181               }
182
183           Note that in a few cases this translation can't be perfectly car‐
184           ried back into the source code -- if the loop's initializer
185           declares a my variable, for instance, it won't have the correct
186           scope outside of the loop.
187
188           If LEVEL is at least 5, "use" declarations will be translated into
189           "BEGIN" blocks containing calls to "require" and "import"; for
190           instance,
191
192               use strict 'refs';
193
194           turns into
195
196               sub BEGIN {
197                   require strict;
198                   do {
199                       'strict'->import('refs')
200                   };
201               }
202
203           If LEVEL is at least 7, "if" statements will be translated into
204           equivalent expressions using "&&", "?:" and "do {}"; for instance
205
206               print 'hi' if $nice;
207               if ($nice) {
208                   print 'hi';
209               }
210               if ($nice) {
211                   print 'hi';
212               } else {
213                   print 'bye';
214               }
215
216           turns into
217
218               $nice and print 'hi';
219               $nice and do { print 'hi' };
220               $nice ? do { print 'hi' } : do { print 'bye' };
221
222           Long sequences of elsifs will turn into nested ternary operators,
223           which B::Deparse doesn't know how to indent nicely.
224

USING B::Deparse AS A MODULE

226       Synopsis
227
228           use B::Deparse;
229           $deparse = B::Deparse->new("-p", "-sC");
230           $body = $deparse->coderef2text(\&func);
231           eval "sub func $body"; # the inverse operation
232
233       Description
234
235       B::Deparse can also be used on a sub-by-sub basis from other perl pro‐
236       grams.
237
238       new
239
240           $deparse = B::Deparse->new(OPTIONS)
241
242       Create an object to store the state of a deparsing operation and any
243       options. The options are the same as those that can be given on the
244       command line (see "OPTIONS"); options that are separated by commas
245       after -MO=Deparse should be given as separate strings. Some options,
246       like -u, don't make sense for a single subroutine, so don't pass them.
247
248       ambient_pragmas
249
250           $deparse->ambient_pragmas(strict => 'all', '$[' => $[);
251
252       The compilation of a subroutine can be affected by a few compiler
253       directives, pragmas. These are:
254
255       ·   use strict;
256
257       ·   use warnings;
258
259       ·   Assigning to the special variable $[
260
261       ·   use integer;
262
263       ·   use bytes;
264
265       ·   use utf8;
266
267       ·   use re;
268
269       Ordinarily, if you use B::Deparse on a subroutine which has been com‐
270       piled in the presence of one or more of these pragmas, the output will
271       include statements to turn on the appropriate directives. So if you
272       then compile the code returned by coderef2text, it will behave the same
273       way as the subroutine which you deparsed.
274
275       However, you may know that you intend to use the results in a particu‐
276       lar context, where some pragmas are already in scope. In this case, you
277       use the ambient_pragmas method to describe the assumptions you wish to
278       make.
279
280       Not all of the options currently have any useful effect. See "BUGS" for
281       more details.
282
283       The parameters it accepts are:
284
285       strict
286           Takes a string, possibly containing several values separated by
287           whitespace. The special values "all" and "none" mean what you'd
288           expect.
289
290               $deparse->ambient_pragmas(strict => 'subs refs');
291
292       $[  Takes a number, the value of the array base $[.
293
294       bytes
295       utf8
296       integer
297           If the value is true, then the appropriate pragma is assumed to be
298           in the ambient scope, otherwise not.
299
300       re  Takes a string, possibly containing a whitespace-separated list of
301           values. The values "all" and "none" are special. It's also permis‐
302           sible to pass an array reference here.
303
304               $deparser->ambient_pragmas(re => 'eval');
305
306       warnings
307           Takes a string, possibly containing a whitespace-separated list of
308           values. The values "all" and "none" are special, again. It's also
309           permissible to pass an array reference here.
310
311               $deparser->ambient_pragmas(warnings => [qw[void io]]);
312
313           If one of the values is the string "FATAL", then all the warnings
314           in that list will be considered fatal, just as with the warnings
315           pragma itself. Should you need to specify that some warnings are
316           fatal, and others are merely enabled, you can pass the warnings
317           parameter twice:
318
319               $deparser->ambient_pragmas(
320                   warnings => 'all',
321                   warnings => [FATAL => qw/void io/],
322               );
323
324           See perllexwarn for more information about lexical warnings.
325
326       hint_bits
327       warning_bits
328           These two parameters are used to specify the ambient pragmas in the
329           format used by the special variables $^H and ${^WARNING_BITS}.
330
331           They exist principally so that you can write code like:
332
333               { my ($hint_bits, $warning_bits);
334               BEGIN {($hint_bits, $warning_bits) = ($^H, ${^WARNING_BITS})}
335               $deparser->ambient_pragmas (
336                   hint_bits    => $hint_bits,
337                   warning_bits => $warning_bits,
338                   '$['         => 0 + $[
339               ); }
340
341           which specifies that the ambient pragmas are exactly those which
342           are in scope at the point of calling.
343
344       coderef2text
345
346           $body = $deparse->coderef2text(\&func)
347           $body = $deparse->coderef2text(sub ($$) { ... })
348
349       Return source code for the body of a subroutine (a block, optionally
350       preceded by a prototype in parens), given a reference to the sub.
351       Because a subroutine can have no names, or more than one name, this
352       method doesn't return a complete subroutine definition -- if you want
353       to eval the result, you should prepend "sub subname ", or "sub " for an
354       anonymous function constructor. Unless the sub was defined in the
355       main:: package, the code will include a package declaration.
356

BUGS

358       ·   The only pragmas to be completely supported are: "use warnings",
359           "use strict 'refs'", "use bytes", and "use integer". ($[, which
360           behaves like a pragma, is also supported.)
361
362           Excepting those listed above, we're currently unable to guarantee
363           that B::Deparse will produce a pragma at the correct point in the
364           program.  (Specifically, pragmas at the beginning of a block often
365           appear right before the start of the block instead.)  Since the
366           effects of pragmas are often lexically scoped, this can mean that
367           the pragma holds sway over a different portion of the program than
368           in the input file.
369
370       ·   In fact, the above is a specific instance of a more general prob‐
371           lem: we can't guarantee to produce BEGIN blocks or "use" declara‐
372           tions in exactly the right place. So if you use a module which
373           affects compilation (such as by over-riding keywords, overloading
374           constants or whatever) then the output code might not work as
375           intended.
376
377           This is the most serious outstanding problem, and will require some
378           help from the Perl core to fix.
379
380       ·   If a keyword is over-ridden, and your program explicitly calls the
381           built-in version by using CORE::keyword, the output of B::Deparse
382           will not reflect this. If you run the resulting code, it will call
383           the over-ridden version rather than the built-in one. (Maybe there
384           should be an option to always print keyword calls as "CORE::name".)
385
386       ·   Some constants don't print correctly either with or without -d.
387           For instance, neither B::Deparse nor Data::Dumper know how to print
388           dual-valued scalars correctly, as in:
389
390               use constant E2BIG => ($!=7); $y = E2BIG; print $y, 0+$y;
391
392       ·   An input file that uses source filtering probably won't be deparsed
393           into runnable code, because it will still include the use declara‐
394           tion for the source filtering module, even though the code that is
395           produced is already ordinary Perl which shouldn't be filtered
396           again.
397
398       ·   Optimised away statements are rendered as '???'. This includes
399           statements that have a compile-time side-effect, such as the
400           obscure
401
402               my $x if 0;
403
404           which is not, consequently, deparsed correctly.
405
406       ·   There are probably many more bugs on non-ASCII platforms (EBCDIC).
407

AUTHOR

409       Stephen McCamant <smcc@CSUA.Berkeley.EDU>, based on an earlier version
410       by Malcolm Beattie <mbeattie@sable.ox.ac.uk>, with contributions from
411       Gisle Aas, James Duncan, Albert Dvornik, Robin Houston, Dave Mitchell,
412       Hugo van der Sanden, Gurusamy Sarathy, Nick Ing-Simmons, and Rafael
413       Garcia-Suarez.
414
415
416
417perl v5.8.8                       2001-09-21                   B::Deparse(3pm)
Impressum