]> git.donarmstrong.com Git - perltidy.git/blob - lib/Perl/Tidy.pod
New upstream version 20221112
[perltidy.git] / lib / Perl / Tidy.pod
1
2 =head1 NAME
3
4 Perl::Tidy - Parses and beautifies perl source
5
6 =head1 SYNOPSIS
7
8     use Perl::Tidy;
9
10     my $error_flag = Perl::Tidy::perltidy(
11         source            => $source,
12         destination       => $destination,
13         stderr            => $stderr,
14         argv              => $argv,
15         perltidyrc        => $perltidyrc,
16         logfile           => $logfile,
17         errorfile         => $errorfile,
18         teefile           => $teefile,
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,
25     );
26
27 =head1 DESCRIPTION
28
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.
33
34 For example, the perltidy script is basically just this:
35
36     use Perl::Tidy;
37     Perl::Tidy::perltidy();
38
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.
43
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.
48
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
60
61 The following chart illustrates the logic used to decide how to
62 treat a parameter.
63
64    ref($param)  $param is assumed to be:
65    -----------  ---------------------
66    undef        a filename
67    SCALAR       ref to string
68    ARRAY        ref to array
69    (other)      object with getline (if source) or print method
70
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.
73
74 =over 4
75
76 =item B<source>
77
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.
81
82 =item B<destination>
83
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.
86
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.
93
94 =over 4
95
96 =item *
97
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
102 data.
103
104 =item *
105
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.
110
111 =back
112
113 For some background information see
114 L<https://github.com/perltidy/perltidy/blob/master/docs/eos_flag.md>.
115
116 This change in default behavior was made over a period of time as follows:
117
118 =over 4
119
120 =item *
121
122 For versions before 20220217 the B<-eos> flag was not available and the behavior was equivalent to B<-neos>.
123
124 =item *
125
126 In version 20220217 the B<-eos> flag was added but the default remained B<-neos>.
127
128 =item *
129
130 For versions after 20220217 the default was set to B<-eos>.
131
132 =back
133
134 =item B<stderr>
135
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.
140
141 =item B<perltidyrc>
142
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.
145
146 =item B<errorfile>
147
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.
152
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.
156
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.
161
162 =item B<logfile>
163
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
167 debugging.
168
169 =item B<teefile>
170
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>.
174
175 =item B<debugfile>
176
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.
180
181 =item B<argv>
182
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
187 command line string.
188
189 =item B<dump_options>
190
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.
200
201 Any combination of the B<dump_> parameters may be used together.
202
203 =item B<dump_options_type>
204
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.
209
210 =item B<dump_getopt_flags>
211
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.
218
219 =item B<dump_options_category>
220
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.
225
226 =item B<dump_abbreviations>
227
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.
231
232 =item B<prefilter>
233
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
236 transformed content.
237
238 =item B<postfilter>
239
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.
243
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.
248
249 =back
250
251 =head1 ERROR HANDLING
252
253 An exit value of 0, 1, or 2 is returned by perltidy to indicate the status of the result.
254
255 A exit value of 0 indicates that perltidy ran to completion with no error messages.
256
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.
263
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.
268
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
272 program development.
273
274 =head1 NOTES ON FORMATTING PARAMETERS
275
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
278 B<argv> parameter.
279
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.
285
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.
290
291 =head1 EXAMPLES
292
293 The following example uses string references to hold the input and output
294 code and error streams, and illustrates checking for errors.
295
296   use Perl::Tidy;
297
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,);
302   EOT
303
304   my $dest_string;
305   my $stderr_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
312
313   print "<<RAW SOURCE>>\n$source_string\n";
314
315   my $error = Perl::Tidy::perltidy(
316       argv        => $argv,
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
322   );
323
324   if ($error) {
325
326       # serious error in input parameters, no tidied output
327       print "<<STDERR>>\n$stderr_string\n";
328       die "Exiting because of serious errors\n";
329   }
330
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" }
334
335 Additional examples are given in examples section of the perltidy distribution.
336
337 =head1 Using the B<formatter> Callback Object
338
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:
344
345                     |-- (normal route)   -> code beautification
346   caller->perltidy->|-- (-html flag )    -> create html
347                     |-- (formatter given)-> callback to write_line
348
349 This can be useful for processing perl scripts in some way.  The
350 parameter C<$formatter> in the perltidy call,
351
352         formatter   => $formatter,
353
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:
358
359  sub write_line {
360
361      # This is called from perltidy line-by-line
362      my $self              = shift;
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";
368  }
369
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:
374
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:
378
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
393
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:
399
400  sub write_line {
401
402      # This is called back from perltidy line-by-line
403      # We're looking for $`, $&, and $'
404      my ( $self, $line_of_tokens ) = @_;
405
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};
412      chomp $input_line;
413
414      # skip comments, pod, etc
415      return if ( $line_type ne 'CODE' );
416
417      # loop over tokens looking for $`, $&, and $'
418      for ( my $j = 0 ; $j < @$rtoken_type ; $j++ ) {
419
420          # we only want to examine token types 'i' (identifier)
421          next unless $$rtoken_type[$j] eq 'i';
422
423          # pull out the actual token text
424          my $token = $$rtokens[$j];
425
426          # and check it
427          if ( $token =~ /^\$[\`\&\']$/ ) {
428              print STDERR
429                "$input_line_number: $token\n";
430          }
431      }
432  }
433
434 This example pulls out these tokenization variables from the $line_of_tokens
435 hash reference:
436
437      $rtoken_type = $line_of_tokens->{_rtoken_type};
438      $rtokens     = $line_of_tokens->{_rtokens};
439
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
445 command line with
446
447      perltidy --dump-token-types
448
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
453 line number.
454
455 The B<examples> section of the source distribution has some examples of programs which use the B<formatter> option.
456
457 For help with perltidy's peculiar way of breaking lines into tokens, you
458 might run, from the command line,
459
460  perltidy -D filename
461
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>
467
468 =head1 EXPORT
469
470   &perltidy
471
472 =head1 INSTALLATION
473
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'.
475
476 =head1 VERSION
477
478 This man page documents Perl::Tidy version 20221112
479
480 =head1 LICENSE
481
482 This package is free software; you can redistribute it and/or modify it
483 under the terms of the "GNU General Public License".
484
485 Please refer to the file "COPYING" for details.
486
487 =head1 BUG REPORTS
488
489 The source code repository is at L<https://github.com/perltidy/perltidy>.
490
491 To report a new bug or problem, use the "issues" link on this page.
492
493 =head1 SEE ALSO
494
495 The perltidy(1) man page describes all of the features of perltidy.  It
496 can be found at http://perltidy.sourceforge.net.
497
498 =cut