4 Perl::Tidy - Parses and beautifies perl source
10 my $error_flag = Perl::Tidy::perltidy(
12 destination => $destination,
15 perltidyrc => $perltidyrc,
17 errorfile => $errorfile,
19 debugfile => $debugfile,
20 formatter => $formatter, # callback object (see below)
21 dump_options => $dump_options,
22 dump_options_type => $dump_options_type,
23 prefilter => $prefilter_coderef,
24 postfilter => $postfilter_coderef,
29 This module makes the functionality of the perltidy utility available to perl
30 scripts. Any or all of the input parameters may be omitted, in which case the
31 @ARGV array will be used to provide input parameters as described
32 in the perltidy(1) man page.
34 For example, the perltidy script is basically just this:
37 Perl::Tidy::perltidy();
39 The call to B<perltidy> returns a scalar B<$error_flag> which is TRUE if an
40 error caused premature termination, and FALSE if the process ran to normal
41 completion. Additional discuss of errors is contained below in the
42 L<ERROR HANDLING|"ERROR HANDLING"> section.
44 The module accepts input and output streams by a variety of methods.
45 The following list of parameters may be any of the following: a
46 filename, an ARRAY reference, a SCALAR reference, or an object with
47 either a B<getline> or B<print> method, as appropriate.
49 source - the source of the script to be formatted
50 destination - the destination of the formatted output
51 stderr - standard error output
52 perltidyrc - the .perltidyrc file
53 logfile - the .LOG file stream, if any
54 errorfile - the .ERR file stream, if any
55 dump_options - ref to a hash to receive parameters (see below),
56 dump_options_type - controls contents of dump_options
57 dump_getopt_flags - ref to a hash to receive Getopt flags
58 dump_options_category - ref to a hash giving category of options
59 dump_abbreviations - ref to a hash giving all abbreviations
61 The following chart illustrates the logic used to decide how to
64 ref($param) $param is assumed to be:
65 ----------- ---------------------
69 (other) object with getline (if source) or print method
71 If the parameter is an object, and the object has a B<close> method, that
72 close method will be called at the end of the stream.
78 If the B<source> parameter is given, it defines the source of the input stream.
79 If an input stream is defined with the B<source> parameter then no other source
80 filenames may be specified in the @ARGV array or B<argv> parameter.
84 If the B<destination> parameter is given, it will be used to define the
85 file or memory location to receive output of perltidy.
87 B<Important note if destination is a string or array reference>. Perl strings
88 of characters which are decoded as utf8 by Perl::Tidy can be returned in either
89 of two possible states, decoded or encoded, and it is important that the
90 calling program and Perl::Tidy are in agreement regarding the state to be
91 returned. A flag B<--encode-output-strings>, or simply B<-eos>, was added in
92 Perl::Tidy version 20220217 for this purpose.
98 Use B<-eos> if Perl::Tidy should encode any string which it decodes. This is
99 the current default because it makes perltidy behave well as a filter, and is
100 the correct setting for most programs. But do not use this setting if the
101 calling program will encode the data too, because double encoding will corrupt
106 Use B<-neos> if a string should remain decoded if it was decoded by Perl::Tidy.
107 This is only appropriate if the calling program will handle any needed encoding
108 before outputting the string. If needed, this flag can be added to the end of
109 the B<argv> parameter passed to Perl::Tidy.
113 For some background information see
114 L<https://github.com/perltidy/perltidy/blob/master/docs/eos_flag.md>.
116 This change in default behavior was made over a period of time as follows:
122 For versions before 20220217 the B<-eos> flag was not available and the behavior was equivalent to B<-neos>.
126 In version 20220217 the B<-eos> flag was added but the default remained B<-neos>.
130 For versions after 20220217 the default was set to B<-eos>.
136 The B<stderr> parameter allows the calling program to redirect the stream that
137 would otherwise go to the standard error output device to any of the stream
138 types listed above. This stream contains important warnings and errors
139 related to the parameters passed to perltidy.
143 If the B<perltidyrc> file is given, it will be used instead of any
144 F<.perltidyrc> configuration file that would otherwise be used.
148 The B<errorfile> parameter allows the calling program to capture
149 the stream that would otherwise go to either a .ERR file. This
150 stream contains warnings or errors related to the contents of one
151 source file or stream.
153 The reason that this is different from the stderr stream is that when perltidy
154 is called to process multiple files there will be up to one .ERR file created
155 for each file and it would be very confusing if they were combined.
157 However if perltidy is called to process just a single perl script then it may
158 be more convenient to combine the B<errorfile> stream with the B<stderr>
159 stream. This can be done by setting the B<-se> parameter, in which case this
160 parameter is ignored.
164 The B<logfile> parameter allows the calling program to capture the log stream.
165 This stream is only created if requested with a B<-g> parameter. It contains
166 detailed diagnostic information about a script which may be useful for
171 The B<teefile> parameter allows the calling program to capture the tee stream.
172 This stream is only created if requested with one of the 'tee' parameters,
173 a B<--tee-pod> , B<--tee-block-comments>, B<--tee-side-commnts>, or B<--tee-all-comments>.
177 The B<debugfile> parameter allows the calling program to capture the stream
178 produced by the B<--DEBUG> parameter. This parameter is mainly used for
179 debugging perltidy itself.
183 If the B<argv> parameter is given, it will be used instead of the
184 B<@ARGV> array. The B<argv> parameter may be a string, a reference to a
185 string, or a reference to an array. If it is a string or reference to a
186 string, it will be parsed into an array of items just as if it were a
189 =item B<dump_options>
191 If the B<dump_options> parameter is given, it must be the reference to a hash.
192 In this case, the parameters contained in any perltidyrc configuration file
193 will be placed in this hash and perltidy will return immediately. This is
194 equivalent to running perltidy with --dump-options, except that the parameters
195 are returned in a hash rather than dumped to standard output. Also, by default
196 only the parameters in the perltidyrc file are returned, but this can be
197 changed (see the next parameter). This parameter provides a convenient method
198 for external programs to read a perltidyrc file. An example program using
199 this feature, F<perltidyrc_dump.pl>, is included in the distribution.
201 Any combination of the B<dump_> parameters may be used together.
203 =item B<dump_options_type>
205 This parameter is a string which can be used to control the parameters placed
206 in the hash reference supplied by B<dump_options>. The possible values are
207 'perltidyrc' (default) and 'full'. The 'full' parameter causes both the
208 default options plus any options found in a perltidyrc file to be returned.
210 =item B<dump_getopt_flags>
212 If the B<dump_getopt_flags> parameter is given, it must be the reference to a
213 hash. This hash will receive all of the parameters that perltidy understands
214 and flags that are passed to Getopt::Long. This parameter may be
215 used alone or with the B<dump_options> flag. Perltidy will
216 exit immediately after filling this hash. See the demo program
217 F<perltidyrc_dump.pl> for example usage.
219 =item B<dump_options_category>
221 If the B<dump_options_category> parameter is given, it must be the reference to a
222 hash. This hash will receive a hash with keys equal to all long parameter names
223 and values equal to the title of the corresponding section of the perltidy manual.
224 See the demo program F<perltidyrc_dump.pl> for example usage.
226 =item B<dump_abbreviations>
228 If the B<dump_abbreviations> parameter is given, it must be the reference to a
229 hash. This hash will receive all abbreviations used by Perl::Tidy. See the
230 demo program F<perltidyrc_dump.pl> for example usage.
234 A code reference that will be applied to the source before tidying. It is
235 expected to take the full content as a string in its input, and output the
240 A code reference that will be applied to the tidied result before outputting.
241 It is expected to take the full content as a string in its input, and output
242 the transformed content.
244 Note: A convenient way to check the function of your custom prefilter and
245 postfilter code is to use the --notidy option, first with just the prefilter
246 and then with both the prefilter and postfilter. See also the file
247 B<filter_example.pl> in the perltidy distribution.
251 =head1 ERROR HANDLING
253 An exit value of 0, 1, or 2 is returned by perltidy to indicate the status of the result.
255 A exit value of 0 indicates that perltidy ran to completion with no error messages.
257 An exit value of 1 indicates that the process had to be terminated early due to
258 errors in the input parameters. This can happen for example if a parameter is
259 misspelled or given an invalid value. The calling program should check for
260 this flag because if it is set the destination stream will be empty or
261 incomplete and should be ignored. Error messages in the B<stderr> stream will
262 indicate the cause of any problem.
264 An exit value of 2 indicates that perltidy ran to completion but there there
265 are warning messages in the B<stderr> stream related to parameter errors or
266 conflicts and/or warning messages in the B<errorfile> stream relating to
267 possible syntax errors in the source code being tidied.
269 In the event of a catastrophic error for which recovery is not possible
270 B<perltidy> terminates by making calls to B<croak> or B<confess> to help the
271 programmer localize the problem. These should normally only occur during
274 =head1 NOTES ON FORMATTING PARAMETERS
276 Parameters which control formatting may be passed in several ways: in a
277 F<.perltidyrc> configuration file, in the B<perltidyrc> parameter, and in the
280 The B<-syn> (B<--check-syntax>) flag may be used with all source and
281 destination streams except for standard input and output. However
282 data streams which are not associated with a filename will
283 be copied to a temporary file before being passed to Perl. This
284 use of temporary files can cause somewhat confusing output from Perl.
286 If the B<-pbp> style is used it will typically be necessary to also
287 specify a B<-nst> flag. This is necessary to turn off the B<-st> flag
288 contained in the B<-pbp> parameter set which otherwise would direct
289 the output stream to the standard output.
293 The following example uses string references to hold the input and output
294 code and error streams, and illustrates checking for errors.
298 my $source_string = <<'EOT';
299 my$error=Perl::Tidy::perltidy(argv=>$argv,source=>\$source_string,
300 destination=>\$dest_string,stderr=>\$stderr_string,
301 errorfile=>\$errorfile_string,);
306 my $errorfile_string;
307 my $argv = "-npro"; # Ignore any .perltidyrc at this site
308 $argv .= " -pbp"; # Format according to perl best practices
309 $argv .= " -nst"; # Must turn off -st in case -pbp is specified
310 $argv .= " -se"; # -se appends the errorfile to stderr
311 ## $argv .= " --spell-check"; # uncomment to trigger an error
313 print "<<RAW SOURCE>>\n$source_string\n";
315 my $error = Perl::Tidy::perltidy(
317 source => \$source_string,
318 destination => \$dest_string,
319 stderr => \$stderr_string,
320 errorfile => \$errorfile_string, # ignored when -se flag is set
321 ##phasers => 'stun', # uncomment to trigger an error
326 # serious error in input parameters, no tidied output
327 print "<<STDERR>>\n$stderr_string\n";
328 die "Exiting because of serious errors\n";
331 if ($dest_string) { print "<<TIDIED SOURCE>>\n$dest_string\n" }
332 if ($stderr_string) { print "<<STDERR>>\n$stderr_string\n" }
333 if ($errorfile_string) { print "<<.ERR file>>\n$errorfile_string\n" }
335 Additional examples are given in examples section of the perltidy distribution.
337 =head1 Using the B<formatter> Callback Object
339 The B<formatter> parameter is an optional callback object which allows
340 the calling program to receive tokenized lines directly from perltidy for
341 further specialized processing. When this parameter is used, the two
342 formatting options which are built into perltidy (beautification or
343 html) are ignored. The following diagram illustrates the logical flow:
345 |-- (normal route) -> code beautification
346 caller->perltidy->|-- (-html flag ) -> create html
347 |-- (formatter given)-> callback to write_line
349 This can be useful for processing perl scripts in some way. The
350 parameter C<$formatter> in the perltidy call,
352 formatter => $formatter,
354 is an object created by the caller with a C<write_line> method which
355 will accept and process tokenized lines, one line per call. Here is
356 a simple example of a C<write_line> which merely prints the line number,
357 the line type (as determined by perltidy), and the text of the line:
361 # This is called from perltidy line-by-line
363 my $line_of_tokens = shift;
364 my $line_type = $line_of_tokens->{_line_type};
365 my $input_line_number = $line_of_tokens->{_line_number};
366 my $input_line = $line_of_tokens->{_line_text};
367 print "$input_line_number:$line_type:$input_line";
370 The complete program, B<perllinetype>, is contained in the examples section of
371 the source distribution. As this example shows, the callback method
372 receives a parameter B<$line_of_tokens>, which is a reference to a hash
373 of other useful information. This example uses these hash entries:
375 $line_of_tokens->{_line_number} - the line number (1,2,...)
376 $line_of_tokens->{_line_text} - the text of the line
377 $line_of_tokens->{_line_type} - the type of the line, one of:
379 SYSTEM - system-specific code before hash-bang line
380 CODE - line of perl code (including comments)
381 POD_START - line starting pod, such as '=head'
382 POD - pod documentation text
383 POD_END - last line of pod section, '=cut'
384 HERE - text of here-document
385 HERE_END - last line of here-doc (target word)
386 FORMAT - format section
387 FORMAT_END - last line of format section, '.'
388 DATA_START - __DATA__ line
389 DATA - unidentified text following __DATA__
390 END_START - __END__ line
391 END - unidentified text following __END__
392 ERROR - we are in big trouble, probably not a perl script
394 Most applications will be only interested in lines of type B<CODE>. For
395 another example, let's write a program which checks for one of the
396 so-called I<naughty matching variables> C<&`>, C<$&>, and C<$'>, which
397 can slow down processing. Here is a B<write_line>, from the example
398 program B<find_naughty.pl>, which does that:
402 # This is called back from perltidy line-by-line
403 # We're looking for $`, $&, and $'
404 my ( $self, $line_of_tokens ) = @_;
406 # pull out some stuff we might need
407 my $line_type = $line_of_tokens->{_line_type};
408 my $input_line_number = $line_of_tokens->{_line_number};
409 my $input_line = $line_of_tokens->{_line_text};
410 my $rtoken_type = $line_of_tokens->{_rtoken_type};
411 my $rtokens = $line_of_tokens->{_rtokens};
414 # skip comments, pod, etc
415 return if ( $line_type ne 'CODE' );
417 # loop over tokens looking for $`, $&, and $'
418 for ( my $j = 0 ; $j < @$rtoken_type ; $j++ ) {
420 # we only want to examine token types 'i' (identifier)
421 next unless $$rtoken_type[$j] eq 'i';
423 # pull out the actual token text
424 my $token = $$rtokens[$j];
427 if ( $token =~ /^\$[\`\&\']$/ ) {
429 "$input_line_number: $token\n";
434 This example pulls out these tokenization variables from the $line_of_tokens
437 $rtoken_type = $line_of_tokens->{_rtoken_type};
438 $rtokens = $line_of_tokens->{_rtokens};
440 The variable C<$rtoken_type> is a reference to an array of token type codes,
441 and C<$rtokens> is a reference to a corresponding array of token text.
442 These are obviously only defined for lines of type B<CODE>.
443 Perltidy classifies tokens into types, and has a brief code for each type.
444 You can get a complete list at any time by running perltidy from the
447 perltidy --dump-token-types
449 In the present example, we are only looking for tokens of type B<i>
450 (identifiers), so the for loop skips past all other types. When an
451 identifier is found, its actual text is checked to see if it is one
452 being sought. If so, the above write_line prints the token and its
455 The B<examples> section of the source distribution has some examples of programs which use the B<formatter> option.
457 For help with perltidy's peculiar way of breaking lines into tokens, you
458 might run, from the command line,
462 where F<filename> is a short script of interest. This will produce
463 F<filename.DEBUG> with interleaved lines of text and their token types.
464 The B<-D> flag has been in perltidy from the beginning for this purpose.
465 If you want to see the code which creates this file, it is
466 C<sub Perl::Tidy::Debugger::write_debug_entry>
474 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'.
478 This man page documents Perl::Tidy version 20220613
482 This package is free software; you can redistribute it and/or modify it
483 under the terms of the "GNU General Public License".
485 Please refer to the file "COPYING" for details.
489 The source code repository is at L<https://github.com/perltidy/perltidy>.
491 To report a new bug or problem, use the "issues" link on this page.
495 The perltidy(1) man page describes all of the features of perltidy. It
496 can be found at http://perltidy.sourceforge.net.