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               formatter         => $formatter,           # callback object (see below)
20               dump_options      => $dump_options,
21               dump_options_type => $dump_options_type,
22               prefilter         => $prefilter_coderef,
23               postfilter        => $postfilter_coderef,
24           );
25

DESCRIPTION

27       This module makes the functionality of the perltidy utility available
28       to perl scripts.  Any or all of the input parameters may be omitted, in
29       which case the @ARGV array will be used to provide input parameters as
30       described in the perltidy(1) man page.
31
32       For example, the perltidy script is basically just this:
33
34           use Perl::Tidy;
35           Perl::Tidy::perltidy();
36
37       The call to perltidy returns a scalar $error_flag which is TRUE if an
38       error caused premature termination, and FALSE if the process ran to
39       normal completion.  Additional discuss of errors is contained below in
40       the ERROR HANDLING section.
41
42       The module accepts input and output streams by a variety of methods.
43       The following list of parameters may be any of the following: a
44       filename, an ARRAY reference, a SCALAR reference, or an object with
45       either a getline or print method, as appropriate.
46
47               source            - the source of the script to be formatted
48               destination       - the destination of the formatted output
49               stderr            - standard error output
50               perltidyrc        - the .perltidyrc file
51               logfile           - the .LOG file stream, if any
52               errorfile         - the .ERR file stream, if any
53               dump_options      - ref to a hash to receive parameters (see below),
54               dump_options_type - controls contents of dump_options
55               dump_getopt_flags - ref to a hash to receive Getopt flags
56               dump_options_category - ref to a hash giving category of options
57               dump_abbreviations    - ref to a hash giving all abbreviations
58
59       The following chart illustrates the logic used to decide how to treat a
60       parameter.
61
62          ref($param)  $param is assumed to be:
63          -----------  ---------------------
64          undef        a filename
65          SCALAR       ref to string
66          ARRAY        ref to array
67          (other)      object with getline (if source) or print method
68
69       If the parameter is an object, and the object has a close method, that
70       close method will be called at the end of the stream.
71
72       source
73           If the source parameter is given, it defines the source of the
74           input stream.  If an input stream is defined with the source
75           parameter then no other source filenames may be specified in the
76           @ARGV array or argv parameter.
77
78       destination
79           If the destination parameter is given, it will be used to define
80           the file or memory location to receive output of perltidy.
81
82       stderr
83           The stderr parameter allows the calling program to redirect the
84           stream that would otherwise go to the standard error output device
85           to any of the stream types listed above.  This stream contains
86           important warnings and errors related to the parameters passed to
87           perltidy.
88
89       perltidyrc
90           If the perltidyrc file is given, it will be used instead of any
91           .perltidyrc configuration file that would otherwise be used.
92
93       errorfile
94           The errorfile parameter allows the calling program to capture the
95           stream that would otherwise go to either a .ERR file.  This stream
96           contains warnings or errors related to the contents of one source
97           file or stream.
98
99           The reason that this is different from the stderr stream is that
100           when perltidy is called to process multiple files there will be up
101           to one .ERR file created for each file and it would be very
102           confusing if they were combined.
103
104           However if perltidy is called to process just a single perl script
105           then it may be more conveninent to combine the errorfile stream
106           with the stderr stream.  This can be done by setting the -se
107           parameter, in which case this parameter is ignored.
108
109       logfile
110           The logfile parameter allows the calling program to capture the
111           stream that would otherwise go to a .LOG file.  This stream is only
112           created if requested with a -g parameter.  It contains detailed
113           diagnostic information about a script which may be useful for
114           debugging.
115
116       argv
117           If the argv parameter is given, it will be used instead of the
118           @ARGV array.  The argv parameter may be a string, a reference to a
119           string, or a reference to an array.  If it is a string or reference
120           to a string, it will be parsed into an array of items just as if it
121           were a command line string.
122
123       dump_options
124           If the dump_options parameter is given, it must be the reference to
125           a hash.  In this case, the parameters contained in any perltidyrc
126           configuration file will be placed in this hash and perltidy will
127           return immediately.  This is equivalent to running perltidy with
128           --dump-options, except that the perameters are returned in a hash
129           rather than dumped to standard output.  Also, by default only the
130           parameters in the perltidyrc file are returned, but this can be
131           changed (see the next parameter).  This parameter provides a
132           convenient method for external programs to read a perltidyrc file.
133           An example program using this feature, perltidyrc_dump.pl, is
134           included in the distribution.
135
136           Any combination of the dump_ parameters may be used together.
137
138       dump_options_type
139           This parameter is a string which can be used to control the
140           parameters placed in the hash reference supplied by dump_options.
141           The possible values are 'perltidyrc' (default) and 'full'.  The
142           'full' parameter causes both the default options plus any options
143           found in a perltidyrc file to be returned.
144
145       dump_getopt_flags
146           If the dump_getopt_flags parameter is given, it must be the
147           reference to a hash.  This hash will receive all of the parameters
148           that perltidy understands and flags that are passed to
149           Getopt::Long.  This parameter may be used alone or with the
150           dump_options flag.  Perltidy will exit immediately after filling
151           this hash.  See the demo program perltidyrc_dump.pl for example
152           usage.
153
154       dump_options_category
155           If the dump_options_category parameter is given, it must be the
156           reference to a hash.  This hash will receive a hash with keys equal
157           to all long parameter names and values equal to the title of the
158           corresponding section of the perltidy manual.  See the demo program
159           perltidyrc_dump.pl for example usage.
160
161       dump_abbreviations
162           If the dump_abbreviations parameter is given, it must be the
163           reference to a hash.  This hash will receive all abbreviations used
164           by Perl::Tidy.  See the demo program perltidyrc_dump.pl for example
165           usage.
166
167       prefilter
168           A code reference that will be applied to the source before tidying.
169           It is expected to take the full content as a string in its input,
170           and output the transformed content.
171
172       postfilter
173           A code reference that will be applied to the tidied result before
174           outputting.  It is expected to take the full content as a string in
175           its input, and output the transformed content.
176
177           Note: A convenient way to check the function of your custom
178           prefilter and postfilter code is to use the --notidy option, first
179           with just the prefilter and then with both the prefilter and
180           postfilter.  See also the file filter_example.pl in the perltidy
181           distribution.
182

