1Perl::Tidy(3)         User Contributed Perl Documentation        Perl::Tidy(3)
2
3
4

NAME

6       Perl::Tidy - Parses and beautifies perl source
7

SYNOPSIS

9           use Perl::Tidy;
10
11           my $error_flag = Perl::Tidy::perltidy(
12               source            => $source,
13               destination       => $destination,
14               stderr            => $stderr,
15               argv              => $argv,
16               perltidyrc        => $perltidyrc,
17               logfile           => $logfile,
18               errorfile         => $errorfile,
19               teefile           => $teefile,
20               debugfile         => $debugfile,
21               formatter         => $formatter,           # callback object (see below)
22               dump_options      => $dump_options,
23               dump_options_type => $dump_options_type,
24               prefilter         => $prefilter_coderef,
25               postfilter        => $postfilter_coderef,
26           );
27

DESCRIPTION

29       This module makes the functionality of the perltidy utility available
30       to perl scripts.  Any or all of the input parameters may be omitted, in
31       which case the @ARGV array will be used to provide input parameters as
32       described 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 perltidy returns a scalar $error_flag which is TRUE if an
40       error caused premature termination, and FALSE if the process ran to
41       normal completion.  Additional discuss of errors is contained below in
42       the 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 getline or 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 treat a
62       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 close method, that
72       close method will be called at the end of the stream.
73
74       source
75           If the source parameter is given, it defines the source of the
76           input stream.  If an input stream is defined with the source
77           parameter then no other source filenames may be specified in the
78           @ARGV array or argv parameter.
79
80       destination
81           If the destination parameter is given, it will be used to define
82           the file or memory location to receive output of perltidy.
83
84           Important note if destination is a string or array reference.  Perl
85           strings of characters which are decoded as utf8 by Perl::Tidy can
86           be returned in either of two possible states, decoded or encoded,
87           and it is important that the calling program and Perl::Tidy are in
88           agreement regarding the state to be returned.  A flag
89           --encode-output-strings, or simply -eos, was added in Perl::Tidy
90           version 20220217 for this purpose.
91
92           •   Use -eos if Perl::Tidy should encode any string which it
93               decodes.  This is the current default because it makes perltidy
94               behave well as a filter, and is the correct setting for most
95               programs.  But do not use this setting if the calling program
96               will encode the data too, because double encoding will corrupt
97               data.
98
99           •   Use -neos if a string should remain decoded if it was decoded
100               by Perl::Tidy.  This is only appropriate if the calling program
101               will handle any needed encoding before outputting the string.
102               If needed, this flag can be added to the end of the argv
103               parameter passed to Perl::Tidy.
104
105           For some background information see
106           <https://github.com/perltidy/perltidy/blob/master/docs/eos_flag.md>.
107
108           This change in default behavior was made over a period of time as
109           follows:
110
111           •   For versions before 20220217 the -eos flag was not available
112               and the behavior was equivalent to -neos.
113
114           •   In version 20220217 the -eos flag was added but the default
115               remained -neos.
116
117           •   For versions after 20220217 the default was set to -eos.
118
119       stderr
120           The stderr parameter allows the calling program to redirect the
121           stream that would otherwise go to the standard error output device
122           to any of the stream types listed above.  This stream contains
123           important warnings and errors related to the parameters passed to
124           perltidy.
125
126       perltidyrc
127           If the perltidyrc file is given, it will be used instead of any
128           .perltidyrc configuration file that would otherwise be used.
129
130       errorfile
131           The errorfile parameter allows the calling program to capture the
132           stream that would otherwise go to either a .ERR file.  This stream
133           contains warnings or errors related to the contents of one source
134           file or stream.
135
136           The reason that this is different from the stderr stream is that
137           when perltidy is called to process multiple files there will be up
138           to one .ERR file created for each file and it would be very
139           confusing if they were combined.
140
141           However if perltidy is called to process just a single perl script
142           then it may be more convenient to combine the errorfile stream with
143           the stderr stream.  This can be done by setting the -se parameter,
144           in which case this parameter is ignored.
145
146       logfile
147           The logfile parameter allows the calling program to capture the log
148           stream.  This stream is only created if requested with a -g
149           parameter.  It contains detailed diagnostic information about a
150           script which may be useful for debugging.
151
152       teefile
153           The teefile parameter allows the calling program to capture the tee
154           stream.  This stream is only created if requested with one of the
155           'tee' parameters, a --tee-pod , --tee-block-comments,
156           --tee-side-commnts, or --tee-all-comments.
157
158       debugfile
159           The debugfile parameter allows the calling program to capture the
160           stream produced by the --DEBUG parameter.  This parameter is mainly
161           used for debugging perltidy itself.
162
163       argv
164           If the argv parameter is given, it will be used instead of the
165           @ARGV array.  The argv parameter may be a string, a reference to a
166           string, or a reference to an array.  If it is a string or reference
167           to a string, it will be parsed into an array of items just as if it
168           were a command line string.
169
170       dump_options
171           If the dump_options parameter is given, it must be the reference to
172           a hash.  In this case, the parameters contained in any perltidyrc
173           configuration file will be placed in this hash and perltidy will
174           return immediately.  This is equivalent to running perltidy with
175           --dump-options, except that the parameters are returned in a hash
176           rather than dumped to standard output.  Also, by default only the
177           parameters in the perltidyrc file are returned, but this can be
178           changed (see the next parameter).  This parameter provides a
179           convenient method for external programs to read a perltidyrc file.
180           An example program using this feature, perltidyrc_dump.pl, is
181           included in the distribution.
182
183           Any combination of the dump_ parameters may be used together.
184
185       dump_options_type
186           This parameter is a string which can be used to control the
187           parameters placed in the hash reference supplied by dump_options.
188           The possible values are 'perltidyrc' (default) and 'full'.  The
189           'full' parameter causes both the default options plus any options
190           found in a perltidyrc file to be returned.
191
192       dump_getopt_flags
193           If the dump_getopt_flags parameter is given, it must be the
194           reference to a hash.  This hash will receive all of the parameters
195           that perltidy understands and flags that are passed to
196           Getopt::Long.  This parameter may be used alone or with the
197           dump_options flag.  Perltidy will exit immediately after filling
198           this hash.  See the demo program perltidyrc_dump.pl for example
199           usage.
200
201       dump_options_category
202           If the dump_options_category parameter is given, it must be the
203           reference to a hash.  This hash will receive a hash with keys equal
204           to all long parameter names and values equal to the title of the
205           corresponding section of the perltidy manual.  See the demo program
206           perltidyrc_dump.pl for example usage.
207
208       dump_abbreviations
209           If the dump_abbreviations parameter is given, it must be the
210           reference to a hash.  This hash will receive all abbreviations used
211           by Perl::Tidy.  See the demo program perltidyrc_dump.pl for example
212           usage.
213
214       prefilter
215           A code reference that will be applied to the source before tidying.
216           It is expected to take the full content as a string in its input,
217           and output the transformed content.
218
219       postfilter
220           A code reference that will be applied to the tidied result before
221           outputting.  It is expected to take the full content as a string in
222           its input, and output the transformed content.
223
224           Note: A convenient way to check the function of your custom
225           prefilter and postfilter code is to use the --notidy option, first
226           with just the prefilter and then with both the prefilter and
227           postfilter.  See also the file filter_example.pl in the perltidy
228           distribution.
229

