6 my $arg_string = undef;
8 # give Macs a chance to provide command line parameters
10 $arg_string = MacPerl::Ask(
11 'Please enter @ARGV (-h for help)',
12 defined $ARGV[0] ? "\"$ARGV[0]\"" : ""
16 # Exit codes returned by perltidy:
18 # 1 = perltidy could not run to completion due to errors
19 # 2 = perltidy ran to completion with error messages
20 exit Perl::Tidy::perltidy( argv => $arg_string );
26 perltidy - a perl script indenter and reformatter
30 perltidy [ options ] file1 file2 file3 ...
31 (output goes to file1.tdy, file2.tdy, file3.tdy, ...)
32 perltidy [ options ] file1 -o outfile
33 perltidy [ options ] file1 -st >outfile
34 perltidy [ options ] <infile >outfile
38 Perltidy reads a perl script and writes an indented, reformatted script.
40 Many users will find enough information in L<"EXAMPLES"> to get
41 started. New users may benefit from the short tutorial
43 http://perltidy.sourceforge.net/tutorial.html
45 A convenient aid to systematically defining a set of style parameters
47 http://perltidy.sourceforge.net/stylekey.html
49 Perltidy can produce output on either of two modes, depending on the
50 existence of an B<-html> flag. Without this flag, the output is passed
51 through a formatter. The default formatting tries to follow the
52 recommendations in perlstyle(1), but it can be controlled in detail with
53 numerous input parameters, which are described in L<"FORMATTING
56 When the B<-html> flag is given, the output is passed through an HTML
57 formatter which is described in L<"HTML OPTIONS">.
63 This will produce a file F<somefile.pl.tdy> containing the script reformatted
64 using the default options, which approximate the style suggested in
65 perlstyle(1). The source file F<somefile.pl> is unchanged.
69 Execute perltidy on all F<.pl> files in the current directory with the
70 default options. The output will be in files with an appended F<.tdy>
71 extension. For any file with an error, there will be a file with extension
74 perltidy -b file1.pl file2.pl
76 Modify F<file1.pl> and F<file2.pl> in place, and backup the originals to
77 F<file1.pl.bak> and F<file2.pl.bak>. If F<file1.pl.bak> and/or F<file2.pl.bak>
78 already exist, they will be overwritten.
80 perltidy -b -bext='/' file1.pl file2.pl
82 Same as the previous example except that the backup files F<file1.pl.bak> and F<file2.pl.bak> will be deleted if there are no errors.
84 perltidy -gnu somefile.pl
86 Execute perltidy on file F<somefile.pl> with a style which approximates the
87 GNU Coding Standards for C programs. The output will be F<somefile.pl.tdy>.
89 perltidy -i=3 somefile.pl
91 Execute perltidy on file F<somefile.pl>, with 3 columns for each level of
92 indentation (B<-i=3>) instead of the default 4 columns. There will not be any
93 tabs in the reformatted script, except for any which already exist in comments,
94 pod documents, quotes, and here documents. Output will be F<somefile.pl.tdy>.
96 perltidy -i=3 -et=8 somefile.pl
98 Same as the previous example, except that leading whitespace will
99 be entabbed with one tab character per 8 spaces.
101 perltidy -ce -l=72 somefile.pl
103 Execute perltidy on file F<somefile.pl> with all defaults except use "cuddled
104 elses" (B<-ce>) and a maximum line length of 72 columns (B<-l=72>) instead of
105 the default 80 columns.
107 perltidy -g somefile.pl
109 Execute perltidy on file F<somefile.pl> and save a log file F<somefile.pl.LOG>
110 which shows the nesting of braces, parentheses, and square brackets at
111 the start of every line.
113 perltidy -html somefile.pl
115 This will produce a file F<somefile.pl.html> containing the script with
116 html markup. The output file will contain an embedded style sheet in
117 the <HEAD> section which may be edited to change the appearance.
119 perltidy -html -css=mystyle.css somefile.pl
121 This will produce a file F<somefile.pl.html> containing the script with
122 html markup. This output file will contain a link to a separate style
123 sheet file F<mystyle.css>. If the file F<mystyle.css> does not exist,
124 it will be created. If it exists, it will not be overwritten.
126 perltidy -html -pre somefile.pl
128 Write an html snippet with only the PRE section to F<somefile.pl.html>.
129 This is useful when code snippets are being formatted for inclusion in a
130 larger web page. No style sheet will be written in this case.
132 perltidy -html -ss >mystyle.css
134 Write a style sheet to F<mystyle.css> and exit.
136 perltidy -html -frm mymodule.pm
138 Write html with a frame holding a table of contents and the source code. The
139 output files will be F<mymodule.pm.html> (the frame), F<mymodule.pm.toc.html>
140 (the table of contents), and F<mymodule.pm.src.html> (the source code).
142 =head1 OPTIONS - OVERVIEW
144 The entire command line is scanned for options, and they are processed
145 before any files are processed. As a result, it does not matter
146 whether flags are before or after any filenames. However, the relative
147 order of parameters is important, with later parameters overriding the
148 values of earlier parameters.
150 For each parameter, there is a long name and a short name. The short
151 names are convenient for keyboard input, while the long names are
152 self-documenting and therefore useful in scripts. It is customary to
153 use two leading dashes for long names, but one may be used.
155 Most parameters which serve as on/off flags can be negated with a
156 leading "n" (for the short name) or a leading "no" or "no-" (for the
157 long name). For example, the flag to outdent long quotes is B<-olq>
158 or B<--outdent-long-quotes>. The flag to skip this is B<-nolq>
159 or B<--nooutdent-long-quotes> or B<--no-outdent-long-quotes>.
161 Options may not be bundled together. In other words, options B<-q> and
162 B<-g> may NOT be entered as B<-qg>.
164 Option names may be terminated early as long as they are uniquely identified.
165 For example, instead of B<--dump-token-types>, it would be sufficient to enter
166 B<--dump-tok>, or even B<--dump-t>, to uniquely identify this command.
170 The following parameters concern the files which are read and written.
174 =item B<-h>, B<--help>
176 Show summary of usage and exit.
178 =item B<-o>=filename, B<--outfile>=filename
180 Name of the output file (only if a single input file is being
181 processed). If no output file is specified, and output is not
182 redirected to the standard output (see B<-st>), the output will go to
183 F<filename.tdy>. [Note: - does not redirect to standard output. Use
186 =item B<-st>, B<--standard-output>
188 Perltidy must be able to operate on an arbitrarily large number of files
189 in a single run, with each output being directed to a different output
190 file. Obviously this would conflict with outputting to the single
191 standard output device, so a special flag, B<-st>, is required to
192 request outputting to the standard output. For example,
194 perltidy somefile.pl -st >somefile.new.pl
196 This option may only be used if there is just a single input file.
197 The default is B<-nst> or B<--nostandard-output>.
199 =item B<-se>, B<--standard-error-output>
201 If perltidy detects an error when processing file F<somefile.pl>, its
202 default behavior is to write error messages to file F<somefile.pl.ERR>.
203 Use B<-se> to cause all error messages to be sent to the standard error
204 output stream instead. This directive may be negated with B<-nse>.
205 Thus, you may place B<-se> in a F<.perltidyrc> and override it when
206 desired with B<-nse> on the command line.
208 =item B<-oext>=ext, B<--output-file-extension>=ext
210 Change the extension of the output file to be F<ext> instead of the
211 default F<tdy> (or F<html> in case the -B<-html> option is used).
212 See L<Specifying File Extensions>.
214 =item B<-opath>=path, B<--output-path>=path
216 When perltidy creates a filename for an output file, by default it merely
217 appends an extension to the path and basename of the input file. This
218 parameter causes the path to be changed to F<path> instead.
220 The path should end in a valid path separator character, but perltidy will try
221 to add one if it is missing.
225 perltidy somefile.pl -opath=/tmp/
227 will produce F</tmp/somefile.pl.tdy>. Otherwise, F<somefile.pl.tdy> will
228 appear in whatever directory contains F<somefile.pl>.
230 If the path contains spaces, it should be placed in quotes.
232 This parameter will be ignored if output is being directed to standard output,
233 or if it is being specified explicitly with the B<-o=s> parameter.
235 =item B<-b>, B<--backup-and-modify-in-place>
237 Modify the input file or files in-place and save the original with the
238 extension F<.bak>. Any existing F<.bak> file will be deleted. See next
239 item for changing the default backup extension, and for eliminating the
240 backup file altogether.
242 A B<-b> flag will be ignored if input is from standard input or goes to
243 standard output, or if the B<-html> flag is set.
245 In particular, if you want to use both the B<-b> flag and the B<-pbp>
246 (--perl-best-practices) flag, then you must put a B<-nst> flag after the
247 B<-pbp> flag because it contains a B<-st> flag as one of its components,
248 which means that output will go to the standard output stream.
250 =item B<-bext>=ext, B<--backup-file-extension>=ext
252 This parameter serves two purposes: (1) to change the extension of the backup
253 file to be something other than the default F<.bak>, and (2) to indicate
254 that no backup file should be saved.
256 To change the default extension to something other than F<.bak> see
257 L<Specifying File Extensions>.
259 A backup file of the source is always written, but you can request that it
260 be deleted at the end of processing if there were no errors. This is risky
261 unless the source code is being maintained with a source code control
264 To indicate that the backup should be deleted include one forward slash,
265 B</>, in the extension. If any text remains after the slash is removed
266 it will be used to define the backup file extension (which is always
267 created and only deleted if there were no errors).
269 Here are some examples:
271 Parameter Extension Backup File Treatment
272 <-bext=bak> F<.bak> Keep (same as the default behavior)
273 <-bext='/'> F<.bak> Delete if no errors
274 <-bext='/backup'> F<.backup> Delete if no errors
275 <-bext='original/'> F<.original> Delete if no errors
277 =item B<-w>, B<--warning-output>
279 Setting B<-w> causes any non-critical warning
280 messages to be reported as errors. These include messages
281 about possible pod problems, possibly bad starting indentation level,
282 and cautions about indirect object usage. The default, B<-nw> or
283 B<--nowarning-output>, is not to include these warnings.
285 =item B<-q>, B<--quiet>
287 Deactivate error messages (for running under an editor).
289 For example, if you use a vi-style editor, such as vim, you may execute
290 perltidy as a filter from within the editor using something like
294 where C<n1,n2> represents the selected text. Without the B<-q> flag,
295 any error message may mess up your screen, so be prepared to use your
298 =item B<-log>, B<--logfile>
300 Save the F<.LOG> file, which has many useful diagnostics. Perltidy always
301 creates a F<.LOG> file, but by default it is deleted unless a program bug is
302 suspected. Setting the B<-log> flag forces the log file to be saved.
304 =item B<-g=n>, B<--logfile-gap=n>
306 Set maximum interval between input code lines in the logfile. This purpose of
307 this flag is to assist in debugging nesting errors. The value of C<n> is
308 optional. If you set the flag B<-g> without the value of C<n>, it will be
309 taken to be 1, meaning that every line will be written to the log file. This
310 can be helpful if you are looking for a brace, paren, or bracket nesting error.
312 Setting B<-g> also causes the logfile to be saved, so it is not necessary to
313 also include B<-log>.
315 If no B<-g> flag is given, a value of 50 will be used, meaning that at least
316 every 50th line will be recorded in the logfile. This helps prevent
317 excessively long log files.
319 Setting a negative value of C<n> is the same as not setting B<-g> at all.
321 =item B<-npro> B<--noprofile>
323 Ignore any F<.perltidyrc> command file. Normally, perltidy looks first in
324 your current directory for a F<.perltidyrc> file of parameters. (The format
325 is described below). If it finds one, it applies those options to the
326 initial default values, and then it applies any that have been defined
327 on the command line. If no F<.perltidyrc> file is found, it looks for one
328 in your home directory.
330 If you set the B<-npro> flag, perltidy will not look for this file.
332 =item B<-pro=filename> or B<--profile=filename>
334 To simplify testing and switching .perltidyrc files, this command may be
335 used to specify a configuration file which will override the default
336 name of .perltidyrc. There must not be a space on either side of the
337 '=' sign. For example, the line
339 perltidy -pro=testcfg
341 would cause file F<testcfg> to be used instead of the
342 default F<.perltidyrc>.
344 A pathname begins with three dots, e.g. ".../.perltidyrc", indicates that
345 the file should be searched for starting in the current directory and
346 working upwards. This makes it easier to have multiple projects each with
347 their own .perltidyrc in their root directories.
349 =item B<-opt>, B<--show-options>
351 Write a list of all options used to the F<.LOG> file.
352 Please see B<--dump-options> for a simpler way to do this.
354 =item B<-f>, B<--force-read-binary>
356 Force perltidy to process binary files. To avoid producing excessive
357 error messages, perltidy skips files identified by the system as non-text.
358 However, valid perl scripts containing binary data may sometimes be identified
359 as non-text, and this flag forces perltidy to process them.
361 =item B<-ast>, B<--assert-tidy>
363 This flag asserts that the input and output code streams are identical, or in
364 other words that the input code is already 'tidy' according to the formatting
365 parameters. If this is not the case, an error message noting this is produced.
366 This error message will cause the process to return a non-zero exit code.
367 The test for this is made by comparing an MD5 hash value for the input and
368 output code streams. This flag has no other effect on the functioning of
369 perltidy. This might be useful for certain code maintenance operations.
370 Note: you will not see this message if you have error messages turned off with the
373 =item B<-asu>, B<--assert-untidy>
375 This flag asserts that the input and output code streams are different, or in
376 other words that the input code is 'untidy' according to the formatting
377 parameters. If this is not the case, an error message noting this is produced.
378 This flag has no other effect on the functioning of perltidy.
380 =item B<-sal=s>, B<--sub-alias-list=s>
382 This flag causes one or more words to be treated the same as if they were the keyword 'sub'. The string B<s> contains one or more alias words, separated by spaces or commas.
386 perltidy -sal='method fun _sub M4'
388 will cause the perltidy to treate the words 'method', 'fun', '_sub' and 'M4' to be treated the same as if they were 'sub'. Note that if the alias words are separated by spaces then the string of words should be placed in quotes.
390 Note that several other parameters accept a list of keywords, including 'sub' (see L<Specifying Block Types>).
391 You do not need to include any sub aliases in these lists. Just include keyword 'sub' if you wish, and all aliases are automatically included.
393 =item B<-gal=s>, B<--grep-alias-list=s>
395 This flag allows a code block following an external 'list operator' function to be formatted as if it followed one of the builtin keywords B<grep>, B<map> or B<sort>. The string B<s> contains the names of one or more such list operators, separated by spaces or commas.
397 By 'list operator' is meant a function which is invoked in the form
401 Perltidy tries to keep code blocks for these functions intact, since they are usually short, and does not automatically break after the closing brace since a list may follow. It also does some special handling of continuation indentation.
403 For example, the code block arguments to functions 'My_grep' and 'My_map' can be given formatting like 'grep' with
405 perltidy -gal='My_grep My_map'
407 By default, the following list operators in List::Util are automatically included:
409 all any first none notall reduce reductions
411 Any operators specified with B<--grep-alias-list> are added to this list.
412 The next parameter can be used to remove words from this default list.
414 =item B<-gaxl=s>, B<--grep-alias-exclusion-list=s>
416 The B<-gaxl=s> flag provides a method for removing any of the default list operators given above
417 by listing them in the string B<s>. To remove all of the default operators use B<-gaxl='*'>.
421 =head1 FORMATTING OPTIONS
429 This flag disables all formatting and causes the input to be copied unchanged
430 to the output except for possible changes in line ending characters and any
431 pre- and post-filters. This can be useful in conjunction with a hierarchical
432 set of F<.perltidyrc> files to avoid unwanted code tidying. See also
433 L<Skipping Selected Sections of Code> for a way to avoid tidying specific
436 =item B<-i=n>, B<--indent-columns=n>
438 Use n columns per indentation level (default n=4).
440 =item B<-l=n>, B<--maximum-line-length=n>
442 The default maximum line length is n=80 characters. Perltidy will try
443 to find line break points to keep lines below this length. However, long
444 quotes and side comments may cause lines to exceed this length.
446 The default length of 80 comes from the past when this was the standard CRT
447 screen width. Many programmers prefer to increase this to something like 120.
449 Setting B<-l=0> is equivalent to setting B<-l=(a very large number)>. But this is
450 not recommended because, for example, a very long list will be formatted in a
453 =item B<-vmll>, B<--variable-maximum-line-length>
455 A problem arises using a fixed maximum line length with very deeply nested code
456 and data structures because eventually the amount of leading whitespace used
457 for indicating indentation takes up most or all of the available line width,
458 leaving little or no space for the actual code or data. One solution is to use
459 a very long line length. Another solution is to use the B<-vmll> flag, which
460 basically tells perltidy to ignore leading whitespace when measuring the line
463 To be precise, when the B<-vmll> parameter is set, the maximum line length of a
464 line of code will be M+L*I, where
466 M is the value of --maximum-line-length=M (-l=M), default 80,
467 I is the value of --indent-columns=I (-i=I), default 4,
468 L is the indentation level of the line of code
470 When this flag is set, the choice of breakpoints for a block of code should be
471 essentially independent of its nesting depth. However, the absolute line
472 lengths, including leading whitespace, can still be arbitrarily large. This
473 problem can be avoided by including the next parameter.
475 The default is not to do this (B<-nvmll>).
477 =item B<-wc=n>, B<--whitespace-cycle=n>
479 This flag also addresses problems with very deeply nested code and data
480 structures. When the nesting depth exceeds the value B<n> the leading
481 whitespace will be reduced and start at a depth of 1 again. The result is that
482 blocks of code will shift back to the left rather than moving arbitrarily far
483 to the right. This occurs cyclically to any depth.
485 For example if one level of indentation equals 4 spaces (B<-i=4>, the default),
486 and one uses B<-wc=15>, then if the leading whitespace on a line exceeds about
487 4*15=60 spaces it will be reduced back to 4*1=4 spaces and continue increasing
488 from there. If the whitespace never exceeds this limit the formatting remains
491 The combination of B<-vmll> and B<-wc=n> provides a solution to the problem of
492 displaying arbitrarily deep data structures and code in a finite window,
493 although B<-wc=n> may of course be used without B<-vmll>.
495 The default is not to use this, which can also be indicated using B<-wc=0>.
499 Using tab characters will almost certainly lead to future portability
500 and maintenance problems, so the default and recommendation is not to
501 use them. For those who prefer tabs, however, there are two different
504 Except for possibly introducing tab indentation characters, as outlined
505 below, perltidy does not introduce any tab characters into your file,
506 and it removes any tabs from the code (unless requested not to do so
507 with B<-fws>). If you have any tabs in your comments, quotes, or
508 here-documents, they will remain.
512 =item B<-et=n>, B<--entab-leading-whitespace>
514 This flag causes each B<n> initial space characters to be replaced by
517 The value of the integer B<n> can be any value but can be coordinated with the
518 number of spaces used for intentation. For example, B<-et=4 -ci=4 -i=4> will
519 produce one tab for each indentation level and and one for each continuation
520 indentation level. You may want to coordinate the value of B<n> with what your
521 display software assumes for the spacing of a tab.
523 =item B<-t>, B<--tabs>
525 This flag causes one leading tab character to be inserted for each level
526 of indentation. Certain other features are incompatible with this
527 option, and if these options are also given, then a warning message will
528 be issued and this flag will be unset. One example is the B<-lp>
529 option. This flag is retained for backwards compatibility, but
530 if you use tabs, the B<-et=n> flag is recommended.
532 =item B<-dt=n>, B<--default-tabsize=n>
534 If the first line of code passed to perltidy contains leading tabs but no
535 tab scheme is specified for the output stream then perltidy must guess how many
536 spaces correspond to each leading tab. This number of spaces B<n>
537 corresponding to each leading tab of the input stream may be specified with
538 B<-dt=n>. The default is B<n=8>.
540 This flag has no effect if a tab scheme is specified for the output stream,
541 because then the input stream is assumed to use the same tab scheme and
542 indentation spaces as for the output stream (any other assumption would lead to
547 =item B<-xs>, B<--extended-syntax>
549 A problem with formatting Perl code is that some modules can introduce new
550 syntax. This flag allows perltidy to handle certain common extensions
551 to the standard syntax without complaint.
553 For example, without this flag a structure such as the following would generate
554 a syntax error and the braces would not be balanced:
556 method deposit( Num $amount) {
557 $self->balance( $self->balance + $amount );
560 For one of the extensions, module Switch::Plain, colons are marked as labels.
561 If you use this module, you may want to also use the B<--nooutdent-labels> flag
562 to prevent lines such as 'default:' from being outdented.
564 This flag is enabled by default but it can be deactivated with B<-nxs>.
565 Probably the only reason to deactivate this flag is to generate more diagnostic
566 messages when debugging a script.
568 For another method of handling extended syntax see the section L<Skipping Selected Sections of Code>.
570 =item B<-io>, B<--indent-only>
572 This flag is used to deactivate all whitespace and line break changes
573 within non-blank lines of code.
574 When it is in effect, the only change to the script will be
575 to the indentation and to the number of blank lines.
576 And any flags controlling whitespace and newlines will be ignored. You
577 might want to use this if you are perfectly happy with your whitespace
578 and line breaks, and merely want perltidy to handle the indentation.
579 (This also speeds up perltidy by well over a factor of two, so it might be
580 useful when perltidy is merely being used to help find a brace error in
583 Setting this flag is equivalent to setting B<--freeze-newlines> and
584 B<--freeze-whitespace>.
586 If you also want to keep your existing blank lines exactly
587 as they are, you can add B<--freeze-blank-lines>.
589 With this option perltidy is still free to modify the indenting (and
590 outdenting) of code and comments as it normally would. If you also want to
591 prevent long comment lines from being outdented, you can add either B<-noll> or
594 Setting this flag will prevent perltidy from doing any special operations on
595 closing side comments. You may still delete all side comments however when
596 this flag is in effect.
599 =item B<-enc=s>, B<--character-encoding=s>
601 This flag indicates if the input data stream use a character encoding.
602 Perltidy does not look for the encoding directives in the soure stream, such
603 as B<use utf8>, and instead relies on this flag to determine the encoding.
604 (Note that perltidy often works on snippets of code rather than complete files
605 so it cannot rely on B<use utf8> directives).
607 The possible values for B<s> are:
609 -enc=none if no encoding is used, or
610 -enc=utf8 for encoding in utf8
611 -enc=guess if perltidy should guess between these two possibilities.
613 The value B<none> causes the stream to be processed without special encoding
614 assumptions. This is appropriate for files which are written in single-byte
615 character encodings such as latin-1.
617 The value B<utf8> causes the stream to be read and written as
618 UTF-8. If the input stream cannot be decoded with this encoding then
619 processing is not done.
621 The value B<guess> tells perltidy to guess between either utf8 encoding or no
622 encoding (meaning one character per byte). The B<guess> option uses the
623 Encode::Guess module which has been found to be reliable at detecting
624 if a file is encoded in utf8 or not.
626 The current default is B<guess>.
628 The abbreviations B<-utf8> or B<-UTF8> are equivalent to B<-enc=utf8>, and the
629 abbreviation B<-guess> is equivalent to B<-enc=guess>. So to process a file
630 named B<file.pl> which is encoded in UTF-8 you can use:
632 perltidy -utf8 file.pl
636 perltidy -guess file.pl
642 since B<-guess> is the default.
644 To process files with an encoding other than UTF-8, it would be necessary to
645 write a short program which calls the Perl::Tidy module with some pre- and
646 post-processing to handle decoding and encoding.
648 =item B<-eos=s>, B<--encode-output-strings=s>
650 This flag has been added to resolve an issue involving the interface between
651 Perl::Tidy and calling programs, and in particular B<Code::TidyAll (tidyall)>.
652 By default Perl::Tidy returns unencoded strings to the calling
653 program, but some programs expect encoded strings. Setting this flag causes
654 Perl::Tidy to return encoded output strings which it decoded. For some
655 background information see
656 L<https://github.com/perltidy/perltidy/issues/83> and
657 L<https://github.com/houseabsolute/perl-code-tidyall/issues/84>.
659 If you only run the B<perltidy> binary this flag has no effect.
661 If you use B<tidyall> with encoded files and encounter irregularities such as
662 B<wide character> messages you should set this flag.
664 Additional information can be found in the man pages for the B<Perl::Tidy> module.
666 =item B<-gcs>, B<--use-unicode-gcstring>
668 This flag controls whether or not perltidy may use module Unicode::GCString to
669 obtain accurate display widths of wide characters. The default
670 is B<--nouse-unicode-gcstring>.
672 If this flag is set, and text is encoded, perltidy will look for the module
673 Unicode::GCString and, if found, will use it to obtain character display
674 widths. This can improve displayed vertical alignment for files with wide
675 characters. It is a nice feature but it is off by default to avoid conflicting
676 formatting when there are multiple developers. Perltidy installation does not
677 require Unicode::GCString, so users wanting to use this feature need set this
678 flag and also to install Unicode::GCString separately.
680 If this flag is set and perltidy does not find module Unicode::GCString,
681 a warning message will be produced and processing will continue but without
682 the potential benefit provided by the module.
684 Also note that actual vertical alignment depends upon the fonts used by the
685 text display software, so vertical alignment may not be optimal even when
686 Unicode::GCString is used.
688 =item B<-ole=s>, B<--output-line-ending=s>
690 where s=C<win>, C<dos>, C<unix>, or C<mac>. This flag tells perltidy
691 to output line endings for a specific system. Normally,
692 perltidy writes files with the line separator character of the host
693 system. The C<win> and C<dos> flags have an identical result.
695 =item B<-ple>, B<--preserve-line-endings>
697 This flag tells perltidy to write its output files with the same line
698 endings as the input file, if possible. It should work for
699 B<dos>, B<unix>, and B<mac> line endings. It will only work if perltidy
700 input comes from a filename (rather than stdin, for example). If
701 perltidy has trouble determining the input file line ending, it will
702 revert to the default behavior of using the line ending of the host system.
704 =item B<-atnl>, B<--add-terminal-newline>
706 This flag, which is enabled by default, allows perltidy to terminate the last
707 line of the output stream with a newline character, regardless of whether or
708 not the input stream was terminated with a newline character. If this flag is
709 negated, with B<-natnl>, then perltidy will add a terminal newline to the the
710 output stream only if the input stream is terminated with a newline.
712 Negating this flag may be useful for manipulating one-line scripts intended for
713 use on a command line.
715 =item B<-it=n>, B<--iterations=n>
717 This flag causes perltidy to do B<n> complete iterations. The reason for this
718 flag is that code beautification is an iterative process and in some
719 cases the output from perltidy can be different if it is applied a second time.
720 For most purposes the default of B<n=1> should be satisfactory. However B<n=2>
721 can be useful when a major style change is being made, or when code is being
722 beautified on check-in to a source code control system. It has been found to
723 be extremely rare for the output to change after 2 iterations. If a value
724 B<n> is greater than 2 is input then a convergence test will be used to stop
725 the iterations as soon as possible, almost always after 2 iterations. See
726 the next item for a simplified iteration control.
728 This flag has no effect when perltidy is used to generate html.
730 =item B<-conv>, B<--converge>
732 This flag is equivalent to B<-it=4> and is included to simplify iteration
733 control. For all practical purposes one either does or does not want to be
734 sure that the output is converged, and there is no penalty to using a large
735 iteration limit since perltidy will check for convergence and stop iterating as
736 soon as possible. The default is B<-nconv> (no convergence check). Using
737 B<-conv> will approximately double run time since typically one extra iteration
738 is required to verify convergence. No extra iterations are required if no new
739 line breaks are made, and two extra iterations are occasionally needed when
740 reformatting complex code structures, such as deeply nested ternary statements.
744 =head2 Code Indentation Control
748 =item B<-ci=n>, B<--continuation-indentation=n>
750 Continuation indentation is extra indentation spaces applied when
751 a long line is broken. The default is n=2, illustrated here:
754 ( $max_index_to_go >= 0 ) ? $levels_to_go[0] : $last_output_level;
756 The same example, with n=0, is a little harder to read:
759 ( $max_index_to_go >= 0 ) ? $levels_to_go[0] : $last_output_level;
761 The value given to B<-ci> is also used by some commands when a small
762 space is required. Examples are commands for outdenting labels,
763 B<-ola>, and control keywords, B<-okw>.
765 When default values are not used, it is recommended that either
767 (1) the value B<n> given with B<-ci=n> be no more than about one-half of the
768 number of spaces assigned to a full indentation level on the B<-i=n> command, or
770 (2) the flag B<-extended-continuation-indentation> is used (see next section).
772 =item B<-xci>, B<--extended-continuation-indentation>
774 This flag allows perltidy to use some improvements which have been made to its
775 indentation model. One of the things it does is "extend" continuation
776 indentation deeper into structures, hence the name. The improved indentation
777 is particularly noticeable when the flags B<-ci=n> and B<-i=n> use the same value of
778 B<n>. There are no significant disadvantages to using this flag, but to avoid
779 disturbing existing formatting the default is not to use it, B<-nxci>.
781 Please see the section L<"B<-pbp>, B<--perl-best-practices>"> for an example of
782 how this flag can improve the formatting of ternary statements. It can also
783 improve indentation of some multi-line qw lists as shown below.
788 AntiqueWhite3 Bisque1 Bisque2 Bisque3 Bisque4
789 SlateBlue3 RoyalBlue1 SteelBlue2 DeepSkyBlue3
792 LightBlue1 DarkSlateGray1 Aquamarine2 DarkSeaGreen2
793 SeaGreen1 Yellow1 IndianRed1 IndianRed2 Tan1 Tan4
800 AntiqueWhite3 Bisque1 Bisque2 Bisque3 Bisque4
801 SlateBlue3 RoyalBlue1 SteelBlue2 DeepSkyBlue3
804 LightBlue1 DarkSlateGray1 Aquamarine2 DarkSeaGreen2
805 SeaGreen1 Yellow1 IndianRed1 IndianRed2 Tan1 Tan4
809 =item B<-sil=n> B<--starting-indentation-level=n>
811 By default, perltidy examines the input file and tries to determine the
812 starting indentation level. While it is often zero, it may not be
813 zero for a code snippet being sent from an editing session.
815 To guess the starting indentation level perltidy simply assumes that
816 indentation scheme used to create the code snippet is the same as is being used
817 for the current perltidy process. This is the only sensible guess that can be
818 made. It should be correct if this is true, but otherwise it probably won't.
819 For example, if the input script was written with -i=2 and the current peltidy
820 flags have -i=4, the wrong initial indentation will be guessed for a code
821 snippet which has non-zero initial indentation. Likewise, if an entabbing
822 scheme is used in the input script and not in the current process then the
823 guessed indentation will be wrong.
825 If the default method does not work correctly, or you want to change the
826 starting level, use B<-sil=n>, to force the starting level to be n.
828 =item B<List indentation> using B<--line-up-parentheses>, B<-lp> or B<--extended--line-up-parentheses> , B<-xlp>
830 These flags provide an alternative indentation method for list data. The
831 original flag for this is B<-lp>, but it has some limitations (explained below)
832 which are avoided with the newer B<-xlp> flag. So B<-xlp> is probably the better
833 choice for new work, but the B<-lp> flag is retained to minimize changes to
835 If you enter both B<-lp> and B<-xlp>, then B<-xlp> will be used.
838 In the default indentation method perltidy indents lists with 4 spaces, or
839 whatever value is specified with B<-i=n>. Here is a small list formatted in
844 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
845 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'
848 The B<-lp> or B<-xlp> flags add extra indentation to cause the data to begin
849 past the opening parentheses of a sub call or list, or opening square bracket
850 of an anonymous array, or opening curly brace of an anonymous hash. With this
851 option, the above list would become:
853 # perltidy -lp or -xlp
855 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
856 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'
859 If the available line length (see B<-l=n> ) does not permit this much
860 space, perltidy will use less. For alternate placement of the
861 closing paren, see the next section.
863 These flags have no effect on code BLOCKS, such as if/then/else blocks,
864 which always use whatever is specified with B<-i=n>.
866 Some limitiations on these flags are:
872 A limitation on B<-lp>, but not B<-xlp>, occurs in situations where perltidy
873 does not have complete freedom to choose line breaks. Then it may temporarily revert
874 to its default indentation method. This can occur for example if there are
875 blank lines, block comments, multi-line quotes, or side comments between the
876 opening and closing parens, braces, or brackets. It will also occur if a
877 multi-line anonymous sub occurs within a container since that will impose
878 specific line breaks (such as line breaks after statements).
882 For both the B<-lp> and B<-xlp> flags, any parameter which significantly
883 restricts the ability of perltidy to choose newlines will conflict with these
884 flags and will cause them to be deactivated. These include B<-io>, B<-fnl>,
885 B<-nanl>, and B<-ndnl>.
889 The B<-lp> and B<-xlp> options may not be used together with the B<-t> tabs option.
890 They may, however, be used with the B<-et=n> tab method
894 There are some potential disadvantages of this indentation method compared to
895 the default method that should be noted:
901 The available line length can quickly be used up if variable names are
902 long. This can cause deeply nested code to quickly reach the line length
903 limit, and become badly formatted, much sooner than would occur with the
904 default indentation method.
908 Since the indentation depends on the lengths of variable names, small
909 changes in variable names can cause changes in indentation over many lines in a
910 file. This means that minor name changes can produce significant file
911 differences. This can be annoying and does not occur with the default
916 Some things that can be done to minimize these problems are:
922 Increase B<--maximum-line-length=n> above the default B<n=80> characters if
927 If you use B<-xlp> then long side comments can limit the indentation over
928 multiple lines. Consider adding the flag B<--ignore-side-comment-lengths> to
929 prevent this, or minimizing the use of side comments.
933 Apply this style in a limited way. By default, it applies to all list
934 containers (not just lists in parentheses). The next section describes how to
935 limit this style to, for example, just function calls. The default indentation
936 method will be applied elsewhere.
940 =item B<-lpil=s>, B<--line-up-parentheses-inclusion-list> and B<-lpxl=s>, B<--line-up-parentheses-exclusion-list>
942 The following discussion is written for B<-lp> but applies equally to the newer B<-xlp> version.
943 By default, the B<-lp> flag applies to as many containers as possible.
944 The set of containers to which the B<-lp> style applies can be reduced by
945 either one of these two flags:
947 Use B<-lpil=s> to specify the containers to which B<-lp> applies, or
949 use B<-lpxl=s> to specify the containers to which B<-lp> does NOT apply.
951 Only one of these two flags may be used. Both flags can achieve the same
952 result, but the B<-lpil=s> flag is much easier to describe and use and is
953 recommended. The B<-lpxl=s> flag was the original implementation and is
954 only retained for backwards compatibility.
956 This list B<s> for these parametes is a string with space-separated items.
957 Each item consists of up to three pieces of information in this order: (1) an
958 optional letter code (2) a required container type, and (3) an optional numeric
961 The only required piece of information is a container type, which is one of
962 '(', '[', or '{'. For example the string
966 means use -lp formatting only on lists within parentheses, not lists in square-bracets or braces.
967 The same thing could alternatively be specified with
971 which says to exclude lists within square-brackets and braces. So what remains is lists within parentheses.
973 A second optional item of information which can be given for parentheses is an alphanumeric
974 letter which is used to limit the selection further depending on the type of
975 token immediately before the paren. The possible letters are currently 'k',
976 'K', 'f', 'F', 'w', and 'W', with these meanings for matching whatever precedes an opening paren:
978 'k' matches if the previous nonblank token is a perl builtin keyword (such as 'if', 'while'),
979 'K' matches if 'k' does not, meaning that the previous token is not a keyword.
980 'f' matches if the previous token is a function other than a keyword.
981 'F' matches if 'f' does not.
982 'w' matches if either 'k' or 'f' match.
983 'W' matches if 'w' does not.
989 means only apply -lp to function calls, and
993 means only apply -lp to parenthesized lists which follow a function or a keyword.
995 This last example could alternatively be written using the B<-lpxl=s> flag as
999 which says exclude B<-lp> for lists within square-brackets, braces, and parens NOT preceded by
1000 a keyword or function. Clearly, the B<-lpil=s> method is easier to understand.
1002 An optional numeric code may follow any of the container types to further refine the selection based
1003 on container contents. The numeric codes are:
1005 '0' or blank: no check on contents is made
1006 '1' exclude B<-lp> unless the contents is a simple list without sublists
1007 '2' exclude B<-lp> unless the contents is a simple list without sublists, without
1008 code blocks, and without ternary operators
1014 means only apply -lp to function call lists which do not contain any sublists,
1015 code blocks or ternary expressions.
1017 =item B<-cti=n>, B<--closing-token-indentation>
1019 The B<-cti=n> flag controls the indentation of a line beginning with
1020 a C<)>, C<]>, or a non-block C<}>. Such a line receives:
1022 -cti = 0 no extra indentation (default)
1023 -cti = 1 extra indentation such that the closing token
1024 aligns with its opening token.
1025 -cti = 2 one extra indentation level if the line looks like:
1027 -cti = 3 one extra indentation level always
1029 The flags B<-cti=1> and B<-cti=2> work well with the B<-lp> flag (previous
1032 # perltidy -lp -cti=1
1034 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
1035 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'
1038 # perltidy -lp -cti=2
1040 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
1041 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'
1044 These flags are merely hints to the formatter and they may not always be
1045 followed. In particular, if -lp is not being used, the indentation for
1046 B<cti=1> is constrained to be no more than one indentation level.
1048 If desired, this control can be applied independently to each of the
1049 closing container token types. In fact, B<-cti=n> is merely an
1050 abbreviation for B<-cpi=n -csbi=n -cbi=n>, where:
1051 B<-cpi> or B<--closing-paren-indentation> controls B<)>'s,
1052 B<-csbi> or B<--closing-square-bracket-indentation> controls B<]>'s,
1053 B<-cbi> or B<--closing-brace-indentation> controls non-block B<}>'s.
1055 =item B<-icp>, B<--indent-closing-paren>
1057 The B<-icp> flag is equivalent to
1058 B<-cti=2>, described in the previous section. The B<-nicp> flag is
1059 equivalent B<-cti=0>. They are included for backwards compatibility.
1061 =item B<-icb>, B<--indent-closing-brace>
1063 The B<-icb> option gives one extra level of indentation to a brace which
1064 terminates a code block . For example,
1073 The default is not to do this, indicated by B<-nicb>.
1076 =item B<-nib>, B<--non-indenting-braces>
1078 Normally, lines of code contained within a pair of block braces receive one
1079 additional level of indentation. This flag, which is enabled by default,
1080 causes perltidy to look for
1081 opening block braces which are followed by a special side comment. This special
1082 side comment is B<#<<<> by default. If found, the code between this opening brace and its
1083 corresponding closing brace will not be given the normal extra indentation
1086 { #<<< a closure to contain lexical vars
1088 my $var; # this line does not get one level of indentation
1093 # this line does not 'see' $var;
1095 This can be useful, for example, when combining code from different files.
1096 Different sections of code can be placed within braces to keep their lexical
1097 variables from being visible to the end of the file. To keep the new braces
1098 from causing all of their contained code to be indented if you run perltidy,
1099 and possibly introducing new line breaks in long lines, you can mark the
1100 opening braces with this special side comment.
1102 Only the opening brace needs to be marked, since perltidy knows where the
1103 closing brace is. Braces contained within marked braces may also be marked
1106 If your code happens to have some opening braces followed by '#<<<', and you
1107 don't want this behavior, you can use B<-nnib> to deactivate it. To make it
1108 easy to remember, the default string is the same as the string for starting a
1109 B<format-skipping> section. There is no confusion because in that case it is
1110 for a block comment rather than a side-comment.
1112 The special side comment can be changed with the next parameter.
1115 =item B<-nibp=s>, B<--non-indenting-brace-prefix=s>
1117 The B<-nibp=string> parameter may be used to change the marker for
1118 non-indenting braces. The default is equivalent to -nibp='#<<<'. The string
1119 that you enter must begin with a # and should be in quotes as necessary to get
1120 past the command shell of your system. This string is the leading text of a
1121 regex pattern that is constructed by appending pre-pending a '^' and appending
1122 a'\s', so you must also include backslashes for characters to be taken
1123 literally rather than as patterns.
1125 For example, to match the side comment '#++', the parameter would be
1130 =item B<-olq>, B<--outdent-long-quotes>
1132 When B<-olq> is set, lines which is a quoted string longer than the
1133 value B<maximum-line-length> will have their indentation removed to make
1134 them more readable. This is the default. To prevent such out-denting,
1135 use B<-nolq> or B<--nooutdent-long-lines>.
1137 =item B<-oll>, B<--outdent-long-lines>
1139 This command is equivalent to B<--outdent-long-quotes> and
1140 B<--outdent-long-comments>, and it is included for compatibility with previous
1141 versions of perltidy. The negation of this also works, B<-noll> or
1142 B<--nooutdent-long-lines>, and is equivalent to setting B<-nolq> and B<-nolc>.
1144 =item B<Outdenting Labels:> B<-ola>, B<--outdent-labels>
1146 This command will cause labels to be outdented by 2 spaces (or whatever B<-ci>
1147 has been set to), if possible. This is the default. For example:
1150 LOOP: while ( $i = <FOTOS> ) {
1156 Use B<-nola> to not outdent labels. To control line breaks after labels see L<bal=n, --break-after-labels=n>.
1158 =item B<Outdenting Keywords>
1162 =item B<-okw>, B<--outdent-keywords>
1164 The command B<-okw> will cause certain leading control keywords to
1165 be outdented by 2 spaces (or whatever B<-ci> has been set to), if
1166 possible. By default, these keywords are C<redo>, C<next>, C<last>,
1167 C<goto>, and C<return>. The intention is to make these control keywords
1168 easier to see. To change this list of keywords being outdented, see
1171 For example, using C<perltidy -okw> on the previous example gives:
1174 LOOP: while ( $i = <FOTOS> ) {
1180 The default is not to do this.
1182 =item B<Specifying Outdented Keywords:> B<-okwl=string>, B<--outdent-keyword-list=string>
1184 This command can be used to change the keywords which are outdented with
1185 the B<-okw> command. The parameter B<string> is a required list of perl
1186 keywords, which should be placed in quotes if there are more than one.
1187 By itself, it does not cause any outdenting to occur, so the B<-okw>
1188 command is still required.
1190 For example, the commands C<-okwl="next last redo goto" -okw> will cause
1191 those four keywords to be outdented. It is probably simplest to place
1192 any B<-okwl> command in a F<.perltidyrc> file.
1198 =head2 Whitespace Control
1200 Whitespace refers to the blank space between variables, operators,
1201 and other code tokens.
1205 =item B<-fws>, B<--freeze-whitespace>
1207 This flag causes your original whitespace to remain unchanged, and
1208 causes the rest of the whitespace commands in this section, the
1209 Code Indentation section, and
1210 the Comment Control section to be ignored.
1212 =item B<Tightness of curly braces, parentheses, and square brackets>
1214 Here the term "tightness" will mean the closeness with which
1215 pairs of enclosing tokens, such as parentheses, contain the quantities
1216 within. A numerical value of 0, 1, or 2 defines the tightness, with
1217 0 being least tight and 2 being most tight. Spaces within containers
1218 are always symmetric, so if there is a space after a C<(> then there
1219 will be a space before the corresponding C<)>.
1221 The B<-pt=n> or B<--paren-tightness=n> parameter controls the space within
1222 parens. The example below shows the effect of the three possible
1223 values, 0, 1, and 2:
1225 if ( ( my $len_tab = length( $tabstr ) ) > 0 ) { # -pt=0
1226 if ( ( my $len_tab = length($tabstr) ) > 0 ) { # -pt=1 (default)
1227 if ((my $len_tab = length($tabstr)) > 0) { # -pt=2
1229 When n is 0, there is always a space to the right of a '(' and to the left
1230 of a ')'. For n=2 there is never a space. For n=1, the default, there
1231 is a space unless the quantity within the parens is a single token, such
1232 as an identifier or quoted string.
1234 Likewise, the parameter B<-sbt=n> or B<--square-bracket-tightness=n>
1235 controls the space within square brackets, as illustrated below.
1237 $width = $col[ $j + $k ] - $col[ $j ]; # -sbt=0
1238 $width = $col[ $j + $k ] - $col[$j]; # -sbt=1 (default)
1239 $width = $col[$j + $k] - $col[$j]; # -sbt=2
1241 Curly braces which do not contain code blocks are controlled by
1242 the parameter B<-bt=n> or B<--brace-tightness=n>.
1244 $obj->{ $parsed_sql->{ 'table' }[0] }; # -bt=0
1245 $obj->{ $parsed_sql->{'table'}[0] }; # -bt=1 (default)
1246 $obj->{$parsed_sql->{'table'}[0]}; # -bt=2
1248 And finally, curly braces which contain blocks of code are controlled by the
1249 parameter B<-bbt=n> or B<--block-brace-tightness=n> as illustrated in the
1252 %bf = map { $_ => -M $_ } grep { /\.deb$/ } dirents '.'; # -bbt=0 (default)
1253 %bf = map { $_ => -M $_ } grep {/\.deb$/} dirents '.'; # -bbt=1
1254 %bf = map {$_ => -M $_} grep {/\.deb$/} dirents '.'; # -bbt=2
1256 To simplify input in the case that all of the tightness flags have the same
1257 value <n>, the parameter <-act=n> or B<--all-containers-tightness=n> is an
1258 abbreviation for the combination <-pt=n -sbt=n -bt=n -bbt=n>.
1261 =item B<-tso>, B<--tight-secret-operators>
1263 The flag B<-tso> causes certain perl token sequences (secret operators)
1264 which might be considered to be a single operator to be formatted "tightly"
1265 (without spaces). The operators currently modified by this flag are:
1267 0+ +0 ()x!! ~~<> ,=> =( )=
1269 For example the sequence B<0 +>, which converts a string to a number,
1270 would be formatted without a space: B<0+> when the B<-tso> flag is set. This
1271 flag is off by default.
1273 =item B<-sts>, B<--space-terminal-semicolon>
1275 Some programmers prefer a space before all terminal semicolons. The
1276 default is for no such space, and is indicated with B<-nsts> or
1277 B<--nospace-terminal-semicolon>.
1280 $i = 1; # -nsts (default)
1282 =item B<-sfs>, B<--space-for-semicolon>
1284 Semicolons within B<for> loops may sometimes be hard to see,
1285 particularly when commas are also present. This option places spaces on
1286 both sides of these special semicolons, and is the default. Use
1287 B<-nsfs> or B<--nospace-for-semicolon> to deactivate it.
1289 for ( @a = @$ap, $u = shift @a ; @a ; $u = $v ) { # -sfs (default)
1290 for ( @a = @$ap, $u = shift @a; @a; $u = $v ) { # -nsfs
1292 =item B<-asc>, B<--add-semicolons>
1294 Setting B<-asc> allows perltidy to add any missing optional semicolon at the end
1295 of a line which is followed by a closing curly brace on the next line. This
1296 is the default, and may be deactivated with B<-nasc> or B<--noadd-semicolons>.
1298 =item B<-dsm>, B<--delete-semicolons>
1300 Setting B<-dsm> allows perltidy to delete extra semicolons which are
1301 simply empty statements. This is the default, and may be deactivated
1302 with B<-ndsm> or B<--nodelete-semicolons>. (Such semicolons are not
1303 deleted, however, if they would promote a side comment to a block
1306 =item B<-aws>, B<--add-whitespace>
1308 Setting this option allows perltidy to add certain whitespace to improve
1309 code readability. This is the default. If you do not want any
1310 whitespace added, but are willing to have some whitespace deleted, use
1311 B<-naws>. (Use B<-fws> to leave whitespace completely unchanged).
1313 =item B<-dws>, B<--delete-old-whitespace>
1315 Setting this option allows perltidy to remove some old whitespace
1316 between characters, if necessary. This is the default. If you
1317 do not want any old whitespace removed, use B<-ndws> or
1318 B<--nodelete-old-whitespace>.
1320 =item B<Detailed whitespace controls around tokens>
1322 For those who want more detailed control over the whitespace around
1323 tokens, there are four parameters which can directly modify the default
1324 whitespace rules built into perltidy for any token. They are:
1326 B<-wls=s> or B<--want-left-space=s>,
1328 B<-nwls=s> or B<--nowant-left-space=s>,
1330 B<-wrs=s> or B<--want-right-space=s>,
1332 B<-nwrs=s> or B<--nowant-right-space=s>.
1334 These parameters are each followed by a quoted string, B<s>, containing a
1335 list of token types. No more than one of each of these parameters
1336 should be specified, because repeating a command-line parameter
1337 always overwrites the previous one before perltidy ever sees it.
1339 To illustrate how these are used, suppose it is desired that there be no
1340 space on either side of the token types B<= + - / *>. The following two
1341 parameters would specify this desire:
1343 -nwls="= + - / *" -nwrs="= + - / *"
1345 (Note that the token types are in quotes, and that they are separated by
1346 spaces). With these modified whitespace rules, the following line of math:
1348 $root = -$b + sqrt( $b * $b - 4. * $a * $c ) / ( 2. * $a );
1352 $root=-$b+sqrt( $b*$b-4.*$a*$c )/( 2.*$a );
1354 These parameters should be considered to be hints to perltidy rather
1355 than fixed rules, because perltidy must try to resolve conflicts that
1356 arise between them and all of the other rules that it uses. One
1357 conflict that can arise is if, between two tokens, the left token wants
1358 a space and the right one doesn't. In this case, the token not wanting
1359 a space takes priority.
1361 It is necessary to have a list of all token types in order to create
1362 this type of input. Such a list can be obtained by the command
1363 B<--dump-token-types>. Also try the B<-D> flag on a short snippet of code
1364 and look at the .DEBUG file to see the tokenization.
1366 B<WARNING> Be sure to put these tokens in quotes to avoid having them
1367 misinterpreted by your command shell.
1369 =item B<Note1: Perltidy does always follow whitespace controls>
1371 The various parameters controlling whitespace within a program are requests which perltidy follows as well as possible, but there are a number of situations where changing whitespace could change program behavior and is not done. Some of these are obvious; for example, we should not remove the space between the two plus symbols in '$x+ +$y' to avoid creating a '++' operator. Some are more subtle and involve the whitespace around bareword symbols and locations of possible filehandles. For example, consider the problem of formatting the following subroutine:
1378 Suppose the user requests that / signs have a space to the left but not to the right. Perltidy will refuse to do this, but if this were done the result would be
1385 If formatted in this way, the program will not run (at least with recent versions of perl) because the $x is taken to be a filehandle and / is assumed to start a quote. In a complex program, there might happen to be a / which terminates the multiline quote without a syntax error, allowing the program to run, but not as intended.
1387 Related issues arise with other binary operator symbols, such as + and -, and in older versions of perl there could be problems with ternary operators. So to avoid changing program behavior, perltidy has the simple rule that whitespace around possible filehandles is left unchanged. Likewise, whitespace around barewords is left unchanged. The reason is that if the barewords are defined in other modules, or in code that has not even been written yet, perltidy will not have seen their prototypes and must treat them cautiously.
1389 In perltidy this is implemented in the tokenizer by marking token following a
1390 B<print> keyword as a special type B<Z>. When formatting is being done,
1391 whitespace following this token type is generally left unchanged as a precaution
1392 against changing program behavior. This is excessively conservative but simple
1393 and easy to implement. Keywords which are treated similarly to B<print> include
1394 B<printf>, B<sort>, B<exec>, B<system>. Changes in spacing around parameters
1395 following these keywords may have to be made manually. For example, the space,
1396 or lack of space, after the parameter $foo in the following line will be
1397 unchanged in formatting.
1402 To find if a token is of type B<Z> you can use B<perltidy -DEBUG>. For the
1403 first line above the result is
1408 which shows that B<system> is type B<k> (keyword) and $foo is type B<Z>.
1410 =item B<Note2: Perltidy's whitespace rules are not perfect>
1412 Despite these precautions, it is still possible to introduce syntax errors with
1413 some asymmetric whitespace rules, particularly when call parameters are not
1414 placed in containing parens or braces. For example, the following two lines will
1415 be parsed by perl without a syntax error:
1417 # original programming, syntax ok
1418 my @newkeys = map $_-$nrecs+@data, @oldkeys;
1420 # perltidy default, syntax ok
1421 my @newkeys = map $_ - $nrecs + @data, @oldkeys;
1423 But the following will give a syntax error:
1425 # perltidy -nwrs='-'
1426 my @newkeys = map $_ -$nrecs + @data, @oldkeys;
1428 For another example, the following two lines will be parsed without syntax error:
1430 # original programming, syntax ok
1431 for my $severity ( reverse $SEVERITY_LOWEST+1 .. $SEVERITY_HIGHEST ) { ... }
1433 # perltidy default, syntax ok
1434 for my $severity ( reverse $SEVERITY_LOWEST + 1 .. $SEVERITY_HIGHEST ) { ... }
1436 But the following will give a syntax error:
1438 # perltidy -nwrs='+', syntax error:
1439 for my $severity ( reverse $SEVERITY_LOWEST +1 .. $SEVERITY_HIGHEST ) { ... }
1441 To avoid subtle parsing problems like this, it is best to avoid spacing a
1442 binary operator asymetrically with a space on the left but not on the right.
1444 =item B<Space between specific keywords and opening paren>
1446 When an opening paren follows a Perl keyword, no space is introduced after the
1447 keyword, unless it is (by default) one of these:
1449 my local our and or xor eq ne if else elsif until unless
1450 while for foreach return switch case given when
1452 These defaults can be modified with two commands:
1454 B<-sak=s> or B<--space-after-keyword=s> adds keywords.
1456 B<-nsak=s> or B<--nospace-after-keyword=s> removes keywords.
1458 where B<s> is a list of keywords (in quotes if necessary). For example,
1460 my ( $a, $b, $c ) = @_; # default
1461 my( $a, $b, $c ) = @_; # -nsak="my local our"
1463 The abbreviation B<-nsak='*'> is equivalent to including all of the
1464 keywords in the above list.
1466 When both B<-nsak=s> and B<-sak=s> commands are included, the B<-nsak=s>
1467 command is executed first. For example, to have space after only the
1468 keywords (my, local, our) you could use B<-nsak="*" -sak="my local our">.
1470 To put a space after all keywords, see the next item.
1472 =item B<Space between all keywords and opening parens>
1474 When an opening paren follows a function or keyword, no space is introduced
1475 after the keyword except for the keywords noted in the previous item. To
1476 always put a space between a function or keyword and its opening paren,
1479 B<-skp> or B<--space-keyword-paren>
1481 You may also want to use the flag B<-sfp> (next item) too.
1483 =item B<Space between all function names and opening parens>
1485 When an opening paren follows a function the default and recommended formatting
1486 is not to introduce a space. To cause a space to be introduced use:
1488 B<-sfp> or B<--space-function-paren>
1490 myfunc( $a, $b, $c ); # default
1491 myfunc ( $a, $b, $c ); # -sfp
1493 You will probably also want to use the flag B<-skp> (previous item) too.
1495 The reason this is not recommended is that spacing a function paren can make a
1496 program vulnerable to parsing problems by Perl. For example, the following
1497 two-line program will run as written but will have a syntax error if
1498 reformatted with -sfp:
1500 if ( -e filename() ) { print "I'm here\n"; }
1501 sub filename { return $0 }
1503 In this particular case the syntax error can be removed if the line order is
1504 reversed, so that Perl parses 'sub filename' first.
1506 =item B<-fpva> or B<--function-paren-vertical-alignment>
1508 A side-effect of using the B<-sfp> flag is that the parens may become vertically
1509 aligned. For example,
1512 myfun ( $aaa, $b, $cc );
1513 mylongfun ( $a, $b, $c );
1515 This is the default behavior. To prevent this alignment use B<-nfpva>:
1517 # perltidy -sfp -nfpva
1518 myfun ( $aaa, $b, $cc );
1519 mylongfun ( $a, $b, $c );
1521 =item B<-spp=n> or B<--space-prototype-paren=n>
1523 This flag can be used to control whether a function prototype is preceded by a space. For example, the following prototype does not have a space.
1527 This integer B<n> may have the value 0, 1, or 2 as follows:
1529 -spp=0 means no space before the paren
1530 -spp=1 means follow the example of the source code [DEFAULT]
1531 -spp=2 means always put a space before the paren
1533 The default is B<-spp=1>, meaning that a space will be used if and only if there is one in the source code. Given the above line of code, the result of
1534 applying the different options would be:
1536 sub usage(); # n=0 [no space]
1537 sub usage(); # n=1 [default; follows input]
1538 sub usage (); # n=2 [space]
1540 =item B<-kpit=n> or B<--keyword-paren-inner-tightness=n>
1542 The space inside of an opening paren, which itself follows a certain keyword,
1543 can be controlled by this parameter. The space on the inside of the
1544 corresponding closing paren will be treated in the same (balanced) manner.
1545 This parameter has precedence over any other paren spacing rules. The values
1546 of B<n> are as follows:
1548 -kpit=0 means always put a space (not tight)
1549 -kpit=1 means ignore this parameter [default]
1550 -kpit=2 means never put a space (tight)
1552 To illustrate, the following snippet is shown formatted in three ways:
1554 if ( seek( DATA, 0, 0 ) ) { ... } # perltidy (default)
1555 if (seek(DATA, 0, 0)) { ... } # perltidy -pt=2
1556 if ( seek(DATA, 0, 0) ) { ... } # perltidy -pt=2 -kpit=0
1558 In the second case the -pt=2 parameter makes all of the parens tight. In the
1559 third case the -kpit=0 flag causes the space within the 'if' parens to have a
1560 space, since 'if' is one of the keywords to which the -kpit flag applies by
1561 default. The remaining parens are still tight because of the -pt=2 parameter.
1563 The set of keywords to which this parameter applies are by default are:
1565 if elsif unless while until for foreach
1567 These can be changed with the parameter B<-kpitl=s> described in the next section.
1570 =item B<-kpitl=string> or B<--keyword-paren-inner-tightness=string>
1572 This command can be used to change the keywords to which the the B<-kpit=n>
1573 command applies. The parameter B<string> is a required list either keywords or
1574 functions, which should be placed in quotes if there are more than one. By
1575 itself, this parameter does not cause any change in spacing, so the B<-kpit=n>
1576 command is still required.
1578 For example, the commands C<-kpitl="if else while" -kpit=2> will cause the just
1579 the spaces inside parens following 'if', 'else', and 'while' keywords to
1580 follow the tightness value indicated by the B<-kpit=2> flag.
1582 =item B<-lop> or B<--logical-padding>
1584 In the following example some extra space has been inserted on the second
1585 line between the two open parens. This extra space is called "logical padding"
1586 and is intended to help align similar things vertically in some logical
1587 or ternary expressions.
1589 # perltidy [default formatting]
1594 && ( $a->{'title'} eq $b->{'title'} )
1595 && ( $a->{'href'} eq $b->{'href'} ) );
1597 Note that this is considered to be a different operation from "vertical
1598 alignment" because space at just one line is being adjusted, whereas in
1599 "vertical alignment" the spaces at all lines are being adjusted. So it sort of
1600 a local version of vertical alignment.
1602 Here is an example involving a ternary operator:
1604 # perltidy [default formatting]
1611 This behavior is controlled with the flag B<--logical-padding>, which is set
1612 'on' by default. If it is not desired it can be turned off using
1613 B<--nological-padding> or B<-nlop>. The above two examples become, with
1621 && ( $a->{'title'} eq $b->{'title'} )
1622 && ( $a->{'href'} eq $b->{'href'} ) );
1632 =item B<Trimming whitespace around C<qw> quotes>
1634 B<-tqw> or B<--trim-qw> provide the default behavior of trimming
1635 spaces around multi-line C<qw> quotes and indenting them appropriately.
1637 B<-ntqw> or B<--notrim-qw> cause leading and trailing whitespace around
1638 multi-line C<qw> quotes to be left unchanged. This option will not
1639 normally be necessary, but was added for testing purposes, because in
1640 some versions of perl, trimming C<qw> quotes changes the syntax tree.
1642 =item B<-sbq=n> or B<--space-backslash-quote=n>
1649 can confuse syntax highlighters unless a space is included between the backslash and the single or double quotation mark.
1651 this can be controlled with the value of B<n> as follows:
1653 -sbq=0 means no space between the backslash and quote
1654 -sbq=1 means follow the example of the source code
1655 -sbq=2 means always put a space between the backslash and quote
1657 The default is B<-sbq=1>, meaning that a space will be used if there is one in the source code.
1659 =item B<Trimming trailing whitespace from lines of POD>
1661 B<-trp> or B<--trim-pod> will remove trailing whitespace from lines of POD.
1662 The default is not to do this.
1666 =head2 Comment Controls
1668 Perltidy has a number of ways to control the appearance of both block comments
1669 and side comments. The term B<block comment> here refers to a full-line
1670 comment, whereas B<side comment> will refer to a comment which appears on a
1671 line to the right of some code.
1675 =item B<-ibc>, B<--indent-block-comments>
1677 Block comments normally look best when they are indented to the same
1678 level as the code which follows them. This is the default behavior, but
1679 you may use B<-nibc> to keep block comments left-justified. Here is an
1682 # this comment is indented (-ibc, default)
1683 if ($task) { yyy(); }
1685 The alternative is B<-nibc>:
1687 # this comment is not indented (-nibc)
1688 if ($task) { yyy(); }
1690 See also the next item, B<-isbc>, as well as B<-sbc>, for other ways to
1691 have some indented and some outdented block comments.
1693 =item B<-isbc>, B<--indent-spaced-block-comments>
1695 If there is no leading space on the line, then the comment will not be
1696 indented, and otherwise it may be.
1698 If both B<-ibc> and B<-isbc> are set, then B<-isbc> takes priority.
1700 =item B<-olc>, B<--outdent-long-comments>
1702 When B<-olc> is set, lines which are full-line (block) comments longer
1703 than the value B<maximum-line-length> will have their indentation
1704 removed. This is the default; use B<-nolc> to prevent outdenting.
1706 =item B<-msc=n>, B<--minimum-space-to-comment=n>
1708 Side comments look best when lined up several spaces to the right of
1709 code. Perltidy will try to keep comments at least n spaces to the
1710 right. The default is n=4 spaces.
1712 =item B<-fpsc=n>, B<--fixed-position-side-comment=n>
1714 This parameter tells perltidy to line up side comments in column number B<n>
1715 whenever possible. The default, n=0, will not do this.
1717 =item B<-iscl>, B<--ignore-side-comment-lengths>
1719 This parameter causes perltidy to ignore the length of side comments when
1720 setting line breaks. The default, B<-niscl>, is to include the length of
1721 side comments when breaking lines to stay within the length prescribed
1722 by the B<-l=n> maximum line length parameter. For example, the following
1723 long single line would remain intact with -l=80 and -iscl:
1725 perltidy -l=80 -iscl
1726 $vmsfile =~ s/;[\d\-]*$//; # Clip off version number; we can use a newer version as well
1728 whereas without the -iscl flag the line will be broken:
1731 $vmsfile =~ s/;[\d\-]*$//
1732 ; # Clip off version number; we can use a newer version as well
1735 =item B<-hsc>, B<--hanging-side-comments>
1737 By default, perltidy tries to identify and align "hanging side
1738 comments", which are something like this:
1740 my $IGNORE = 0; # This is a side comment
1741 # This is a hanging side comment
1744 A comment is considered to be a hanging side comment if (1) it immediately
1745 follows a line with a side comment, or another hanging side comment, and
1746 (2) there is some leading whitespace on the line.
1747 To deactivate this feature, use B<-nhsc> or B<--nohanging-side-comments>.
1748 If block comments are preceded by a blank line, or have no leading
1749 whitespace, they will not be mistaken as hanging side comments.
1751 =item B<Closing Side Comments>
1753 A closing side comment is a special comment which perltidy can
1754 automatically create and place after the closing brace of a code block.
1755 They can be useful for code maintenance and debugging. The command
1756 B<-csc> (or B<--closing-side-comments>) adds or updates closing side
1757 comments. For example, here is a small code snippet
1760 if ( !defined( $_[0] ) ) {
1761 print("Hello, World\n");
1764 print( $_[0], "\n" );
1768 And here is the result of processing with C<perltidy -csc>:
1771 if ( !defined( $_[0] ) ) {
1772 print("Hello, World\n");
1775 print( $_[0], "\n" );
1777 } ## end sub message
1779 A closing side comment was added for C<sub message> in this case, but not
1780 for the C<if> and C<else> blocks, because they were below the 6 line
1781 cutoff limit for adding closing side comments. This limit may be
1782 changed with the B<-csci> command, described below.
1784 The command B<-dcsc> (or B<--delete-closing-side-comments>) reverses this
1785 process and removes these comments.
1787 Several commands are available to modify the behavior of these two basic
1788 commands, B<-csc> and B<-dcsc>:
1792 =item B<-csci=n>, or B<--closing-side-comment-interval=n>
1794 where C<n> is the minimum number of lines that a block must have in
1795 order for a closing side comment to be added. The default value is
1796 C<n=6>. To illustrate:
1798 # perltidy -csci=2 -csc
1800 if ( !defined( $_[0] ) ) {
1801 print("Hello, World\n");
1802 } ## end if ( !defined( $_[0] ))
1804 print( $_[0], "\n" );
1805 } ## end else [ if ( !defined( $_[0] ))
1806 } ## end sub message
1808 Now the C<if> and C<else> blocks are commented. However, now this has
1809 become very cluttered.
1811 =item B<-cscp=string>, or B<--closing-side-comment-prefix=string>
1813 where string is the prefix used before the name of the block type. The
1814 default prefix, shown above, is C<## end>. This string will be added to
1815 closing side comments, and it will also be used to recognize them in
1816 order to update, delete, and format them. Any comment identified as a
1817 closing side comment will be placed just a single space to the right of
1820 =item B<-cscl=string>, or B<--closing-side-comment-list>
1822 where C<string> is a list of block types to be tagged with closing side
1823 comments. By default, all code block types preceded by a keyword or
1824 label (such as C<if>, C<sub>, and so on) will be tagged. The B<-cscl>
1825 command changes the default list to be any selected block types; see
1826 L<Specifying Block Types>.
1827 For example, the following command
1828 requests that only C<sub>'s, labels, C<BEGIN>, and C<END> blocks be
1829 affected by any B<-csc> or B<-dcsc> operation:
1831 -cscl="sub : BEGIN END"
1833 =item B<-csct=n>, or B<--closing-side-comment-maximum-text=n>
1835 The text appended to certain block types, such as an C<if> block, is
1836 whatever lies between the keyword introducing the block, such as C<if>,
1837 and the opening brace. Since this might be too much text for a side
1838 comment, there needs to be a limit, and that is the purpose of this
1839 parameter. The default value is C<n=20>, meaning that no additional
1840 tokens will be appended to this text after its length reaches 20
1841 characters. Omitted text is indicated with C<...>. (Tokens, including
1842 sub names, are never truncated, however, so actual lengths may exceed
1843 this). To illustrate, in the above example, the appended text of the
1844 first block is C< ( !defined( $_[0] )...>. The existing limit of
1845 C<n=20> caused this text to be truncated, as indicated by the C<...>. See
1846 the next flag for additional control of the abbreviated text.
1848 =item B<-cscb>, or B<--closing-side-comments-balanced>
1850 As discussed in the previous item, when the
1851 closing-side-comment-maximum-text limit is exceeded the comment text must
1852 be truncated. Older versions of perltidy terminated with three dots, and this
1853 can still be achieved with -ncscb:
1855 perltidy -csc -ncscb
1856 } ## end foreach my $foo (sort { $b cmp $a ...
1858 However this causes a problem with editors which cannot recognize
1859 comments or are not configured to do so because they cannot "bounce" around in
1860 the text correctly. The B<-cscb> flag has been added to
1861 help them by appending appropriate balancing structure:
1864 } ## end foreach my $foo (sort { $b cmp $a ... })
1866 The default is B<-cscb>.
1868 =item B<-csce=n>, or B<--closing-side-comment-else-flag=n>
1870 The default, B<n=0>, places the text of the opening C<if> statement after any
1873 If B<n=2> is used, then each C<elsif> is also given the text of the opening
1874 C<if> statement. Also, an C<else> will include the text of a preceding
1875 C<elsif> statement. Note that this may result some long closing
1878 If B<n=1> is used, the results will be the same as B<n=2> whenever the
1879 resulting line length is less than the maximum allowed.
1881 =item B<-cscb>, or B<--closing-side-comments-balanced>
1883 When using closing-side-comments, and the closing-side-comment-maximum-text
1884 limit is exceeded, then the comment text must be abbreviated.
1885 It is terminated with three dots if the B<-cscb> flag is negated:
1887 perltidy -csc -ncscb
1888 } ## end foreach my $foo (sort { $b cmp $a ...
1890 This causes a problem with older editors which do not recognize comments
1891 because they cannot "bounce" around in the text correctly. The B<-cscb>
1892 flag tries to help them by appending appropriate terminal balancing structures:
1895 } ## end foreach my $foo (sort { $b cmp $a ... })
1897 The default is B<-cscb>.
1900 =item B<-cscw>, or B<--closing-side-comment-warnings>
1902 This parameter is intended to help make the initial transition to the use of
1903 closing side comments.
1905 things to happen if a closing side comment replaces an existing, different
1906 closing side comment: first, an error message will be issued, and second, the
1907 original side comment will be placed alone on a new specially marked comment
1908 line for later attention.
1910 The intent is to avoid clobbering existing hand-written side comments
1911 which happen to match the pattern of closing side comments. This flag
1912 should only be needed on the first run with B<-csc>.
1916 B<Important Notes on Closing Side Comments:>
1922 Closing side comments are only placed on lines terminated with a closing
1923 brace. Certain closing styles, such as the use of cuddled elses
1924 (B<-ce>), preclude the generation of some closing side comments.
1928 Please note that adding or deleting of closing side comments takes
1929 place only through the commands B<-csc> or B<-dcsc>. The other commands,
1930 if used, merely modify the behavior of these two commands.
1934 It is recommended that the B<-cscw> flag be used along with B<-csc> on
1935 the first use of perltidy on a given file. This will prevent loss of
1936 any existing side comment data which happens to have the csc prefix.
1940 Once you use B<-csc>, you should continue to use it so that any
1941 closing side comments remain correct as code changes. Otherwise, these
1942 comments will become incorrect as the code is updated.
1946 If you edit the closing side comments generated by perltidy, you must also
1947 change the prefix to be different from the closing side comment prefix.
1948 Otherwise, your edits will be lost when you rerun perltidy with B<-csc>. For
1949 example, you could simply change C<## end> to be C<## End>, since the test is
1950 case sensitive. You may also want to use the B<-ssc> flag to keep these
1951 modified closing side comments spaced the same as actual closing side comments.
1955 Temporarily generating closing side comments is a useful technique for
1956 exploring and/or debugging a perl script, especially one written by someone
1957 else. You can always remove them with B<-dcsc>.
1961 =item B<Static Block Comments>
1963 Static block comments are block comments with a special leading pattern,
1964 C<##> by default, which will be treated slightly differently from other
1965 block comments. They effectively behave as if they had glue along their
1966 left and top edges, because they stick to the left edge and previous line
1967 when there is no blank spaces in those places. This option is
1968 particularly useful for controlling how commented code is displayed.
1972 =item B<-sbc>, B<--static-block-comments>
1974 When B<-sbc> is used, a block comment with a special leading pattern, C<##> by
1975 default, will be treated specially.
1977 Comments so identified are treated as follows:
1983 If there is no leading space on the line, then the comment will not
1984 be indented, and otherwise it may be,
1988 no new blank line will be
1989 inserted before such a comment, and
1993 such a comment will never become
1994 a hanging side comment.
1998 For example, assuming C<@month_of_year> is
2001 @month_of_year = ( # -sbc (default)
2002 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct',
2006 Without this convention, the above code would become
2008 @month_of_year = ( # -nsbc
2009 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct',
2015 which is not as clear.
2016 The default is to use B<-sbc>. This may be deactivated with B<-nsbc>.
2018 =item B<-sbcp=string>, B<--static-block-comment-prefix=string>
2020 This parameter defines the prefix used to identify static block comments
2021 when the B<-sbc> parameter is set. The default prefix is C<##>,
2022 corresponding to C<-sbcp=##>. The prefix is actually part of a perl
2023 pattern used to match lines and it must either begin with C<#> or C<^#>.
2024 In the first case a prefix ^\s* will be added to match any leading
2025 whitespace, while in the second case the pattern will match only
2026 comments with no leading whitespace. For example, to
2027 identify all comments as static block comments, one would use C<-sbcp=#>.
2028 To identify all left-adjusted comments as static block comments, use C<-sbcp='^#'>.
2030 Please note that B<-sbcp> merely defines the pattern used to identify static
2031 block comments; it will not be used unless the switch B<-sbc> is set. Also,
2032 please be aware that since this string is used in a perl regular expression
2033 which identifies these comments, it must enable a valid regular expression to
2036 A pattern which can be useful is:
2040 This pattern requires a static block comment to have at least one character
2041 which is neither a # nor a space. It allows a line containing only '#'
2042 characters to be rejected as a static block comment. Such lines are often used
2043 at the start and end of header information in subroutines and should not be
2044 separated from the intervening comments, which typically begin with just a
2047 =item B<-osbc>, B<--outdent-static-block-comments>
2049 The command B<-osbc> will cause static block comments to be outdented by 2
2050 spaces (or whatever B<-ci=n> has been set to), if possible.
2054 =item B<Static Side Comments>
2056 Static side comments are side comments with a special leading pattern.
2057 This option can be useful for controlling how commented code is displayed
2058 when it is a side comment.
2062 =item B<-ssc>, B<--static-side-comments>
2064 When B<-ssc> is used, a side comment with a static leading pattern, which is
2065 C<##> by default, will be spaced only a single space from previous
2066 character, and it will not be vertically aligned with other side comments.
2068 The default is B<-nssc>.
2070 =item B<-sscp=string>, B<--static-side-comment-prefix=string>
2072 This parameter defines the prefix used to identify static side comments
2073 when the B<-ssc> parameter is set. The default prefix is C<##>,
2074 corresponding to C<-sscp=##>.
2076 Please note that B<-sscp> merely defines the pattern used to identify
2077 static side comments; it will not be used unless the switch B<-ssc> is
2078 set. Also, note that this string is used in a perl regular expression
2079 which identifies these comments, so it must enable a valid regular
2080 expression to be formed.
2086 =head2 Skipping Selected Sections of Code
2088 Selected lines of code may be passed verbatim to the output without any
2089 formatting by marking the starting and ending lines with special comments.
2090 There are two options for doing this. The first option is called
2091 B<--format-skipping> or B<-fs>, and the second option is called
2092 B<--code-skipping> or B<-cs>.
2094 In both cases the lines of code will be output without any changes.
2095 The difference is that in B<--format-skipping>
2096 perltidy will still parse the marked lines of code and check for errors,
2097 whereas in B<--code-skipping> perltidy will simply pass the lines to the output without any checking.
2099 Both of these features are enabled by default and are invoked with special
2100 comment markers. B<--format-skipping> uses starting and ending markers '#<<<'
2101 and '#>>>', like this:
2103 #<<< format skipping: do not let perltidy change my nice formatting
2111 B<--code-skipping> uses starting and ending markers '#<<V' and '#>>V', like
2114 #<<V code skipping: perltidy will pass this verbatim without error checking
2117 [ [ <?word> | _ | <?digit> ] <?ident_digit>
2124 Additional text may appear on the special comment lines provided that it
2125 is separated from the marker by at least one space, as in the above examples.
2127 Any number of code-skipping or format-skipping sections may appear in a file.
2128 If an opening code-skipping or format-skipping comment is not followed by a
2129 corresponding closing comment, then skipping continues to the end of the file.
2130 If a closing code-skipping or format-skipping comment appears in a file but
2131 does not follow a corresponding opening comment, then it is treated as an
2132 ordinary comment without any special meaning.
2134 It is recommended to use B<--code-skipping> only if you need to hide a block of
2135 an extended syntax which would produce errors if parsed by perltidy, and use
2136 B<--format-skipping> otherwise. This is because the B<--format-skipping>
2137 option provides the benefits of error checking, and there are essentially no
2138 limitations on which lines to which it can be applied. The B<--code-skipping>
2139 option, on the other hand, does not do error checking and its use is more
2140 restrictive because the code which remains, after skipping the marked lines,
2141 must be syntactically correct code with balanced containers.
2143 These features should be used sparingly to avoid littering code with markers,
2144 but they can be helpful for working around occasional problems.
2146 Note that it may be possible to avoid the use of B<--format-skipping> for the
2147 specific case of a comma-separated list of values, as in the above example, by
2148 simply inserting a blank or comment somewhere between the opening and closing
2149 parens. See the section L<Controlling List Formatting>.
2151 The following sections describe the available controls for these options. They
2152 should not normally be needed.
2156 =item B<-fs>, B<--format-skipping>
2158 As explained above, this flag, which is enabled by default, causes any code
2159 between special beginning and ending comment markers to be passed to the output
2160 without formatting. The code between the comments is still checked for errors
2161 however. The default beginning marker is #<<< and the default ending marker is
2164 Format skipping begins when a format skipping beginning comment is seen and
2165 continues until a format-skipping ending comment is found.
2167 This feature can be disabled with B<-nfs>. This should not normally be necessary.
2169 =item B<-fsb=string>, B<--format-skipping-begin=string>
2171 This and the next parameter allow the special beginning and ending comments to
2172 be changed. However, it is recommended that they only be changed if there is a
2173 conflict between the default values and some other use. If they are used, it
2174 is recommended that they only be entered in a B<.perltidyrc> file, rather than
2175 on a command line. This is because properly escaping these parameters on a
2176 command line can be difficult.
2178 If changed comment markers do not appear to be working, use the B<-log> flag and
2179 examine the F<.LOG> file to see if and where they are being detected.
2181 The B<-fsb=string> parameter may be used to change the beginning marker for
2182 format skipping. The default is equivalent to -fsb='#<<<'. The string that
2183 you enter must begin with a # and should be in quotes as necessary to get past
2184 the command shell of your system. It is actually the leading text of a pattern
2185 that is constructed by appending a '\s', so you must also include backslashes
2186 for characters to be taken literally rather than as patterns.
2188 Some examples show how example strings become patterns:
2190 -fsb='#\{\{\{' becomes /^#\{\{\{\s/ which matches #{{{ but not #{{{{
2191 -fsb='#\*\*' becomes /^#\*\*\s/ which matches #** but not #***
2192 -fsb='#\*{2,}' becomes /^#\*{2,}\s/ which matches #** and #*****
2194 =item B<-fse=string>, B<--format-skipping-end=string>
2196 The B<-fse=string> is the corresponding parameter used to change the
2197 ending marker for format skipping. The default is equivalent to
2200 The beginning and ending strings may be the same, but it is preferable
2201 to make them different for clarity.
2203 =item B<-cs>, B<--code-skipping>
2205 As explained above, this flag, which is enabled by default, causes any code
2206 between special beginning and ending comment markers to be directly passed to
2207 the output without any error checking or formatting. Essentially, perltidy
2208 treats it as if it were a block of arbitrary text. The default beginning
2209 marker is #<<V and the default ending marker is #>>V.
2211 This feature can be disabled with B<-ncs>. This should not normally be
2214 =item B<-csb=string>, B<--code-skipping-begin=string>
2216 This may be used to change the beginning comment for a B<--code-skipping> section, and its use is similar to the B<-fsb=string>.
2217 The default is equivalent to -csb='#<<V'.
2219 =item B<-cse=string>, B<--code-skipping-end=string>
2221 This may be used to change the ending comment for a B<--code-skipping> section, and its use is similar to the B<-fse=string>.
2222 The default is equivalent to -cse='#>>V'.
2226 =head2 Line Break Control
2228 The parameters in this section control breaks after
2229 non-blank lines of code. Blank lines are controlled
2230 separately by parameters in the section L<Blank Line
2235 =item B<-fnl>, B<--freeze-newlines>
2237 If you do not want any changes to the line breaks within
2238 lines of code in your script, set
2239 B<-fnl>, and they will remain fixed, and the rest of the commands in
2240 this section and sections
2241 L<Controlling List Formatting>,
2242 L<Retaining or Ignoring Existing Line Breaks>.
2243 You may want to use B<-noll> with this.
2245 Note: If you also want to keep your blank lines exactly
2246 as they are, you can use the B<-fbl> flag which is described
2247 in the section L<Blank Line Control>.
2249 =item B<-ce>, B<--cuddled-else>
2251 Enable the "cuddled else" style, in which C<else> and C<elsif> are
2252 follow immediately after the curly brace closing the previous block.
2253 The default is not to use cuddled elses, and is indicated with the flag
2254 B<-nce> or B<--nocuddled-else>. Here is a comparison of the
2272 In this example the keyword B<else> is placed on the same line which begins with
2273 the preceding closing block brace and is followed by its own opening block brace
2274 on the same line. Other keywords and function names which are formatted with
2275 this "cuddled" style are B<elsif>, B<continue>, B<catch>, B<finally>.
2277 Other block types can be formatted by specifying their names on a
2278 separate parameter B<-cbl>, described in a later section.
2280 Cuddling between a pair of code blocks requires that the closing brace of the
2281 first block start a new line. If this block is entirely on one line in the
2282 input file, it is necessary to decide if it should be broken to allow cuddling.
2283 This decision is controlled by the flag B<-cbo=n> discussed below. The default
2284 and recommended value of B<-cbo=1> bases this decision on the first block in
2285 the chain. If it spans multiple lines then cuddling is made and continues
2286 along the chain, regardless of the sizes of subsequent blocks. Otherwise, short
2287 lines remain intact.
2289 So for example, the B<-ce> flag would not have any effect if the above snippet
2292 if ($task) { yyy() }
2295 If the first block spans multiple lines, then cuddling can be done and will
2296 continue for the subsequent blocks in the chain, as illustrated in the previous
2299 If there are blank lines between cuddled blocks they will be eliminated. If
2300 there are comments after the closing brace where cuddling would occur then
2301 cuddling will be prevented. If this occurs, cuddling will restart later in the
2304 =item B<-cb>, B<--cuddled-blocks>
2306 This flag is equivalent to B<-ce>.
2309 =item B<-cbl>, B<--cuddled-block-list>
2311 The built-in default cuddled block types are B<else, elsif, continue, catch, finally>.
2313 Additional block types to which the B<-cuddled-blocks> style applies can be defined by
2314 this parameter. This parameter is a character string, giving a list of
2315 block types separated by commas or spaces. For example, to cuddle code blocks
2316 of type sort, map and grep, in addition to the default types, the string could
2319 -cbl="sort map grep"
2325 Note however that these particular block types are typically short so there might not be much
2326 opportunity for the cuddled format style.
2328 Using commas avoids the need to protect spaces with quotes.
2330 As a diagnostic check, the flag B<--dump-cuddled-block-list> or B<-dcbl> can be
2331 used to view the hash of values that are generated by this flag.
2333 Finally, note that the B<-cbl> flag by itself merely specifies which blocks are formatted
2334 with the cuddled format. It has no effect unless this formatting style is activated with
2337 =item B<-cblx>, B<--cuddled-block-list-exclusive>
2339 When cuddled else formatting is selected with B<-ce>, setting this flag causes
2340 perltidy to ignore its built-in defaults and rely exclusively on the block types
2341 specified on the B<-cbl> flag described in the previous section. For example,
2342 to avoid using cuddled B<catch> and B<finally>, which among in the defaults, the
2343 following set of parameters could be used:
2345 perltidy -ce -cbl='else elsif continue' -cblx
2348 =item B<-cbo=n>, B<--cuddled-break-option=n>
2350 Cuddled formatting is only possible between a pair of code blocks if the
2351 closing brace of the first block starts a new line. If a block is encountered
2352 which is entirely on a single line, and cuddled formatting is selected, it is
2353 necessary to make a decision as to whether or not to "break" the block, meaning
2354 to cause it to span multiple lines. This parameter controls that decision. The
2357 cbo=0 Never force a short block to break.
2358 cbo=1 If the first of a pair of blocks is broken in the input file,
2359 then break the second [DEFAULT].
2360 cbo=2 Break open all blocks for maximal cuddled formatting.
2362 The default and recommended value is B<cbo=1>. With this value, if the starting
2363 block of a chain spans multiple lines, then a cascade of breaks will occur for
2364 remaining blocks causing the entire chain to be cuddled.
2366 The option B<cbo=0> can produce erratic cuddling if there are numerous one-line
2369 The option B<cbo=2> produces maximal cuddling but will not allow any short blocks.
2372 =item B<-bl>, B<--opening-brace-on-new-line>, or B<--brace-left>
2374 Use the flag B<-bl> to place an opening block brace on a new line:
2376 if ( $input_file eq '-' )
2381 By default it applies to all structural blocks except B<sort map grep eval> and
2384 The default is B<-nbl> which places an opening brace on the same line as
2385 the keyword introducing it if possible. For example,
2388 if ( $input_file eq '-' ) {
2392 When B<-bl> is set, the blocks to which this applies can be controlled with the
2393 parameters B<--brace-left-list> and B<-brace-left-exclusion-list> described in the next sections.
2395 =item B<-bll=s>, B<--brace-left-list=s>
2397 Use this parameter to change the types of block braces for which the
2398 B<-bl> flag applies; see L<Specifying Block Types>. For example,
2399 B<-bll='if elsif else sub'> would apply it to only C<if/elsif/else>
2400 and named sub blocks. The default is all blocks, B<-bll='*'>.
2402 =item B<-blxl=s>, B<--brace-left-exclusion-list=s>
2404 Use this parameter to exclude types of block braces for which the
2405 B<-bl> flag applies; see L<Specifying Block Types>. For example,
2406 the default settings B<-bll='*'> and B<-blxl='sort map grep eval asub'>
2407 mean all blocks except B<sort map grep eval> and anonymous sub blocks.
2409 Note that the lists B<-bll=s> and B<-blxl=s> control the behavior of the
2410 B<-bl> flag but have no effect unless the B<-bl> flag is set.
2412 =item B<-sbl>, B<--opening-sub-brace-on-new-line>
2414 The flag B<-sbl> provides a shortcut way to turn on B<-bl> just for named
2415 subs. The same effect can be achieved by turning on B<-bl>
2416 with the block list set as B<-bll='sub'>.
2422 produces this result:
2426 if (!defined($_[0])) {
2427 print("Hello, World\n");
2434 This flag is negated with B<-nsbl>, which is the default.
2436 =item B<-asbl>, B<--opening-anonymous-sub-brace-on-new-line>
2438 The flag B<-asbl> is like the B<-sbl> flag except that it applies
2439 to anonymous sub's instead of named subs. For example
2443 produces this result:
2447 if ( !defined( $_[0] ) ) {
2448 print("Hello, World\n");
2451 print( $_[0], "\n" );
2455 This flag is negated with B<-nasbl>, and the default is B<-nasbl>.
2457 =item B<-bli>, B<--brace-left-and-indent>
2459 The flag B<-bli> is similar to the B<-bl> flag but in addition it causes one
2460 unit of continuation indentation ( see B<-ci> ) to be placed before
2461 an opening and closing block braces.
2463 For example, perltidy -bli gives
2465 if ( $input_file eq '-' )
2467 important_function();
2470 By default, this extra indentation occurs for block types:
2471 B<if>, B<elsif>, B<else>, B<unless>, B<while>, B<for>, B<foreach>, B<do>, and
2472 also B<named subs> and blocks preceded by a B<label>. The next item shows how to
2475 B<Note>: The B<-bli> flag is similar to the B<-bl> flag, with the difference being
2476 that braces get indented. But these two flags are implemented independently,
2477 and have different default settings for historical reasons. If desired, a
2478 mixture of effects can be achieved if desired by turning them both on with
2479 different B<-list> settings. In the event that both settings are selected for
2480 a certain block type, the B<-bli> style has priority.
2482 =item B<-blil=s>, B<--brace-left-and-indent-list=s>
2484 Use this parameter to change the types of block braces for which the
2485 B<-bli> flag applies; see L<Specifying Block Types>.
2487 The default is B<-blil='if else elsif unless while for foreach do : sub'>.
2489 =item B<-blixl=s>, B<--brace-left-and-indent-exclusion-list=s>
2491 Use this parameter to exclude types of block braces for which the B<-bli> flag
2492 applies; see L<Specifying Block Types>.
2494 This might be useful in conjunction with selecting all blocks B<-blil='*'>.
2495 The default setting is B<-blixl=' '>, which does not exclude any blocks.
2497 Note that the two parameters B<-blil> and B<-blixl> control the behavior of
2498 the B<-bli> flag but have no effect unless the B<-bli> flag is set.
2500 =item B<-bar>, B<--opening-brace-always-on-right>
2502 The default style, B<-nbl> places the opening code block brace on a new
2503 line if it does not fit on the same line as the opening keyword, like
2506 if ( $bigwasteofspace1 && $bigwasteofspace2
2507 || $bigwasteofspace3 && $bigwasteofspace4 )
2509 big_waste_of_time();
2512 To force the opening brace to always be on the right, use the B<-bar>
2513 flag. In this case, the above example becomes
2515 if ( $bigwasteofspace1 && $bigwasteofspace2
2516 || $bigwasteofspace3 && $bigwasteofspace4 ) {
2517 big_waste_of_time();
2520 A conflict occurs if both B<-bl> and B<-bar> are specified.
2522 =item B<-otr>, B<--opening-token-right> and related flags
2524 The B<-otr> flag is a hint that perltidy should not place a break between a
2525 comma and an opening token. For example:
2527 # default formatting
2528 push @{ $self->{$module}{$key} },
2530 accno => $ref->{accno},
2531 description => $ref->{description}
2535 push @{ $self->{$module}{$key} }, {
2536 accno => $ref->{accno},
2537 description => $ref->{description}
2540 The flag B<-otr> is actually an abbreviation for three other flags
2541 which can be used to control parens, hash braces, and square brackets
2542 separately if desired:
2544 -opr or --opening-paren-right
2545 -ohbr or --opening-hash-brace-right
2546 -osbr or --opening-square-bracket-right
2548 =item B<-bbhb=n>, B<--break-before-hash-brace=n> and related flags
2550 When a list of items spans multiple lines, the default formatting is to place
2551 the opening brace (or other container token) at the end of the starting line,
2561 This flag can change the default behavior to cause a line break to be placed
2562 before the opening brace according to the value given to the integer B<n>:
2564 -bbhb=0 never break [default]
2565 -bbhb=1 stable: break if the input script had a break
2566 -bbhb=2 break if list is 'complex' (see note below)
2567 -bbhb=3 always break
2580 There are several points to note about this flag:
2586 This parameter only applies if the opening brace is preceded by an '='
2591 This parameter only applies if the contents of the container looks like a list.
2592 The contents need to contain some commas or '=>'s at the next interior level to
2593 be considered a list.
2597 For the B<n=2> option, a list is considered 'complex' if it is part of a nested list
2598 structure which spans multiple lines in the input file.
2602 If multiple opening tokens have been 'welded' together with the B<-wn> parameter, then
2603 this parameter has no effect.
2607 The indentation of the braces will normally be one level of continuation
2608 indentation by default. This can be changed with the parameter
2609 B<-bbhbi=n> in the next section.
2613 Similar flags for controlling parens and square brackets are given in the subsequent section.
2617 =item B<-bbhbi=n>, B<--break-before-hash-brace-and-indent=n>
2619 This flag is a companion to B<-bbhb=n> for controlling the indentation of an opening hash brace
2620 which is placed on a new line by that parameter. The indentation is as follows:
2622 -bbhbi=0 one continuation level [default]
2623 -bbhbi=1 outdent by one continuation level
2624 -bbhbi=2 indent one full indentation level
2628 # perltidy -bbhb=3 -bbhbi=1
2637 # perltidy -bbhb=3 -bbhbi=2
2646 Note that this parameter has no effect unless B<-bbhb=n> is also set.
2648 =item B<-bbsb=n>, B<--break-before-square-bracket=n>
2650 This flag is similar to the flag described above, except it applies to lists contained within square brackets.
2652 -bbsb=0 never break [default]
2653 -bbsb=1 stable: break if the input script had a break
2654 -bbsb=2 break if list is 'complex' (part of nested list structure)
2655 -bbsb=3 always break
2657 =item B<-bbsbi=n>, B<--break-before-square-bracket-and-indent=n>
2659 This flag is a companion to B<-bbsb=n> for controlling the indentation of an opening square bracket
2660 which is placed on a new line by that parameter. The indentation is as follows:
2662 -bbsbi=0 one continuation level [default]
2663 -bbsbi=1 outdent by one continuation level
2664 -bbsbi=2 indent one full indentation level
2666 =item B<-bbp=n>, B<--break-before-paren=n>
2668 This flag is similar to B<-bbhb=n>, described above, except it applies to lists contained within parens.
2670 -bbp=0 never break [default]
2671 -bbp=1 stable: break if the input script had a break
2672 -bpb=2 break if list is 'complex' (part of nested list structure)
2675 =item B<-bbpi=n>, B<--break-before-paren-and-indent=n>
2677 This flag is a companion to B<-bbp=n> for controlling the indentation of an opening paren
2678 which is placed on a new line by that parameter. The indentation is as follows:
2680 -bbpi=0 one continuation level [default]
2681 -bbpi=1 outdent by one continuation level
2682 -bbpi=2 indent one full indentation level
2684 =item B<-wn>, B<--weld-nested-containers>
2686 The B<-wn> flag causes closely nested pairs of opening and closing container
2687 symbols (curly braces, brackets, or parens) to be "welded" together, meaning
2688 that they are treated as if combined into a single unit, with the indentation
2689 of the innermost code reduced to be as if there were just a single container
2694 # default formatting
2704 } } until $x++ > $z;
2706 When this flag is set perltidy makes a preliminary pass through the file and
2707 identifies all nested pairs of containers. To qualify as a nested pair, the
2708 closing container symbols must be immediately adjacent and the opening symbols
2709 must either (1) be adjacent as in the above example, or (2) have an anonymous
2710 sub declaration following an outer opening container symbol which is not a
2711 code block brace, or (3) have an outer opening paren separated from the inner
2712 opening symbol by any single non-container symbol or something that looks like
2713 a function evaluation, as illustrated in the next examples.
2715 Any container symbol may serve as both the inner container of one pair and as
2716 the outer container of an adjacent pair. Consequently, any number of adjacent
2717 opening or closing symbols may join together in weld. For example, here are
2718 three levels of wrapped function calls:
2720 # default formatting
2721 my (@date_time) = Localtime(
2724 $year, $month, $day, $hour, $minute, $second,
2725 '0', $offset, '0', '0'
2731 my (@date_time) = Localtime( Date_to_Time( Add_Delta_DHMS(
2732 $year, $month, $day, $hour, $minute, $second,
2733 '0', $offset, '0', '0'
2736 Notice how the indentation of the inner lines are reduced by two levels in this
2737 case. This example also shows the typical result of this formatting, namely it
2738 is a sandwich consisting of an initial opening layer, a central section of any
2739 complexity forming the "meat" of the sandwich, and a final closing layer. This
2740 predictable structure helps keep the compacted structure readable.
2742 The inner sandwich layer is required to be at least one line thick. If this
2743 cannot be achieved, welding does not occur. This constraint can cause
2744 formatting to take a couple of iterations to stabilize when it is first applied
2745 to a script. The B<-conv> flag can be used to insure that the final format is
2746 achieved in a single run.
2748 Here is an example illustrating a welded container within a welded containers:
2750 # default formatting
2755 $sx * int( $xr->numify() ) & $sy * int( $yr->numify() )
2765 $sx * int( $xr->numify() ) & $sy * int( $yr->numify() )
2770 The welded closing tokens are by default on a separate line but this can be
2771 modified with the B<-vtc=n> flag (described in the next section). For example,
2772 the same example adding B<-vtc=2> is
2774 # perltidy -wn -vtc=2
2777 $sx * int( $xr->numify() ) & $sy * int( $yr->numify() ) ) ),
2780 This format option is quite general but there are some limitations.
2782 One limitation is that any line length limit still applies and can cause long
2783 welded sections to be broken into multiple lines.
2785 Another limitation is that an opening symbol which delimits quoted text cannot
2786 be included in a welded pair. This is because quote delimiters are treated
2787 specially in perltidy.
2789 Finally, the stacking of containers defined by this flag have priority over
2790 any other container stacking flags. This is because any welding is done first.
2792 =item B<-wnxl=s>, B<--weld-nested-exclusion-list>
2794 The B<-wnxl=s> flag provides some control over the types of containers which
2795 can be welded. The B<-wn> flag by default is "greedy" in welding adjacent
2796 containers. If it welds more types of containers than desired, this flag
2797 provides a capability to reduce the amount of welding by specifying a list
2798 of things which should B<not> be welded.
2800 The logic in perltidy to apply this is straightforward. As each container
2801 token is being considered for joining a weld, any exclusion rules are consulted
2802 and used to reject the weld if necessary.
2804 This list is a string with space-separated items. Each item consists of up to
2805 three pieces of information: (1) an optional position, (2) an optional
2806 preceding type, and (3) a container type.
2808 The only required piece of information is a container type, which is one of
2809 '(', '[', '{' or 'q'. The first three of these are container tokens and the
2810 last represents a quoted list. For example the string
2814 means do B<NOT> include square-bracets, braces, or quotes in any welds. The only unspecified
2815 container is '(', so this string means that only welds involving parens will be made.
2817 To illustrate, following welded snippet consists of a chain of three welded
2818 containers with types '(' '[' and 'q':
2828 Even though the qw term uses parens as the quote delimiter, it has a special
2829 type 'q' here. If it appears in a weld it always appears at the end of the
2832 Any of the container types '[', '{', and '(' may be prefixed with a position
2833 indicator which is either '^', to indicate the first token of a welded
2834 sequence, or '.', to indicate an interior token of a welded sequence. (Since
2835 a quoted string 'q' always ends a chain it does need a position indicator).
2837 For example, if we do not want a sequence of welded containers to start with a
2838 square bracket we could use
2842 In the above snippet, there is a square bracket but it does not start the chain,
2843 so the formatting would be unchanged if it were formatted with this restriction.
2845 A third optional item of information which can be given is an alphanumeric
2846 letter which is used to limit the selection further depending on the type of
2847 token immediately before the container. If given, it goes just before the
2848 container symbol. The possible letters are currently 'k', 'K', 'f', 'F',
2849 'w', and 'W', with these meanings:
2851 'k' matches if the previous nonblank token is a perl builtin keyword (such as 'if', 'while'),
2852 'K' matches if 'k' does not, meaning that the previous token is not a keyword.
2853 'f' matches if the previous token is a function other than a keyword.
2854 'F' matches if 'f' does not.
2855 'w' matches if either 'k' or 'f' match.
2856 'W' matches if 'w' does not.
2858 For example, compare
2861 if ( defined( $_Cgi_Query{
2862 $Config{'methods'}{'authentication'}{'remote'}{'cgi'}{'username'}
2867 # perltidy -wn -wnxl='^K( {'
2869 $_Cgi_Query{ $Config{'methods'}{'authentication'}{'remote'}{'cgi'}
2873 The first case does maximum welding. In the second case the leading paren is
2874 retained by the rule (it would have been rejected if preceded by a non-keyword)
2875 but the curly brace is rejected by the rule.
2877 Here are some additional example strings and their meanings:
2879 '^(' - the weld must not start with a paren
2880 '.(' - the second and later tokens may not be parens
2881 '.w(' - the second and later tokens may not keyword or function call parens
2882 '(' - no parens in a weld
2883 '^K(' - exclude a leading paren preceded by a non-keyword
2884 '.k(' - exclude a secondary paren preceded by a keyword
2885 '[ {' - exclude all brackets and braces
2886 '[ ( ^K{' - exclude everything except nested structures like do {{ ... }}
2888 =item B<Vertical tightness> of non-block curly braces, parentheses, and square brackets.
2890 These parameters control what shall be called vertical tightness. Here are the
2897 Opening tokens (except for block braces) are controlled by B<-vt=n>, or
2898 B<--vertical-tightness=n>, where
2900 -vt=0 always break a line after opening token (default).
2901 -vt=1 do not break unless this would produce more than one
2902 step in indentation in a line.
2903 -vt=2 never break a line after opening token
2907 You must also use the B<-lp> flag when you use the B<-vt> flag; the
2908 reason is explained below.
2912 Closing tokens (except for block braces) are controlled by B<-vtc=n>, or
2913 B<--vertical-tightness-closing=n>, where
2915 -vtc=0 always break a line before a closing token (default),
2916 -vtc=1 do not break before a closing token which is followed
2917 by a semicolon or another closing token, and is not in
2919 -vtc=2 never break before a closing token.
2920 -vtc=3 Like -vtc=1 except always break before a closing token
2921 if the corresponding opening token follows an = or =>.
2923 The rules for B<-vtc=1> and B<-vtc=3> are designed to maintain a reasonable
2924 balance between tightness and readability in complex lists.
2928 Different controls may be applied to different token types,
2929 and it is also possible to control block braces; see below.
2933 Finally, please note that these vertical tightness flags are merely
2934 hints to the formatter, and it cannot always follow them. Things which
2935 make it difficult or impossible include comments, blank lines, blocks of
2936 code within a list, and possibly the lack of the B<-lp> parameter.
2937 Also, these flags may be ignored for very small lists (2 or 3 lines in
2942 Here are some examples:
2944 # perltidy -lp -vt=0 -vtc=0
2952 # perltidy -lp -vt=1 -vtc=0
2953 %romanNumerals = ( one => 'I',
2959 # perltidy -lp -vt=1 -vtc=1
2960 %romanNumerals = ( one => 'I',
2980 In the last example for B<-vtc=3>, the opening paren is preceded by an equals
2981 so the closing paren is placed on a new line.
2983 The difference between B<-vt=1> and B<-vt=2> is shown here:
2985 # perltidy -lp -vt=1
2987 mysprintf( "(void)find_threadsv(%s);",
2988 cstring( $threadsv_names[ $op->targ ] )
2992 # perltidy -lp -vt=2
2993 $init->add( mysprintf( "(void)find_threadsv(%s);",
2994 cstring( $threadsv_names[ $op->targ ] )
2998 With B<-vt=1>, the line ending in C<add(> does not combine with the next
2999 line because the next line is not balanced. This can help with
3000 readability, but B<-vt=2> can be used to ignore this rule.
3002 The tightest, and least readable, code is produced with both C<-vt=2> and
3005 # perltidy -lp -vt=2 -vtc=2
3006 $init->add( mysprintf( "(void)find_threadsv(%s);",
3007 cstring( $threadsv_names[ $op->targ ] ) ) );
3009 Notice how the code in all of these examples collapses vertically as
3010 B<-vt> increases, but the indentation remains unchanged. This is
3011 because perltidy implements the B<-vt> parameter by first formatting as
3012 if B<-vt=0>, and then simply overwriting one output line on top of the
3013 next, if possible, to achieve the desired vertical tightness. The
3014 B<-lp> indentation style has been designed to allow this vertical
3015 collapse to occur, which is why it is required for the B<-vt> parameter.
3017 The B<-vt=n> and B<-vtc=n> parameters apply to each type of container
3018 token. If desired, vertical tightness controls can be applied
3019 independently to each of the closing container token types.
3021 The parameters for controlling parentheses are B<-pvt=n> or
3022 B<--paren-vertical-tightness=n>, and B<-pvtc=n> or
3023 B<--paren-vertical-tightness-closing=n>.
3025 Likewise, the parameters for square brackets are B<-sbvt=n> or
3026 B<--square-bracket-vertical-tightness=n>, and B<-sbvtc=n> or
3027 B<--square-bracket-vertical-tightness-closing=n>.
3029 Finally, the parameters for controlling non-code block braces are
3030 B<-bvt=n> or B<--brace-vertical-tightness=n>, and B<-bvtc=n> or
3031 B<--brace-vertical-tightness-closing=n>.
3033 In fact, the parameter B<-vt=n> is actually just an abbreviation for
3034 B<-pvt=n -bvt=n sbvt=n>, and likewise B<-vtc=n> is an abbreviation
3035 for B<-pvtc=n -bvtc=n -sbvtc=n>.
3037 =item B<-bbvt=n> or B<--block-brace-vertical-tightness=n>
3039 The B<-bbvt=n> flag is just like the B<-vt=n> flag but applies
3040 to opening code block braces.
3042 -bbvt=0 break after opening block brace (default).
3043 -bbvt=1 do not break unless this would produce more than one
3044 step in indentation in a line.
3045 -bbvt=2 do not break after opening block brace.
3047 It is necessary to also use either B<-bl> or B<-bli> for this to work,
3048 because, as with other vertical tightness controls, it is implemented by
3049 simply overwriting a line ending with an opening block brace with the
3050 subsequent line. For example:
3052 # perltidy -bli -bbvt=0
3053 if ( open( FILE, "< $File" ) )
3055 while ( $File = <FILE> )
3063 # perltidy -bli -bbvt=1
3064 if ( open( FILE, "< $File" ) )
3065 { while ( $File = <FILE> )
3072 By default this applies to blocks associated with keywords B<if>,
3073 B<elsif>, B<else>, B<unless>, B<for>, B<foreach>, B<sub>, B<while>,
3074 B<until>, and also with a preceding label. This can be changed with
3075 the parameter B<-bbvtl=string>, or
3076 B<--block-brace-vertical-tightness-list=string>, where B<string> is a
3077 space-separated list of block types. For more information on the
3078 possible values of this string, see L<Specifying Block Types>
3080 For example, if we want to just apply this style to C<if>,
3081 C<elsif>, and C<else> blocks, we could use
3082 C<perltidy -bli -bbvt=1 -bbvtl='if elsif else'>.
3084 There is no vertical tightness control for closing block braces; with
3085 one exception they will be placed on separate lines.
3086 The exception is that a cascade of closing block braces may
3087 be stacked on a single line. See B<-scbb>.
3089 =item B<-sot>, B<--stack-opening-tokens> and related flags
3091 The B<-sot> flag tells perltidy to "stack" opening tokens
3092 when possible to avoid lines with isolated opening tokens.
3097 $opt_c = Text::CSV_XS->new(
3106 $opt_c = Text::CSV_XS->new( {
3113 For detailed control of individual closing tokens the following
3114 controls can be used:
3116 -sop or --stack-opening-paren
3117 -sohb or --stack-opening-hash-brace
3118 -sosb or --stack-opening-square-bracket
3119 -sobb or --stack-opening-block-brace
3121 The flag B<-sot> is an abbreviation for B<-sop -sohb -sosb>.
3123 The flag B<-sobb> is an abbreviation for B<-bbvt=2 -bbvtl='*'>. This
3124 will case a cascade of opening block braces to appear on a single line,
3125 although this an uncommon occurrence except in test scripts.
3127 =item B<-sct>, B<--stack-closing-tokens> and related flags
3129 The B<-sct> flag tells perltidy to "stack" closing tokens
3130 when possible to avoid lines with isolated closing tokens.
3135 $opt_c = Text::CSV_XS->new(
3144 $opt_c = Text::CSV_XS->new(
3151 The B<-sct> flag is somewhat similar to the B<-vtc> flags, and in some
3152 cases it can give a similar result. The difference is that the B<-vtc>
3153 flags try to avoid lines with leading opening tokens by "hiding" them at
3154 the end of a previous line, whereas the B<-sct> flag merely tries to
3155 reduce the number of lines with isolated closing tokens by stacking them
3156 but does not try to hide them. For example:
3159 $opt_c = Text::CSV_XS->new(
3163 always_quote => 1, } );
3165 For detailed control of the stacking of individual closing tokens the
3166 following controls can be used:
3168 -scp or --stack-closing-paren
3169 -schb or --stack-closing-hash-brace
3170 -scsb or --stack-closing-square-bracket
3171 -scbb or --stack-closing-block-brace
3173 The flag B<-sct> is an abbreviation for stacking the non-block closing
3174 tokens, B<-scp -schb -scsb>.
3176 Stacking of closing block braces, B<-scbb>, causes a cascade of isolated
3177 closing block braces to be combined into a single line as in the following
3185 push( @lines, "$w1 $w2 $w3 $w4\n" );
3188 To simplify input even further for the case in which both opening and closing
3189 non-block containers are stacked, the flag B<-sac> or B<--stack-all-containers>
3190 is an abbreviation for B<-sot -sct>.
3192 Please note that if both opening and closing tokens are to be stacked, then the
3193 newer flag B<-weld-nested-containers> may be preferable because it insures that
3194 stacking is always done symmetrically. It also removes an extra level of
3195 unnecessary indentation within welded containers. It is able to do this
3196 because it works on formatting globally rather than locally, as the B<-sot> and
3199 =item B<-dnl>, B<--delete-old-newlines>
3201 By default, perltidy first deletes all old line break locations, and then it
3202 looks for good break points to match the desired line length. Use B<-ndnl>
3203 or B<--nodelete-old-newlines> to force perltidy to retain all old line break
3206 =item B<-anl>, B<--add-newlines>
3208 By default, perltidy will add line breaks when necessary to create
3209 continuations of long lines and to improve the script appearance. Use
3210 B<-nanl> or B<--noadd-newlines> to prevent any new line breaks.
3212 This flag does not prevent perltidy from eliminating existing line
3213 breaks; see B<--freeze-newlines> to completely prevent changes to line
3216 =item B<Controlling whether perltidy breaks before or after operators>
3218 Four command line parameters provide some control over whether
3219 a line break should be before or after specific token types.
3220 Two parameters give detailed control:
3222 B<-wba=s> or B<--want-break-after=s>, and
3224 B<-wbb=s> or B<--want-break-before=s>.
3226 These parameters are each followed by a quoted string, B<s>, containing
3227 a list of token types (separated only by spaces). No more than one of each
3228 of these parameters should be specified, because repeating a
3229 command-line parameter always overwrites the previous one before
3230 perltidy ever sees it.
3232 By default, perltidy breaks B<after> these token types:
3233 % + - * / x != == >= <= =~ !~ < > | &
3234 = **= += *= &= <<= &&= -= /= |= >>= ||= //= .= %= ^= x=
3236 And perltidy breaks B<before> these token types by default:
3239 To illustrate, to cause a break after a concatenation operator, C<'.'>,
3240 rather than before it, the command line would be
3244 As another example, the following command would cause a break before
3245 math operators C<'+'>, C<'-'>, C<'/'>, and C<'*'>:
3249 These commands should work well for most of the token types that perltidy uses
3250 (use B<--dump-token-types> for a list). Also try the B<-D> flag on a short
3251 snippet of code and look at the .DEBUG file to see the tokenization. However,
3252 for a few token types there may be conflicts with hardwired logic which cause
3253 unexpected results. One example is curly braces, which should be controlled
3254 with the parameter B<bl> provided for that purpose.
3256 B<WARNING> Be sure to put these tokens in quotes to avoid having them
3257 misinterpreted by your command shell.
3259 Two additional parameters are available which, though they provide no further
3260 capability, can simplify input are:
3262 B<-baao> or B<--break-after-all-operators>,
3264 B<-bbao> or B<--break-before-all-operators>.
3266 The -baao sets the default to be to break after all of the following operators:
3268 % + - * / x != == >= <= =~ !~ < > | &
3269 = **= += *= &= <<= &&= -= /= |= >>= ||= //= .= %= ^= x=
3270 . : ? && || and or err xor
3272 and the B<-bbao> flag sets the default to break before all of these operators.
3273 These can be used to define an initial break preference which can be fine-tuned
3274 with the B<-wba> and B<-wbb> flags. For example, to break before all operators
3275 except an B<=> one could use --bbao -wba='=' rather than listing every
3276 single perl operator except B<=> on a -wbb flag.
3278 =item B<bal=n, --break-after-labels=n>
3280 This flag controls whether or not a line break occurs after a label. There
3281 are three possible valuse for B<n>:
3283 -bal=0 break if there is a break in the input [DEFAULt]
3284 -bal=1 always break after a label
3285 -bal=2 never break after a label
3298 =head2 Controlling List Formatting
3300 Perltidy attempts to format lists of comma-separated values in tables which
3301 look good. Its default algorithms usually work well, but sometimes they don't.
3302 In this case, there are several methods available to control list formatting.
3304 A very simple way to prevent perltidy from changing the line breaks
3305 within a comma-separated list of values is to insert a blank line,
3306 comment, or side-comment anywhere between the opening and closing
3307 parens (or braces or brackets). This causes perltidy to skip
3308 over its list formatting logic. (The reason is that any of
3309 these items put a constraint on line breaks, and perltidy
3310 needs complete control over line breaks within a container to
3311 adjust a list layout). For example, let us consider
3319 The default formatting, which allows a maximum line length of 80,
3320 will flatten this down to one line:
3322 # perltidy (default)
3323 my @list = ( 1, 1, 1, 1, 2, 1, 1, 3, 3, 1, 1, 4, 6, 4, 1, );
3325 This formatting loses the nice structure. If we place a side comment anywhere
3326 between the opening and closing parens, the original line break points are
3327 retained. For example,
3330 1, # a side comment forces the original line breakpoints to be kept
3337 The side comment can be a single hash symbol without any text.
3338 We could achieve the same result with a blank line or full comment
3339 anywhere between the opening and closing parens. Vertical alignment
3340 of the list items will still occur if possible.
3342 For another possibility see
3343 the -fs flag in L<Skipping Selected Sections of Code>.
3347 =item B<-boc>, B<--break-at-old-comma-breakpoints>
3349 The B<-boc> flag is another way to prevent comma-separated lists from being
3350 reformatted. Using B<-boc> on the above example, plus additional flags to retain
3351 the original style, yields
3353 # perltidy -boc -lp -pt=2 -vt=1 -vtc=1
3360 A disadvantage of this flag compared to the methods discussed above is that all
3361 tables in the file must already be nicely formatted.
3363 =item B<-mft=n>, B<--maximum-fields-per-table=n>
3365 If the computed number of fields for any table exceeds B<n>, then it
3366 will be reduced to B<n>. The default value for B<n> is a large number,
3367 40. While this value should probably be left unchanged as a general
3368 rule, it might be used on a small section of code to force a list to
3369 have a particular number of fields per line, and then either the B<-boc>
3370 flag could be used to retain this formatting, or a single comment could
3371 be introduced somewhere to freeze the formatting in future applications
3384 =item B<-cab=n>, B<--comma-arrow-breakpoints=n>
3386 A comma which follows a comma arrow, '=>', is given special
3387 consideration. In a long list, it is common to break at all such
3388 commas. This parameter can be used to control how perltidy breaks at
3389 these commas. (However, it will have no effect if old comma breaks are
3390 being forced because B<-boc> is used). The possible values of B<n> are:
3392 n=0 break at all commas after =>
3393 n=1 stable: break at all commas after => if container is open,
3394 EXCEPT FOR one-line containers
3395 n=2 break at all commas after =>, BUT try to form the maximum
3396 one-line container lengths
3397 n=3 do not treat commas after => specially at all
3398 n=4 break everything: like n=0 but ALSO break a short container with
3399 a => not followed by a comma when -vt=0 is used
3400 n=5 stable: like n=1 but ALSO break at open one-line containers when
3401 -vt=0 is used (default)
3403 For example, given the following single line, perltidy by default will
3404 not add any line breaks because it would break the existing one-line
3407 bless { B => $B, Root => $Root } => $package;
3409 Using B<-cab=0> will force a break after each comma-arrow item:
3417 If perltidy is subsequently run with this container broken, then by
3418 default it will break after each '=>' because the container is now
3419 broken. To reform a one-line container, the parameter B<-cab=2> could
3422 The flag B<-cab=3> can be used to prevent these commas from being
3423 treated specially. In this case, an item such as "01" => 31 is
3424 treated as a single item in a table. The number of fields in this table
3425 will be determined by the same rules that are used for any other table.
3430 "01" => 31, "02" => 29, "03" => 31, "04" => 30,
3431 "05" => 31, "06" => 30, "07" => 31, "08" => 31,
3432 "09" => 30, "10" => 31, "11" => 30, "12" => 31
3437 =head2 Retaining or Ignoring Existing Line Breaks
3439 Several additional parameters are available for controlling the extent
3440 to which line breaks in the input script influence the output script.
3441 In most cases, the default parameter values are set so that, if a choice
3442 is possible, the output style follows the input style. For example, if
3443 a short logical container is broken in the input script, then the
3444 default behavior is for it to remain broken in the output script.
3446 Most of the parameters in this section would only be required for a
3447 one-time conversion of a script from short container lengths to longer
3448 container lengths. The opposite effect, of converting long container
3449 lengths to shorter lengths, can be obtained by temporarily using a short
3450 maximum line length.
3454 =item B<-bol>, B<--break-at-old-logical-breakpoints>
3456 By default, if a logical expression is broken at a C<&&>, C<||>, C<and>,
3457 or C<or>, then the container will remain broken. Also, breaks
3458 at internal keywords C<if> and C<unless> will normally be retained.
3459 To prevent this, and thus form longer lines, use B<-nbol>.
3461 Please note that this flag does not duplicate old logical breakpoints. They
3462 are merely used as a hint with this flag that a statement should remain
3463 broken. Without this flag, perltidy will normally try to combine relatively
3464 short expressions into a single line.
3466 For example, given this snippet:
3468 return unless $cmd = $cmd || ($dot
3469 && $Last_Shell) || &prompt('|');
3471 # perltidy -bol [default]
3479 return unless $cmd = $cmd || ( $dot && $Last_Shell ) || &prompt('|');
3481 =item B<-bom>, B<--break-at-old-method-breakpoints>
3483 By default, a method call arrow C<-E<gt>> is considered a candidate for
3484 a breakpoint, but method chains will fill to the line width before a break is
3485 considered. With B<-bom>, breaks before the arrow are preserved, so if you
3486 have preformatted a method chain:
3489 ->related_resultset('CDs')
3490 ->related_resultset('Tracks')
3492 'track.id' => {-ident => 'none_search.id'},
3495 It will B<keep> these breaks, rather than become this:
3497 my $q = $rs->related_resultset('CDs')->related_resultset('Tracks')->search({
3498 'track.id' => {-ident => 'none_search.id'},
3501 This flag will also look for and keep a 'cuddled' style of calls,
3502 in which lines begin with a closing paren followed by a call arrow,
3506 my $q = $rs->related_resultset(
3508 )->related_resultset(
3511 'track.id' => { -ident => 'none_search.id' },
3514 You may want to include the B<-weld-nested-containers> flag in this case to keep
3515 nested braces and parens together, as in the last line.
3517 =item B<-bos>, B<--break-at-old-semicolon-breakpoints>
3519 Semicolons are normally placed at the end of a statement. This means that formatted lines do not normally begin with semicolons. If the input stream has some lines which begin with semicolons, these can be retained by setting this flag. For example, consider
3520 the following two-line input snippet:
3522 $z = sqrt($x**2 + $y**2)
3525 The default formatting will be:
3527 $z = sqrt( $x**2 + $y**2 );
3529 The result using B<perltidy -bos> keeps the isolated semicolon:
3531 $z = sqrt( $x**2 + $y**2 )
3534 The default is not to do this, B<-nbos>.
3537 =item B<-bok>, B<--break-at-old-keyword-breakpoints>
3539 By default, perltidy will retain a breakpoint before keywords which may
3540 return lists, such as C<sort> and <map>. This allows chains of these
3541 operators to be displayed one per line. Use B<-nbok> to prevent
3542 retaining these breakpoints.
3544 =item B<-bot>, B<--break-at-old-ternary-breakpoints>
3546 By default, if a conditional (ternary) operator is broken at a C<:>,
3547 then it will remain broken. To prevent this, and thereby
3548 form longer lines, use B<-nbot>.
3550 =item B<-boa>, B<--break-at-old-attribute-breakpoints>
3552 By default, if an attribute list is broken at a C<:> in the source file, then
3553 it will remain broken. For example, given the following code, the line breaks
3554 at the ':'s will be retained:
3559 : Get('Name' => 'foo') : Set('Name');
3561 If the attributes are on a single line in the source code then they will remain
3562 on a single line if possible.
3564 To prevent this, and thereby always form longer lines, use B<-nboa>.
3566 =item B<Keeping old breakpoints at specific token types>
3568 It is possible to override the choice of line breaks made by perltidy, and
3569 force it to follow certain line breaks in the input stream, with these two
3572 B<-kbb=s> or B<--keep-old-breakpoints-before=s>, and
3574 B<-kba=s> or B<--keep-old-breakpoints-after=s>
3576 These parameters are each followed by a quoted string, B<s>, containing
3577 a list of token types (separated only by spaces). No more than one of each
3578 of these parameters should be specified, because repeating a
3579 command-line parameter always overwrites the previous one before
3580 perltidy ever sees it.
3582 For example, -kbb='=>' means that if an input line begins with a '=>' then the
3583 output script should also have a line break before that token.
3585 For example, given the script:
3590 my ( $self, $x, $y ) = ( shift, @_ );
3594 # perltidy [default]
3595 method 'foo' => [ Int, Int ] => sub {
3596 my ( $self, $x, $y ) = ( shift, @_ );
3600 # perltidy -kbb='=>'
3604 my ( $self, $x, $y ) = ( shift, @_ );
3608 For the container tokens '{', '[' and '(' and, their closing counterparts, use the token symbol. Thus,
3609 the command to keep a break after all opening parens is:
3613 It is possible to be more specific in matching parentheses by preceding them
3614 with a letter. The possible letters are 'k', 'K', 'f', 'F', 'w', and 'W', with
3615 these meanings (these are the same as used in the
3616 B<--weld-nested-exclusion-list> and B<--line-up-parentheses-exclusion-list>
3619 'k' matches if the previous nonblank token is a perl builtin keyword (such as 'if', 'while'),
3620 'K' matches if 'k' does not, meaning that the previous token is not a keyword.
3621 'f' matches if the previous token is a function other than a keyword.
3622 'F' matches if 'f' does not.
3623 'w' matches if either 'k' or 'f' match.
3624 'W' matches if 'w' does not.
3626 So for example the the following parameter will keep breaks after opening function call
3631 B<NOTE>: To match all opening curly braces, and no other opening tokens, please
3632 prefix the brace it with an asterisk, like this: '*{'. Otherwise a warning
3633 message will occur. This is necessary to avoid problems while the input scheme
3634 is being updated and generalized. A single bare curly brace previously matched
3635 all container tokens, and tentatively still does. Likewise, to match all
3636 closing curly braces, and no other closing tokens, use '*}'.
3638 =item B<-iob>, B<--ignore-old-breakpoints>
3640 Use this flag to tell perltidy to ignore existing line breaks to the
3641 maximum extent possible. This will tend to produce the longest possible
3642 containers, regardless of type, which do not exceed the line length
3643 limit. But please note that this parameter has priority over all
3644 other parameters requesting that certain old breakpoints be kept.
3646 To illustrate, consider the following input text:
3650 default => sub { [] },
3653 The default formatting will keep the container broken, giving
3655 # perltidy [default]
3658 default => sub { [] },
3661 If old breakpoints are ignored, the list will be flattened:
3664 has subcmds => ( is => 'ro', default => sub { [] }, );
3666 Besides flattening lists, this parameter also applies to lines broken
3667 at certain logical breakpoints such as 'if' and 'or'.
3669 Even if this is parameter is not used globally, it provides a convenient way to
3670 flatten selected lists from within an editor.
3672 =item B<-kis>, B<--keep-interior-semicolons>
3674 Use the B<-kis> flag to prevent breaking at a semicolon if
3675 there was no break there in the input file. Normally
3676 perltidy places a newline after each semicolon which
3677 terminates a statement unless several statements are
3678 contained within a one-line brace block. To illustrate,
3679 consider the following input lines:
3681 dbmclose(%verb_delim); undef %verb_delim;
3682 dbmclose(%expanded); undef %expanded;
3684 The default is to break after each statement, giving
3686 dbmclose(%verb_delim);
3688 dbmclose(%expanded);
3691 With B<perltidy -kis> the multiple statements are retained:
3693 dbmclose(%verb_delim); undef %verb_delim;
3694 dbmclose(%expanded); undef %expanded;
3696 The statements are still subject to the specified value
3697 of B<maximum-line-length> and will be broken if this
3698 maximum is exceeded.
3702 =head2 Blank Line Control
3704 Blank lines can improve the readability of a script if they are carefully
3705 placed. Perltidy has several commands for controlling the insertion,
3706 retention, and removal of blank lines.
3710 =item B<-fbl>, B<--freeze-blank-lines>
3712 Set B<-fbl> if you want to the blank lines in your script to
3713 remain exactly as they are. The rest of the parameters in
3714 this section may then be ignored. (Note: setting the B<-fbl> flag
3715 is equivalent to setting B<-mbl=0> and B<-kbl=2>).
3717 =item B<-bbc>, B<--blanks-before-comments>
3719 A blank line will be introduced before a full-line comment. This is the
3720 default. Use B<-nbbc> or B<--noblanks-before-comments> to prevent
3721 such blank lines from being introduced.
3723 =item B<-blbs=n>, B<--blank-lines-before-subs=n>
3725 The parameter B<-blbs=n> requests that least B<n> blank lines precede a sub
3726 definition which does not follow a comment and which is more than one-line
3727 long. The default is <-blbs=1>. B<BEGIN> and B<END> blocks are included.
3729 The requested number of blanks statement will be inserted regardless of the
3730 value of B<--maximum-consecutive-blank-lines=n> (B<-mbl=n>) with the exception
3731 that if B<-mbl=0> then no blanks will be output.
3733 This parameter interacts with the value B<k> of the parameter B<--maximum-consecutive-blank-lines=k> (B<-mbl=k>) as follows:
3735 1. If B<-mbl=0> then no blanks will be output. This allows all blanks to be suppressed with a single parameter. Otherwise,
3737 2. If the number of old blank lines in the script is less than B<n> then
3738 additional blanks will be inserted to make the total B<n> regardless of the
3741 3. If the number of old blank lines in the script equals or exceeds B<n> then
3742 this parameter has no effect, however the total will not exceed
3743 value specified on the B<-mbl=k> flag.
3746 =item B<-blbp=n>, B<--blank-lines-before-packages=n>
3748 The parameter B<-blbp=n> requests that least B<n> blank lines precede a package
3749 which does not follow a comment. The default is B<-blbp=1>.
3751 This parameter interacts with the value B<k> of the parameter
3752 B<--maximum-consecutive-blank-lines=k> (B<-mbl=k>) in the same way as described
3753 for the previous item B<-blbs=n>.
3756 =item B<-bbs>, B<--blanks-before-subs>
3758 For compatibility with previous versions, B<-bbs> or B<--blanks-before-subs>
3759 is equivalent to F<-blbp=1> and F<-blbs=1>.
3761 Likewise, B<-nbbs> or B<--noblanks-before-subs>
3762 is equivalent to F<-blbp=0> and F<-blbs=0>.
3764 =item B<-bbb>, B<--blanks-before-blocks>
3766 A blank line will be introduced before blocks of coding delimited by
3767 B<for>, B<foreach>, B<while>, B<until>, and B<if>, B<unless>, in the following
3774 The block is not preceded by a comment.
3778 The block is not a one-line block.
3782 The number of consecutive non-blank lines at the current indentation depth is at least B<-lbl>
3787 This is the default. The intention of this option is to introduce
3788 some space within dense coding.
3789 This is negated with B<-nbbb> or B<--noblanks-before-blocks>.
3791 =item B<-lbl=n> B<--long-block-line-count=n>
3793 This controls how often perltidy is allowed to add blank lines before
3794 certain block types (see previous section). The default is 8. Entering
3795 a value of B<0> is equivalent to entering a very large number.
3797 =item B<-blao=i> or B<--blank-lines-after-opening-block=i>
3799 This control places a minimum of B<i> blank lines B<after> a line which B<ends>
3800 with an opening block brace of a specified type. By default, this only applies
3801 to the block of a named B<sub>, but this can be changed (see B<-blaol> below).
3802 The default is not to do this (B<i=0>).
3804 Please see the note below on using the B<-blao> and B<-blbc> options.
3806 =item B<-blbc=i> or B<--blank-lines-before-closing-block=i>
3808 This control places a minimum of B<i> blank lines B<before> a line which
3809 B<begins> with a closing block brace of a specified type. By default, this
3810 only applies to the block of a named B<sub>, but this can be changed (see
3811 B<-blbcl> below). The default is not to do this (B<i=0>).
3813 =item B<-blaol=s> or B<--blank-lines-after-opening-block-list=s>
3815 The parameter B<s> is a list of block type keywords to which the flag B<-blao>
3816 should apply. The section L<"Specifying Block Types"> explains how to list
3819 =item B<-blbcl=s> or B<--blank-lines-before-closing-block-list=s>
3821 This parameter is a list of block type keywords to which the flag B<-blbc>
3822 should apply. The section L<"Specifying Block Types"> explains how to list
3825 =item B<Note on using the> B<-blao> and B<-blbc> options.
3827 These blank line controls introduce a certain minimum number of blank lines in
3828 the text, but the final number of blank lines may be greater, depending on
3829 values of the other blank line controls and the number of old blank lines. A
3830 consequence is that introducing blank lines with these and other controls
3831 cannot be exactly undone, so some experimentation with these controls is
3832 recommended before using them.
3834 For example, suppose that for some reason we decide to introduce one blank
3835 space at the beginning and ending of all blocks. We could do
3838 perltidy -blao=2 -blbc=2 -blaol='*' -blbcl='*' filename
3840 Now suppose the script continues to be developed, but at some later date we
3841 decide we don't want these spaces after all. We might expect that running with
3842 the flags B<-blao=0> and B<-blbc=0> will undo them. However, by default
3843 perltidy retains single blank lines, so the blank lines remain.
3845 We can easily fix this by telling perltidy to ignore old blank lines by
3846 including the added parameter B<-kbl=0> and rerunning. Then the unwanted blank
3847 lines will be gone. However, this will cause all old blank lines to be
3848 ignored, perhaps even some that were added by hand to improve formatting. So
3849 please be cautious when using these parameters.
3851 =item B<-mbl=n> B<--maximum-consecutive-blank-lines=n>
3853 This parameter specifies the maximum number of consecutive blank lines which
3854 will be output within code sections of a script. The default is n=1. If the
3855 input file has more than n consecutive blank lines, the number will be reduced
3856 to n except as noted above for the B<-blbp> and B<-blbs> parameters. If B<n=0>
3857 then no blank lines will be output (unless all old blank lines are retained
3858 with the B<-kbl=2> flag of the next section).
3860 This flag obviously does not apply to pod sections,
3861 here-documents, and quotes.
3863 =item B<-kbl=n>, B<--keep-old-blank-lines=n>
3865 The B<-kbl=n> flag gives you control over how your existing blank lines are
3868 The possible values of B<n> are:
3870 n=0 ignore all old blank lines
3871 n=1 stable: keep old blanks, but limited by the value of the B<-mbl=n> flag
3872 n=2 keep all old blank lines, regardless of the value of the B<-mbl=n> flag
3874 The default is B<n=1>.
3876 =item B<-sob>, B<--swallow-optional-blank-lines>
3878 This is equivalent to B<kbl=0> and is included for compatibility with
3881 =item B<-nsob>, B<--noswallow-optional-blank-lines>
3883 This is equivalent to B<kbl=1> and is included for compatibility with
3888 B<Controls for blank lines around lines of consecutive keywords>
3890 The parameters in this section provide some control over the placement of blank
3891 lines within and around groups of statements beginning with selected keywords.
3892 These blank lines are called here B<keyword group blanks>, and all of the
3893 parameters begin with B<--keyword-group-blanks*>, or B<-kgb*> for short. The
3894 default settings do not employ these controls but they can be enabled with the
3895 following parameters:
3897 B<-kgbl=s> or B<--keyword-group-blanks-list=s>; B<s> is a quoted string of keywords
3899 B<-kgbs=s> or B<--keyword-group-blanks-size=s>; B<s> gives the number of keywords required to form a group.
3901 B<-kgbb=n> or B<--keyword-group-blanks-before=n>; B<n> = (0, 1, or 2) controls a leading blank
3903 B<-kgba=n> or B<--keyword-group-blanks-after=n>; B<n> = (0, 1, or 2) controls a trailing blank
3905 B<-kgbi> or B<--keyword-group-blanks-inside> is a switch for adding blanks between subgroups
3907 B<-kgbd> or B<--keyword-group-blanks-delete> is a switch for removing initial blank lines between keywords
3909 B<-kgbr=n> or B<--keyword-group-blanks-repeat-count=n> can limit the number of times this logic is applied
3911 In addition, the following abbreviations are available to for simplified usage:
3913 B<-kgb> or B<--keyword-group-blanks> is short for B<-kgbb=2 -kgba=2 kgbi>
3915 B<-nkgb> or B<--nokeyword-group-blanks>, is short for B<-kgbb=1 -kgba=1 nkgbi>
3917 Before describing the meaning of the parameters in detail let us look at an
3918 example which is formatted with default parameter settings.
3920 print "Entering test 2\n";
3922 use Encode qw(from_to encode decode
3923 encode_utf8 decode_utf8
3924 find_encoding is_utf8);
3925 use charnames qw(greek);
3926 my @encodings = grep( /iso-?8859/, Encode::encodings() );
3927 my @character_set = ( '0' .. '9', 'A' .. 'Z', 'a' .. 'z' );
3928 my @source = qw(ascii iso8859-1 cp1250);
3929 my @destiny = qw(cp1047 cp37 posix-bc);
3930 my @ebcdic_sets = qw(cp1047 cp37 posix-bc);
3931 my $str = join( '', map( chr($_), 0x20 .. 0x7E ) );
3932 return unless ($str);
3934 using B<perltidy -kgb> gives:
3936 print "Entering test 2\n";
3937 <----------this blank controlled by -kgbb
3939 use Encode qw(from_to encode decode
3940 encode_utf8 decode_utf8
3941 find_encoding is_utf8);
3942 use charnames qw(greek);
3943 <---------this blank controlled by -kgbi
3944 my @encodings = grep( /iso-?8859/, Encode::encodings() );
3945 my @character_set = ( '0' .. '9', 'A' .. 'Z', 'a' .. 'z' );
3946 my @source = qw(ascii iso8859-1 cp1250);
3947 my @destiny = qw(cp1047 cp37 posix-bc);
3948 my @ebcdic_sets = qw(cp1047 cp37 posix-bc);
3949 my $str = join( '', map( chr($_), 0x20 .. 0x7E ) );
3950 <----------this blank controlled by -kgba
3951 return unless ($str);
3953 Blank lines have been introduced around the B<my> and B<use> sequences. What
3954 happened is that the default keyword list includes B<my> and B<use> but not
3955 B<print> and B<return>. So a continuous sequence of nine B<my> and B<use>
3956 statements was located. This number exceeds the default threshold of five, so
3957 blanks were placed before and after the entire group. Then, since there was
3958 also a subsequence of six B<my> lines, a blank line was introduced to separate
3961 Finer control over blank placement can be achieved by using the individual
3962 parameters rather than the B<-kgb> flag. The individual controls are as follows.
3964 B<-kgbl=s> or B<--keyword-group-blanks-list=s>, where B<s> is a quoted string,
3965 defines the set of keywords which will be formed into groups. The string is a
3966 space separated list of keywords. The default set is B<s="use require local
3967 our my">, but any list of keywords may be used. Comment lines may also be included in a keyword group, even though they are not keywords. To include ordinary block comments, include the symbol B<BC>. To include static block comments (which normally begin with '##'), include the symbol B<SBC>.
3969 B<-kgbs=s> or B<--keyword-group-blanks-size=s>, where B<s> is a string
3970 describing the number of consecutive keyword statements forming a group (Note:
3971 statements separated by blank lines in the input file are considered
3972 consecutive for purposes of this count). If B<s> is an integer then it is the
3973 minimum number required for a group. A maximum value may also be given with
3974 the format B<s=min.max>, where B<min> is the minimum number and B<max> is the
3975 maximum number, and the min and max values are separated by one or more dots.
3976 No groups will be found if the maximum is less than the minimum. The maximum
3977 is unlimited if not given. The default is B<s=5>. Some examples:
3979 s min max number for group
3980 3 3 unlimited 3 or more
3985 There is no really good default value for this parameter. If it is set too
3986 small, then an excessive number of blank lines may be generated. However, some
3987 users may prefer reducing the value somewhat below the default, perhaps to
3990 B<-kgbb=n> or B<--keyword-group-blanks-before=n> specifies whether
3991 a blank should appear before the first line of the group, as follows:
3993 n=0 => (delete) an existing blank line will be removed
3994 n=1 => (stable) no change to the input file is made [DEFAULT]
3995 n=2 => (insert) a blank line is introduced if possible
3997 B<-kgba=n> or B<--keyword-group-blanks-after=n> likewise specifies
3998 whether a blank should appear after the last line of the group, using the same
3999 scheme (0=delete, 1=stable, 2=insert).
4001 B<-kgbi> or B<--keyword-group-blanks-inside> controls
4002 the insertion of blank lines between the first and last statement of the entire
4003 group. If there is a continuous run of a single statement type with more than
4004 the minimum threshold number (as specified with B<-kgbs=s>) then this
4005 switch causes a blank line be inserted between this
4006 subgroup and the others. In the example above this happened between the
4007 B<use> and B<my> statements.
4009 B<-kgbd> or B<--keyword-group-blanks-delete> controls the deletion of any
4010 blank lines that exist in the the group when it is first scanned. When
4011 statements are initially scanned, any existing blank lines are included in the
4012 collection. Any such orignial blank lines will be deleted before any other
4013 insertions are made when the parameter B<-kgbd> is set. The default is not to
4016 B<-kgbr=n> or B<--keyword-group-blanks-repeat-count=n> specifies B<n>, the
4017 maximum number of times this logic will be applied to any file. The special
4018 value B<n=0> is the same as n=infinity which means it will be applied to an
4019 entire script [Default]. A value B<n=1> could be used to make it apply just
4020 one time for example. This might be useful for adjusting just the B<use>
4021 statements in the top part of a module for example.
4023 B<-kgb> or B<--keyword-group-blanks> is an abbreviation equivalent to setting
4024 B<-kgbb=1 -kgba=1 -kgbi>. This turns on keyword group formatting with a set of
4027 B<-nkgb> or B<--nokeyword-group-blanks> is equivalent to B<-kgbb=0 -kgba
4028 nkgbi>. This flag turns off keyword group blank lines and is the default
4031 Here are a few notes about the functioning of this technique.
4037 These parameters are probably more useful as part of a major code reformatting
4038 operation rather than as a routine formatting operation.
4040 In particular, note that deleting old blank lines with B<-kgbd> is an
4041 irreversible operation so it should be applied with care. Existing blank lines
4042 may be serving an important role in controlling vertical alignment.
4046 Conflicts which arise among these B<kgb*> parameters and other blank line
4047 controls are generally resolved by producing the maximum number of blank lines
4048 implied by any parameter.
4050 For example, if the flags B<--freeze-blank-lines>, or
4051 B<--keep-old-blank-lines=2>, are set, then they have priority over any blank
4052 line deletion implied by the B<-kgb> flags of this section, so no blank lines
4055 For another example, if a keyword group ends at a B<sub> and the flag B<kgba=0> requests no blank line there, but we also have B<--blank-lines-before-subs=2>, then two blank lines will still be introduced before the sub.
4059 The introduction of blank lines does not occur if it would conflict with other
4060 input controls or code validity. For example, a blank line will not be placed
4061 within a here-doc or within a section of code marked with format skipping
4062 comments. And in general, a blank line will only be introduced at the end of a
4063 group if the next statement is a line of code.
4067 The count which is used to determine the group size is not the number of lines
4068 but rather the total number of keywords which are found. Individual statements
4069 with a certain leading keyword may continue on multiple lines, but if any of
4070 these lines is nested more than one level deep then that group will be ended.
4074 The search for groups of lines with similar leading keywords is based on the
4075 input source, not the final formatted source. Consequently, if the source code
4076 is badly formatted, it would be best to make a first formatting pass without
4083 A style refers to a convenient collection of existing parameters.
4087 =item B<-gnu>, B<--gnu-style>
4089 B<-gnu> gives an approximation to the GNU Coding Standards (which do
4090 not apply to perl) as they are sometimes implemented. At present, this
4091 style overrides the default style with the following parameters:
4093 -lp -bl -noll -pt=2 -bt=2 -sbt=2 -icp
4095 To use this style with B<-xlp> instead of B<-lp> use B<-gnu -xlp>.
4097 =item B<-pbp>, B<--perl-best-practices>
4099 B<-pbp> is an abbreviation for the parameters in the book B<Perl Best Practices>
4102 -l=78 -i=4 -ci=4 -st -se -vt=2 -cti=0 -pt=1 -bt=1 -sbt=1 -bbt=1 -nsfs -nolq
4103 -wbb="% + - * / x != == >= <= =~ !~ < > | & =
4104 **= += *= &= <<= &&= -= /= |= >>= ||= //= .= %= ^= x="
4106 Please note that this parameter set includes -st and -se flags, which make
4107 perltidy act as a filter on one file only. These can be overridden by placing
4108 B<-nst> and/or B<-nse> after the -pbp parameter.
4110 Also note that the value of continuation indentation, -ci=4, is equal to the
4111 value of the full indentation, -i=4. It is recommended that the either (1) the
4112 parameter B<-ci=2> be used instead, or the flag B<-xci> be set. This will help
4113 show structure, particularly when there are ternary statements. The following
4114 snippet illustrates these options.
4119 : $type eq 'item' ? "the $section entry"
4120 : "the section on $section"
4124 ? ( $section ? ' in ' : '' ) . "the $page$page_ext manpage"
4125 : ' elsewhere in this document'
4128 # perltidy -pbp -ci=2
4131 : $type eq 'item' ? "the $section entry"
4132 : "the section on $section"
4136 ? ( $section ? ' in ' : '' ) . "the $page$page_ext manpage"
4137 : ' elsewhere in this document'
4140 # perltidy -pbp -xci
4143 : $type eq 'item' ? "the $section entry"
4144 : "the section on $section"
4147 ? ( $section ? ' in ' : '' ) . "the $page$page_ext manpage"
4148 : ' elsewhere in this document'
4151 The B<-xci> flag was developed after the B<-pbp> parameters were published so you need
4152 to include it separately.
4154 =item B<One-line blocks>
4156 There are a few points to note regarding one-line blocks. A one-line
4157 block is something like this,
4159 if ( -e $file ) { print "'$file' exists\n" }
4161 where the contents within the curly braces is short enough to fit
4164 With few exceptions, perltidy retains existing one-line blocks, if it
4165 is possible within the line-length constraint, but it does not attempt
4166 to form new ones. In other words, perltidy will try to follow the
4167 one-line block style of the input file.
4169 If an existing one-line block is longer than the maximum line length,
4170 however, it will be broken into multiple lines. When this happens, perltidy
4171 checks for and adds any optional terminating semicolon (unless the B<-nasc>
4172 option is used) if the block is a code block.
4174 The main exception is that perltidy will attempt to form new one-line
4175 blocks following the keywords C<map>, C<eval>, and C<sort>, because
4176 these code blocks are often small and most clearly displayed in a single
4179 One-line block rules can conflict with the cuddled-else option. When
4180 the cuddled-else option is used, perltidy retains existing one-line
4181 blocks, even if they do not obey cuddled-else formatting.
4183 Occasionally, when one-line blocks get broken because they exceed the
4184 available line length, the formatting will violate the requested brace style.
4185 If this happens, reformatting the script a second time should correct
4188 Sometimes it might be desirable to convert a script to have one-line blocks
4189 whenever possible. Although there is currently no flag for this, a simple
4190 workaround is to execute perltidy twice, once with the flag B<-noadd-newlines>
4191 and then once again with normal parameters, like this:
4193 cat infile | perltidy -nanl | perltidy >outfile
4195 When executed on this snippet
4198 die "failed to execute: $!\n";
4201 print "Had enough.\n";
4202 die "failed to execute: $!\n";
4207 if ( $? == -1 ) { die "failed to execute: $!\n"; }
4209 print "Had enough.\n";
4210 die "failed to execute: $!\n";
4213 This shows that blocks with a single statement become one-line blocks.
4215 =item B<-olbs=n>, B<--one-line-block-semicolons=n>
4217 This flag controls the placement of semicolons at the end of one-line blocks.
4218 Semicolons are optional before a closing block brace, and frequently they are
4219 omitted at the end of a one-line block containing just a single statement.
4220 By default, perltidy follows the input file regarding these semicolons,
4221 but this behavior can be controlled by this flag. The values of n are:
4223 n=0 remove terminal semicolons in one-line blocks having a single statement
4224 n=1 stable; keep input file placement of terminal semicolons [DEFAULT ]
4225 n=2 add terminal semicolons in all one-line blocks
4227 Note that the B<n=2> option has no effect if adding semicolons is prohibited
4228 with the B<-nasc> flag. Also not that while B<n=2> adds missing semicolons to
4229 all one-line blocks, regardless of complexity, the B<n=0> option only removes
4230 ending semicolons which terminate one-line blocks containing just one
4231 semicolon. So these two options are not exact inverses.
4233 =item B<-olbn=n>, B<--one-line-block-nesting=n>
4235 Nested one-line blocks are lines with code blocks which themselves contain code
4236 blocks. For example, the following line is a nested one-line block.
4238 foreach (@list) { if ($_ eq $asked_for) { last } ++$found }
4240 The default behavior is to break such lines into multiple lines, but this
4241 behavior can be controlled with this flag. The values of n are:
4243 n=0 break nested one-line blocks into multiple lines [DEFAULT]
4244 n=1 stable: keep existing nested-one line blocks intact
4246 For the above example, the default formatting (B<-olbn=0>) is
4249 if ( $_ eq $asked_for ) { last }
4253 If the parameter B<-olbn=1> is given, then the line will be left intact if it
4254 is a single line in the source, or it will be broken into multiple lines if it
4255 is broken in multiple lines in the source.
4261 =head2 Controlling Vertical Alignment
4263 Vertical alignment refers to lining up certain symbols in a list of consecutive
4264 similar lines to improve readability. For example, the "fat commas" are
4265 aligned in the following statement:
4268 PeerAddr => join( ".", @port[ 0 .. 3 ] ),
4269 PeerPort => $port[4] * 256 + $port[5],
4273 Vertical alignment can be completely turned off using the B<-novalign> flag
4274 mentioned below. However, vertical alignment can be forced to
4275 stop and restart by selectively introducing blank lines. For example, a blank
4276 has been inserted in the following code to keep somewhat similar things
4280 'format' => [ 'tidy', 'html', 'user' ],
4281 'output-line-ending' => [ 'dos', 'win', 'mac', 'unix' ],
4282 'character-encoding' => [ 'none', 'utf8' ],
4284 'block-brace-tightness' => [ 0, 2 ],
4285 'brace-tightness' => [ 0, 2 ],
4286 'paren-tightness' => [ 0, 2 ],
4287 'square-bracket-tightness' => [ 0, 2 ],
4290 Vertical alignment is implemented by locally increasing an existing blank space
4291 to produce alignment with an adjacent line. It cannot occur if there is no
4292 blank space to increase. So if a particular space is removed by one of the
4293 existing controls then vertical alignment cannot occur. Likewise, if a space is
4294 added with one of the controls, then vertical alignment might occur.
4298 # perltidy -nwls='=>'
4300 PeerAddr=> join( ".", @port[ 0 .. 3 ] ),
4301 PeerPort=> $port[4] * 256 + $port[5],
4307 =item B<Completely turning off vertical alignment with -novalign>
4309 The default is to use vertical alignment, but bertical alignment can be
4310 completely turned of with the B<-novalign> flag.
4312 A lower level of control of vertical alignment is possible with three parameters
4313 B<-vc>, B<-vsc>, and B<-vbc>. These independently control alignment
4314 of code, side comments and block comments. They are described in the
4317 The parameter B<-valign> is in fact an alias for B<-vc -vsc -vbc>, and its
4318 negative B<-novalign> is an alias for B<-nvc -nvsc -nvbc>.
4320 =item B<Controlling code alignment with --valign-code or -vc>
4322 The B<-vc> flag enables alignment of code symbols such as B<=>. The default is B<-vc>.
4323 For detailed control of which symbols to align, see the B<-valign-exclude-list> parameter
4326 =item B<Controlling side comment alignment with --valign-side-comments or -vsc>
4328 The B<-vsc> flag enables alignment of side comments and is enabled by default. If side
4329 comment aligment is disabled with B<-nvsc> they will appear at a fixed space from the
4330 preceding code token. The default is B<-vsc>
4332 =item B<Controlling block comment alignment with --valign-block-comments or -vbc>
4334 When B<-vbc> is enabled, block comments can become aligned for example if one
4335 comment of a consecutive sequence of comments becomes outdented due a length in
4336 excess of the maximum line length. If this occurs, the entire group of
4337 comments will remain aligned and be outdented by the same amount. This coordinated
4338 alignment will not occur if B<-nvbc> is set. The default is B<-vbc>.
4340 =item B<Finer alignment control with --valign-exclusion-list=s or -vxl=s and --valign-inclusion-list=s or -vil=s>
4342 More detailed control of alignment types is available with these two
4343 parameters. Most of the vertical alignments in typical programs occur at one
4344 of the tokens ',', '=', and '=>', but many other alignments are possible and are given in the following list:
4346 = **= += *= &= <<= &&= -= /= |= >>= ||= //= .= %= ^= x=
4347 { ( ? : , ; => && || ~~ !~~ =~ !~ // <=> ->
4348 if unless and or err for foreach while until
4350 These alignments are all enabled by default, but they can be selectively disabled by including one or more of these tokens in the space-separated list B<valign-exclusion-list=s>.
4351 For example, the following would prevent alignment at B<=> and B<if>:
4353 --valign-exclusion-list='= if'
4355 If it is simpler to specify only the token types which are to be aligned, then
4356 include the types which are to be aligned in the list of B<--valign-inclusion-list>.
4357 You may leave the B<valign-exclusion-list> undefined, or use the special symbol B<*> for the exclusion list.
4358 For example, the following parameters enable alignment only at commas and 'fat commas':
4360 --valign-inclusion-list=', =>'
4361 --valign-exclusion-list='*' ( this is optional and may be omitted )
4363 These parameter lists should consist of space-separated tokens from the above
4364 list of possible alignment tokens, or a '*'. If an unrecognized token
4365 appears, it is simply ignored. And if a specific token is entered in both lists by
4366 mistake then the exclusion list has priority.
4368 The default values of these parameters enable all alignments and are equivalent to
4370 --valign-exclusion-list=' '
4371 --valign-inclusion-list='*'
4373 To illustrate, consider the following snippet with default formatting
4376 $co_description = ($color) ? 'bold cyan' : ''; # description
4377 $co_prompt = ($color) ? 'bold green' : ''; # prompt
4378 $co_unused = ($color) ? 'on_green' : 'reverse'; # unused
4380 To exclude all alignments except the equals (i.e., include only equals) we could use:
4383 $co_description = ($color) ? 'bold cyan' : ''; # description
4384 $co_prompt = ($color) ? 'bold green' : ''; # prompt
4385 $co_unused = ($color) ? 'on_green' : 'reverse'; # unused
4387 To exclude only the equals we could use:
4390 $co_description = ($color) ? 'bold cyan' : ''; # description
4391 $co_prompt = ($color) ? 'bold green' : ''; # prompt
4392 $co_unused = ($color) ? 'on_green' : 'reverse'; # unused
4394 Notice in this last example that although only the equals alignment was
4395 excluded, the ternary alignments were also lost. This happens because the
4396 vertical aligner sweeps from left-to-right and usually stops if an important
4397 alignment cannot be made for some reason.
4399 But also notice that side comments remain aligned because their alignment is
4400 controlled separately with the parameter B<--valign-side_comments> described above.
4404 =head2 Other Controls
4408 =item B<Deleting selected text>
4410 Perltidy can selectively delete comments and/or pod documentation. The
4411 command B<-dac> or B<--delete-all-comments> will delete all comments
4412 B<and> all pod documentation, leaving just code and any leading system
4415 The command B<-dp> or B<--delete-pod> will remove all pod documentation
4418 Two commands which remove comments (but not pod) are: B<-dbc> or
4419 B<--delete-block-comments> and B<-dsc> or B<--delete-side-comments>.
4420 (Hanging side comments will be deleted with side comments here.)
4422 The negatives of these commands also work, and are the defaults. When
4423 block comments are deleted, any leading 'hash-bang' will be retained.
4424 Also, if the B<-x> flag is used, any system commands before a leading
4425 hash-bang will be retained (even if they are in the form of comments).
4427 =item B<Writing selected text to a file>
4429 When perltidy writes a formatted text file, it has the ability to also
4430 send selected text to a file with a F<.TEE> extension. This text can
4431 include comments and pod documentation.
4433 The command B<-tac> or B<--tee-all-comments> will write all comments
4434 B<and> all pod documentation.
4436 The command B<-tp> or B<--tee-pod> will write all pod documentation (but
4439 The commands which write comments (but not pod) are: B<-tbc> or
4440 B<--tee-block-comments> and B<-tsc> or B<--tee-side-comments>.
4441 (Hanging side comments will be written with side comments here.)
4443 The negatives of these commands also work, and are the defaults.
4445 =item B<Using a F<.perltidyrc> command file>
4447 If you use perltidy frequently, you probably won't be happy until you
4448 create a F<.perltidyrc> file to avoid typing commonly-used parameters.
4449 Perltidy will first look in your current directory for a command file
4450 named F<.perltidyrc>. If it does not find one, it will continue looking
4451 for one in other standard locations.
4453 These other locations are system-dependent, and may be displayed with
4454 the command C<perltidy -dpro>. Under Unix systems, it will first look
4455 for an environment variable B<PERLTIDY>. Then it will look for a
4456 F<.perltidyrc> file in the home directory, and then for a system-wide
4457 file F</usr/local/etc/perltidyrc>, and then it will look for
4458 F</etc/perltidyrc>. Note that these last two system-wide files do not
4459 have a leading dot. Further system-dependent information will be found
4460 in the INSTALL file distributed with perltidy.
4462 Under Windows, perltidy will also search for a configuration file named perltidy.ini since Windows does not allow files with a leading period (.).
4463 Use C<perltidy -dpro> to see the possible locations for your system.
4464 An example might be F<C:\Documents and Settings\All Users\perltidy.ini>.
4466 Another option is the use of the PERLTIDY environment variable.
4467 The method for setting environment variables depends upon the version of
4468 Windows that you are using. Instructions for Windows 95 and later versions can
4471 http://www.netmanage.com/000/20021101_005_tcm21-6336.pdf
4473 Under Windows NT / 2000 / XP the PERLTIDY environment variable can be placed in
4474 either the user section or the system section. The later makes the
4475 configuration file common to all users on the machine. Be sure to enter the
4476 full path of the configuration file in the value of the environment variable.
4477 Ex. PERLTIDY=C:\Documents and Settings\perltidy.ini
4479 The configuration file is free format, and simply a list of parameters, just as
4480 they would be entered on a command line. Any number of lines may be used, with
4481 any number of parameters per line, although it may be easiest to read with one
4482 parameter per line. Comment text begins with a #, and there must
4483 also be a space before the # for side comments. It is a good idea to
4484 put complex parameters in either single or double quotes.
4486 Here is an example of a F<.perltidyrc> file:
4488 # This is a simple of a .perltidyrc configuration file
4489 # This implements a highly spaced style
4490 -se # errors to standard error output
4491 -w # show all warnings
4492 -bl # braces on new lines
4493 -pt=0 # parens not tight at all
4494 -bt=0 # braces not tight
4495 -sbt=0 # square brackets not tight
4497 The parameters in the F<.perltidyrc> file are installed first, so any
4498 parameters given on the command line will have priority over them.
4500 To avoid confusion, perltidy ignores any command in the .perltidyrc
4501 file which would cause some kind of dump and an exit. These are:
4503 -h -v -ddf -dln -dop -dsn -dtt -dwls -dwrs -ss
4505 There are several options may be helpful in debugging a F<.perltidyrc>
4512 A very helpful command is B<--dump-profile> or B<-dpro>. It writes a
4513 list of all configuration filenames tested to standard output, and
4514 if a file is found, it dumps the content to standard output before
4515 exiting. So, to find out where perltidy looks for its configuration
4516 files, and which one if any it selects, just enter
4522 It may be simplest to develop and test configuration files with
4523 alternative names, and invoke them with B<-pro=filename> on the command
4524 line. Then rename the desired file to F<.perltidyrc> when finished.
4528 The parameters in the F<.perltidyrc> file can be switched off with
4529 the B<-npro> option.
4533 The commands B<--dump-options>, B<--dump-defaults>, B<--dump-long-names>,
4534 and B<--dump-short-names>, all described below, may all be helpful.
4538 =item B<Creating a new abbreviation>
4540 A special notation is available for use in a F<.perltidyrc> file
4541 for creating an abbreviation for a group
4542 of options. This can be used to create a
4543 shorthand for one or more styles which are frequently, but not always,
4544 used. The notation is to group the options within curly braces which
4545 are preceded by the name of the alias (without leading dashes), like this:
4552 where B<newword> is the abbreviation, and B<opt1>, etc, are existing parameters
4553 I<or other abbreviations>. The main syntax requirement is that the new
4554 abbreviation along with its opening curly brace must begin on a new line.
4555 Space before and after the curly braces is optional.
4557 For a specific example, the following line
4559 oneliner { --maximum-line-length=0 --noadd-newlines --noadd-terminal-newline}
4561 or equivalently with abbreviations
4563 oneliner { -l=0 -nanl -natnl }
4565 could be placed in a F<.perltidyrc> file to temporarily override the maximum
4566 line length with a large value, to temporarily prevent new line breaks from
4567 being added, and to prevent an extra newline character from being added the
4568 file. All other settings in the F<.perltidyrc> file still apply. Thus it
4569 provides a way to format a long 'one liner' when perltidy is invoked with
4571 perltidy --oneliner ...
4573 (Either C<-oneliner> or C<--oneliner> may be used).
4575 =item Skipping leading non-perl commands with B<-x> or B<--look-for-hash-bang>
4577 If your script has leading lines of system commands or other text which
4578 are not valid perl code, and which are separated from the start of the
4579 perl code by a "hash-bang" line, ( a line of the form C<#!...perl> ),
4580 you must use the B<-x> flag to tell perltidy not to parse and format any
4581 lines before the "hash-bang" line. This option also invokes perl with a
4582 -x flag when checking the syntax. This option was originally added to
4583 allow perltidy to parse interactive VMS scripts, but it should be used
4584 for any script which is normally invoked with C<perl -x>.
4586 Please note: do not use this flag unless you are sure your script needs it.
4587 Parsing errors can occur if it does not have a hash-bang, or, for example, if
4588 the actual first hash-bang is in a here-doc. In that case a parsing error will
4589 occur because the tokenization will begin in the middle of the here-doc.
4591 =item B<Making a file unreadable>
4593 The goal of perltidy is to improve the readability of files, but there
4594 are two commands which have the opposite effect, B<--mangle> and
4595 B<--extrude>. They are actually
4596 merely aliases for combinations of other parameters. Both of these
4597 strip all possible whitespace, but leave comments and pod documents,
4598 so that they are essentially reversible. The
4599 difference between these is that B<--mangle> puts the fewest possible
4600 line breaks in a script while B<--extrude> puts the maximum possible.
4601 Note that these options do not provided any meaningful obfuscation, because
4602 perltidy can be used to reformat the files. They were originally
4603 developed to help test the tokenization logic of perltidy, but they
4605 One use for B<--mangle> is the following:
4607 perltidy --mangle myfile.pl -st | perltidy -o myfile.pl.new
4609 This will form the maximum possible number of one-line blocks (see next
4610 section), and can sometimes help clean up a badly formatted script.
4612 A similar technique can be used with B<--extrude> instead of B<--mangle>
4613 to make the minimum number of one-line blocks.
4615 Another use for B<--mangle> is to combine it with B<-dac> to reduce
4616 the file size of a perl script.
4620 The following flags are available for debugging:
4622 B<--dump-cuddled-block-list> or B<-dcbl> will dump to standard output the
4623 internal hash of cuddled block types created by a B<-cuddled-block-list> input
4626 B<--dump-defaults> or B<-ddf> will write the default option set to standard output and quit
4628 B<--dump-profile> or B<-dpro> will write the name of the current
4629 configuration file and its contents to standard output and quit.
4631 B<--dump-options> or B<-dop> will write current option set to standard
4634 B<--dump-long-names> or B<-dln> will write all command line long names (passed
4635 to Get_options) to standard output and quit.
4637 B<--dump-short-names> or B<-dsn> will write all command line short names
4638 to standard output and quit.
4640 B<--dump-token-types> or B<-dtt> will write a list of all token types
4641 to standard output and quit.
4643 B<--dump-want-left-space> or B<-dwls> will write the hash %want_left_space
4644 to standard output and quit. See the section on controlling whitespace
4647 B<--dump-want-right-space> or B<-dwrs> will write the hash %want_right_space
4648 to standard output and quit. See the section on controlling whitespace
4651 B<--no-memoize> or B<-nmem> will turn of memoizing.
4652 Memoization can reduce run time when running perltidy repeatedly in a
4653 single process. It is on by default but can be deactivated for
4654 testing with B<-nmem>.
4656 B<--no-timestamp> or B<-nts> will eliminate any time stamps in output files to prevent
4657 differences in dates from causing test installation scripts to fail. There are just
4658 a couple of places where timestamps normally occur. One is in the headers of
4659 html files, and another is when the B<-cscw> option is selected. The default is
4660 to allow timestamps (B<--timestamp> or B<-ts>).
4662 B<--file-size-order> or B<-fso> will cause files to be processed in order of
4663 increasing size, when multiple files are being processed. This is useful
4664 during program development, when large numbers of files with varying sizes are
4665 processed, because it can reduce virtual memory usage.
4667 B<--maximum-file-size-mb=n> or B<-maxfs=n> specifies the maximum file size in
4668 megabytes that perltidy will attempt to format. This parameter is provided to
4669 avoid causing system problems by accidentally attempting to format an extremely
4670 large data file. Most perl scripts are less than about 2 MB in size. The
4671 integer B<n> has a default value of 10, so perltidy will skip formatting files
4672 which have a size greater than 10 MB. The command to increase the limit to 20
4673 MB for example would be
4677 This only applies to files specified by filename on the command line.
4679 B<--maximum-level-errors=n> or B<-maxle=n> specifies the maximum number of
4680 indentation level errors are allowed before perltidy skips formatting and just
4681 outputs a file verbatim. The default is B<n=1>. This means that if the final
4682 indentation of a script differs from the starting indentation by more than 1
4683 levels, the file will be output verbatim. To avoid formatting if there are any
4684 indentation level errors use -maxle=0. To skip this check you can either set n
4685 equal to a large number, such as B<n=100>, or set B<n=-1>.
4687 For example, the following script has level error of 3 and will be output verbatim
4689 Input and default output:
4698 B<--maximum-unexpected-errors=n> or B<-maxue=n> specifies the maximum number of
4699 unexpected tokenization errors are allowed before formatting is skipped and a
4700 script is output verbatim. The intention is to avoid accidentally formatting
4701 a non-perl script, such as an html file for example. This check can be turned
4702 off by setting B<n=0>.
4704 A recommended value is B<n=3>. However, the default is B<n=0> (skip this check)
4705 to avoid causing problems with scripts which have extended syntaxes.
4707 B<-DEBUG> will write a file with extension F<.DEBUG> for each input file
4708 showing the tokenization of all lines of code.
4710 =item B<Working with MakeMaker, AutoLoader and SelfLoader>
4712 The first $VERSION line of a file which might be eval'd by MakeMaker
4713 is passed through unchanged except for indentation.
4714 Use B<--nopass-version-line>, or B<-npvl>, to deactivate this feature.
4716 If the AutoLoader module is used, perltidy will continue formatting
4717 code after seeing an __END__ line.
4718 Use B<--nolook-for-autoloader>, or B<-nlal>, to deactivate this feature.
4720 Likewise, if the SelfLoader module is used, perltidy will continue formatting
4721 code after seeing a __DATA__ line.
4722 Use B<--nolook-for-selfloader>, or B<-nlsl>, to deactivate this feature.
4724 =item B<Working around problems with older version of Perl>
4726 Perltidy contains a number of rules which help avoid known subtleties
4727 and problems with older versions of perl, and these rules always
4728 take priority over whatever formatting flags have been set. For example,
4729 perltidy will usually avoid starting a new line with a bareword, because
4730 this might cause problems if C<use strict> is active.
4732 There is no way to override these rules.
4740 =item The B<-html> master switch
4742 The flag B<-html> causes perltidy to write an html file with extension
4743 F<.html>. So, for example, the following command
4745 perltidy -html somefile.pl
4747 will produce a syntax-colored html file named F<somefile.pl.html>
4748 which may be viewed with a browser.
4750 B<Please Note>: In this case, perltidy does not do any formatting to the
4751 input file, and it does not write a formatted file with extension
4752 F<.tdy>. This means that two perltidy runs are required to create a
4753 fully reformatted, html copy of a script.
4755 =item The B<-pre> flag for code snippets
4757 When the B<-pre> flag is given, only the pre-formatted section, within
4758 the <PRE> and </PRE> tags, will be output. This simplifies inclusion
4759 of the output in other files. The default is to output a complete
4762 =item The B<-nnn> flag for line numbering
4764 When the B<-nnn> flag is given, the output lines will be numbered.
4766 =item The B<-toc>, or B<--html-table-of-contents> flag
4768 By default, a table of contents to packages and subroutines will be
4769 written at the start of html output. Use B<-ntoc> to prevent this.
4770 This might be useful, for example, for a pod document which contains a
4771 number of unrelated code snippets. This flag only influences the code
4772 table of contents; it has no effect on any table of contents produced by
4773 pod2html (see next item).
4775 =item The B<-pod>, or B<--pod2html> flag
4777 There are two options for formatting pod documentation. The default is
4778 to pass the pod through the Pod::Html module (which forms the basis of
4779 the pod2html utility). Any code sections are formatted by perltidy, and
4780 the results then merged. Note: perltidy creates a temporary file when
4781 Pod::Html is used; see L<"FILES">. Also, Pod::Html creates temporary
4782 files for its cache.
4784 NOTE: Perltidy counts the number of C<=cut> lines, and either moves the
4785 pod text to the top of the html file if there is one C<=cut>, or leaves
4786 the pod text in its original order (interleaved with code) otherwise.
4788 Most of the flags accepted by pod2html may be included in the perltidy
4789 command line, and they will be passed to pod2html. In some cases,
4790 the flags have a prefix C<pod> to emphasize that they are for the
4791 pod2html, and this prefix will be removed before they are passed to
4792 pod2html. The flags which have the additional C<pod> prefix are:
4794 --[no]podheader --[no]podindex --[no]podrecurse --[no]podquiet
4795 --[no]podverbose --podflush
4797 The flags which are unchanged from their use in pod2html are:
4799 --backlink=s --cachedir=s --htmlroot=s --libpods=s --title=s
4800 --podpath=s --podroot=s
4802 where 's' is an appropriate character string. Not all of these flags are
4803 available in older versions of Pod::Html. See your Pod::Html documentation for
4806 The alternative, indicated with B<-npod>, is not to use Pod::Html, but
4807 rather to format pod text in italics (or whatever the stylesheet
4808 indicates), without special html markup. This is useful, for example,
4809 if pod is being used as an alternative way to write comments.
4811 =item The B<-frm>, or B<--frames> flag
4813 By default, a single html output file is produced. This can be changed
4814 with the B<-frm> option, which creates a frame holding a table of
4815 contents in the left panel and the source code in the right side. This
4816 simplifies code browsing. Assume, for example, that the input file is
4817 F<MyModule.pm>. Then, for default file extension choices, these three
4818 files will be created:
4820 MyModule.pm.html - the frame
4821 MyModule.pm.toc.html - the table of contents
4822 MyModule.pm.src.html - the formatted source code
4824 Obviously this file naming scheme requires that output be directed to a real
4825 file (as opposed to, say, standard output). If this is not the
4826 case, or if the file extension is unknown, the B<-frm> option will be
4829 =item The B<-text=s>, or B<--html-toc-extension> flag
4831 Use this flag to specify the extra file extension of the table of contents file
4832 when html frames are used. The default is "toc".
4833 See L<Specifying File Extensions>.
4835 =item The B<-sext=s>, or B<--html-src-extension> flag
4837 Use this flag to specify the extra file extension of the content file when html
4838 frames are used. The default is "src".
4839 See L<Specifying File Extensions>.
4841 =item The B<-hent>, or B<--html-entities> flag
4843 This flag controls the use of Html::Entities for html formatting. By
4844 default, the module Html::Entities is used to encode special symbols.
4845 This may not be the right thing for some browser/language
4846 combinations. Use --nohtml-entities or -nhent to prevent this.
4848 =item B<Style Sheets>
4850 Style sheets make it very convenient to control and adjust the
4851 appearance of html pages. The default behavior is to write a page of
4852 html with an embedded style sheet.
4854 An alternative to an embedded style sheet is to create a page with a
4855 link to an external style sheet. This is indicated with the
4856 B<-css=filename>, where the external style sheet is F<filename>. The
4857 external style sheet F<filename> will be created if and only if it does
4858 not exist. This option is useful for controlling multiple pages from a
4861 To cause perltidy to write a style sheet to standard output and exit,
4862 use the B<-ss>, or B<--stylesheet>, flag. This is useful if the style
4863 sheet could not be written for some reason, such as if the B<-pre> flag
4864 was used. Thus, for example,
4866 perltidy -html -ss >mystyle.css
4868 will write a style sheet with the default properties to file
4871 The use of style sheets is encouraged, but a web page without a style
4872 sheets can be created with the flag B<-nss>. Use this option if you
4873 must to be sure that older browsers (roughly speaking, versions prior to
4874 4.0 of Netscape Navigator and Internet Explorer) can display the
4875 syntax-coloring of the html files.
4877 =item B<Controlling HTML properties>
4879 Note: It is usually more convenient to accept the default properties
4880 and then edit the stylesheet which is produced. However, this section
4881 shows how to control the properties with flags to perltidy.
4883 Syntax colors may be changed from their default values by flags of the either
4884 the long form, B<-html-color-xxxxxx=n>, or more conveniently the short form,
4885 B<-hcx=n>, where B<xxxxxx> is one of the following words, and B<x> is the
4886 corresponding abbreviation:
4889 ---------- -------- --
4892 identifier identifier i
4893 bareword, function bareword w
4895 quite, pattern quote q
4896 here doc text here-doc-text h
4897 here doc target here-doc-target hh
4898 punctuation punctuation pu
4900 structural braces structure s
4901 semicolon semicolon sc
4905 sub definition name subroutine m
4906 pod text pod-text pd
4908 A default set of colors has been defined, but they may be changed by providing
4909 values to any of the following parameters, where B<n> is either a 6 digit
4910 hex RGB color value or an ascii name for a color, such as 'red'.
4912 To illustrate, the following command will produce an html
4913 file F<somefile.pl.html> with "aqua" keywords:
4915 perltidy -html -hck=00ffff somefile.pl
4917 and this should be equivalent for most browsers:
4919 perltidy -html -hck=aqua somefile.pl
4921 Perltidy merely writes any non-hex names that it sees in the html file.
4922 The following 16 color names are defined in the HTML 3.2 standard:
4941 Many more names are supported in specific browsers, but it is safest
4942 to use the hex codes for other colors. Helpful color tables can be
4943 located with an internet search for "HTML color tables".
4945 Besides color, two other character attributes may be set: bold, and italics.
4946 To set a token type to use bold, use the flag
4947 B<--html-bold-xxxxxx> or B<-hbx>, where B<xxxxxx> or B<x> are the long
4948 or short names from the above table. Conversely, to set a token type to
4949 NOT use bold, use B<--nohtml-bold-xxxxxx> or B<-nhbx>.
4951 Likewise, to set a token type to use an italic font, use the flag
4952 B<--html-italic-xxxxxx> or B<-hix>, where again B<xxxxxx> or B<x> are the
4953 long or short names from the above table. And to set a token type to
4954 NOT use italics, use B<--nohtml-italic-xxxxxx> or B<-nhix>.
4956 For example, to use bold braces and lime color, non-bold, italics keywords the
4957 following command would be used:
4959 perltidy -html -hbs -hck=00FF00 -nhbk -hik somefile.pl
4961 The background color can be specified with B<--html-color-background=n>,
4962 or B<-hcbg=n> for short, where n is a 6 character hex RGB value. The
4963 default color of text is the value given to B<punctuation>, which is
4966 Here are some notes and hints:
4968 1. If you find a preferred set of these parameters, you may want
4969 to create a F<.perltidyrc> file containing them. See the perltidy man
4970 page for an explanation.
4972 2. Rather than specifying values for these parameters, it is probably
4973 easier to accept the defaults and then edit a style sheet. The style
4974 sheet contains comments which should make this easy.
4976 3. The syntax-colored html files can be very large, so it may be best to
4977 split large files into smaller pieces to improve download times.
4981 =head1 SOME COMMON INPUT CONVENTIONS
4983 =head2 Specifying Block Types
4985 Several parameters which refer to code block types may be customized by also
4986 specifying an associated list of block types. The type of a block is the name
4987 of the keyword which introduces that block, such as B<if>, B<else>, or B<sub>.
4988 An exception is a labeled block, which has no keyword, and should be specified
4989 with just a colon. To specify all blocks use B<'*'>.
4991 The keyword B<sub> indicates a named sub. For anonymous subs, use the special
4994 For example, the following parameter specifies C<sub>, labels, C<BEGIN>, and
4997 -cscl="sub : BEGIN END"
4999 (the meaning of the -cscl parameter is described above.) Note that
5000 quotes are required around the list of block types because of the
5001 spaces. For another example, the following list specifies all block types
5002 for vertical tightness:
5006 =head2 Specifying File Extensions
5008 Several parameters allow default file extensions to be overridden. For
5009 example, a backup file extension may be specified with B<-bext=ext>,
5010 where B<ext> is some new extension. In order to provides the user some
5011 flexibility, the following convention is used in all cases to decide if
5012 a leading '.' should be used. If the extension C<ext> begins with
5013 C<A-Z>, C<a-z>, or C<0-9>, then it will be appended to the filename with
5014 an intermediate '.' (or perhaps a '_' on VMS systems). Otherwise, it
5015 will be appended directly.
5017 For example, suppose the file is F<somefile.pl>. For C<-bext=old>, a '.' is
5018 added to give F<somefile.pl.old>. For C<-bext=.old>, no additional '.' is
5019 added, so again the backup file is F<somefile.pl.old>. For C<-bext=~>, then no
5020 dot is added, and the backup file will be F<somefile.pl~> .
5022 =head1 SWITCHES WHICH MAY BE NEGATED
5024 The following list shows all short parameter names which allow a prefix
5025 'n' to produce the negated form:
5027 D anl asbl asc ast asu atnl aws b baa
5028 baao bar bbao bbb bbc bbs bl bli boa boc
5029 bok bol bom bos bot cblx ce conv cs csc
5030 cscb cscw dac dbc dcbl dcsc ddf dln dnl dop
5031 dp dpro dsc dsm dsn dtt dwls dwrs dws eos
5032 f fll fpva frm fs fso gcs hbc hbcm hbco
5033 hbh hbhh hbi hbj hbk hbm hbn hbp hbpd hbpu
5034 hbq hbs hbsc hbv hbw hent hic hicm hico hih
5035 hihh hii hij hik him hin hip hipd hipu hiq
5036 his hisc hiv hiw hsc html ibc icb icp iob
5037 isbc iscl kgb kgbd kgbi kis lal log lop lp
5038 lsl mem nib ohbr okw ola olc oll olq opr
5039 opt osbc osbr otr ple pod pvl q sac sbc
5040 sbl scbb schb scp scsb sct se sfp sfs skp
5041 sob sobb sohb sop sosb sot ssc st sts t
5042 tac tbc toc tp tqw trp ts tsc tso vbc
5043 vc vmll vsc w wn x xci xlp xs
5045 Equivalently, the prefix 'no' or 'no-' on the corresponding long names may be
5052 =item B<Parsing Limitations>
5054 Perltidy should work properly on most perl scripts. It does a lot of
5055 self-checking, but still, it is possible that an error could be
5056 introduced and go undetected. Therefore, it is essential to make
5057 careful backups and to test reformatted scripts.
5059 The main current limitation is that perltidy does not scan modules
5060 included with 'use' statements. This makes it necessary to guess the
5061 context of any bare words introduced by such modules. Perltidy has good
5062 guessing algorithms, but they are not infallible. When it must guess,
5063 it leaves a message in the log file.
5065 If you encounter a bug, please report it.
5067 =item B<What perltidy does not parse and format>
5069 Perltidy indents but does not reformat comments and C<qw> quotes.
5070 Perltidy does not in any way modify the contents of here documents or
5071 quoted text, even if they contain source code. (You could, however,
5072 reformat them separately). Perltidy does not format 'format' sections
5073 in any way. And, of course, it does not modify pod documents.
5081 =item B<Temporary files>
5083 Under the -html option with the default --pod2html flag, a temporary file is
5084 required to pass text to Pod::Html. Unix systems will try to use the POSIX
5085 tmpnam() function. Otherwise the file F<perltidy.TMP> will be temporarily
5086 created in the current working directory.
5088 =item B<Special files when standard input is used>
5090 When standard input is used, the log file, if saved, is F<perltidy.LOG>,
5091 and any errors are written to F<perltidy.ERR> unless the B<-se> flag is
5092 set. These are saved in the current working directory.
5094 =item B<Files overwritten>
5096 The following file extensions are used by perltidy, and files with these
5097 extensions may be overwritten or deleted: F<.ERR>, F<.LOG>, F<.TEE>,
5098 and/or F<.tdy>, F<.html>, and F<.bak>, depending on the run type and
5101 =item B<Files extensions limitations>
5103 Perltidy does not operate on files for which the run could produce a file with
5104 a duplicated file extension. These extensions include F<.LOG>, F<.ERR>,
5105 F<.TEE>, and perhaps F<.tdy> and F<.bak>, depending on the run type. The
5106 purpose of this rule is to prevent generating confusing filenames such as
5107 F<somefile.tdy.tdy.tdy>.
5111 =head1 ERROR HANDLING
5113 An exit value of 0, 1, or 2 is returned by perltidy to indicate the status of the result.
5115 A exit value of 0 indicates that perltidy ran to completion with no error messages.
5117 A non-zero exit value indicates some kind of problem was detected.
5119 An exit value of 1 indicates that perltidy terminated prematurely, usually due
5120 to some kind of errors in the input parameters. This can happen for example if
5121 a parameter is misspelled or given an invalid value. Error messages in the
5122 standard error output will indicate the cause of any problem. If perltidy
5123 terminates prematurely then no output files will be produced.
5125 An exit value of 2 indicates that perltidy was able to run to completion but
5126 there there are (1) warning messages in the standard error output related to
5127 parameter errors or problems and/or (2) warning messages in the perltidy error
5128 file(s) relating to possible syntax errors in one or more of the source
5129 script(s) being tidied. When multiple files are being processed, an error
5130 detected in any single file will produce this type of exit condition.
5134 perlstyle(1), Perl::Tidy(3)
5138 The perltidy binary uses the Perl::Tidy module and is installed when that module is installed. The module name is case-sensitive. For example, the basic command for installing with cpanm is 'cpanm Perl::Tidy'.
5142 This man page documents perltidy version 20220217
5146 A list of current bugs and issues can be found at the CPAN site L<https://rt.cpan.org/Public/Dist/Display.html?Name=Perl-Tidy>
5148 To report a new bug or problem, use the link on this page.
5150 The source code repository is at L<https://github.com/perltidy/perltidy>.
5154 Copyright (c) 2000-2022 by Steve Hancock
5158 This package is free software; you can redistribute it and/or modify it
5159 under the terms of the "GNU General Public License".
5161 Please refer to the file "COPYING" for details.
5165 This package is distributed in the hope that it will be useful,
5166 but WITHOUT ANY WARRANTY; without even the implied warranty of
5167 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5169 See the "GNU General Public License" for more details.