ERROR HANDLING

184       Perltidy will return with an error flag indicating if the process had
185       to be terminated early due to errors in the input parameters.  This can
186       happen for example if a parameter is misspelled or given an invalid
187       value.  The calling program should check this flag because if it is set
188       the destination stream will be empty or incomplete and should be
189       ignored.  Error messages in the stderr stream will indicate the cause
190       of any problem.
191
192       If the error flag is not set then perltidy ran to completion.   However
193       there may still be warning messages in the stderr stream related to
194       control parameters, and there may be warning messages in the errorfile
195       stream relating to possible syntax errors in the source code being
196       tidied.
197
198       In the event of a catastrophic error for which recovery is not possible
199       perltidy terminates by making calls to croak or confess to help the
200       programmer localize the problem.  These should normally only occur
201       during program development.
202

NOTES ON FORMATTING PARAMETERS

204       Parameters which control formatting may be passed in several ways: in a
205       .perltidyrc configuration file, in the perltidyrc parameter, and in the
206       argv parameter.
207
208       The -syn (--check-syntax) flag may be used with all source and
209       destination streams except for standard input and output.  However data
210       streams which are not associated with a filename will be copied to a
211       temporary file before being be passed to Perl.  This use of temporary
212       files can cause somewhat confusing output from Perl.
213
214       If the -pbp style is used it will typically be necessary to also
215       specify a -nst flag.  This is necessary to turn off the -st flag
216       contained in the -pbp parameter set which otherwise would direct the
217       output stream to the standard output.
218

EXAMPLES