ERROR HANDLING

231       An exit value of 0, 1, or 2 is returned by perltidy to indicate the
232       status of the result.
233
234       A exit value of 0 indicates that perltidy ran to completion with no
235       error messages.
236
237       An exit value of 1 indicates that the process had to be terminated
238       early due to errors in the input parameters.  This can happen for
239       example if a parameter is misspelled or given an invalid value.  The
240       calling program should check for this flag because if it is set the
241       destination stream will be empty or incomplete and should be ignored.
242       Error messages in the stderr stream will indicate the cause of any
243       problem.
244
245       An exit value of 2 indicates that perltidy ran to completion but there
246       there are warning messages in the stderr stream related to parameter
247       errors or conflicts and/or warning messages in the errorfile stream
248       relating to possible syntax errors in the source code being tidied.
249
250       In the event of a catastrophic error for which recovery is not possible
251       perltidy terminates by making calls to croak or confess to help the
252       programmer localize the problem.  These should normally only occur
253       during program development.
254

NOTES ON FORMATTING PARAMETERS

256       Parameters which control formatting may be passed in several ways: in a
257       .perltidyrc configuration file, in the perltidyrc parameter, and in the
258       argv parameter.
259
260       The -syn (--check-syntax) flag may be used with all source and
261       destination streams except for standard input and output.  However data
262       streams which are not associated with a filename will be copied to a
263       temporary file before being passed to Perl.  This use of temporary
264       files can cause somewhat confusing output from Perl.
265
266       If the -pbp style is used it will typically be necessary to also
267       specify a -nst flag.  This is necessary to turn off the -st flag
268       contained in the -pbp parameter set which otherwise would direct the
269       output stream to the standard output.
270

