X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=bin%2Fperltidy;h=a24aace57c31444087e392ab00baf170791931cb;hb=880633cc084e9d787eb9f760d3851c5d660db17c;hp=9839ed4ef8e9447aa16b656e42cefd0a5e7b8206;hpb=5af8685bea60c00aae46266c726ddfb0132d7d12;p=perltidy.git diff --git a/bin/perltidy b/bin/perltidy index 9839ed4..a24aace 100755 --- a/bin/perltidy +++ b/bin/perltidy @@ -13,7 +13,11 @@ if ( $^O =~ /Mac/ ) { ); } -Perl::Tidy::perltidy( argv => $arg_string ); +# Exit codes returned by perltidy: +# 0 = no errors +# 1 = perltidy could not run to completion due to errors +# 2 = perltidy ran to completion with error messages +exit Perl::Tidy::perltidy( argv => $arg_string ); __END__ @@ -33,8 +37,8 @@ perltidy - a perl script indenter and reformatter Perltidy reads a perl script and writes an indented, reformatted script. -Many users will find enough information in L<"EXAMPLES"> to get -started. New users may benefit from the short tutorial +Many users will find enough information in L<"EXAMPLES"> to get +started. New users may benefit from the short tutorial which can be found at http://perltidy.sourceforge.net/tutorial.html @@ -47,17 +51,17 @@ existence of an B<-html> flag. Without this flag, the output is passed through a formatter. The default formatting tries to follow the recommendations in perlstyle(1), but it can be controlled in detail with numerous input parameters, which are described in L<"FORMATTING -OPTIONS">. +OPTIONS">. When the B<-html> flag is given, the output is passed through an HTML -formatter which is described in L<"HTML OPTIONS">. +formatter which is described in L<"HTML OPTIONS">. =head1 EXAMPLES perltidy somefile.pl This will produce a file F containing the script reformatted -using the default options, which approximate the style suggested in +using the default options, which approximate the style suggested in perlstyle(1). The source file F is unchanged. perltidy *.pl @@ -87,7 +91,7 @@ GNU Coding Standards for C programs. The output will be F. Execute perltidy on file F, with 3 columns for each level of indentation (B<-i=3>) instead of the default 4 columns. There will not be any tabs in the reformatted script, except for any which already exist in comments, -pod documents, quotes, and here documents. Output will be F. +pod documents, quotes, and here documents. Output will be F. perltidy -i=3 -et=8 somefile.pl @@ -98,7 +102,7 @@ be entabbed with one tab character per 8 spaces. Execute perltidy on file F with all defaults except use "cuddled elses" (B<-ce>) and a maximum line length of 72 columns (B<-l=72>) instead of -the default 80 columns. +the default 80 columns. perltidy -g somefile.pl @@ -123,7 +127,7 @@ it will be created. If it exists, it will not be overwritten. Write an html snippet with only the PRE section to F. This is useful when code snippets are being formatted for inclusion in a -larger web page. No style sheet will be written in this case. +larger web page. No style sheet will be written in this case. perltidy -html -ss >mystyle.css @@ -141,7 +145,7 @@ The entire command line is scanned for options, and they are processed before any files are processed. As a result, it does not matter whether flags are before or after any filenames. However, the relative order of parameters is important, with later parameters overriding the -values of earlier parameters. +values of earlier parameters. For each parameter, there is a long name and a short name. The short names are convenient for keyboard input, while the long names are @@ -167,15 +171,17 @@ The following parameters concern the files which are read and written. =over 4 -=item B<-h>, B<--help> +=item B<-h>, B<--help> Show summary of usage and exit. -=item B<-o>=filename, B<--outfile>=filename +=item B<-o>=filename, B<--outfile>=filename Name of the output file (only if a single input file is being processed). If no output file is specified, and output is not -redirected to the standard output, the output will go to F. +redirected to the standard output (see B<-st>), the output will go to +F. [Note: - does not redirect to standard output. Use +B<-st> instead.] =item B<-st>, B<--standard-output> @@ -187,7 +193,7 @@ request outputting to the standard output. For example, perltidy somefile.pl -st >somefile.new.pl -This option may only be used if there is just a single input file. +This option may only be used if there is just a single input file. The default is B<-nst> or B<--nostandard-output>. =item B<-se>, B<--standard-error-output> @@ -199,13 +205,13 @@ output stream instead. This directive may be negated with B<-nse>. Thus, you may place B<-se> in a F<.perltidyrc> and override it when desired with B<-nse> on the command line. -=item B<-oext>=ext, B<--output-file-extension>=ext +=item B<-oext>=ext, B<--output-file-extension>=ext Change the extension of the output file to be F instead of the default F (or F in case the -B<-html> option is used). See L. -=item B<-opath>=path, B<--output-path>=path +=item B<-opath>=path, B<--output-path>=path When perltidy creates a filename for an output file, by default it merely appends an extension to the path and basename of the input file. This @@ -215,7 +221,7 @@ The path should end in a valid path separator character, but perltidy will try to add one if it is missing. For example - + perltidy somefile.pl -opath=/tmp/ will produce F. Otherwise, F will @@ -231,17 +237,17 @@ or if it is being specified explicitly with the B<-o=s> parameter. Modify the input file or files in-place and save the original with the extension F<.bak>. Any existing F<.bak> file will be deleted. See next item for changing the default backup extension, and for eliminating the -backup file altogether. +backup file altogether. A B<-b> flag will be ignored if input is from standard input or goes to -standard output, or if the B<-html> flag is set. +standard output, or if the B<-html> flag is set. In particular, if you want to use both the B<-b> flag and the B<-pbp> (--perl-best-practices) flag, then you must put a B<-nst> flag after the B<-pbp> flag because it contains a B<-st> flag as one of its components, which means that output will go to the standard output stream. -=item B<-bext>=ext, B<--backup-file-extension>=ext +=item B<-bext>=ext, B<--backup-file-extension>=ext This parameter serves two purposes: (1) to change the extension of the backup file to be something other than the default F<.bak>, and (2) to indicate @@ -253,7 +259,7 @@ L. A backup file of the source is always written, but you can request that it be deleted at the end of processing if there were no errors. This is risky unless the source code is being maintained with a source code control -system. +system. To indicate that the backup should be deleted include one forward slash, B, in the extension. If any text remains after the slash is removed @@ -268,7 +274,7 @@ Here are some examples: <-bext='/backup'> F<.backup> Delete if no errors <-bext='original/'> F<.original> Delete if no errors -=item B<-w>, B<--warning-output> +=item B<-w>, B<--warning-output> Setting B<-w> causes any non-critical warning messages to be reported as errors. These include messages @@ -276,10 +282,9 @@ about possible pod problems, possibly bad starting indentation level, and cautions about indirect object usage. The default, B<-nw> or B<--nowarning-output>, is not to include these warnings. -=item B<-q>, B<--quiet> +=item B<-q>, B<--quiet> -Deactivate error messages and syntax checking (for running under -an editor). +Deactivate error messages (for running under an editor). For example, if you use a vi-style editor, such as vim, you may execute perltidy as a filter from within the editor using something like @@ -290,7 +295,7 @@ where C represents the selected text. Without the B<-q> flag, any error message may mess up your screen, so be prepared to use your "undo" key. -=item B<-log>, B<--logfile> +=item B<-log>, B<--logfile> Save the F<.LOG> file, which has many useful diagnostics. Perltidy always creates a F<.LOG> file, but by default it is deleted unless a program bug is @@ -302,18 +307,18 @@ Set maximum interval between input code lines in the logfile. This purpose of this flag is to assist in debugging nesting errors. The value of C is optional. If you set the flag B<-g> without the value of C, it will be taken to be 1, meaning that every line will be written to the log file. This -can be helpful if you are looking for a brace, paren, or bracket nesting error. +can be helpful if you are looking for a brace, paren, or bracket nesting error. Setting B<-g> also causes the logfile to be saved, so it is not necessary to -also include B<-log>. +also include B<-log>. If no B<-g> flag is given, a value of 50 will be used, meaning that at least every 50th line will be recorded in the logfile. This helps prevent -excessively long log files. +excessively long log files. Setting a negative value of C is the same as not setting B<-g> at all. -=item B<-npro> B<--noprofile> +=item B<-npro> B<--noprofile> Ignore any F<.perltidyrc> command file. Normally, perltidy looks first in your current directory for a F<.perltidyrc> file of parameters. (The format @@ -324,7 +329,7 @@ in your home directory. If you set the B<-npro> flag, perltidy will not look for this file. -=item B<-pro=filename> or B<--profile=filename> +=item B<-pro=filename> or B<--profile=filename> To simplify testing and switching .perltidyrc files, this command may be used to specify a configuration file which will override the default @@ -333,7 +338,7 @@ name of .perltidyrc. There must not be a space on either side of the perltidy -pro=testcfg -would cause file F to be used instead of the +would cause file F to be used instead of the default F<.perltidyrc>. A pathname begins with three dots, e.g. ".../.perltidyrc", indicates that @@ -341,18 +346,76 @@ the file should be searched for starting in the current directory and working upwards. This makes it easier to have multiple projects each with their own .perltidyrc in their root directories. -=item B<-opt>, B<--show-options> +=item B<-opt>, B<--show-options> -Write a list of all options used to the F<.LOG> file. +Write a list of all options used to the F<.LOG> file. Please see B<--dump-options> for a simpler way to do this. -=item B<-f>, B<--force-read-binary> +=item B<-f>, B<--force-read-binary> Force perltidy to process binary files. To avoid producing excessive error messages, perltidy skips files identified by the system as non-text. However, valid perl scripts containing binary data may sometimes be identified as non-text, and this flag forces perltidy to process them. +=item B<-ast>, B<--assert-tidy> + +This flag asserts that the input and output code streams are identical, or in +other words that the input code is already 'tidy' according to the formatting +parameters. If this is not the case, an error message noting this is produced. +This error message will cause the process to return a non-zero exit code. +The test for this is made by comparing an MD5 hash value for the input and +output code streams. This flag has no other effect on the functioning of +perltidy. This might be useful for certain code maintenance operations. +Note: you will not see this message if you have error messages turned off with the +-quiet flag. + +=item B<-asu>, B<--assert-untidy> + +This flag asserts that the input and output code streams are different, or in +other words that the input code is 'untidy' according to the formatting +parameters. If this is not the case, an error message noting this is produced. +This flag has no other effect on the functioning of perltidy. + +=item B<-sal=s>, B<--sub-alias-list=s> + +This flag causes one or more words to be treated the same as if they were the keyword 'sub'. The string B contains one or more alias words, separated by spaces or commas. + +For example, + + perltidy -sal='method fun _sub M4' + +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. + +Note that several other parameters accept a list of keywords, including 'sub' (see L). +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. + +=item B<-gal=s>, B<--grep-alias-list=s> + +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, B or B. The string B contains the names of one or more such list operators, separated by spaces or commas. + +By 'list operator' is meant a function which is invoked in the form + + word {BLOCK} @list + +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. + +For example, the code block arguments to functions 'My_grep' and 'My_map' can be given formatting like 'grep' with + + perltidy -gal='My_grep My_map' + +By default, the following list operators in List::Util are automatically included: + + all any first none notall reduce reductions + +Any operators specified with B<--grep-alias-list> are added to this list. +The next parameter can be used to remove words from this default list. + +=item B<-gaxl=s>, B<--grep-alias-exclusion-list=s> + +The B<-gaxl=s> flag provides a method for removing any of the default list operators given above +by listing them in the string B. To remove all of the default operators use B<-gaxl='*'>. + =back =head1 FORMATTING OPTIONS @@ -370,7 +433,7 @@ set of F<.perltidyrc> files to avoid unwanted code tidying. See also L for a way to avoid tidying specific sections of code. -=item B<-i=n>, B<--indent-columns=n> +=item B<-i=n>, B<--indent-columns=n> Use n columns per indentation level (default n=4). @@ -378,8 +441,14 @@ Use n columns per indentation level (default n=4). The default maximum line length is n=80 characters. Perltidy will try to find line break points to keep lines below this length. However, long -quotes and side comments may cause lines to exceed this length. -Setting B<-l=0> is equivalent to setting B<-l=(a large number)>. +quotes and side comments may cause lines to exceed this length. + +The default length of 80 comes from the past when this was the standard CRT +screen width. Many programmers prefer to increase this to something like 120. + +Setting B<-l=0> is equivalent to setting B<-l=(a very large number)>. But this is +not recommended because, for example, a very long list will be formatted in a +single long line. =item B<-vmll>, B<--variable-maximum-line-length> @@ -387,9 +456,9 @@ A problem arises using a fixed maximum line length with very deeply nested code and data structures because eventually the amount of leading whitespace used for indicating indentation takes up most or all of the available line width, leaving little or no space for the actual code or data. One solution is to use -a vary long line length. Another solution is to use the B<-vmll> flag, which +a very long line length. Another solution is to use the B<-vmll> flag, which basically tells perltidy to ignore leading whitespace when measuring the line -length. +length. To be precise, when the B<-vmll> parameter is set, the maximum line length of a line of code will be M+L*I, where @@ -401,7 +470,7 @@ line of code will be M+L*I, where When this flag is set, the choice of breakpoints for a block of code should be essentially independent of its nesting depth. However, the absolute line lengths, including leading whitespace, can still be arbitrarily large. This -problem can be avoided by including the next parameter. +problem can be avoided by including the next parameter. The default is not to do this (B<-nvmll>). @@ -411,7 +480,7 @@ This flag also addresses problems with very deeply nested code and data structures. When the nesting depth exceeds the value B the leading whitespace will be reduced and start at a depth of 1 again. The result is that blocks of code will shift back to the left rather than moving arbitrarily far -to the right. This occurs cyclically to any depth. +to the right. This occurs cyclically to any depth. For example if one level of indentation equals 4 spaces (B<-i=4>, the default), and one uses B<-wc=15>, then if the leading whitespace on a line exceeds about @@ -425,12 +494,12 @@ although B<-wc=n> may of course be used without B<-vmll>. The default is not to use this, which can also be indicated using B<-wc=0>. -=item tabs +=item B Using tab characters will almost certainly lead to future portability and maintenance problems, so the default and recommendation is not to use them. For those who prefer tabs, however, there are two different -options. +options. Except for possibly introducing tab indentation characters, as outlined below, perltidy does not introduce any tab characters into your file, @@ -443,8 +512,13 @@ here-documents, they will remain. =item B<-et=n>, B<--entab-leading-whitespace> This flag causes each B initial space characters to be replaced by -one tab character. Note that the integer B is completely independent -of the integer specified for indentation parameter, B<-i=n>. +one tab character. + +The value of the integer B can be any value but can be coordinated with the +number of spaces used for intentation. For example, B<-et=4 -ci=4 -i=4> will +produce one tab for each indentation level and and one for each continuation +indentation level. You may want to coordinate the value of B with what your +display software assumes for the spacing of a tab. =item B<-t>, B<--tabs> @@ -452,7 +526,8 @@ This flag causes one leading tab character to be inserted for each level of indentation. Certain other features are incompatible with this option, and if these options are also given, then a warning message will be issued and this flag will be unset. One example is the B<-lp> -option. +option. This flag is retained for backwards compatibility, but +if you use tabs, the B<-et=n> flag is recommended. =item B<-dt=n>, B<--default-tabsize=n> @@ -460,7 +535,7 @@ If the first line of code passed to perltidy contains leading tabs but no tab scheme is specified for the output stream then perltidy must guess how many spaces correspond to each leading tab. This number of spaces B corresponding to each leading tab of the input stream may be specified with -B<-dt=n>. The default is B. +B<-dt=n>. The default is B. This flag has no effect if a tab scheme is specified for the output stream, because then the input stream is assumed to use the same tab scheme and @@ -469,46 +544,11 @@ unstable editing). =back -=item B<-syn>, B<--check-syntax> - -This flag is now ignored for safety, but the following documentation -has been retained for reference. - -This flag causes perltidy to run C to check syntax of input -and output. (To change the flags passed to perl, see the next -item, B<-pscf>). The results are written to the F<.LOG> file, which -will be saved if an error is detected in the output script. The output -script is not checked if the input script has a syntax error. Perltidy -does its own checking, but this option employs perl to get a "second -opinion". - -If perl reports errors in the input file, they will not be reported in -the error output unless the B<--warning-output> flag is given. - -The default is B to do this type of syntax checking (although -perltidy will still do as much self-checking as possible). The reason -is that it causes all code in BEGIN blocks to be executed, for all -modules being used, and this opens the door to security issues and -infinite loops when running perltidy. - -=item B<-pscf=s>, B<-perl-syntax-check-flags=s> - -When perl is invoked to check syntax, the normal flags are C<-c -T>. In -addition, if the B<-x> flag is given to perltidy, then perl will also be -passed a B<-x> flag. It should not normally be necessary to change -these flags, but it can be done with the B<-pscf=s> flag. For example, -if the taint flag, C<-T>, is not wanted, the flag could be set to be just -B<-pscf=-c>. - -Perltidy will pass your string to perl with the exception that it will -add a B<-c> and B<-x> if appropriate. The F<.LOG> file will show -exactly what flags were passed to perl. - -=item B<-xs>, B<--extended-syntax> +=item B<-xs>, B<--extended-syntax> A problem with formatting Perl code is that some modules can introduce new syntax. This flag allows perltidy to handle certain common extensions -to the standard syntax without complaint. +to the standard syntax without complaint. For example, without this flag a structure such as the following would generate a syntax error and the braces would not be balanced: @@ -517,12 +557,17 @@ a syntax error and the braces would not be balanced: $self->balance( $self->balance + $amount ); } +For one of the extensions, module Switch::Plain, colons are marked as labels. +If you use this module, you may want to also use the B<--nooutdent-labels> flag +to prevent lines such as 'default:' from being outdented. + This flag is enabled by default but it can be deactivated with B<-nxs>. Probably the only reason to deactivate this flag is to generate more diagnostic messages when debugging a script. +For another method of handling extended syntax see the section L. -=item B<-io>, B<--indent-only> +=item B<-io>, B<--indent-only> This flag is used to deactivate all whitespace and line break changes within non-blank lines of code. @@ -536,10 +581,10 @@ useful when perltidy is merely being used to help find a brace error in a large script). Setting this flag is equivalent to setting B<--freeze-newlines> and -B<--freeze-whitespace>. +B<--freeze-whitespace>. If you also want to keep your existing blank lines exactly -as they are, you can add B<--freeze-blank-lines>. +as they are, you can add B<--freeze-blank-lines>. With this option perltidy is still free to modify the indenting (and outdenting) of code and comments as it normally would. If you also want to @@ -553,22 +598,93 @@ this flag is in effect. =item B<-enc=s>, B<--character-encoding=s> -where B=B or B. This flag tells perltidy the character encoding -of both the input and output character streams. The value B causes the -stream to be read and written as UTF-8. The value B causes the stream to -be processed without special encoding assumptions. At present there is no -automatic detection of character encoding (even if there is a C<'use utf8'> -statement in your code) so this flag must be set for streams encoded in UTF-8. -Incorrectly setting this parameter can cause data corruption, so please -carefully check the output. +This flag indicates if the input data stream use a character encoding. +Perltidy does not look for the encoding directives in the soure stream, such +as B, and instead relies on this flag to determine the encoding. +(Note that perltidy often works on snippets of code rather than complete files +so it cannot rely on B directives). + +The possible values for B are: + + -enc=none if no encoding is used, or + -enc=utf8 for encoding in utf8 + -enc=guess if perltidy should guess between these two possibilities. + +The value B causes the stream to be processed without special encoding +assumptions. This is appropriate for files which are written in single-byte +character encodings such as latin-1. + +The value B causes the stream to be read and written as +UTF-8. If the input stream cannot be decoded with this encoding then +processing is not done. -The default is B. +The value B tells perltidy to guess between either utf8 encoding or no +encoding (meaning one character per byte). The B option uses the +Encode::Guess module which has been found to be reliable at detecting +if a file is encoded in utf8 or not. -The abbreviations B<-utf8> or B<-UTF8> are equivalent to B<-enc=utf8>. -So to process a file named B which is encoded in UTF-8 you can use: +The current default is B. + +The abbreviations B<-utf8> or B<-UTF8> are equivalent to B<-enc=utf8>, and the +abbreviation B<-guess> is equivalent to B<-enc=guess>. So to process a file +named B which is encoded in UTF-8 you can use: perltidy -utf8 file.pl +or + + perltidy -guess file.pl + +or simply + + perltidy file.pl + +since B<-guess> is the default. + +To process files with an encoding other than UTF-8, it would be necessary to +write a short program which calls the Perl::Tidy module with some pre- and +post-processing to handle decoding and encoding. + +=item B<-eos=s>, B<--encode-output-strings=s> + +This flag has been added to resolve an issue involving the interface between +Perl::Tidy and calling programs, and in particular B. +By default Perl::Tidy returns unencoded strings to the calling +program, but some programs expect encoded strings. Setting this flag causes +Perl::Tidy to return encoded output strings which it decoded. For some +background information see +L and +L. + +If you only run the B binary this flag has no effect. + +If you use B with encoded files and encounter irregularities such as +B messages you should set this flag. + +Additional information can be found in the man pages for the B module. + +=item B<-gcs>, B<--use-unicode-gcstring> + +This flag controls whether or not perltidy may use module Unicode::GCString to +obtain accurate display widths of wide characters. The default +is B<--nouse-unicode-gcstring>. + +If this flag is set, and text is encoded, perltidy will look for the module +Unicode::GCString and, if found, will use it to obtain character display +widths. This can improve displayed vertical alignment for files with wide +characters. It is a nice feature but it is off by default to avoid conflicting +formatting when there are multiple developers. Perltidy installation does not +require Unicode::GCString, so users wanting to use this feature need set this +flag and also to install Unicode::GCString separately. + +If this flag is set and perltidy does not find module Unicode::GCString, +a warning message will be produced and processing will continue but without +the potential benefit provided by the module. + +Also note that actual vertical alignment depends upon the fonts used by the +text display software, so vertical alignment may not be optimal even when +Unicode::GCString is used. + =item B<-ole=s>, B<--output-line-ending=s> where s=C, C, C, or C. This flag tells perltidy @@ -585,6 +701,17 @@ input comes from a filename (rather than stdin, for example). If perltidy has trouble determining the input file line ending, it will revert to the default behavior of using the line ending of the host system. +=item B<-atnl>, B<--add-terminal-newline> + +This flag, which is enabled by default, allows perltidy to terminate the last +line of the output stream with a newline character, regardless of whether or +not the input stream was terminated with a newline character. If this flag is +negated, with B<-natnl>, then perltidy will add a terminal newline to the the +output stream only if the input stream is terminated with a newline. + +Negating this flag may be useful for manipulating one-line scripts intended for +use on a command line. + =item B<-it=n>, B<--iterations=n> This flag causes perltidy to do B complete iterations. The reason for this @@ -607,8 +734,10 @@ control. For all practical purposes one either does or does not want to be sure that the output is converged, and there is no penalty to using a large iteration limit since perltidy will check for convergence and stop iterating as soon as possible. The default is B<-nconv> (no convergence check). Using -B<-conv> will approximately double run time since normally one extra iteration -is required to verify convergence. +B<-conv> will approximately double run time since typically one extra iteration +is required to verify convergence. No extra iterations are required if no new +line breaks are made, and two extra iterations are occasionally needed when +reformatting complex code structures, such as deeply nested ternary statements. =back @@ -621,27 +750,67 @@ is required to verify convergence. Continuation indentation is extra indentation spaces applied when a long line is broken. The default is n=2, illustrated here: - my $level = # -ci=2 + my $level = # -ci=2 ( $max_index_to_go >= 0 ) ? $levels_to_go[0] : $last_output_level; The same example, with n=0, is a little harder to read: - my $level = # -ci=0 + my $level = # -ci=0 ( $max_index_to_go >= 0 ) ? $levels_to_go[0] : $last_output_level; The value given to B<-ci> is also used by some commands when a small space is required. Examples are commands for outdenting labels, -B<-ola>, and control keywords, B<-okw>. +B<-ola>, and control keywords, B<-okw>. + +When default values are not used, it is recommended that either + +(1) the value B given with B<-ci=n> be no more than about one-half of the +number of spaces assigned to a full indentation level on the B<-i=n> command, or + +(2) the flag B<-extended-continuation-indentation> is used (see next section). -When default values are not used, it is suggested that the value B -given with B<-ci=n> be no more than about one-half of the number of -spaces assigned to a full indentation level on the B<-i=n> command. +=item B<-xci>, B<--extended-continuation-indentation> -=item B<-sil=n> B<--starting-indentation-level=n> +This flag allows perltidy to use some improvements which have been made to its +indentation model. One of the things it does is "extend" continuation +indentation deeper into structures, hence the name. The improved indentation +is particularly noticeable when the flags B<-ci=n> and B<-i=n> use the same value of +B. There are no significant disadvantages to using this flag, but to avoid +disturbing existing formatting the default is not to use it, B<-nxci>. + +Please see the section L<"B<-pbp>, B<--perl-best-practices>"> for an example of +how this flag can improve the formatting of ternary statements. It can also +improve indentation of some multi-line qw lists as shown below. + + # perltidy + foreach $color ( + qw( + AntiqueWhite3 Bisque1 Bisque2 Bisque3 Bisque4 + SlateBlue3 RoyalBlue1 SteelBlue2 DeepSkyBlue3 + ), + qw( + LightBlue1 DarkSlateGray1 Aquamarine2 DarkSeaGreen2 + SeaGreen1 Yellow1 IndianRed1 IndianRed2 Tan1 Tan4 + ) + ) + + # perltidy -xci + foreach $color ( + qw( + AntiqueWhite3 Bisque1 Bisque2 Bisque3 Bisque4 + SlateBlue3 RoyalBlue1 SteelBlue2 DeepSkyBlue3 + ), + qw( + LightBlue1 DarkSlateGray1 Aquamarine2 DarkSeaGreen2 + SeaGreen1 Yellow1 IndianRed1 IndianRed2 Tan1 Tan4 + ) + ) + +=item B<-sil=n> B<--starting-indentation-level=n> By default, perltidy examines the input file and tries to determine the starting indentation level. While it is often zero, it may not be -zero for a code snippet being sent from an editing session. +zero for a code snippet being sent from an editing session. To guess the starting indentation level perltidy simply assumes that indentation scheme used to create the code snippet is the same as is being used @@ -656,10 +825,19 @@ guessed indentation will be wrong. If the default method does not work correctly, or you want to change the starting level, use B<-sil=n>, to force the starting level to be n. -=item List indentation using B<-lp>, B<--line-up-parentheses> +=item B using B<--line-up-parentheses>, B<-lp> or B<--extended--line-up-parentheses> , B<-xlp> -By default, perltidy indents lists with 4 spaces, or whatever value -is specified with B<-i=n>. Here is a small list formatted in this way: +These flags provide an alternative indentation method for list data. The +original flag for this is B<-lp>, but it has some limitations (explained below) +which are avoided with the newer B<-xlp> flag. So B<-xlp> is probably the better +choice for new work, but the B<-lp> flag is retained to minimize changes to +existing formatting. +If you enter both B<-lp> and B<-xlp>, then B<-xlp> will be used. + + +In the default indentation method perltidy indents lists with 4 spaces, or +whatever value is specified with B<-i=n>. Here is a small list formatted in +this way: # perltidy (default) @month_of_year = ( @@ -667,40 +845,178 @@ is specified with B<-i=n>. Here is a small list formatted in this way: 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ); -Use the B<-lp> flag to add extra indentation to cause the data to begin -past the opening parentheses of a sub call or list, or opening square -bracket of an anonymous array, or opening curly brace of an anonymous -hash. With this option, the above list would become: +The B<-lp> or B<-xlp> flags add extra indentation to cause the data to begin +past the opening parentheses of a sub call or list, or opening square bracket +of an anonymous array, or opening curly brace of an anonymous hash. With this +option, the above list would become: - # perltidy -lp + # perltidy -lp or -xlp @month_of_year = ( 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ); -If the available line length (see B<-l=n> ) does not permit this much +If the available line length (see B<-l=n> ) does not permit this much space, perltidy will use less. For alternate placement of the closing paren, see the next section. -This option has no effect on code BLOCKS, such as if/then/else blocks, -which always use whatever is specified with B<-i=n>. Also, the -existence of line breaks and/or block comments between the opening and -closing parens may cause perltidy to temporarily revert to its default -method. +These flags have no effect on code BLOCKS, such as if/then/else blocks, +which always use whatever is specified with B<-i=n>. + +Some limitiations on these flags are: + +=over 4 + +=item * + +A limitation on B<-lp>, but not B<-xlp>, occurs in situations where perltidy +does not have complete freedom to choose line breaks. Then it may temporarily revert +to its default indentation method. This can occur for example if there are +blank lines, block comments, multi-line quotes, or side comments between the +opening and closing parens, braces, or brackets. It will also occur if a +multi-line anonymous sub occurs within a container since that will impose +specific line breaks (such as line breaks after statements). + +=item * + +For both the B<-lp> and B<-xlp> flags, any parameter which significantly +restricts the ability of perltidy to choose newlines will conflict with these +flags and will cause them to be deactivated. These include B<-io>, B<-fnl>, +B<-nanl>, and B<-ndnl>. + +=item * + +The B<-lp> and B<-xlp> options may not be used together with the B<-t> tabs option. +They may, however, be used with the B<-et=n> tab method + +=back + +There are some potential disadvantages of this indentation method compared to +the default method that should be noted: + +=over 4 + +=item * + +The available line length can quickly be used up if variable names are +long. This can cause deeply nested code to quickly reach the line length +limit, and become badly formatted, much sooner than would occur with the +default indentation method. + +=item * + +Since the indentation depends on the lengths of variable names, small +changes in variable names can cause changes in indentation over many lines in a +file. This means that minor name changes can produce significant file +differences. This can be annoying and does not occur with the default +indentation method. + +=back + +Some things that can be done to minimize these problems are: + +=over 4 + +=item * + +Increase B<--maximum-line-length=n> above the default B characters if +necessary. + +=item * + +If you use B<-xlp> then long side comments can limit the indentation over +multiple lines. Consider adding the flag B<--ignore-side-comment-lengths> to +prevent this, or minimizing the use of side comments. + +=item * + +Apply this style in a limited way. By default, it applies to all list +containers (not just lists in parentheses). The next section describes how to +limit this style to, for example, just function calls. The default indentation +method will be applied elsewhere. + +=back + +=item B<-lpil=s>, B<--line-up-parentheses-inclusion-list> and B<-lpxl=s>, B<--line-up-parentheses-exclusion-list> + +The following discussion is written for B<-lp> but applies equally to the newer B<-xlp> version. +By default, the B<-lp> flag applies to as many containers as possible. +The set of containers to which the B<-lp> style applies can be reduced by +either one of these two flags: + +Use B<-lpil=s> to specify the containers to which B<-lp> applies, or + +use B<-lpxl=s> to specify the containers to which B<-lp> does NOT apply. + +Only one of these two flags may be used. Both flags can achieve the same +result, but the B<-lpil=s> flag is much easier to describe and use and is +recommended. The B<-lpxl=s> flag was the original implementation and is +only retained for backwards compatibility. + +This list B for these parametes is a string with space-separated items. +Each item consists of up to three pieces of information in this order: (1) an +optional letter code (2) a required container type, and (3) an optional numeric +code. + +The only required piece of information is a container type, which is one of +'(', '[', or '{'. For example the string + + -lpil='(' + +means use -lp formatting only on lists within parentheses, not lists in square-bracets or braces. +The same thing could alternatively be specified with + + -lpxl = '[ {' + +which says to exclude lists within square-brackets and braces. So what remains is lists within parentheses. + +A second optional item of information which can be given for parentheses is an alphanumeric +letter which is used to limit the selection further depending on the type of +token immediately before the paren. The possible letters are currently 'k', +'K', 'f', 'F', 'w', and 'W', with these meanings for matching whatever precedes an opening paren: + + 'k' matches if the previous nonblank token is a perl builtin keyword (such as 'if', 'while'), + 'K' matches if 'k' does not, meaning that the previous token is not a keyword. + 'f' matches if the previous token is a function other than a keyword. + 'F' matches if 'f' does not. + 'w' matches if either 'k' or 'f' match. + 'W' matches if 'w' does not. + +For example: + + -lpil = 'f(' + +means only apply -lp to function calls, and + + -lpil = 'w(' + +means only apply -lp to parenthesized lists which follow a function or a keyword. + +This last example could alternatively be written using the B<-lpxl=s> flag as + + -lpxl = '[ { W(' + +which says exclude B<-lp> for lists within square-brackets, braces, and parens NOT preceded by +a keyword or function. Clearly, the B<-lpil=s> method is easier to understand. + +An optional numeric code may follow any of the container types to further refine the selection based +on container contents. The numeric codes are: -Note: The B<-lp> option may not be used together with the B<-t> tabs option. -It may, however, be used with the B<-et=n> tab method. + '0' or blank: no check on contents is made + '1' exclude B<-lp> unless the contents is a simple list without sublists + '2' exclude B<-lp> unless the contents is a simple list without sublists, without + code blocks, and without ternary operators -In addition, any parameter which significantly restricts the ability of -perltidy to choose newlines will conflict with B<-lp> and will cause -B<-lp> to be deactivated. These include B<-io>, B<-fnl>, B<-nanl>, and -B<-ndnl>. The reason is that the B<-lp> indentation style can require -the careful coordination of an arbitrary number of break points in -hierarchical lists, and these flags may prevent that. +For example, + + -lpil = 'f(2' + +means only apply -lp to function call lists which do not contain any sublists, +code blocks or ternary expressions. =item B<-cti=n>, B<--closing-token-indentation> -The B<-cti=n> flag controls the indentation of a line beginning with +The B<-cti=n> flag controls the indentation of a line beginning with a C<)>, C<]>, or a non-block C<}>. Such a line receives: -cti = 0 no extra indentation (default) @@ -712,7 +1028,7 @@ a C<)>, C<]>, or a non-block C<}>. Such a line receives: The flags B<-cti=1> and B<-cti=2> work well with the B<-lp> flag (previous section). - + # perltidy -lp -cti=1 @month_of_year = ( 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', @@ -731,10 +1047,10 @@ B is constrained to be no more than one indentation level. If desired, this control can be applied independently to each of the closing container token types. In fact, B<-cti=n> is merely an -abbreviation for B<-cpi=n -csbi=n -cbi=n>, where: +abbreviation for B<-cpi=n -csbi=n -cbi=n>, where: B<-cpi> or B<--closing-paren-indentation> controls B<)>'s, -B<-csbi> or B<--closing-square-bracket-indentation> controls B<]>'s, -B<-cbi> or B<--closing-brace-indentation> controls non-block B<}>'s. +B<-csbi> or B<--closing-square-bracket-indentation> controls B<]>'s, +B<-cbi> or B<--closing-brace-indentation> controls non-block B<}>'s. =item B<-icp>, B<--indent-closing-paren> @@ -756,6 +1072,61 @@ terminates a code block . For example, The default is not to do this, indicated by B<-nicb>. + +=item B<-nib>, B<--non-indenting-braces> + +Normally, lines of code contained within a pair of block braces receive one +additional level of indentation. This flag, which is enabled by default, +causes perltidy to look for +opening block braces which are followed by a special side comment. This special +side comment is B<#<<<> by default. If found, the code between this opening brace and its +corresponding closing brace will not be given the normal extra indentation +level. For example: + + { #<<< a closure to contain lexical vars + + my $var; # this line does not get one level of indentation + ... + + } + + # this line does not 'see' $var; + +This can be useful, for example, when combining code from different files. +Different sections of code can be placed within braces to keep their lexical +variables from being visible to the end of the file. To keep the new braces +from causing all of their contained code to be indented if you run perltidy, +and possibly introducing new line breaks in long lines, you can mark the +opening braces with this special side comment. + +Only the opening brace needs to be marked, since perltidy knows where the +closing brace is. Braces contained within marked braces may also be marked +as non-indenting. + +If your code happens to have some opening braces followed by '#<<<', and you +don't want this behavior, you can use B<-nnib> to deactivate it. To make it +easy to remember, the default string is the same as the string for starting a +B section. There is no confusion because in that case it is +for a block comment rather than a side-comment. + +The special side comment can be changed with the next parameter. + + +=item B<-nibp=s>, B<--non-indenting-brace-prefix=s> + +The B<-nibp=string> parameter may be used to change the marker for +non-indenting braces. The default is equivalent to -nibp='#<<<'. The string +that you enter must begin with a # and should be in quotes as necessary to get +past the command shell of your system. This string is the leading text of a +regex pattern that is constructed by appending pre-pending a '^' and appending +a'\s', so you must also include backslashes for characters to be taken +literally rather than as patterns. + +For example, to match the side comment '#++', the parameter would be + + -nibp='#\+\+' + + =item B<-olq>, B<--outdent-long-quotes> When B<-olq> is set, lines which is a quoted string longer than the @@ -770,7 +1141,7 @@ B<--outdent-long-comments>, and it is included for compatibility with previous versions of perltidy. The negation of this also works, B<-noll> or B<--nooutdent-long-lines>, and is equivalent to setting B<-nolq> and B<-nolc>. -=item Outdenting Labels: B<-ola>, B<--outdent-labels> +=item B B<-ola>, B<--outdent-labels> This command will cause labels to be outdented by 2 spaces (or whatever B<-ci> has been set to), if possible. This is the default. For example: @@ -782,9 +1153,9 @@ has been set to), if possible. This is the default. For example: fixit($i); } -Use B<-nola> to not outdent labels. +Use B<-nola> to not outdent labels. To control line breaks after labels see L. -=item Outdenting Keywords +=item B =over 4 @@ -806,9 +1177,9 @@ For example, using C on the previous example gives: fixit($i); } -The default is not to do this. +The default is not to do this. -=item Specifying Outdented Keywords: B<-okwl=string>, B<--outdent-keyword-list=string> +=item B B<-okwl=string>, B<--outdent-keyword-list=string> This command can be used to change the keywords which are outdented with the B<-okw> command. The parameter B is a required list of perl @@ -838,7 +1209,7 @@ causes the rest of the whitespace commands in this section, the Code Indentation section, and the Comment Control section to be ignored. -=item Tightness of curly braces, parentheses, and square brackets. +=item B Here the term "tightness" will mean the closeness with which pairs of enclosing tokens, such as parentheses, contain the quantities @@ -858,17 +1229,17 @@ values, 0, 1, and 2: When n is 0, there is always a space to the right of a '(' and to the left of a ')'. For n=2 there is never a space. For n=1, the default, there is a space unless the quantity within the parens is a single token, such -as an identifier or quoted string. +as an identifier or quoted string. Likewise, the parameter B<-sbt=n> or B<--square-bracket-tightness=n> controls the space within square brackets, as illustrated below. $width = $col[ $j + $k ] - $col[ $j ]; # -sbt=0 $width = $col[ $j + $k ] - $col[$j]; # -sbt=1 (default) - $width = $col[$j + $k] - $col[$j]; # -sbt=2 + $width = $col[$j + $k] - $col[$j]; # -sbt=2 Curly braces which do not contain code blocks are controlled by -the parameter B<-bt=n> or B<--brace-tightness=n>. +the parameter B<-bt=n> or B<--brace-tightness=n>. $obj->{ $parsed_sql->{ 'table' }[0] }; # -bt=0 $obj->{ $parsed_sql->{'table'}[0] }; # -bt=1 (default) @@ -876,7 +1247,7 @@ the parameter B<-bt=n> or B<--brace-tightness=n>. And finally, curly braces which contain blocks of code are controlled by the parameter B<-bbt=n> or B<--block-brace-tightness=n> as illustrated in the -example below. +example below. %bf = map { $_ => -M $_ } grep { /\.deb$/ } dirents '.'; # -bbt=0 (default) %bf = map { $_ => -M $_ } grep {/\.deb$/} dirents '.'; # -bbt=1 @@ -891,9 +1262,9 @@ abbreviation for the combination <-pt=n -sbt=n -bt=n -bbt=n>. The flag B<-tso> causes certain perl token sequences (secret operators) which might be considered to be a single operator to be formatted "tightly" -(without spaces). The operators currently modified by this flag are: +(without spaces). The operators currently modified by this flag are: - 0+ +0 ()x!! ~~<> ,=> =( )= + 0+ +0 ()x!! ~~<> ,=> =( )= For example the sequence B<0 +>, which converts a string to a number, would be formatted without a space: B<0+> when the B<-tso> flag is set. This @@ -920,7 +1291,7 @@ B<-nsfs> or B<--nospace-for-semicolon> to deactivate it. =item B<-asc>, B<--add-semicolons> -Setting B<-asc> allows perltidy to add any missing optional semicolon at the end +Setting B<-asc> allows perltidy to add any missing optional semicolon at the end of a line which is followed by a closing curly brace on the next line. This is the default, and may be deactivated with B<-nasc> or B<--noadd-semicolons>. @@ -934,7 +1305,7 @@ comment). =item B<-aws>, B<--add-whitespace> -Setting this option allows perltidy to add certain whitespace improve +Setting this option allows perltidy to add certain whitespace to improve code readability. This is the default. If you do not want any whitespace added, but are willing to have some whitespace deleted, use B<-naws>. (Use B<-fws> to leave whitespace completely unchanged). @@ -946,7 +1317,7 @@ between characters, if necessary. This is the default. If you do not want any old whitespace removed, use B<-ndws> or B<--nodelete-old-whitespace>. -=item Detailed whitespace controls around tokens +=item B For those who want more detailed control over the whitespace around tokens, there are four parameters which can directly modify the default @@ -985,22 +1356,97 @@ than fixed rules, because perltidy must try to resolve conflicts that arise between them and all of the other rules that it uses. One conflict that can arise is if, between two tokens, the left token wants a space and the right one doesn't. In this case, the token not wanting -a space takes priority. +a space takes priority. It is necessary to have a list of all token types in order to create this type of input. Such a list can be obtained by the command B<--dump-token-types>. Also try the B<-D> flag on a short snippet of code -and look at the .DEBUG file to see the tokenization. +and look at the .DEBUG file to see the tokenization. B Be sure to put these tokens in quotes to avoid having them misinterpreted by your command shell. -=item Space between specific keywords and opening paren +=item B + +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: + + sub print_div { + my ($x,$y)=@_; + print $x/$y; + } + +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 + + sub print_div { + my ($x,$y)=@_; + print $x /$y; + } + +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. + +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. + +In perltidy this is implemented in the tokenizer by marking token following a +B keyword as a special type B. When formatting is being done, +whitespace following this token type is generally left unchanged as a precaution +against changing program behavior. This is excessively conservative but simple +and easy to implement. Keywords which are treated similarly to B include +B, B, B, B. Changes in spacing around parameters +following these keywords may have to be made manually. For example, the space, +or lack of space, after the parameter $foo in the following line will be +unchanged in formatting. + + system($foo ); + system($foo); + +To find if a token is of type B you can use B. For the +first line above the result is + + 1: system($foo ); + 1: kkkkkk{ZZZZb}; + +which shows that B is type B (keyword) and $foo is type B. + +=item B + +Despite these precautions, it is still possible to introduce syntax errors with +some asymmetric whitespace rules, particularly when call parameters are not +placed in containing parens or braces. For example, the following two lines will +be parsed by perl without a syntax error: + + # original programming, syntax ok + my @newkeys = map $_-$nrecs+@data, @oldkeys; + + # perltidy default, syntax ok + my @newkeys = map $_ - $nrecs + @data, @oldkeys; + +But the following will give a syntax error: + + # perltidy -nwrs='-' + my @newkeys = map $_ -$nrecs + @data, @oldkeys; + +For another example, the following two lines will be parsed without syntax error: + + # original programming, syntax ok + for my $severity ( reverse $SEVERITY_LOWEST+1 .. $SEVERITY_HIGHEST ) { ... } + + # perltidy default, syntax ok + for my $severity ( reverse $SEVERITY_LOWEST + 1 .. $SEVERITY_HIGHEST ) { ... } + +But the following will give a syntax error: + + # perltidy -nwrs='+', syntax error: + for my $severity ( reverse $SEVERITY_LOWEST +1 .. $SEVERITY_HIGHEST ) { ... } + +To avoid subtle parsing problems like this, it is best to avoid spacing a +binary operator asymetrically with a space on the left but not on the right. + +=item B When an opening paren follows a Perl keyword, no space is introduced after the keyword, unless it is (by default) one of these: - my local our and or eq ne if else elsif until unless + my local our and or xor eq ne if else elsif until unless while for foreach return switch case given when These defaults can be modified with two commands: @@ -1009,7 +1455,7 @@ B<-sak=s> or B<--space-after-keyword=s> adds keywords. B<-nsak=s> or B<--nospace-after-keyword=s> removes keywords. -where B is a list of keywords (in quotes if necessary). For example, +where B is a list of keywords (in quotes if necessary). For example, my ( $a, $b, $c ) = @_; # default my( $a, $b, $c ) = @_; # -nsak="my local our" @@ -1023,7 +1469,7 @@ keywords (my, local, our) you could use B<-nsak="*" -sak="my local our">. To put a space after all keywords, see the next item. -=item Space between all keywords and opening parens +=item B When an opening paren follows a function or keyword, no space is introduced after the keyword except for the keywords noted in the previous item. To @@ -1032,21 +1478,158 @@ use the command: B<-skp> or B<--space-keyword-paren> -You will probably also want to use the flag B<-sfp> (next item) too. +You may also want to use the flag B<-sfp> (next item) too. -=item Space between all function names and opening parens +=item B -When an opening paren follows a function the default is not to introduce -a space. To cause a space to be introduced use: +When an opening paren follows a function the default and recommended formatting +is not to introduce a space. To cause a space to be introduced use: B<-sfp> or B<--space-function-paren> - myfunc( $a, $b, $c ); # default + myfunc( $a, $b, $c ); # default myfunc ( $a, $b, $c ); # -sfp You will probably also want to use the flag B<-skp> (previous item) too. -=item Trimming whitespace around C quotes +The reason this is not recommended is that spacing a function paren can make a +program vulnerable to parsing problems by Perl. For example, the following +two-line program will run as written but will have a syntax error if +reformatted with -sfp: + + if ( -e filename() ) { print "I'm here\n"; } + sub filename { return $0 } + +In this particular case the syntax error can be removed if the line order is +reversed, so that Perl parses 'sub filename' first. + +=item B<-fpva> or B<--function-paren-vertical-alignment> + +A side-effect of using the B<-sfp> flag is that the parens may become vertically +aligned. For example, + + # perltidy -sfp + myfun ( $aaa, $b, $cc ); + mylongfun ( $a, $b, $c ); + +This is the default behavior. To prevent this alignment use B<-nfpva>: + + # perltidy -sfp -nfpva + myfun ( $aaa, $b, $cc ); + mylongfun ( $a, $b, $c ); + +=item B<-spp=n> or B<--space-prototype-paren=n> + +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. + + sub usage(); + +This integer B may have the value 0, 1, or 2 as follows: + + -spp=0 means no space before the paren + -spp=1 means follow the example of the source code [DEFAULT] + -spp=2 means always put a space before the paren + +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 +applying the different options would be: + + sub usage(); # n=0 [no space] + sub usage(); # n=1 [default; follows input] + sub usage (); # n=2 [space] + +=item B<-kpit=n> or B<--keyword-paren-inner-tightness=n> + +The space inside of an opening paren, which itself follows a certain keyword, +can be controlled by this parameter. The space on the inside of the +corresponding closing paren will be treated in the same (balanced) manner. +This parameter has precedence over any other paren spacing rules. The values +of B are as follows: + + -kpit=0 means always put a space (not tight) + -kpit=1 means ignore this parameter [default] + -kpit=2 means never put a space (tight) + +To illustrate, the following snippet is shown formatted in three ways: + + if ( seek( DATA, 0, 0 ) ) { ... } # perltidy (default) + if (seek(DATA, 0, 0)) { ... } # perltidy -pt=2 + if ( seek(DATA, 0, 0) ) { ... } # perltidy -pt=2 -kpit=0 + +In the second case the -pt=2 parameter makes all of the parens tight. In the +third case the -kpit=0 flag causes the space within the 'if' parens to have a +space, since 'if' is one of the keywords to which the -kpit flag applies by +default. The remaining parens are still tight because of the -pt=2 parameter. + +The set of keywords to which this parameter applies are by default are: + + if elsif unless while until for foreach + +These can be changed with the parameter B<-kpitl=s> described in the next section. + + +=item B<-kpitl=string> or B<--keyword-paren-inner-tightness=string> + +This command can be used to change the keywords to which the the B<-kpit=n> +command applies. The parameter B is a required list either keywords or +functions, which should be placed in quotes if there are more than one. By +itself, this parameter does not cause any change in spacing, so the B<-kpit=n> +command is still required. + +For example, the commands C<-kpitl="if else while" -kpit=2> will cause the just +the spaces inside parens following 'if', 'else', and 'while' keywords to +follow the tightness value indicated by the B<-kpit=2> flag. + +=item B<-lop> or B<--logical-padding> + +In the following example some extra space has been inserted on the second +line between the two open parens. This extra space is called "logical padding" +and is intended to help align similar things vertically in some logical +or ternary expressions. + + # perltidy [default formatting] + $same = + ( ( $aP eq $bP ) + && ( $aS eq $bS ) + && ( $aT eq $bT ) + && ( $a->{'title'} eq $b->{'title'} ) + && ( $a->{'href'} eq $b->{'href'} ) ); + +Note that this is considered to be a different operation from "vertical +alignment" because space at just one line is being adjusted, whereas in +"vertical alignment" the spaces at all lines are being adjusted. So it sort of +a local version of vertical alignment. + +Here is an example involving a ternary operator: + + # perltidy [default formatting] + $bits = + $top > 0xffff ? 32 + : $top > 0xff ? 16 + : $top > 1 ? 8 + : 1; + +This behavior is controlled with the flag B<--logical-padding>, which is set +'on' by default. If it is not desired it can be turned off using +B<--nological-padding> or B<-nlop>. The above two examples become, with +B<-nlop>: + + # perltidy -nlop + $same = + ( ( $aP eq $bP ) + && ( $aS eq $bS ) + && ( $aT eq $bT ) + && ( $a->{'title'} eq $b->{'title'} ) + && ( $a->{'href'} eq $b->{'href'} ) ); + + # perltidy -nlop + $bits = + $top > 0xffff ? 32 + : $top > 0xff ? 16 + : $top > 1 ? 8 + : 1; + + +=item B quotes> B<-tqw> or B<--trim-qw> provide the default behavior of trimming spaces around multi-line C quotes and indenting them appropriately. @@ -1058,22 +1641,22 @@ some versions of perl, trimming C quotes changes the syntax tree. =item B<-sbq=n> or B<--space-backslash-quote=n> -Lines like +lines like $str1=\"string1"; $str2=\'string2'; can confuse syntax highlighters unless a space is included between the backslash and the single or double quotation mark. -This can be controlled with the value of B as follows: +this can be controlled with the value of B as follows: -sbq=0 means no space between the backslash and quote -sbq=1 means follow the example of the source code -sbq=2 means always put a space between the backslash and quote -The default is B<-sbq=1>, meaning that a space will be used 0if there is one in the source code. +The default is B<-sbq=1>, meaning that a space will be used if there is one in the source code. -=item Trimming trailing whitespace from lines of POD +=item B B<-trp> or B<--trim-pod> will remove trailing whitespace from lines of POD. The default is not to do this. @@ -1134,7 +1717,7 @@ whenever possible. The default, n=0, will not do this. =item B<-iscl>, B<--ignore-side-comment-lengths> This parameter causes perltidy to ignore the length of side comments when -setting line breaks. The default, B<-niscl>, is to include the length of +setting line breaks. The default, B<-niscl>, is to include the length of side comments when breaking lines to stay within the length prescribed by the B<-l=n> maximum line length parameter. For example, the following long single line would remain intact with -l=80 and -iscl: @@ -1147,7 +1730,7 @@ whereas without the -iscl flag the line will be broken: perltidy -l=80 $vmsfile =~ s/;[\d\-]*$// ; # Clip off version number; we can use a newer version as well - + =item B<-hsc>, B<--hanging-side-comments> @@ -1161,11 +1744,11 @@ comments", which are something like this: A comment is considered to be a hanging side comment if (1) it immediately follows a line with a side comment, or another hanging side comment, and (2) there is some leading whitespace on the line. -To deactivate this feature, use B<-nhsc> or B<--nohanging-side-comments>. +To deactivate this feature, use B<-nhsc> or B<--nohanging-side-comments>. If block comments are preceded by a blank line, or have no leading whitespace, they will not be mistaken as hanging side comments. -=item Closing Side Comments +=item B A closing side comment is a special comment which perltidy can automatically create and place after the closing brace of a code block. @@ -1198,7 +1781,7 @@ for the C and C blocks, because they were below the 6 line cutoff limit for adding closing side comments. This limit may be changed with the B<-csci> command, described below. -The command B<-dcsc> (or B<--delete-closing-side-comments>) reverses this +The command B<-dcsc> (or B<--delete-closing-side-comments>) reverses this process and removes these comments. Several commands are available to modify the behavior of these two basic @@ -1206,7 +1789,7 @@ commands, B<-csc> and B<-dcsc>: =over 4 -=item B<-csci=n>, or B<--closing-side-comment-interval=n> +=item B<-csci=n>, or B<--closing-side-comment-interval=n> where C is the minimum number of lines that a block must have in order for a closing side comment to be added. The default value is @@ -1225,7 +1808,7 @@ C. To illustrate: Now the C and C blocks are commented. However, now this has become very cluttered. -=item B<-cscp=string>, or B<--closing-side-comment-prefix=string> +=item B<-cscp=string>, or B<--closing-side-comment-prefix=string> where string is the prefix used before the name of the block type. The default prefix, shown above, is C<## end>. This string will be added to @@ -1234,7 +1817,7 @@ order to update, delete, and format them. Any comment identified as a closing side comment will be placed just a single space to the right of its closing brace. -=item B<-cscl=string>, or B<--closing-side-comment-list> +=item B<-cscl=string>, or B<--closing-side-comment-list> where C is a list of block types to be tagged with closing side comments. By default, all code block types preceded by a keyword or @@ -1247,7 +1830,7 @@ affected by any B<-csc> or B<-dcsc> operation: -cscl="sub : BEGIN END" -=item B<-csct=n>, or B<--closing-side-comment-maximum-text=n> +=item B<-csct=n>, or B<--closing-side-comment-maximum-text=n> The text appended to certain block types, such as an C block, is whatever lies between the keyword introducing the block, such as C, @@ -1262,7 +1845,7 @@ first block is C< ( !defined( $_[0] )...>. The existing limit of C caused this text to be truncated, as indicated by the C<...>. See the next flag for additional control of the abbreviated text. -=item B<-cscb>, or B<--closing-side-comments-balanced> +=item B<-cscb>, or B<--closing-side-comments-balanced> As discussed in the previous item, when the closing-side-comment-maximum-text limit is exceeded the comment text must @@ -1282,7 +1865,7 @@ help them by appending appropriate balancing structure: The default is B<-cscb>. -=item B<-csce=n>, or B<--closing-side-comment-else-flag=n> +=item B<-csce=n>, or B<--closing-side-comment-else-flag=n> The default, B, places the text of the opening C statement after any terminal C. @@ -1295,10 +1878,10 @@ side comments. If B is used, the results will be the same as B whenever the resulting line length is less than the maximum allowed. -=item B<-cscb>, or B<--closing-side-comments-balanced> +=item B<-cscb>, or B<--closing-side-comments-balanced> When using closing-side-comments, and the closing-side-comment-maximum-text -limit is exceeded, then the comment text must be abbreviated. +limit is exceeded, then the comment text must be abbreviated. It is terminated with three dots if the B<-cscb> flag is negated: perltidy -csc -ncscb @@ -1311,18 +1894,18 @@ flag tries to help them by appending appropriate terminal balancing structures: perltidy -csc -cscb } ## end foreach my $foo (sort { $b cmp $a ... }) -The default is B<-cscb>. +The default is B<-cscb>. -=item B<-cscw>, or B<--closing-side-comment-warnings> +=item B<-cscw>, or B<--closing-side-comment-warnings> This parameter is intended to help make the initial transition to the use of -closing side comments. +closing side comments. It causes two things to happen if a closing side comment replaces an existing, different closing side comment: first, an error message will be issued, and second, the original side comment will be placed alone on a new specially marked comment -line for later attention. +line for later attention. The intent is to avoid clobbering existing hand-written side comments which happen to match the pattern of closing side comments. This flag @@ -1330,7 +1913,7 @@ should only be needed on the first run with B<-csc>. =back -B +B =over 4 @@ -1344,7 +1927,7 @@ brace. Certain closing styles, such as the use of cuddled elses Please note that adding or deleting of closing side comments takes place only through the commands B<-csc> or B<-dcsc>. The other commands, -if used, merely modify the behavior of these two commands. +if used, merely modify the behavior of these two commands. =item * @@ -1375,7 +1958,7 @@ else. You can always remove them with B<-dcsc>. =back -=item Static Block Comments +=item B Static block comments are block comments with a special leading pattern, C<##> by default, which will be treated slightly differently from other @@ -1389,9 +1972,9 @@ particularly useful for controlling how commented code is displayed. =item B<-sbc>, B<--static-block-comments> When B<-sbc> is used, a block comment with a special leading pattern, C<##> by -default, will be treated specially. +default, will be treated specially. -Comments so identified are treated as follows: +Comments so identified are treated as follows: =over 4 @@ -1403,12 +1986,12 @@ be indented, and otherwise it may be, =item * no new blank line will be -inserted before such a comment, and +inserted before such a comment, and =item * such a comment will never become -a hanging side comment. +a hanging side comment. =back @@ -1424,7 +2007,7 @@ Without this convention, the above code would become @month_of_year = ( # -nsbc 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', - + ## 'Dec', 'Nov' 'Nov', 'Dec' ); @@ -1436,8 +2019,8 @@ The default is to use B<-sbc>. This may be deactivated with B<-nsbc>. This parameter defines the prefix used to identify static block comments when the B<-sbc> parameter is set. The default prefix is C<##>, -corresponding to C<-sbcp=##>. The prefix is actually part of a perl -pattern used to match lines and it must either begin with C<#> or C<^#>. +corresponding to C<-sbcp=##>. The prefix is actually part of a perl +pattern used to match lines and it must either begin with C<#> or C<^#>. In the first case a prefix ^\s* will be added to match any leading whitespace, while in the second case the pattern will match only comments with no leading whitespace. For example, to @@ -1452,7 +2035,7 @@ be formed. A pattern which can be useful is: - -sbcp=^#{2,}[^\s#] + -sbcp=^#{2,}[^\s#] This pattern requires a static block comment to have at least one character which is neither a # nor a space. It allows a line containing only '#' @@ -1468,7 +2051,7 @@ spaces (or whatever B<-ci=n> has been set to), if possible. =back -=item Static Side Comments +=item B Static side comments are side comments with a special leading pattern. This option can be useful for controlling how commented code is displayed @@ -1488,7 +2071,7 @@ The default is B<-nssc>. This parameter defines the prefix used to identify static side comments when the B<-ssc> parameter is set. The default prefix is C<##>, -corresponding to C<-sscp=##>. +corresponding to C<-sscp=##>. Please note that B<-sscp> merely defines the pattern used to identify static side comments; it will not be used unless the switch B<-ssc> is @@ -1498,33 +2081,26 @@ expression to be formed. =back - =back =head2 Skipping Selected Sections of Code Selected lines of code may be passed verbatim to the output without any -formatting. This feature is enabled by default but can be disabled with -the B<--noformat-skipping> or B<-nfs> flag. It should be used sparingly to -avoid littering code with markers, but it might be helpful for working -around occasional problems. For example it might be useful for keeping -the indentation of old commented code unchanged, keeping indentation of -long blocks of aligned comments unchanged, keeping certain list -formatting unchanged, or working around a glitch in perltidy. - -=over 4 +formatting by marking the starting and ending lines with special comments. +There are two options for doing this. The first option is called +B<--format-skipping> or B<-fs>, and the second option is called +B<--code-skipping> or B<-cs>. -=item B<-fs>, B<--format-skipping> +In both cases the lines of code will be output without any changes. +The difference is that in B<--format-skipping> +perltidy will still parse the marked lines of code and check for errors, +whereas in B<--code-skipping> perltidy will simply pass the lines to the output without any checking. -This flag, which is enabled by default, causes any code between -special beginning and ending comment markers to be passed to the -output without formatting. The default beginning marker is #<<< -and the default ending marker is #>>> but they -may be changed (see next items below). Additional text may appear on -these special comment lines provided that it is separated from the -marker by at least one space. For example +Both of these features are enabled by default and are invoked with special +comment markers. B<--format-skipping> uses starting and ending markers '#<<<' +and '#>>>', like this: - #<<< do not let perltidy touch this + #<<< format skipping: do not let perltidy change my nice formatting my @list = (1, 1, 1, 1, 2, 1, @@ -1532,30 +2108,118 @@ marker by at least one space. For example 1, 4, 6, 4, 1,); #>>> -The comment markers may be placed at any location that a block comment may -appear. If they do not appear to be working, use the -log flag and examine the -F<.LOG> file. Use B<-nfs> to disable this feature. +B<--code-skipping> uses starting and ending markers '#<>V', like +this: + + #< | _ | ] + | <''> + ] + }; + + #>>V + +Additional text may appear on the special comment lines provided that it +is separated from the marker by at least one space, as in the above examples. + +Any number of code-skipping or format-skipping sections may appear in a file. +If an opening code-skipping or format-skipping comment is not followed by a +corresponding closing comment, then skipping continues to the end of the file. +If a closing code-skipping or format-skipping comment appears in a file but +does not follow a corresponding opening comment, then it is treated as an +ordinary comment without any special meaning. + +It is recommended to use B<--code-skipping> only if you need to hide a block of +an extended syntax which would produce errors if parsed by perltidy, and use +B<--format-skipping> otherwise. This is because the B<--format-skipping> +option provides the benefits of error checking, and there are essentially no +limitations on which lines to which it can be applied. The B<--code-skipping> +option, on the other hand, does not do error checking and its use is more +restrictive because the code which remains, after skipping the marked lines, +must be syntactically correct code with balanced containers. + +These features should be used sparingly to avoid littering code with markers, +but they can be helpful for working around occasional problems. + +Note that it may be possible to avoid the use of B<--format-skipping> for the +specific case of a comma-separated list of values, as in the above example, by +simply inserting a blank or comment somewhere between the opening and closing +parens. See the section L. + +The following sections describe the available controls for these options. They +should not normally be needed. + +=over 4 + +=item B<-fs>, B<--format-skipping> + +As explained above, this flag, which is enabled by default, causes any code +between special beginning and ending comment markers to be passed to the output +without formatting. The code between the comments is still checked for errors +however. The default beginning marker is #<<< and the default ending marker is +#>>>. + +Format skipping begins when a format skipping beginning comment is seen and +continues until a format-skipping ending comment is found. + +This feature can be disabled with B<-nfs>. This should not normally be necessary. =item B<-fsb=string>, B<--format-skipping-begin=string> +This and the next parameter allow the special beginning and ending comments to +be changed. However, it is recommended that they only be changed if there is a +conflict between the default values and some other use. If they are used, it +is recommended that they only be entered in a B<.perltidyrc> file, rather than +on a command line. This is because properly escaping these parameters on a +command line can be difficult. + +If changed comment markers do not appear to be working, use the B<-log> flag and +examine the F<.LOG> file to see if and where they are being detected. + The B<-fsb=string> parameter may be used to change the beginning marker for format skipping. The default is equivalent to -fsb='#<<<'. The string that you enter must begin with a # and should be in quotes as necessary to get past the command shell of your system. It is actually the leading text of a pattern that is constructed by appending a '\s', so you must also include backslashes -for characters to be taken literally rather than as patterns. +for characters to be taken literally rather than as patterns. Some examples show how example strings become patterns: -fsb='#\{\{\{' becomes /^#\{\{\{\s/ which matches #{{{ but not #{{{{ -fsb='#\*\*' becomes /^#\*\*\s/ which matches #** but not #*** - -fsb='#\*{2,}' becomes /^#\*{2,}\s/ which matches #** and #***** + -fsb='#\*{2,}' becomes /^#\*{2,}\s/ which matches #** and #***** =item B<-fse=string>, B<--format-skipping-end=string> -The B<-fsb=string> is the corresponding parameter used to change the +The B<-fse=string> is the corresponding parameter used to change the ending marker for format skipping. The default is equivalent to --fse='#<<<'. +-fse='#<<<'. + +The beginning and ending strings may be the same, but it is preferable +to make them different for clarity. + +=item B<-cs>, B<--code-skipping> + +As explained above, this flag, which is enabled by default, causes any code +between special beginning and ending comment markers to be directly passed to +the output without any error checking or formatting. Essentially, perltidy +treats it as if it were a block of arbitrary text. The default beginning +marker is #<>V. + +This feature can be disabled with B<-ncs>. This should not normally be +necessary. + +=item B<-csb=string>, B<--code-skipping-begin=string> + +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>. +The default is equivalent to -csb='#<, B<--code-skipping-end=string> + +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>. +The default is equivalent to -cse='#>>V'. =back @@ -1573,9 +2237,9 @@ Control>. If you do not want any changes to the line breaks within lines of code in your script, set B<-fnl>, and they will remain fixed, and the rest of the commands in -this section and sections +this section and sections L, -L. +L. You may want to use B<-noll> with this. Note: If you also want to keep your blank lines exactly @@ -1590,35 +2254,28 @@ The default is not to use cuddled elses, and is indicated with the flag B<-nce> or B<--nocuddled-else>. Here is a comparison of the alternatives: + # -ce if ($task) { yyy(); - } else { # -ce + } else { zzz(); } + # -nce (default) if ($task) { yyy(); } - else { # -nce (default) + else { zzz(); } -=item B<-cb>, B<--cuddled-blocks> +In this example the keyword B is placed on the same line which begins with +the preceding closing block brace and is followed by its own opening block brace +on the same line. Other keywords and function names which are formatted with +this "cuddled" style are B, B, B, B. -This flag enables the "cuddled else" format style on a chain of specified block -types. The default is to apply it to a chain consisting of try-catch-finally -blocks, but it can apply to any desired chain of blocks by specifying their -names on a separate parameter B<-cbl>, described in the next section. - - # perltidy -cb: - try { - throw Error::Simple( "ok 2\n", 2 ); - } catch Error::Simple with { - my $err = shift; - print "$err"; - } finally { - print "ok 3\n"; - }; +Other block types can be formatted by specifying their names on a +separate parameter B<-cbl>, described in a later section. Cuddling between a pair of code blocks requires that the closing brace of the first block start a new line. If this block is entirely on one line in the @@ -1629,12 +2286,11 @@ the chain. If it spans multiple lines then cuddling is made and continues along the chain, regardless of the sizes of subsequent blocks. Otherwise, short lines remain intact. -So for example, the B<-cb> flag would not have any effect if the above snippet +So for example, the B<-ce> flag would not have any effect if the above snippet is rewritten as - try { throw Error::Simple( "ok 2\n", 2 ); } - catch Error::Simple with { my $err = shift; print "$err"; } - finally { print "ok 3\n"; }; + if ($task) { yyy() } + else { zzz() } If the first block spans multiple lines, then cuddling can be done and will continue for the subsequent blocks in the chain, as illustrated in the previous @@ -1643,63 +2299,50 @@ snippet. If there are blank lines between cuddled blocks they will be eliminated. If there are comments after the closing brace where cuddling would occur then cuddling will be prevented. If this occurs, cuddling will restart later in the -chain if possible. +chain if possible. -The default for this parameter is B<--nocuddled-blocks> +=item B<-cb>, B<--cuddled-blocks> -=item B<-cbl>, B<--cuddled-block-list> +This flag is equivalent to B<-ce>. -The block types to which the B<-cuddled-blocks> style applies is defined by -this parameter. This parameter is a character string, giving a list of -block types separated by dashes. -The default value for this string is +=item B<-cbl>, B<--cuddled-block-list> - -cbl="try-catch-finally" +The built-in default cuddled block types are B. -This string will cause cuddled formatting to be applied to every block in a chain -starting with a "try" and followed by any number of "catch" and "finally" -blocks. +Additional block types to which the B<-cuddled-blocks> style applies can be defined by +this parameter. This parameter is a character string, giving a list of +block types separated by commas or spaces. For example, to cuddle code blocks +of type sort, map and grep, in addition to the default types, the string could +be set to -In general, a string describing a chain of blocks has the form + -cbl="sort map grep" - -cbl="word1-word2-word3-...-wordn" +or equivalently -In this case, a chain begins when an opening block brace preceded by word1 in -the list is encountered. The chain continues if the closing block brace is -followed immediately by any of word2, word3, etc. + -cbl=sort,map,grep -If the leading word, word1, might be repeated later in a chain then it should -also be included amoung the secondary words. +Note however that these particular block types are typically short so there might not be much +opportunity for the cuddled format style. -Multiple chain types may be specified by separating the strings with commas or -spaces. So for example if we have two chains of code blocks, f1-f2-f3 and g1-g2-g3-g4, -they could be specified as +Using commas avoids the need to protect spaces with quotes. - -cbl="f1-f2-f3 g1-g2-g3-g4" -or - -cbl=f1-f2-f3,g1-g2-g3-g4 +As a diagnostic check, the flag B<--dump-cuddled-block-list> or B<-dcbl> can be +used to view the hash of values that are generated by this flag. -Spaces are easier to read but commas may avoid quotation difficulties when -entering data in a command shell. +Finally, note that the B<-cbl> flag by itself merely specifies which blocks are formatted +with the cuddled format. It has no effect unless this formatting style is activated with +B<-ce>. -To define secondary words that apply to all block types, other than those explicitly specified, -the leading word can be omitted. For example, the built-in cuddled-else format specified by -the B<-ce> flag can be approximately specified by +=item B<-cblx>, B<--cuddled-block-list-exclusive> - -cbl="if-else-elsif unless-else-elsif -continue" -or - -cbl=if-else-elsif,unless-else-elsif,-continue - -The final string -continue allows cuddling the optional continue block -which may follow may other block types. - -As a diagnostic check, the flag B<--dump-cuddled-block-list> or B<-dcbl> can be -used to view the hash of values this flag creates. +When cuddled else formatting is selected with B<-ce>, setting this flag causes +perltidy to ignore its built-in defaults and rely exclusively on the block types +specified on the B<-cbl> flag described in the previous section. For example, +to avoid using cuddled B and B, which among in the defaults, the +following set of parameters could be used: -Finally, note that the B<-cbl> flag by itself merely specifies which blocks are formatted -with the cuddled format. It has no effect unless this formatting style is activated with -B<-cb>. + perltidy -ce -cbl='else elsif continue' -cblx =item B<-cbo=n>, B<--cuddled-break-option=n> @@ -1712,8 +2355,8 @@ to cause it to span multiple lines. This parameter controls that decision. The options are: cbo=0 Never force a short block to break. - cbo=1 If the first of a pair of blocks is broken in the input file, - then break the second. + cbo=1 If the first of a pair of blocks is broken in the input file, + then break the second [DEFAULT]. cbo=2 Break open all blocks for maximal cuddled formatting. The default and recommended value is B. With this value, if the starting @@ -1725,32 +2368,54 @@ blocks. The option B produces maximal cuddling but will not allow any short blocks. -Note: at present, this option currently only applies to blocks controlled by -the B<-cb> flag. Cuddling under the B<-ce> flag corresponds approximately to -B<-cbo=1> but cannot currently be changed. +=item B<-bl>, B<--opening-brace-on-new-line>, or B<--brace-left> -=item B<-bl>, B<--opening-brace-on-new-line> +Use the flag B<-bl> to place an opening block brace on a new line: -Use the flag B<-bl> to place the opening brace on a new line: + if ( $input_file eq '-' ) + { + ... + } + +By default it applies to all structural blocks except B and +anonymous subs. + +The default is B<-nbl> which places an opening brace on the same line as +the keyword introducing it if possible. For example, - if ( $input_file eq '-' ) # -bl - { - important_function(); + # default + if ( $input_file eq '-' ) { + ... } -This flag applies to all structural blocks, including named sub's (unless -the B<-sbl> flag is set -- see next item). +When B<-bl> is set, the blocks to which this applies can be controlled with the +parameters B<--brace-left-list> and B<-brace-left-exclusion-list> described in the next sections. + +=item B<-bll=s>, B<--brace-left-list=s> + +Use this parameter to change the types of block braces for which the +B<-bl> flag applies; see L. For example, +B<-bll='if elsif else sub'> would apply it to only C +and named sub blocks. The default is all blocks, B<-bll='*'>. + +=item B<-blxl=s>, B<--brace-left-exclusion-list=s> + +Use this parameter to exclude types of block braces for which the +B<-bl> flag applies; see L. For example, +the default settings B<-bll='*'> and B<-blxl='sort map grep eval asub'> +mean all blocks except B and anonymous sub blocks. -The default style, B<-nbl>, places an opening brace on the same line as -the keyword introducing it. For example, +Note that the lists B<-bll=s> and B<-blxl=s> control the behavior of the +B<-bl> flag but have no effect unless the B<-bl> flag is set. - if ( $input_file eq '-' ) { # -nbl (default) +=item B<-sbl>, B<--opening-sub-brace-on-new-line> -=item B<-sbl>, B<--opening-sub-brace-on-new-line> +The flag B<-sbl> provides a shortcut way to turn on B<-bl> just for named +subs. The same effect can be achieved by turning on B<-bl> +with the block list set as B<-bll='sub'>. -The flag B<-sbl> can be used to override the value of B<-bl> for -the opening braces of named sub's. For example, +For example, perltidy -sbl @@ -1766,10 +2431,9 @@ produces this result: } } -This flag is negated with B<-nsbl>. If B<-sbl> is not specified, -the value of B<-bl> is used. +This flag is negated with B<-nsbl>, which is the default. -=item B<-asbl>, B<--opening-anonymous-sub-brace-on-new-line> +=item B<-asbl>, B<--opening-anonymous-sub-brace-on-new-line> The flag B<-asbl> is like the B<-sbl> flag except that it applies to anonymous sub's instead of named subs. For example @@ -1790,31 +2454,50 @@ produces this result: This flag is negated with B<-nasbl>, and the default is B<-nasbl>. -=item B<-bli>, B<--brace-left-and-indent> +=item B<-bli>, B<--brace-left-and-indent> -The flag B<-bli> is the same as B<-bl> but in addition it causes one -unit of continuation indentation ( see B<-ci> ) to be placed before +The flag B<-bli> is similar to the B<-bl> flag but in addition it causes one +unit of continuation indentation ( see B<-ci> ) to be placed before an opening and closing block braces. -For example, +For example, perltidy -bli gives - if ( $input_file eq '-' ) # -bli + if ( $input_file eq '-' ) { important_function(); } -By default, this extra indentation occurs for blocks of type: -B, B, B, B, B, B, B, -B, B, and also with a preceding label. The next item -shows how to change this. +By default, this extra indentation occurs for block types: +B, B, B, B, B, B, B, B, and +also B and blocks preceded by a B