]> git.donarmstrong.com Git - perltidy.git/blob - docs/Tidy.html
New upstream version 20210717
[perltidy.git] / docs / Tidy.html
1 <?xml version="1.0" ?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3 <html xmlns="http://www.w3.org/1999/xhtml">
4 <head>
5 <title></title>
6 <meta http-equiv="content-type" content="text/html; charset=utf-8" />
7 <link rev="made" href="mailto:root@localhost" />
8 </head>
9
10 <body>
11
12
13
14 <ul id="index">
15   <li><a href="#NAME">NAME</a></li>
16   <li><a href="#SYNOPSIS">SYNOPSIS</a></li>
17   <li><a href="#DESCRIPTION">DESCRIPTION</a></li>
18   <li><a href="#ERROR-HANDLING">ERROR HANDLING</a></li>
19   <li><a href="#NOTES-ON-FORMATTING-PARAMETERS">NOTES ON FORMATTING PARAMETERS</a></li>
20   <li><a href="#EXAMPLES">EXAMPLES</a></li>
21   <li><a href="#Using-the-formatter-Callback-Object">Using the formatter Callback Object</a></li>
22   <li><a href="#EXPORT">EXPORT</a></li>
23   <li><a href="#INSTALLATION">INSTALLATION</a></li>
24   <li><a href="#VERSION">VERSION</a></li>
25   <li><a href="#LICENSE">LICENSE</a></li>
26   <li><a href="#BUG-REPORTS">BUG REPORTS</a></li>
27   <li><a href="#SEE-ALSO">SEE ALSO</a></li>
28 </ul>
29
30 <h1 id="NAME">NAME</h1>
31
32 <p>Perl::Tidy - Parses and beautifies perl source</p>
33
34 <h1 id="SYNOPSIS">SYNOPSIS</h1>
35
36 <pre><code>    use Perl::Tidy;
37
38     my $error_flag = Perl::Tidy::perltidy(
39         source            =&gt; $source,
40         destination       =&gt; $destination,
41         stderr            =&gt; $stderr,
42         argv              =&gt; $argv,
43         perltidyrc        =&gt; $perltidyrc,
44         logfile           =&gt; $logfile,
45         errorfile         =&gt; $errorfile,
46         teefile           =&gt; $teefile,
47         debugfile         =&gt; $debugfile,
48         formatter         =&gt; $formatter,           # callback object (see below)
49         dump_options      =&gt; $dump_options,
50         dump_options_type =&gt; $dump_options_type,
51         prefilter         =&gt; $prefilter_coderef,
52         postfilter        =&gt; $postfilter_coderef,
53     );</code></pre>
54
55 <h1 id="DESCRIPTION">DESCRIPTION</h1>
56
57 <p>This module makes the functionality of the perltidy utility available to perl scripts. Any or all of the input parameters may be omitted, in which case the @ARGV array will be used to provide input parameters as described in the perltidy(1) man page.</p>
58
59 <p>For example, the perltidy script is basically just this:</p>
60
61 <pre><code>    use Perl::Tidy;
62     Perl::Tidy::perltidy();</code></pre>
63
64 <p>The call to <b>perltidy</b> returns a scalar <b>$error_flag</b> which is TRUE if an error caused premature termination, and FALSE if the process ran to normal completion. Additional discuss of errors is contained below in the <a href="#ERROR-HANDLING">&quot;ERROR HANDLING&quot;</a> section.</p>
65
66 <p>The module accepts input and output streams by a variety of methods. The following list of parameters may be any of the following: a filename, an ARRAY reference, a SCALAR reference, or an object with either a <b>getline</b> or <b>print</b> method, as appropriate.</p>
67
68 <pre><code>        source            - the source of the script to be formatted
69         destination       - the destination of the formatted output
70         stderr            - standard error output
71         perltidyrc        - the .perltidyrc file
72         logfile           - the .LOG file stream, if any 
73         errorfile         - the .ERR file stream, if any
74         dump_options      - ref to a hash to receive parameters (see below), 
75         dump_options_type - controls contents of dump_options
76         dump_getopt_flags - ref to a hash to receive Getopt flags
77         dump_options_category - ref to a hash giving category of options
78         dump_abbreviations    - ref to a hash giving all abbreviations</code></pre>
79
80 <p>The following chart illustrates the logic used to decide how to treat a parameter.</p>
81
82 <pre><code>   ref($param)  $param is assumed to be:
83    -----------  ---------------------
84    undef        a filename
85    SCALAR       ref to string
86    ARRAY        ref to array
87    (other)      object with getline (if source) or print method</code></pre>
88
89 <p>If the parameter is an object, and the object has a <b>close</b> method, that close method will be called at the end of the stream.</p>
90
91 <dl>
92
93 <dt id="source">source</dt>
94 <dd>
95
96 <p>If the <b>source</b> parameter is given, it defines the source of the input stream. If an input stream is defined with the <b>source</b> parameter then no other source filenames may be specified in the @ARGV array or <b>argv</b> parameter.</p>
97
98 </dd>
99 <dt id="destination">destination</dt>
100 <dd>
101
102 <p>If the <b>destination</b> parameter is given, it will be used to define the file or memory location to receive output of perltidy.</p>
103
104 </dd>
105 <dt id="stderr">stderr</dt>
106 <dd>
107
108 <p>The <b>stderr</b> parameter allows the calling program to redirect the stream that would otherwise go to the standard error output device to any of the stream types listed above. This stream contains important warnings and errors related to the parameters passed to perltidy.</p>
109
110 </dd>
111 <dt id="perltidyrc">perltidyrc</dt>
112 <dd>
113
114 <p>If the <b>perltidyrc</b> file is given, it will be used instead of any <i>.perltidyrc</i> configuration file that would otherwise be used.</p>
115
116 </dd>
117 <dt id="errorfile">errorfile</dt>
118 <dd>
119
120 <p>The <b>errorfile</b> parameter allows the calling program to capture the stream that would otherwise go to either a .ERR file. This stream contains warnings or errors related to the contents of one source file or stream.</p>
121
122 <p>The reason that this is different from the stderr stream is that when perltidy is called to process multiple files there will be up to one .ERR file created for each file and it would be very confusing if they were combined.</p>
123
124 <p>However if perltidy is called to process just a single perl script then it may be more convenient to combine the <b>errorfile</b> stream with the <b>stderr</b> stream. This can be done by setting the <b>-se</b> parameter, in which case this parameter is ignored.</p>
125
126 </dd>
127 <dt id="logfile">logfile</dt>
128 <dd>
129
130 <p>The <b>logfile</b> parameter allows the calling program to capture the log stream. This stream is only created if requested with a <b>-g</b> parameter. It contains detailed diagnostic information about a script which may be useful for debugging.</p>
131
132 </dd>
133 <dt id="teefile">teefile</dt>
134 <dd>
135
136 <p>The <b>teefile</b> parameter allows the calling program to capture the tee stream. This stream is only created if requested with one of the &#39;tee&#39; parameters, a <b>--tee-pod</b> , <b>--tee-block-comments</b>, <b>--tee-side-commnts</b>, or <b>--tee-all-comments</b>.</p>
137
138 </dd>
139 <dt id="debugfile">debugfile</dt>
140 <dd>
141
142 <p>The <b>debugfile</b> parameter allows the calling program to capture the stream produced by the <b>--DEBUG</b> parameter. This parameter is mainly used for debugging perltidy itself.</p>
143
144 </dd>
145 <dt id="argv">argv</dt>
146 <dd>
147
148 <p>If the <b>argv</b> parameter is given, it will be used instead of the <b>@ARGV</b> array. The <b>argv</b> parameter may be a string, a reference to a string, or a reference to an array. If it is a string or reference to a string, it will be parsed into an array of items just as if it were a command line string.</p>
149
150 </dd>
151 <dt id="dump_options">dump_options</dt>
152 <dd>
153
154 <p>If the <b>dump_options</b> parameter is given, it must be the reference to a hash. In this case, the parameters contained in any perltidyrc configuration file will be placed in this hash and perltidy will return immediately. This is equivalent to running perltidy with --dump-options, except that the parameters are returned in a hash rather than dumped to standard output. Also, by default only the parameters in the perltidyrc file are returned, but this can be changed (see the next parameter). This parameter provides a convenient method for external programs to read a perltidyrc file. An example program using this feature, <i>perltidyrc_dump.pl</i>, is included in the distribution.</p>
155
156 <p>Any combination of the <b>dump_</b> parameters may be used together.</p>
157
158 </dd>
159 <dt id="dump_options_type">dump_options_type</dt>
160 <dd>
161
162 <p>This parameter is a string which can be used to control the parameters placed in the hash reference supplied by <b>dump_options</b>. The possible values are &#39;perltidyrc&#39; (default) and &#39;full&#39;. The &#39;full&#39; parameter causes both the default options plus any options found in a perltidyrc file to be returned.</p>
163
164 </dd>
165 <dt id="dump_getopt_flags">dump_getopt_flags</dt>
166 <dd>
167
168 <p>If the <b>dump_getopt_flags</b> parameter is given, it must be the reference to a hash. This hash will receive all of the parameters that perltidy understands and flags that are passed to Getopt::Long. This parameter may be used alone or with the <b>dump_options</b> flag. Perltidy will exit immediately after filling this hash. See the demo program <i>perltidyrc_dump.pl</i> for example usage.</p>
169
170 </dd>
171 <dt id="dump_options_category">dump_options_category</dt>
172 <dd>
173
174 <p>If the <b>dump_options_category</b> parameter is given, it must be the reference to a hash. This hash will receive a hash with keys equal to all long parameter names and values equal to the title of the corresponding section of the perltidy manual. See the demo program <i>perltidyrc_dump.pl</i> for example usage.</p>
175
176 </dd>
177 <dt id="dump_abbreviations">dump_abbreviations</dt>
178 <dd>
179
180 <p>If the <b>dump_abbreviations</b> parameter is given, it must be the reference to a hash. This hash will receive all abbreviations used by Perl::Tidy. See the demo program <i>perltidyrc_dump.pl</i> for example usage.</p>
181
182 </dd>
183 <dt id="prefilter">prefilter</dt>
184 <dd>
185
186 <p>A code reference that will be applied to the source before tidying. It is expected to take the full content as a string in its input, and output the transformed content.</p>
187
188 </dd>
189 <dt id="postfilter">postfilter</dt>
190 <dd>
191
192 <p>A code reference that will be applied to the tidied result before outputting. It is expected to take the full content as a string in its input, and output the transformed content.</p>
193
194 <p>Note: A convenient way to check the function of your custom prefilter and postfilter code is to use the --notidy option, first with just the prefilter and then with both the prefilter and postfilter. See also the file <b>filter_example.pl</b> in the perltidy distribution.</p>
195
196 </dd>
197 </dl>
198
199 <h1 id="ERROR-HANDLING">ERROR HANDLING</h1>
200
201 <p>An exit value of 0, 1, or 2 is returned by perltidy to indicate the status of the result.</p>
202
203 <p>A exit value of 0 indicates that perltidy ran to completion with no error messages.</p>
204
205 <p>An exit value of 1 indicates that the process had to be terminated early due to errors in the input parameters. This can happen for example if a parameter is misspelled or given an invalid value. The calling program should check for this flag because if it is set the destination stream will be empty or incomplete and should be ignored. Error messages in the <b>stderr</b> stream will indicate the cause of any problem.</p>
206
207 <p>An exit value of 2 indicates that perltidy ran to completion but there there are warning messages in the <b>stderr</b> stream related to parameter errors or conflicts and/or warning messages in the <b>errorfile</b> stream relating to possible syntax errors in the source code being tidied.</p>
208
209 <p>In the event of a catastrophic error for which recovery is not possible <b>perltidy</b> terminates by making calls to <b>croak</b> or <b>confess</b> to help the programmer localize the problem. These should normally only occur during program development.</p>
210
211 <h1 id="NOTES-ON-FORMATTING-PARAMETERS">NOTES ON FORMATTING PARAMETERS</h1>
212
213 <p>Parameters which control formatting may be passed in several ways: in a <i>.perltidyrc</i> configuration file, in the <b>perltidyrc</b> parameter, and in the <b>argv</b> parameter.</p>
214
215 <p>The <b>-syn</b> (<b>--check-syntax</b>) flag may be used with all source and destination streams except for standard input and output. However data streams which are not associated with a filename will be copied to a temporary file before being passed to Perl. This use of temporary files can cause somewhat confusing output from Perl.</p>
216
217 <p>If the <b>-pbp</b> style is used it will typically be necessary to also specify a <b>-nst</b> flag. This is necessary to turn off the <b>-st</b> flag contained in the <b>-pbp</b> parameter set which otherwise would direct the output stream to the standard output.</p>
218
219 <h1 id="EXAMPLES">EXAMPLES</h1>
220
221 <p>The following example uses string references to hold the input and output code and error streams, and illustrates checking for errors.</p>
222
223 <pre><code>  use Perl::Tidy;
224   
225   my $source_string = &lt;&lt;&#39;EOT&#39;;
226   my$error=Perl::Tidy::perltidy(argv=&gt;$argv,source=&gt;\$source_string,
227     destination=&gt;\$dest_string,stderr=&gt;\$stderr_string,
228   errorfile=&gt;\$errorfile_string,);
229   EOT
230   
231   my $dest_string;
232   my $stderr_string;
233   my $errorfile_string;
234   my $argv = &quot;-npro&quot;;   # Ignore any .perltidyrc at this site
235   $argv .= &quot; -pbp&quot;;     # Format according to perl best practices
236   $argv .= &quot; -nst&quot;;     # Must turn off -st in case -pbp is specified
237   $argv .= &quot; -se&quot;;      # -se appends the errorfile to stderr
238   ## $argv .= &quot; --spell-check&quot;;  # uncomment to trigger an error
239   
240   print &quot;&lt;&lt;RAW SOURCE&gt;&gt;\n$source_string\n&quot;;
241   
242   my $error = Perl::Tidy::perltidy(
243       argv        =&gt; $argv,
244       source      =&gt; \$source_string,
245       destination =&gt; \$dest_string,
246       stderr      =&gt; \$stderr_string,
247       errorfile   =&gt; \$errorfile_string,    # ignored when -se flag is set
248       ##phasers   =&gt; &#39;stun&#39;,                # uncomment to trigger an error
249   );
250   
251   if ($error) {
252   
253       # serious error in input parameters, no tidied output
254       print &quot;&lt;&lt;STDERR&gt;&gt;\n$stderr_string\n&quot;;
255       die &quot;Exiting because of serious errors\n&quot;;
256   }
257   
258   if ($dest_string)      { print &quot;&lt;&lt;TIDIED SOURCE&gt;&gt;\n$dest_string\n&quot; }
259   if ($stderr_string)    { print &quot;&lt;&lt;STDERR&gt;&gt;\n$stderr_string\n&quot; }
260   if ($errorfile_string) { print &quot;&lt;&lt;.ERR file&gt;&gt;\n$errorfile_string\n&quot; }</code></pre>
261
262 <p>Additional examples are given in examples section of the perltidy distribution.</p>
263
264 <h1 id="Using-the-formatter-Callback-Object">Using the <b>formatter</b> Callback Object</h1>
265
266 <p>The <b>formatter</b> parameter is an optional callback object which allows the calling program to receive tokenized lines directly from perltidy for further specialized processing. When this parameter is used, the two formatting options which are built into perltidy (beautification or html) are ignored. The following diagram illustrates the logical flow:</p>
267
268 <pre><code>                    |-- (normal route)   -&gt; code beautification
269   caller-&gt;perltidy-&gt;|-- (-html flag )    -&gt; create html 
270                     |-- (formatter given)-&gt; callback to write_line</code></pre>
271
272 <p>This can be useful for processing perl scripts in some way. The parameter <code>$formatter</code> in the perltidy call,</p>
273
274 <pre><code>        formatter   =&gt; $formatter,  </code></pre>
275
276 <p>is an object created by the caller with a <code>write_line</code> method which will accept and process tokenized lines, one line per call. Here is a simple example of a <code>write_line</code> which merely prints the line number, the line type (as determined by perltidy), and the text of the line:</p>
277
278 <pre><code> sub write_line {
279  
280      # This is called from perltidy line-by-line
281      my $self              = shift;
282      my $line_of_tokens    = shift;
283      my $line_type         = $line_of_tokens-&gt;{_line_type};
284      my $input_line_number = $line_of_tokens-&gt;{_line_number};
285      my $input_line        = $line_of_tokens-&gt;{_line_text};
286      print &quot;$input_line_number:$line_type:$input_line&quot;;
287  }</code></pre>
288
289 <p>The complete program, <b>perllinetype</b>, is contained in the examples section of the source distribution. As this example shows, the callback method receives a parameter <b>$line_of_tokens</b>, which is a reference to a hash of other useful information. This example uses these hash entries:</p>
290
291 <pre><code> $line_of_tokens-&gt;{_line_number} - the line number (1,2,...)
292  $line_of_tokens-&gt;{_line_text}   - the text of the line
293  $line_of_tokens-&gt;{_line_type}   - the type of the line, one of:
294
295     SYSTEM         - system-specific code before hash-bang line
296     CODE           - line of perl code (including comments)
297     POD_START      - line starting pod, such as &#39;=head&#39;
298     POD            - pod documentation text
299     POD_END        - last line of pod section, &#39;=cut&#39;
300     HERE           - text of here-document
301     HERE_END       - last line of here-doc (target word)
302     FORMAT         - format section
303     FORMAT_END     - last line of format section, &#39;.&#39;
304     DATA_START     - __DATA__ line
305     DATA           - unidentified text following __DATA__
306     END_START      - __END__ line
307     END            - unidentified text following __END__
308     ERROR          - we are in big trouble, probably not a perl script</code></pre>
309
310 <p>Most applications will be only interested in lines of type <b>CODE</b>. For another example, let&#39;s write a program which checks for one of the so-called <i>naughty matching variables</i> <code>&amp;`</code>, <code>$&amp;</code>, and <code>$&#39;</code>, which can slow down processing. Here is a <b>write_line</b>, from the example program <b>find_naughty.pl</b>, which does that:</p>
311
312 <pre><code> sub write_line {
313  
314      # This is called back from perltidy line-by-line
315      # We&#39;re looking for $`, $&amp;, and $&#39;
316      my ( $self, $line_of_tokens ) = @_;
317  
318      # pull out some stuff we might need
319      my $line_type         = $line_of_tokens-&gt;{_line_type};
320      my $input_line_number = $line_of_tokens-&gt;{_line_number};
321      my $input_line        = $line_of_tokens-&gt;{_line_text};
322      my $rtoken_type       = $line_of_tokens-&gt;{_rtoken_type};
323      my $rtokens           = $line_of_tokens-&gt;{_rtokens};
324      chomp $input_line;
325  
326      # skip comments, pod, etc
327      return if ( $line_type ne &#39;CODE&#39; );
328  
329      # loop over tokens looking for $`, $&amp;, and $&#39;
330      for ( my $j = 0 ; $j &lt; @$rtoken_type ; $j++ ) {
331  
332          # we only want to examine token types &#39;i&#39; (identifier)
333          next unless $$rtoken_type[$j] eq &#39;i&#39;;
334  
335          # pull out the actual token text
336          my $token = $$rtokens[$j];
337  
338          # and check it
339          if ( $token =~ /^\$[\`\&amp;\&#39;]$/ ) {
340              print STDERR
341                &quot;$input_line_number: $token\n&quot;;
342          }
343      }
344  }</code></pre>
345
346 <p>This example pulls out these tokenization variables from the $line_of_tokens hash reference:</p>
347
348 <pre><code>     $rtoken_type = $line_of_tokens-&gt;{_rtoken_type};
349      $rtokens     = $line_of_tokens-&gt;{_rtokens};</code></pre>
350
351 <p>The variable <code>$rtoken_type</code> is a reference to an array of token type codes, and <code>$rtokens</code> is a reference to a corresponding array of token text. These are obviously only defined for lines of type <b>CODE</b>. Perltidy classifies tokens into types, and has a brief code for each type. You can get a complete list at any time by running perltidy from the command line with</p>
352
353 <pre><code>     perltidy --dump-token-types</code></pre>
354
355 <p>In the present example, we are only looking for tokens of type <b>i</b> (identifiers), so the for loop skips past all other types. When an identifier is found, its actual text is checked to see if it is one being sought. If so, the above write_line prints the token and its line number.</p>
356
357 <p>The <b>formatter</b> feature is relatively new in perltidy, and further documentation needs to be written to complete its description. However, several example programs have been written and can be found in the <b>examples</b> section of the source distribution. Probably the best way to get started is to find one of the examples which most closely matches your application and start modifying it.</p>
358
359 <p>For help with perltidy&#39;s peculiar way of breaking lines into tokens, you might run, from the command line,</p>
360
361 <pre><code> perltidy -D filename</code></pre>
362
363 <p>where <i>filename</i> is a short script of interest. This will produce <i>filename.DEBUG</i> with interleaved lines of text and their token types. The <b>-D</b> flag has been in perltidy from the beginning for this purpose. If you want to see the code which creates this file, it is <code>write_debug_entry</code> in Tidy.pm.</p>
364
365 <h1 id="EXPORT">EXPORT</h1>
366
367 <pre><code>  &amp;perltidy</code></pre>
368
369 <h1 id="INSTALLATION">INSTALLATION</h1>
370
371 <p>The module &#39;Perl::Tidy&#39; comes with a binary &#39;perltidy&#39; which is installed when the module is installed. The module name is case-sensitive. For example, the basic command for installing with cpanm is &#39;cpanm Perl::Tidy&#39;.</p>
372
373 <h1 id="VERSION">VERSION</h1>
374
375 <p>This man page documents Perl::Tidy version 20210717</p>
376
377 <h1 id="LICENSE">LICENSE</h1>
378
379 <p>This package is free software; you can redistribute it and/or modify it under the terms of the &quot;GNU General Public License&quot;.</p>
380
381 <p>Please refer to the file &quot;COPYING&quot; for details.</p>
382
383 <h1 id="BUG-REPORTS">BUG REPORTS</h1>
384
385 <p>A list of current bugs and issues can be found at the CPAN site <a href="https://rt.cpan.org/Public/Dist/Display.html?Name=Perl-Tidy">https://rt.cpan.org/Public/Dist/Display.html?Name=Perl-Tidy</a></p>
386
387 <p>To report a new bug or problem, use the link on this page.</p>
388
389 <p>The source code repository is at <a href="https://github.com/perltidy/perltidy">https://github.com/perltidy/perltidy</a>.</p>
390
391 <h1 id="SEE-ALSO">SEE ALSO</h1>
392
393 <p>The perltidy(1) man page describes all of the features of perltidy. It can be found at http://perltidy.sourceforge.net.</p>
394
395
396 </body>
397
398 </html>
399
400