EXAMPLES

272       The following example uses string references to hold the input and
273       output code and error streams, and illustrates checking for errors.
274
275         use Perl::Tidy;
276
277         my $source_string = <<'EOT';
278         my$error=Perl::Tidy::perltidy(argv=>$argv,source=>\$source_string,
279           destination=>\$dest_string,stderr=>\$stderr_string,
280         errorfile=>\$errorfile_string,);
281         EOT
282
283         my $dest_string;
284         my $stderr_string;
285         my $errorfile_string;
286         my $argv = "-npro";   # Ignore any .perltidyrc at this site
287         $argv .= " -pbp";     # Format according to perl best practices
288         $argv .= " -nst";     # Must turn off -st in case -pbp is specified
289         $argv .= " -se";      # -se appends the errorfile to stderr
290         ## $argv .= " --spell-check";  # uncomment to trigger an error
291
292         print "<<RAW SOURCE>>\n$source_string\n";
293
294         my $error = Perl::Tidy::perltidy(
295             argv        => $argv,
296             source      => \$source_string,
297             destination => \$dest_string,
298             stderr      => \$stderr_string,
299             errorfile   => \$errorfile_string,    # ignored when -se flag is set
300             ##phasers   => 'stun',                # uncomment to trigger an error
301         );
302
303         if ($error) {
304
305             # serious error in input parameters, no tidied output
306             print "<<STDERR>>\n$stderr_string\n";
307             die "Exiting because of serious errors\n";
308         }
309
310         if ($dest_string)      { print "<<TIDIED SOURCE>>\n$dest_string\n" }
311         if ($stderr_string)    { print "<<STDERR>>\n$stderr_string\n" }
312         if ($errorfile_string) { print "<<.ERR file>>\n$errorfile_string\n" }
313
314       Additional examples are given in examples section of the perltidy
315       distribution.
316

Using the formatter Callback Object