220       The following example uses string references to hold the input and
221       output code and error streams, and illustrates checking for errors.
222
223         use Perl::Tidy;
224
225         my $source_string = <<'EOT';
226         my$error=Perl::Tidy::perltidy(argv=>$argv,source=>\$source_string,
227           destination=>\$dest_string,stderr=>\$stderr_string,
228         errorfile=>\$errorfile_string,);
229         EOT
230
231         my $dest_string;
232         my $stderr_string;
233         my $errorfile_string;
234         my $argv = "-npro";   # Ignore any .perltidyrc at this site
235         $argv .= " -pbp";     # Format according to perl best practices
236         $argv .= " -nst";     # Must turn off -st in case -pbp is specified
237         $argv .= " -se";      # -se appends the errorfile to stderr
238         ## $argv .= " --spell-check";  # uncomment to trigger an error
239
240         print "<<RAW SOURCE>>\n$source_string\n";
241
242         my $error = Perl::Tidy::perltidy(
243             argv        => $argv,
244             source      => \$source_string,
245             destination => \$dest_string,
246             stderr      => \$stderr_string,
247             errorfile   => \$errorfile_string,    # ignored when -se flag is set
248             ##phasers   => 'stun',                # uncomment to trigger an error
249         );
250
251         if ($error) {
252
253             # serious error in input parameters, no tidied output
254             print "<<STDERR>>\n$stderr_string\n";
255             die "Exiting because of serious errors\n";
256         }
257
258         if ($dest_string)      { print "<<TIDIED SOURCE>>\n$dest_string\n" }
259         if ($stderr_string)    { print "<<STDERR>>\n$stderr_string\n" }
260         if ($errorfile_string) { print "<<.ERR file>>\n$errorfile_string\n" }
261
262       Additional examples are given in examples section of the perltidy
263       distribution.
264

Using the formatter Callback Object

