3 Perl::Tidy - Parses and beautifies perl source
9 my $error_flag = Perl::Tidy::perltidy(
11 destination => $destination,
14 perltidyrc => $perltidyrc,
16 errorfile => $errorfile,
17 formatter => $formatter, # callback object (see below)
18 dump_options => $dump_options,
19 dump_options_type => $dump_options_type,
20 prefilter => $prefilter_coderef,
21 postfilter => $postfilter_coderef,
26 This module makes the functionality of the perltidy utility available to perl
27 scripts. Any or all of the input parameters may be omitted, in which case the
28 @ARGV array will be used to provide input parameters as described
29 in the perltidy(1) man page.
31 For example, the perltidy script is basically just this:
34 Perl::Tidy::perltidy();
36 The call to B<perltidy> returns a scalar B<$error_flag> which is TRUE if an
37 error caused premature termination, and FALSE if the process ran to normal
38 completion. Additional discuss of errors is contained below in the L<ERROR
41 The module accepts input and output streams by a variety of methods.
42 The following list of parameters may be any of the following: a
43 filename, an ARRAY reference, a SCALAR reference, or an object with
44 either a B<getline> or B<print> method, as appropriate.
46 source - the source of the script to be formatted
47 destination - the destination of the formatted output
48 stderr - standard error output
49 perltidyrc - the .perltidyrc file
50 logfile - the .LOG file stream, if any
51 errorfile - the .ERR file stream, if any
52 dump_options - ref to a hash to receive parameters (see below),
53 dump_options_type - controls contents of dump_options
54 dump_getopt_flags - ref to a hash to receive Getopt flags
55 dump_options_category - ref to a hash giving category of options
56 dump_abbreviations - ref to a hash giving all abbreviations
58 The following chart illustrates the logic used to decide how to
61 ref($param) $param is assumed to be:
62 ----------- ---------------------
66 (other) object with getline (if source) or print method
68 If the parameter is an object, and the object has a B<close> method, that
69 close method will be called at the end of the stream.
75 If the B<source> parameter is given, it defines the source of the input stream.
76 If an input stream is defined with the B<source> parameter then no other source
77 filenames may be specified in the @ARGV array or B<argv> parameter.
81 If the B<destination> parameter is given, it will be used to define the
82 file or memory location to receive output of perltidy.
86 The B<stderr> parameter allows the calling program to redirect the stream that
87 would otherwise go to the standard error output device to any of the stream
88 types listed above. This stream contains important warnings and errors
89 related to the parameters passed to perltidy.
93 If the B<perltidyrc> file is given, it will be used instead of any
94 F<.perltidyrc> configuration file that would otherwise be used.
98 The B<errorfile> parameter allows the calling program to capture
99 the stream that would otherwise go to either a .ERR file. This
100 stream contains warnings or errors related to the contents of one
101 source file or stream.
103 The reason that this is different from the stderr stream is that when perltidy
104 is called to process multiple files there will be up to one .ERR file created
105 for each file and it would be very confusing if they were combined.
107 However if perltidy is called to process just a single perl script then it may
108 be more convenient to combine the B<errorfile> stream with the B<stderr>
109 stream. This can be done by setting the B<-se> parameter, in which case this
110 parameter is ignored.
114 The B<logfile> parameter allows the calling program to capture the log stream.
115 This stream is only created if requested with a B<-g> parameter. It contains
116 detailed diagnostic information about a script which may be useful for
121 If the B<argv> parameter is given, it will be used instead of the
122 B<@ARGV> array. The B<argv> parameter may be a string, a reference to a
123 string, or a reference to an array. If it is a string or reference to a
124 string, it will be parsed into an array of items just as if it were a
129 If the B<dump_options> parameter is given, it must be the reference to a hash.
130 In this case, the parameters contained in any perltidyrc configuration file
131 will be placed in this hash and perltidy will return immediately. This is
132 equivalent to running perltidy with --dump-options, except that the parameters
133 are returned in a hash rather than dumped to standard output. Also, by default
134 only the parameters in the perltidyrc file are returned, but this can be
135 changed (see the next parameter). This parameter provides a convenient method
136 for external programs to read a perltidyrc file. An example program using
137 this feature, F<perltidyrc_dump.pl>, is included in the distribution.
139 Any combination of the B<dump_> parameters may be used together.
141 =item dump_options_type
143 This parameter is a string which can be used to control the parameters placed
144 in the hash reference supplied by B<dump_options>. The possible values are
145 'perltidyrc' (default) and 'full'. The 'full' parameter causes both the
146 default options plus any options found in a perltidyrc file to be returned.
148 =item dump_getopt_flags
150 If the B<dump_getopt_flags> parameter is given, it must be the reference to a
151 hash. This hash will receive all of the parameters that perltidy understands
152 and flags that are passed to Getopt::Long. This parameter may be
153 used alone or with the B<dump_options> flag. Perltidy will
154 exit immediately after filling this hash. See the demo program
155 F<perltidyrc_dump.pl> for example usage.
157 =item dump_options_category
159 If the B<dump_options_category> parameter is given, it must be the reference to a
160 hash. This hash will receive a hash with keys equal to all long parameter names
161 and values equal to the title of the corresponding section of the perltidy manual.
162 See the demo program F<perltidyrc_dump.pl> for example usage.
164 =item dump_abbreviations
166 If the B<dump_abbreviations> parameter is given, it must be the reference to a
167 hash. This hash will receive all abbreviations used by Perl::Tidy. See the
168 demo program F<perltidyrc_dump.pl> for example usage.
172 A code reference that will be applied to the source before tidying. It is
173 expected to take the full content as a string in its input, and output the
178 A code reference that will be applied to the tidied result before outputting.
179 It is expected to take the full content as a string in its input, and output
180 the transformed content.
182 Note: A convenient way to check the function of your custom prefilter and
183 postfilter code is to use the --notidy option, first with just the prefilter
184 and then with both the prefilter and postfilter. See also the file
185 B<filter_example.pl> in the perltidy distribution.
189 =head1 ERROR HANDLING
191 An exit value of 0, 1, or 2 is returned by perltidy to indicate the status of the result.
193 A exit value of 0 indicates that perltidy ran to completion with no error messages.
195 An exit value of 1 indicates that the process had to be terminated early due to
196 errors in the input parameters. This can happen for example if a parameter is
197 misspelled or given an invalid value. The calling program should check for
198 this flag because if it is set the destination stream will be empty or
199 incomplete and should be ignored. Error messages in the B<stderr> stream will
200 indicate the cause of any problem.
202 An exit value of 2 indicates that perltidy ran to completion but there there
203 are warning messages in the B<stderr> stream related to parameter errors or
204 conflicts and/or warning messages in the B<errorfile> stream relating to
205 possible syntax errors in the source code being tidied.
207 In the event of a catastrophic error for which recovery is not possible
208 B<perltidy> terminates by making calls to B<croak> or B<confess> to help the
209 programmer localize the problem. These should normally only occur during
212 =head1 NOTES ON FORMATTING PARAMETERS
214 Parameters which control formatting may be passed in several ways: in a
215 F<.perltidyrc> configuration file, in the B<perltidyrc> parameter, and in the
218 The B<-syn> (B<--check-syntax>) flag may be used with all source and
219 destination streams except for standard input and output. However
220 data streams which are not associated with a filename will
221 be copied to a temporary file before being passed to Perl. This
222 use of temporary files can cause somewhat confusing output from Perl.
224 If the B<-pbp> style is used it will typically be necessary to also
225 specify a B<-nst> flag. This is necessary to turn off the B<-st> flag
226 contained in the B<-pbp> parameter set which otherwise would direct
227 the output stream to the standard output.
231 The following example uses string references to hold the input and output
232 code and error streams, and illustrates checking for errors.
236 my $source_string = <<'EOT';
237 my$error=Perl::Tidy::perltidy(argv=>$argv,source=>\$source_string,
238 destination=>\$dest_string,stderr=>\$stderr_string,
239 errorfile=>\$errorfile_string,);
244 my $errorfile_string;
245 my $argv = "-npro"; # Ignore any .perltidyrc at this site
246 $argv .= " -pbp"; # Format according to perl best practices
247 $argv .= " -nst"; # Must turn off -st in case -pbp is specified
248 $argv .= " -se"; # -se appends the errorfile to stderr
249 ## $argv .= " --spell-check"; # uncomment to trigger an error
251 print "<<RAW SOURCE>>\n$source_string\n";
253 my $error = Perl::Tidy::perltidy(
255 source => \$source_string,
256 destination => \$dest_string,
257 stderr => \$stderr_string,
258 errorfile => \$errorfile_string, # ignored when -se flag is set
259 ##phasers => 'stun', # uncomment to trigger an error
264 # serious error in input parameters, no tidied output
265 print "<<STDERR>>\n$stderr_string\n";
266 die "Exiting because of serious errors\n";
269 if ($dest_string) { print "<<TIDIED SOURCE>>\n$dest_string\n" }
270 if ($stderr_string) { print "<<STDERR>>\n$stderr_string\n" }
271 if ($errorfile_string) { print "<<.ERR file>>\n$errorfile_string\n" }
273 Additional examples are given in examples section of the perltidy distribution.
275 =head1 Using the B<formatter> Callback Object
277 The B<formatter> parameter is an optional callback object which allows
278 the calling program to receive tokenized lines directly from perltidy for
279 further specialized processing. When this parameter is used, the two
280 formatting options which are built into perltidy (beautification or
281 html) are ignored. The following diagram illustrates the logical flow:
283 |-- (normal route) -> code beautification
284 caller->perltidy->|-- (-html flag ) -> create html
285 |-- (formatter given)-> callback to write_line
287 This can be useful for processing perl scripts in some way. The
288 parameter C<$formatter> in the perltidy call,
290 formatter => $formatter,
292 is an object created by the caller with a C<write_line> method which
293 will accept and process tokenized lines, one line per call. Here is
294 a simple example of a C<write_line> which merely prints the line number,
295 the line type (as determined by perltidy), and the text of the line:
299 # This is called from perltidy line-by-line
301 my $line_of_tokens = shift;
302 my $line_type = $line_of_tokens->{_line_type};
303 my $input_line_number = $line_of_tokens->{_line_number};
304 my $input_line = $line_of_tokens->{_line_text};
305 print "$input_line_number:$line_type:$input_line";
308 The complete program, B<perllinetype>, is contained in the examples section of
309 the source distribution. As this example shows, the callback method
310 receives a parameter B<$line_of_tokens>, which is a reference to a hash
311 of other useful information. This example uses these hash entries:
313 $line_of_tokens->{_line_number} - the line number (1,2,...)
314 $line_of_tokens->{_line_text} - the text of the line
315 $line_of_tokens->{_line_type} - the type of the line, one of:
317 SYSTEM - system-specific code before hash-bang line
318 CODE - line of perl code (including comments)
319 POD_START - line starting pod, such as '=head'
320 POD - pod documentation text
321 POD_END - last line of pod section, '=cut'
322 HERE - text of here-document
323 HERE_END - last line of here-doc (target word)
324 FORMAT - format section
325 FORMAT_END - last line of format section, '.'
326 DATA_START - __DATA__ line
327 DATA - unidentified text following __DATA__
328 END_START - __END__ line
329 END - unidentified text following __END__
330 ERROR - we are in big trouble, probably not a perl script
332 Most applications will be only interested in lines of type B<CODE>. For
333 another example, let's write a program which checks for one of the
334 so-called I<naughty matching variables> C<&`>, C<$&>, and C<$'>, which
335 can slow down processing. Here is a B<write_line>, from the example
336 program B<find_naughty.pl>, which does that:
340 # This is called back from perltidy line-by-line
341 # We're looking for $`, $&, and $'
342 my ( $self, $line_of_tokens ) = @_;
344 # pull out some stuff we might need
345 my $line_type = $line_of_tokens->{_line_type};
346 my $input_line_number = $line_of_tokens->{_line_number};
347 my $input_line = $line_of_tokens->{_line_text};
348 my $rtoken_type = $line_of_tokens->{_rtoken_type};
349 my $rtokens = $line_of_tokens->{_rtokens};
352 # skip comments, pod, etc
353 return if ( $line_type ne 'CODE' );
355 # loop over tokens looking for $`, $&, and $'
356 for ( my $j = 0 ; $j < @$rtoken_type ; $j++ ) {
358 # we only want to examine token types 'i' (identifier)
359 next unless $$rtoken_type[$j] eq 'i';
361 # pull out the actual token text
362 my $token = $$rtokens[$j];
365 if ( $token =~ /^\$[\`\&\']$/ ) {
367 "$input_line_number: $token\n";
372 This example pulls out these tokenization variables from the $line_of_tokens
375 $rtoken_type = $line_of_tokens->{_rtoken_type};
376 $rtokens = $line_of_tokens->{_rtokens};
378 The variable C<$rtoken_type> is a reference to an array of token type codes,
379 and C<$rtokens> is a reference to a corresponding array of token text.
380 These are obviously only defined for lines of type B<CODE>.
381 Perltidy classifies tokens into types, and has a brief code for each type.
382 You can get a complete list at any time by running perltidy from the
385 perltidy --dump-token-types
387 In the present example, we are only looking for tokens of type B<i>
388 (identifiers), so the for loop skips past all other types. When an
389 identifier is found, its actual text is checked to see if it is one
390 being sought. If so, the above write_line prints the token and its
393 The B<formatter> feature is relatively new in perltidy, and further
394 documentation needs to be written to complete its description. However,
395 several example programs have been written and can be found in the
396 B<examples> section of the source distribution. Probably the best way
397 to get started is to find one of the examples which most closely matches
398 your application and start modifying it.
400 For help with perltidy's peculiar way of breaking lines into tokens, you
401 might run, from the command line,
405 where F<filename> is a short script of interest. This will produce
406 F<filename.DEBUG> with interleaved lines of text and their token types.
407 The B<-D> flag has been in perltidy from the beginning for this purpose.
408 If you want to see the code which creates this file, it is
409 C<write_debug_entry> in Tidy.pm.
417 The module 'Perl::Tidy' comes with a binary 'perltidy' which is installed when the module is installed. The module name is case-sensitive. For example, the basic command for installing with cpanm is 'cpanm Perl::Tidy'.
421 This man page documents Perl::Tidy version 20200110
425 This package is free software; you can redistribute it and/or modify it
426 under the terms of the "GNU General Public License".
428 Please refer to the file "COPYING" for details.
432 A list of current bugs and issues can be found at the CPAN site L<https://rt.cpan.org/Public/Dist/Display.html?Name=Perl-Tidy>
434 To report a new bug or problem, use the link on this page.
436 The source code repository is at L<https://github.com/perltidy/perltidy>.
440 The perltidy(1) man page describes all of the features of perltidy. It
441 can be found at http://perltidy.sourceforge.net.