318       The formatter parameter is an optional callback object which allows the
319       calling program to receive tokenized lines directly from perltidy for
320       further specialized processing.  When this parameter is used, the two
321       formatting options which are built into perltidy (beautification or
322       html) are ignored.  The following diagram illustrates the logical flow:
323
324                           |-- (normal route)   -> code beautification
325         caller->perltidy->|-- (-html flag )    -> create html
326                           |-- (formatter given)-> callback to write_line
327
328       This can be useful for processing perl scripts in some way.  The
329       parameter $formatter in the perltidy call,
330
331               formatter   => $formatter,
332
333       is an object created by the caller with a "write_line" method which
334       will accept and process tokenized lines, one line per call.  Here is a
335       simple example of a "write_line" which merely prints the line number,
336       the line type (as determined by perltidy), and the text of the line:
337
338        sub write_line {
339
340            # This is called from perltidy line-by-line
341            my $self              = shift;
342            my $line_of_tokens    = shift;
343            my $line_type         = $line_of_tokens->{_line_type};
344            my $input_line_number = $line_of_tokens->{_line_number};
345            my $input_line        = $line_of_tokens->{_line_text};
346            print "$input_line_number:$line_type:$input_line";
347        }
348
349       The complete program, perllinetype, is contained in the examples
350       section of the source distribution.  As this example shows, the
351       callback method receives a parameter $line_of_tokens, which is a
352       reference to a hash of other useful information.  This example uses
353       these hash entries:
354
355        $line_of_tokens->{_line_number} - the line number (1,2,...)
356        $line_of_tokens->{_line_text}   - the text of the line
357        $line_of_tokens->{_line_type}   - the type of the line, one of:
358
359           SYSTEM         - system-specific code before hash-bang line
360           CODE           - line of perl code (including comments)
361           POD_START      - line starting pod, such as '=head'
362           POD            - pod documentation text
363           POD_END        - last line of pod section, '=cut'
364           HERE           - text of here-document
365           HERE_END       - last line of here-doc (target word)
366           FORMAT         - format section
367           FORMAT_END     - last line of format section, '.'
368           DATA_START     - __DATA__ line
369           DATA           - unidentified text following __DATA__
370           END_START      - __END__ line
371           END            - unidentified text following __END__
372           ERROR          - we are in big trouble, probably not a perl script
373
374       Most applications will be only interested in lines of type CODE.  For
375       another example, let's write a program which checks for one of the so-
376       called naughty matching variables "&`", $&, and "$'", which can slow
377       down processing.  Here is a write_line, from the example program
378       find_naughty.pl, which does that:
379
380        sub write_line {
381
382            # This is called back from perltidy line-by-line
383            # We're looking for $`, $&, and $'
384            my ( $self, $line_of_tokens ) = @_;
385
386            # pull out some stuff we might need
387            my $line_type         = $line_of_tokens->{_line_type};
388            my $input_line_number = $line_of_tokens->{_line_number};
389            my $input_line        = $line_of_tokens->{_line_text};
390            my $rtoken_type       = $line_of_tokens->{_rtoken_type};
391            my $rtokens           = $line_of_tokens->{_rtokens};
392            chomp $input_line;
393
394            # skip comments, pod, etc
395            return if ( $line_type ne 'CODE' );
396
397            # loop over tokens looking for $`, $&, and $'
398            for ( my $j = 0 ; $j < @$rtoken_type ; $j++ ) {
399
400                # we only want to examine token types 'i' (identifier)
401                next unless $$rtoken_type[$j] eq 'i';
402
403                # pull out the actual token text
404                my $token = $$rtokens[$j];
405
406                # and check it
407                if ( $token =~ /^\$[\`\&\']$/ ) {
408                    print STDERR
409                      "$input_line_number: $token\n";
410                }
411            }
412        }
413
414       This example pulls out these tokenization variables from the
415       $line_of_tokens hash reference:
416
417            $rtoken_type = $line_of_tokens->{_rtoken_type};
418            $rtokens     = $line_of_tokens->{_rtokens};
419
420       The variable $rtoken_type is a reference to an array of token type
421       codes, and $rtokens is a reference to a corresponding array of token
422       text.  These are obviously only defined for lines of type CODE.
423       Perltidy classifies tokens into types, and has a brief code for each
424       type.  You can get a complete list at any time by running perltidy from
425       the command line with
426
427            perltidy --dump-token-types
428
429       In the present example, we are only looking for tokens of type i
430       (identifiers), so the for loop skips past all other types.  When an
431       identifier is found, its actual text is checked to see if it is one
432       being sought.  If so, the above write_line prints the token and its
433       line number.
434
435       The examples section of the source distribution has some examples of
436       programs which use the formatter option.
437
438       For help with perltidy's peculiar way of breaking lines into tokens,
439       you might run, from the command line,
440
441        perltidy -D filename
442
443       where filename is a short script of interest.  This will produce
444       filename.DEBUG with interleaved lines of text and their token types.
445       The -D flag has been in perltidy from the beginning for this purpose.
446       If you want to see the code which creates this file, it is "sub
447       Perl::Tidy::Debugger::write_debug_entry"
448

EXPORT

450         &perltidy
451

INSTALLATION

453       The module 'Perl::Tidy' comes with a binary 'perltidy' which is
454       installed when the module is installed.  The module name is case-
455       sensitive.  For example, the basic command for installing with cpanm is
456       'cpanm Perl::Tidy'.
457

VERSION

459       This man page documents Perl::Tidy version 20220613
460

LICENSE

462       This package is free software; you can redistribute it and/or modify it
463       under the terms of the "GNU General Public License".
464
465       Please refer to the file "COPYING" for details.
466

BUG REPORTS

468       The source code repository is at
469       <https://github.com/perltidy/perltidy>.
470
471       To report a new bug or problem, use the "issues" link on this page.
472

SEE ALSO

474       The perltidy(1) man page describes all of the features of perltidy.  It
475       can be found at http://perltidy.sourceforge.net.
476
477
478
479perl v5.36.0                      2022-07-22                     Perl::Tidy(3)
Impressum