266       The formatter parameter is an optional callback object which allows the
267       calling program to receive tokenized lines directly from perltidy for
268       further specialized processing.  When this parameter is used, the two
269       formatting options which are built into perltidy (beautification or
270       html) are ignored.  The following diagram illustrates the logical flow:
271
272                           |-- (normal route)   -> code beautification
273         caller->perltidy->|-- (-html flag )    -> create html
274                           |-- (formatter given)-> callback to write_line
275
276       This can be useful for processing perl scripts in some way.  The
277       parameter $formatter in the perltidy call,
278
279               formatter   => $formatter,
280
281       is an object created by the caller with a "write_line" method which
282       will accept and process tokenized lines, one line per call.  Here is a
283       simple example of a "write_line" which merely prints the line number,
284       the line type (as determined by perltidy), and the text of the line:
285
286        sub write_line {
287
288            # This is called from perltidy line-by-line
289            my $self              = shift;
290            my $line_of_tokens    = shift;
291            my $line_type         = $line_of_tokens->{_line_type};
292            my $input_line_number = $line_of_tokens->{_line_number};
293            my $input_line        = $line_of_tokens->{_line_text};
294            print "$input_line_number:$line_type:$input_line";
295        }
296
297       The complete program, perllinetype, is contained in the examples
298       section of the source distribution.  As this example shows, the
299       callback method receives a parameter $line_of_tokens, which is a
300       reference to a hash of other useful information.  This example uses
301       these hash entries:
302
303        $line_of_tokens->{_line_number} - the line number (1,2,...)
304        $line_of_tokens->{_line_text}   - the text of the line
305        $line_of_tokens->{_line_type}   - the type of the line, one of:
306
307           SYSTEM         - system-specific code before hash-bang line
308           CODE           - line of perl code (including comments)
309           POD_START      - line starting pod, such as '=head'
310           POD            - pod documentation text
311           POD_END        - last line of pod section, '=cut'
312           HERE           - text of here-document
313           HERE_END       - last line of here-doc (target word)
314           FORMAT         - format section
315           FORMAT_END     - last line of format section, '.'
316           DATA_START     - __DATA__ line
317           DATA           - unidentified text following __DATA__
318           END_START      - __END__ line
319           END            - unidentified text following __END__
320           ERROR          - we are in big trouble, probably not a perl script
321
322       Most applications will be only interested in lines of type CODE.  For
323       another example, let's write a program which checks for one of the so-
324       called naughty matching variables "&`", $&, and "$'", which can slow
325       down processing.  Here is a write_line, from the example program
326       find_naughty.pl, which does that:
327
328        sub write_line {
329
330            # This is called back from perltidy line-by-line
331            # We're looking for $`, $&, and $'
332            my ( $self, $line_of_tokens ) = @_;
333
334            # pull out some stuff we might need
335            my $line_type         = $line_of_tokens->{_line_type};
336            my $input_line_number = $line_of_tokens->{_line_number};
337            my $input_line        = $line_of_tokens->{_line_text};
338            my $rtoken_type       = $line_of_tokens->{_rtoken_type};
339            my $rtokens           = $line_of_tokens->{_rtokens};
340            chomp $input_line;
341
342            # skip comments, pod, etc
343            return if ( $line_type ne 'CODE' );
344
345            # loop over tokens looking for $`, $&, and $'
346            for ( my $j = 0 ; $j < @$rtoken_type ; $j++ ) {
347
348                # we only want to examine token types 'i' (identifier)
349                next unless $$rtoken_type[$j] eq 'i';
350
351                # pull out the actual token text
352                my $token = $$rtokens[$j];
353
354                # and check it
355                if ( $token =~ /^\$[\`\&\']$/ ) {
356                    print STDERR
357                      "$input_line_number: $token\n";
358                }
359            }
360        }
361
362       This example pulls out these tokenization variables from the
363       $line_of_tokens hash reference:
364
365            $rtoken_type = $line_of_tokens->{_rtoken_type};
366            $rtokens     = $line_of_tokens->{_rtokens};
367
368       The variable $rtoken_type is a reference to an array of token type
369       codes, and $rtokens is a reference to a corresponding array of token
370       text.  These are obviously only defined for lines of type CODE.
371       Perltidy classifies tokens into types, and has a brief code for each
372       type.  You can get a complete list at any time by running perltidy from
373       the command line with
374
375            perltidy --dump-token-types
376
377       In the present example, we are only looking for tokens of type i
378       (identifiers), so the for loop skips past all other types.  When an
379       identifier is found, its actual text is checked to see if it is one
380       being sought.  If so, the above write_line prints the token and its
381       line number.
382
383       The formatter feature is relatively new in perltidy, and further
384       documentation needs to be written to complete its description.
385       However, several example programs have been written and can be found in
386       the examples section of the source distribution.  Probably the best way
387       to get started is to find one of the examples which most closely
388       matches your application and start modifying it.
389
390       For help with perltidy's pecular way of breaking lines into tokens, you
391       might run, from the command line,
392
393        perltidy -D filename
394
395       where filename is a short script of interest.  This will produce
396       filename.DEBUG with interleaved lines of text and their token types.
397       The -D flag has been in perltidy from the beginning for this purpose.
398       If you want to see the code which creates this file, it is
399       "write_debug_entry" in Tidy.pm.
400

EXPORT

402         &perltidy
403

CREDITS

405       Thanks to Hugh Myers who developed the initial modular interface to
406       perltidy.
407

VERSION

409       This man page documents Perl::Tidy version 20121207.
410

LICENSE

412       This package is free software; you can redistribute it and/or modify it
413       under the terms of the "GNU General Public License".
414
415       Please refer to the file "COPYING" for details.
416

AUTHOR

418        Steve Hancock
419        perltidy at users.sourceforge.net
420

SEE ALSO

422       The perltidy(1) man page describes all of the features of perltidy.  It
423       can be found at http://perltidy.sourceforge.net.
424
425
426
427perl v5.16.3                      2014-06-10                     Perl::Tidy(3)
Impressum