perltidy [ options ] file1 file2 file3 ... (output goes to file1.tdy, file2.tdy, file3.tdy, ...) perltidy [ options ] file1 -o outfile perltidy [ options ] file1 -st >outfile perltidy [ options ] <infile >outfile
Many users will find enough information in ``EXAMPLES'' to get started. New users may benefit from the short tutorial which can be found at http://perltidy.sourceforge.net/tutorial.html
A convenient aid to systematically defining a set of style parameters can be found at http://perltidy.sourceforge.net/stylekey.html
Perltidy can produce output on either of two modes, depending on the existence of an -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 ``FORMATTING OPTIONS''.
When the -html flag is given, the output is passed through an HTML formatter which is described in ``HTML OPTIONS''.
perltidy somefile.pl
This will produce a file somefile.pl.tdy containing the script reformatted using the default options, which approximate the style suggested in perlstyle(1). The source file somefile.pl is unchanged.
perltidy *.pl
Execute perltidy on all .pl files in the current directory with the default options. The output will be in files with an appended .tdy extension. For any file with an error, there will be a file with extension .ERR.
perltidy -b file1.pl file2.pl
Modify file1.pl and file2.pl in place, and backup the originals to file1.pl.bak and file2.pl.bak. If file1.pl.bak and/or file2.pl.bak already exist, they will be overwritten.
perltidy -b -bext='/' file1.pl file2.pl
Same as the previous example except that the backup files file1.pl.bak and file2.pl.bak will be deleted if there are no errors.
perltidy -gnu somefile.pl
Execute perltidy on file somefile.pl with a style which approximates the GNU Coding Standards for C programs. The output will be somefile.pl.tdy.
perltidy -i=3 somefile.pl
Execute perltidy on file somefile.pl, with 3 columns for each level of indentation (-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 somefile.pl.tdy.
perltidy -i=3 -et=8 somefile.pl
Same as the previous example, except that leading whitespace will be entabbed with one tab character per 8 spaces.
perltidy -ce -l=72 somefile.pl
Execute perltidy on file somefile.pl with all defaults except use ``cuddled elses'' (-ce) and a maximum line length of 72 columns (-l=72) instead of the default 80 columns.
perltidy -g somefile.pl
Execute perltidy on file somefile.pl and save a log file somefile.pl.LOG which shows the nesting of braces, parentheses, and square brackets at the start of every line.
perltidy -html somefile.pl
This will produce a file somefile.pl.html containing the script with html markup. The output file will contain an embedded style sheet in the <HEAD> section which may be edited to change the appearance.
perltidy -html -css=mystyle.css somefile.pl
This will produce a file somefile.pl.html containing the script with html markup. This output file will contain a link to a separate style sheet file mystyle.css. If the file mystyle.css does not exist, it will be created. If it exists, it will not be overwritten.
perltidy -html -pre somefile.pl
Write an html snippet with only the PRE section to somefile.pl.html. 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.
perltidy -html -ss >mystyle.css
Write a style sheet to mystyle.css and exit.
perltidy -html -frm mymodule.pm
Write html with a frame holding a table of contents and the source code. The output files will be mymodule.pm.html (the frame), mymodule.pm.toc.html (the table of contents), and mymodule.pm.src.html (the source code).
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 self-documenting and therefore useful in scripts. It is customary to use two leading dashes for long names, but one may be used.
Most parameters which serve as on/off flags can be negated with a leading ``n'' (for the short name) or a leading ``no'' or ``no-'' (for the long name). For example, the flag to outdent long quotes is -olq or --outdent-long-quotes. The flag to skip this is -nolq or --nooutdent-long-quotes or --no-outdent-long-quotes.
Options may not be bundled together. In other words, options -q and -g may NOT be entered as -qg.
Option names may be terminated early as long as they are uniquely identified. For example, instead of --dump-token-types, it would be sufficient to enter --dump-tok, or even --dump-t, to uniquely identify this command.
perltidy somefile.pl -st >somefile.new.pl
This option may only be used if there is just a single input file. The default is -nst or --nostandard-output.
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 /tmp/somefile.pl.tdy. Otherwise, somefile.pl.tdy will appear in whatever directory contains somefile.pl.
If the path contains spaces, it should be placed in quotes.
This parameter will be ignored if output is being directed to standard output, or if it is being specified explicitly with the -o=s parameter.
A -b flag will be ignored if input is from standard input or goes to standard output, or if the -html flag is set.
In particular, if you want to use both the -b flag and the -pbp (--perl-best-practices) flag, then you must put a -nst flag after the -pbp flag because it contains a -st flag as one of its components, which means that output will go to the standard output stream.
To change the default extension to something other than .bak see ``Specifying File Extensions''.
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.
To indicate that the backup should be deleted include one forward slash, /, in the extension. If any text remains after the slash is removed it will be used to define the backup file extension (which is always created and only deleted if there were no errors).
Here are some examples:
Parameter Extension Backup File Treatment <-bext=bak> F<.bak> Keep (same as the default behavior) <-bext='/'> F<.bak> Delete if no errors <-bext='/backup'> F<.backup> Delete if no errors <-bext='original/'> F<.original> Delete if no errors
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
:n1,n2!perltidy -q
where "n1,n2" represents the selected text. Without the -q flag, any error message may mess up your screen, so be prepared to use your ``undo'' key.
Setting -g also causes the logfile to be saved, so it is not necessary to also include -log.
If no -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.
Setting a negative value of "n" is the same as not setting -g at all.
If you set the -npro flag, perltidy will not look for this file.
perltidy -pro=testcfg
would cause file testcfg to be used instead of the default .perltidyrc.
A pathname begins with three dots, e.g. ``.../.perltidyrc'', indicates that 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.
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 ``Specifying Block Types''). 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.
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 -l=0 is equivalent to setting -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.
To be precise, when the -vmll parameter is set, the maximum line length of a line of code will be M+L*I, where
M is the value of --maximum-line-length=M (-l=M), default 80, I is the value of --indent-columns=I (-i=I), default 4, L is the indentation level of the line of code
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.
The default is not to do this (-nvmll).
For example if one level of indentation equals 4 spaces (-i=4, the default), and one uses -wc=15, then if the leading whitespace on a line exceeds about 4*15=60 spaces it will be reduced back to 4*1=4 spaces and continue increasing from there. If the whitespace never exceeds this limit the formatting remains unchanged.
The combination of -vmll and -wc=n provides a solution to the problem of displaying arbitrarily deep data structures and code in a finite window, although -wc=n may of course be used without -vmll.
The default is not to use this, which can also be indicated using -wc=0.
Except for possibly introducing tab indentation characters, as outlined below, perltidy does not introduce any tab characters into your file, and it removes any tabs from the code (unless requested not to do so with -fws). If you have any tabs in your comments, quotes, or here-documents, they will remain.
The value of the integer n can be any value but can be coordinated with the number of spaces used for intentation. For example, -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 n with what your display software assumes for the spacing of a tab.
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 indentation spaces as for the output stream (any other assumption would lead to unstable editing).
This flag causes perltidy to run "perl -c -T" to check syntax of input and output. (To change the flags passed to perl, see the next item, -pscf). The results are written to the .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 --warning-output flag is given.
The default is NOT 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.
Perltidy will pass your string to perl with the exception that it will add a -c and -x if appropriate. The .LOG file will show exactly what flags were passed to perl.
For example, without this flag a structure such as the following would generate a syntax error and the braces would not be balanced:
method deposit( Num $amount) { $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 --nooutdent-labels flag to prevent lines such as 'default:' from being outdented.
This flag is enabled by default but it can be deactivated with -nxs. Probably the only reason to deactivate this flag is to generate more diagnostic messages when debugging a script.
Setting this flag is equivalent to setting --freeze-newlines and --freeze-whitespace.
If you also want to keep your existing blank lines exactly as they are, you can add --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 prevent long comment lines from being outdented, you can add either -noll or -l=0.
Setting this flag will prevent perltidy from doing any special operations on closing side comments. You may still delete all side comments however when this flag is in effect.
The possible values for s are (1) the name of an encoding recognized by the Encode.pm module, (2) none if no encoding is used, or (3) <guess> if perltidy should guess.
For example, the value utf8 causes the stream to be read and written as UTF-8. If the input stream cannot be decoded with a specified encoding then processing is not done.
The value none 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 guess tells perltidy to guess between either utf8 encoding or no encoding (meaning one character per byte). The guess uses the Encode::Guess module and this restricted range of guesses covers the most common cases. Testing showed that considering any greater number of encodings as guess suspects is too risky.
The current default is guess.
The abbreviations -utf8 or -UTF8 are equivalent to -enc=utf8, and the abbreviation -guess is equivalent to <-enc=guess>. So to process a file named file.pl which is encoded in UTF-8 you can use:
perltidy -utf8 file.pl
or
perltidy -guess file.pl
To process a file in euc-jp you could use
perltidy -enc=euc-jp file.pl
A perltidy output file is unencoded if the input file is unencoded, and otherwise it is encoded as utf8, even if the input encoding was not utf8.
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.
This flag has no effect when perltidy is used to generate html.
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 ( $max_index_to_go >= 0 ) ? $levels_to_go[0] : $last_output_level;
The value given to -ci is also used by some commands when a small space is required. Examples are commands for outdenting labels, -ola, and control keywords, -okw.
When default values are not used, it is recommended that either
(1) the value n given with -ci=n be no more than about one-half of the number of spaces assigned to a full indentation level on the -i=n command, or
(2) the flag -extended-continuation-indentation is used (see next section).
Please see the section "-pbp, --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 ) )
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 for the current perltidy process. This is the only sensible guess that can be made. It should be correct if this is true, but otherwise it probably won't. For example, if the input script was written with -i=2 and the current peltidy flags have -i=4, the wrong initial indentation will be guessed for a code snippet which has non-zero initial indentation. Likewise, if an entabbing scheme is used in the input script and not in the current process then the guessed indentation will be wrong.
If the default method does not work correctly, or you want to change the starting level, use -sil=n, to force the starting level to be n.
# perltidy (default) @month_of_year = ( 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' );
Use the -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:
# perltidy -lp @month_of_year = ( 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' );
If the available line length (see -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 -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.
Note: The -lp option may not be used together with the -t tabs option. It may, however, be used with the -et=n tab method.
In addition, any parameter which significantly restricts the ability of perltidy to choose newlines will conflict with -lp and will cause -lp to be deactivated. These include -io, -fnl, -nanl, and -ndnl. The reason is that the -lp indentation style can require the careful coordination of an arbitrary number of break points in hierarchical lists, and these flags may prevent that.
The -lp indentation style works well for some types of coding but can produce very long lines when variables have long names and/or containers are very deeply nested. The -lpxl=s flag is intended to help mitigate this problem by providing control over the containers to which the -lp indentation style is applied. The -lp flag by default is ``greedy'' and applies to as many containers as possible. This flag specifies a list of things which should not be use -lp indentation.
This list 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
-lpxl='[ {'
means do NOT include use -lp formatting within square-bracets or braces. The only unspecified container is '(', so this string means that only the contens within parens will use -lp indentation.
An optional numeric code may follow any of the container types to further refine the selection based on container contents. The numeric codes are:
'0' or blank: no check on contents '1' reject -lp unless the contents is a simple list without sublists '2' reject -lp unless the contents is a simple list without sublists, without code blocks, and without ternary operators
For example,
-lpxl = '[ { (2'
means only apply -lp to parenthesized lists which do not contain any sublists, code blocks or ternary expressions.
A third optional item of information which can be given for parens 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:
'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,
-lpxl = '[ { F(2'
means only apply -lp to parenthesized lists which follow a function call and which do not contain any sublists, code blocks or ternary expressions. The logic of writing these codes is somewhat counter-intuitive because they describe what is not getting the -lp indentation. So the 'F' indicates that non-function calls are not getting -lp, or in other words that function calls are getting the -lp indentation.
-cti = 0 no extra indentation (default) -cti = 1 extra indentation such that the closing token aligns with its opening token. -cti = 2 one extra indentation level if the line looks like: ); or ]; or }; -cti = 3 one extra indentation level always
The flags -cti=1 and -cti=2 work well with the -lp flag (previous section).
# perltidy -lp -cti=1 @month_of_year = ( 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ); # perltidy -lp -cti=2 @month_of_year = ( 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' );
These flags are merely hints to the formatter and they may not always be followed. In particular, if -lp is not being used, the indentation for cti=1 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, -cti=n is merely an abbreviation for -cpi=n -csbi=n -cbi=n, where: -cpi or --closing-paren-indentation controls )'s, -csbi or --closing-square-bracket-indentation controls ]'s, -cbi or --closing-brace-indentation controls non-block }'s.
if ($task) { yyy(); } # -icb else { zzz(); }
The default is not to do this, indicated by -nicb.
{ #<<< 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 -nnib to deactivate it. To make it easy to remember, the default string is the same as the string for starting a format-skipping 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.
For example, to match the side comment '#++', the parameter would be
-nibp='#\+\+'
my $i; LOOP: while ( $i = <FOTOS> ) { chomp($i); next unless $i; fixit($i); }
Use -nola to not outdent labels.
For example, using "perltidy -okw" on the previous example gives:
my $i; LOOP: while ( $i = <FOTOS> ) { chomp($i); next unless $i; fixit($i); }
The default is not to do this.
For example, the commands "-okwl="next last redo goto" -okw" will cause those four keywords to be outdented. It is probably simplest to place any -okwl command in a .perltidyrc file.
The -pt=n or --paren-tightness=n parameter controls the space within parens. The example below shows the effect of the three possible values, 0, 1, and 2:
if ( ( my $len_tab = length( $tabstr ) ) > 0 ) { # -pt=0 if ( ( my $len_tab = length($tabstr) ) > 0 ) { # -pt=1 (default) if ((my $len_tab = length($tabstr)) > 0) { # -pt=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.
Likewise, the parameter -sbt=n or --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
Curly braces which do not contain code blocks are controlled by the parameter -bt=n or --brace-tightness=n.
$obj->{ $parsed_sql->{ 'table' }[0] }; # -bt=0 $obj->{ $parsed_sql->{'table'}[0] }; # -bt=1 (default) $obj->{$parsed_sql->{'table'}[0]}; # -bt=2
And finally, curly braces which contain blocks of code are controlled by the parameter -bbt=n or --block-brace-tightness=n as illustrated in the example below.
%bf = map { $_ => -M $_ } grep { /\.deb$/ } dirents '.'; # -bbt=0 (default) %bf = map { $_ => -M $_ } grep {/\.deb$/} dirents '.'; # -bbt=1 %bf = map {$_ => -M $_} grep {/\.deb$/} dirents '.'; # -bbt=2
To simplify input in the case that all of the tightness flags have the same value <n>, the parameter <-act=n> or --all-containers-tightness=n is an abbreviation for the combination <-pt=n -sbt=n -bt=n -bbt=n>.
0+ +0 ()x!! ~~<> ,=> =( )=
For example the sequence 0 +, which converts a string to a number, would be formatted without a space: 0+ when the -tso flag is set. This flag is off by default.
$i = 1 ; # -sts $i = 1; # -nsts (default)
for ( @a = @$ap, $u = shift @a ; @a ; $u = $v ) { # -sfs (default) for ( @a = @$ap, $u = shift @a; @a; $u = $v ) { # -nsfs
-wls=s or --want-left-space=s,
-nwls=s or --nowant-left-space=s,
-wrs=s or --want-right-space=s,
-nwrs=s or --nowant-right-space=s.
These parameters are each followed by a quoted string, s, containing a list of token types. No more than one of each of these parameters should be specified, because repeating a command-line parameter always overwrites the previous one before perltidy ever sees it.
To illustrate how these are used, suppose it is desired that there be no space on either side of the token types = + - / *. The following two parameters would specify this desire:
-nwls="= + - / *" -nwrs="= + - / *"
(Note that the token types are in quotes, and that they are separated by spaces). With these modified whitespace rules, the following line of math:
$root = -$b + sqrt( $b * $b - 4. * $a * $c ) / ( 2. * $a );
becomes this:
$root=-$b+sqrt( $b*$b-4.*$a*$c )/( 2.*$a );
These parameters should be considered to be hints to perltidy rather 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.
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 --dump-token-types. Also try the -D flag on a short snippet of code and look at the .DEBUG file to see the tokenization.
WARNING Be sure to put these tokens in quotes to avoid having them misinterpreted by your command shell.
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 print keyword as a special type Z. 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 print include printf, sort, exec, system. 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 Z you can use perltidy -DEBUG. For the first line above the result is
1: system($foo ); 1: kkkkkk{ZZZZb};
which shows that system is type k (keyword) and $foo is type Z.
# 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.
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:
-sak=s or --space-after-keyword=s adds keywords.
-nsak=s or --nospace-after-keyword=s removes keywords.
where s is a list of keywords (in quotes if necessary). For example,
my ( $a, $b, $c ) = @_; # default my( $a, $b, $c ) = @_; # -nsak="my local our"
The abbreviation -nsak='*' is equivalent to including all of the keywords in the above list.
When both -nsak=s and -sak=s commands are included, the -nsak=s command is executed first. For example, to have space after only the keywords (my, local, our) you could use -nsak=``*'' -sak=``my local our''.
To put a space after all keywords, see the next item.
-skp or --space-keyword-paren
You may also want to use the flag -sfp (next item) too.
-sfp or --space-function-paren
myfunc( $a, $b, $c ); # default myfunc ( $a, $b, $c ); # -sfp
You will probably also want to use the flag -skp (previous item) too.
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.
# perltidy -sfp myfun ( $aaa, $b, $cc ); mylongfun ( $a, $b, $c );
This is the default behavior. To prevent this alignment use -nfpva:
# perltidy -sfp -nfpva myfun ( $aaa, $b, $cc ); mylongfun ( $a, $b, $c );
sub usage();
This integer n 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 -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]
-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 -kpitl=s described in the next section.
For example, the commands "-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 -kpit=2 flag.
# 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 --logical-padding, which is set 'on' by default. If it is not desired it can be turned off using --nological-padding or -nlop. The above two examples become, with -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;
-ntqw or --notrim-qw cause leading and trailing whitespace around multi-line "qw" quotes to be left unchanged. This option will not normally be necessary, but was added for testing purposes, because in some versions of perl, trimming "qw" quotes changes the syntax tree.
$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 n 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 -sbq=1, meaning that a space will be used if there is one in the source code.
# this comment is indented (-ibc, default) if ($task) { yyy(); }
The alternative is -nibc:
# this comment is not indented (-nibc) if ($task) { yyy(); }
See also the next item, -isbc, as well as -sbc, for other ways to have some indented and some outdented block comments.
If both -ibc and -isbc are set, then -isbc takes priority.
perltidy -l=80 -iscl $vmsfile =~ s/;[\d\-]*$//; # Clip off version number; we can use a newer version as well
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
my $IGNORE = 0; # This is a side comment # This is a hanging side comment # And so is 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 -nhsc or --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.
sub message { if ( !defined( $_[0] ) ) { print("Hello, World\n"); } else { print( $_[0], "\n" ); } }
And here is the result of processing with "perltidy -csc":
sub message { if ( !defined( $_[0] ) ) { print("Hello, World\n"); } else { print( $_[0], "\n" ); } } ## end sub message
A closing side comment was added for "sub message" in this case, but not for the "if" and "else" blocks, because they were below the 6 line cutoff limit for adding closing side comments. This limit may be changed with the -csci command, described below.
The command -dcsc (or --delete-closing-side-comments) reverses this process and removes these comments.
Several commands are available to modify the behavior of these two basic commands, -csc and -dcsc:
# perltidy -csci=2 -csc sub message { if ( !defined( $_[0] ) ) { print("Hello, World\n"); } ## end if ( !defined( $_[0] )) else { print( $_[0], "\n" ); } ## end else [ if ( !defined( $_[0] )) } ## end sub message
Now the "if" and "else" blocks are commented. However, now this has become very cluttered.
-cscl="sub : BEGIN END"
perltidy -csc -ncscb } ## end foreach my $foo (sort { $b cmp $a ...
However this causes a problem with editors which cannot recognize comments or are not configured to do so because they cannot ``bounce'' around in the text correctly. The -cscb flag has been added to help them by appending appropriate balancing structure:
perltidy -csc -cscb } ## end foreach my $foo (sort { $b cmp $a ... })
The default is -cscb.
If n=2 is used, then each "elsif" is also given the text of the opening "if" statement. Also, an "else" will include the text of a preceding "elsif" statement. Note that this may result some long closing side comments.
If n=1 is used, the results will be the same as n=2 whenever the resulting line length is less than the maximum allowed.
perltidy -csc -ncscb } ## end foreach my $foo (sort { $b cmp $a ...
This causes a problem with older editors which do not recognize comments because they cannot ``bounce'' around in the text correctly. The -cscb 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 -cscb.
The intent is to avoid clobbering existing hand-written side comments which happen to match the pattern of closing side comments. This flag should only be needed on the first run with -csc.
Important Notes on Closing Side Comments:
Comments so identified are treated as follows:
For example, assuming @month_of_year is left-adjusted:
@month_of_year = ( # -sbc (default) 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', ## 'Dec', 'Nov' 'Nov', 'Dec');
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' );
which is not as clear. The default is to use -sbc. This may be deactivated with -nsbc.
Please note that -sbcp merely defines the pattern used to identify static block comments; it will not be used unless the switch -sbc is set. Also, please be aware that since this string is used in a perl regular expression which identifies these comments, it must enable a valid regular expression to be formed.
A pattern which can be useful is:
-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 '#' characters to be rejected as a static block comment. Such lines are often used at the start and end of header information in subroutines and should not be separated from the intervening comments, which typically begin with just a single '#'.
The default is -nssc.
Please note that -sscp merely defines the pattern used to identify static side comments; it will not be used unless the switch -ssc is set. Also, note that this string is used in a perl regular expression which identifies these comments, so it must enable a valid regular expression to be formed.
#<<< do not let perltidy touch this my @list = (1, 1, 1, 1, 2, 1, 1, 3, 3, 1, 1, 4, 6, 4, 1,); #>>>
Format skipping begins when a format skipping comment is seen and continues until either a format-skipping end pattern is found or until the end of file.
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 .LOG file. Use -nfs to disable this feature.
This method works for any code. For the specific case of a comma-separated list of values, as in this example, another possibility is to insert a blank or comment somewhere between the opening and closing parens. See the section ``Controlling List Formatting''.
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 #*****
The beginning and ending strings may be the same, but it is preferable to make them different for clarity.
Note: If you also want to keep your blank lines exactly as they are, you can use the -fbl flag which is described in the section ``Blank Line Control''.
# -ce if ($task) { yyy(); } else { zzz(); } # -nce (default) if ($task) { yyy(); } else { zzz(); }
In this example the keyword else 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 elsif, continue, catch, finally.
Other block types can be formatted by specifying their names on a separate parameter -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 input file, it is necessary to decide if it should be broken to allow cuddling. This decision is controlled by the flag -cbo=n discussed below. The default and recommended value of -cbo=1 bases this decision on the first block in 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 -ce flag would not have any effect if the above snippet is rewritten as
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 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.
Additional block types to which the -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
-cbl="sort map grep"
or equivalently
-cbl=sort,map,grep
Note however that these particular block types are typically short so there might not be much opportunity for the cuddled format style.
Using commas avoids the need to protect spaces with quotes.
As a diagnostic check, the flag --dump-cuddled-block-list or -dcbl can be used to view the hash of values that are generated by this flag.
Finally, note that the -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 -ce.
perltidy -ce -cbl='else elsif continue' -cblx
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 [DEFAULT]. cbo=2 Break open all blocks for maximal cuddled formatting.
The default and recommended value is cbo=1. With this value, if the starting block of a chain spans multiple lines, then a cascade of breaks will occur for remaining blocks causing the entire chain to be cuddled.
The option cbo=0 can produce erratic cuddling if there are numerous one-line blocks.
The option cbo=2 produces maximal cuddling but will not allow any short blocks.
if ( $input_file eq '-' ) # -bl { important_function(); }
This flag applies to all structural blocks, including named sub's (unless the -sbl flag is set --- see next item).
The default style, -nbl, places an opening brace on the same line as the keyword introducing it. For example,
if ( $input_file eq '-' ) { # -nbl (default)
perltidy -sbl
produces this result:
sub message { if (!defined($_[0])) { print("Hello, World\n"); } else { print($_[0], "\n"); } }
This flag is negated with -nsbl. If -sbl is not specified, the value of -bl is used.
perltidy -asbl
produces this result:
$a = sub { if ( !defined( $_[0] ) ) { print("Hello, World\n"); } else { print( $_[0], "\n" ); } };
This flag is negated with -nasbl, and the default is -nasbl.
For example,
if ( $input_file eq '-' ) # -bli { important_function(); }
By default, this extra indentation occurs for blocks of type: if, elsif, else, unless, for, foreach, sub, while, until, and also with a preceding label. The next item shows how to change this.
if ( $bigwasteofspace1 && $bigwasteofspace2 || $bigwasteofspace3 && $bigwasteofspace4 ) { big_waste_of_time(); }
To force the opening brace to always be on the right, use the -bar flag. In this case, the above example becomes
if ( $bigwasteofspace1 && $bigwasteofspace2 || $bigwasteofspace3 && $bigwasteofspace4 ) { big_waste_of_time(); }
A conflict occurs if both -bl and -bar are specified.
# default formatting push @{ $self->{$module}{$key} }, { accno => $ref->{accno}, description => $ref->{description} }; # perltidy -otr push @{ $self->{$module}{$key} }, { accno => $ref->{accno}, description => $ref->{description} };
The flag -otr is actually an abbreviation for three other flags which can be used to control parens, hash braces, and square brackets separately if desired:
-opr or --opening-paren-right -ohbr or --opening-hash-brace-right -osbr or --opening-square-bracket-right
$romanNumerals = { one => 'I', two => 'II', three => 'III', four => 'IV', };
This flag can change the default behavior to cause a line break to be placed before the opening brace according to the value given to the integer n:
-bbhb=0 never break [default] -bbhb=1 stable: break if the input script had a break -bbhb=2 break if list is 'complex' (see note below) -bbhb=3 always break
For example,
# perltidy -bbhb=3 $romanNumerals = { one => 'I', two => 'II', three => 'III', four => 'IV', };
There are several points to note about this flag:
-bbhbi=0 one continuation level [default] -bbhbi=1 outdent by one continuation level -bbhbi=2 indent one full indentation level
For example:
# perltidy -bbhb=3 -bbhbi=1 $romanNumerals = { one => 'I', two => 'II', three => 'III', four => 'IV', }; # perltidy -bbhb=3 -bbhbi=2 $romanNumerals = { one => 'I', two => 'II', three => 'III', four => 'IV', };
Note that this parameter has no effect unless -bbhb=n is also set.
-bbsb=0 never break [default] -bbsb=1 stable: break if the input script had a break -bbsb=2 break if list is 'complex' (part of nested list structure) -bbsb=3 always break
-bbsbi=0 one continuation level [default] -bbsbi=1 outdent by one continuation level -bbsbi=2 indent one full indentation level
-bbp=0 never break [default] -bbp=1 stable: break if the input script had a break -bpb=2 break if list is 'complex' (part of nested list structure) -bbp=3 always break
-bbpi=0 one continuation level [default] -bbpi=1 outdent by one continuation level -bbpi=2 indent one full indentation level
For example:
# default formatting do { { next if $x == $y; } } until $x++ > $z; # perltidy -wn do { { next if $x == $y; } } until $x++ > $z;
When this flag is set perltidy makes a preliminary pass through the file and identifies all nested pairs of containers. To qualify as a nested pair, the closing container symbols must be immediately adjacent and the opening symbols must either (1) be adjacent as in the above example, or (2) have an anonymous sub declaration following an outer opening container symbol which is not a code block brace, or (3) have an outer opening paren separated from the inner opening symbol by any single non-container symbol or something that looks like a function evaluation, as illustrated in the next examples.
Any container symbol may serve as both the inner container of one pair and as the outer container of an adjacent pair. Consequently, any number of adjacent opening or closing symbols may join together in weld. For example, here are three levels of wrapped function calls:
# default formatting my (@date_time) = Localtime( Date_to_Time( Add_Delta_DHMS( $year, $month, $day, $hour, $minute, $second, '0', $offset, '0', '0' ) ) ); # perltidy -wn my (@date_time) = Localtime( Date_to_Time( Add_Delta_DHMS( $year, $month, $day, $hour, $minute, $second, '0', $offset, '0', '0' ) ) );
Notice how the indentation of the inner lines are reduced by two levels in this case. This example also shows the typical result of this formatting, namely it is a sandwich consisting of an initial opening layer, a central section of any complexity forming the ``meat'' of the sandwich, and a final closing layer. This predictable structure helps keep the compacted structure readable.
The inner sandwich layer is required to be at least one line thick. If this cannot be achieved, welding does not occur. This constraint can cause formatting to take a couple of iterations to stabilize when it is first applied to a script. The -conv flag can be used to insure that the final format is achieved in a single run.
Here is an example illustrating a welded container within a welded containers:
# default formatting $x->badd( bmul( $class->new( abs( $sx * int( $xr->numify() ) & $sy * int( $yr->numify() ) ) ), $m ) ); # perltidy -wn $x->badd( bmul( $class->new( abs( $sx * int( $xr->numify() ) & $sy * int( $yr->numify() ) ) ), $m ) );
The welded closing tokens are by default on a separate line but this can be modified with the -vtc=n flag (described in the next section). For example, the same example adding -vtc=2 is
# perltidy -wn -vtc=2 $x->badd( bmul( $class->new( abs( $sx * int( $xr->numify() ) & $sy * int( $yr->numify() ) ) ), $m ) );
This format option is quite general but there are some limitations.
One limitation is that any line length limit still applies and can cause long welded sections to be broken into multiple lines.
Another limitation is that an opening symbol which delimits quoted text cannot be included in a welded pair. This is because quote delimiters are treated specially in perltidy.
Finally, the stacking of containers defined by this flag have priority over any other container stacking flags. This is because any welding is done first.
The logic in perltidy to apply this is straightforward. As each container token is being considered for joining a weld, any exclusion rules are consulted and used to reject the weld if necessary.
This list is a string with space-separated items. Each item consists of up to three pieces of information: (1) an optional position, (2) an optional preceding type, and (3) a container type.
The only required piece of information is a container type, which is one of '(', '[', '{' or 'q'. The first three of these are container tokens and the last represents a quoted list. For example the string
-wnxl='[ { q'
means do NOT include square-bracets, braces, or quotes in any welds. The only unspecified container is '(', so this string means that only welds involving parens will be made.
To illustrate, following welded snippet consists of a chain of three welded containers with types '(' '[' and 'q':
# perltidy -wn skip_symbols( [ qw( Perl_dump_fds Perl_ErrorNo Perl_GetVars PL_sys_intern ) ] );
Even though the qw term uses parens as the quote delimiter, it has a special type 'q' here. If it appears in a weld it always appears at the end of the welded chain.
Any of the container types '[', '{', and '(' may be prefixed with a position indicator which is either '^', to indicate the first token of a welded sequence, or '.', to indicate an interior token of a welded sequence. (Since a quoted string 'q' always ends a chain it does need a position indicator).
For example, if we do not want a sequence of welded containers to start with a square bracket we could use
-wnxl='^['
In the above snippet, there is a square bracket but it does not start the chain, so the formatting would be unchanged if it were formatted with this restriction.
A third optional item of information which can be given is an alphanumeric letter which is used to limit the selection further depending on the type of token immediately before the container. If given, it goes just before the container symbol. The possible letters are currently 'k', 'K', 'f', 'F', 'w', and 'W', with these meanings:
'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, compare
# perltidy -wn if ( defined( $_Cgi_Query{ $Config{'methods'}{'authentication'}{'remote'}{'cgi'}{'username'} } ) )
with
# perltidy -wn -wnxl='^K( {' if ( defined( $_Cgi_Query{ $Config{'methods'}{'authentication'}{'remote'}{'cgi'} {'username'} } ) )
The first case does maximum welding. In the second case the leading paren is retained by the rule (it would have been rejected if preceded by a non-keyword) but the curly brace is rejected by the rule.
Here are some additional example strings and their meanings:
'^(' - the weld must not start with a paren '.(' - the second and later tokens may not be parens '(' - no parens in a weld '^K(' - exclude a leading paren preceded by a non-keyword '.k(' - exclude a secondary paren preceded by a keyword '[ {' - exclude all brackets and braces
-vt=0 always break a line after opening token (default). -vt=1 do not break unless this would produce more than one step in indentation in a line. -vt=2 never break a line after opening token
-vtc=0 always break a line before a closing token (default), -vtc=1 do not break before a closing token which is followed by a semicolon or another closing token, and is not in a list environment. -vtc=2 never break before a closing token.
The rules for -vtc=1 are designed to maintain a reasonable balance between tightness and readability in complex lists.
Here are some examples:
# perltidy -lp -vt=0 -vtc=0 %romanNumerals = ( one => 'I', two => 'II', three => 'III', four => 'IV', ); # perltidy -lp -vt=1 -vtc=0 %romanNumerals = ( one => 'I', two => 'II', three => 'III', four => 'IV', ); # perltidy -lp -vt=1 -vtc=1 %romanNumerals = ( one => 'I', two => 'II', three => 'III', four => 'IV', );
The difference between -vt=1 and -vt=2 is shown here:
# perltidy -lp -vt=1 $init->add( mysprintf( "(void)find_threadsv(%s);", cstring( $threadsv_names[ $op->targ ] ) ) ); # perltidy -lp -vt=2 $init->add( mysprintf( "(void)find_threadsv(%s);", cstring( $threadsv_names[ $op->targ ] ) ) );
With -vt=1, the line ending in "add(" does not combine with the next line because the next line is not balanced. This can help with readability, but -vt=2 can be used to ignore this rule.
The tightest, and least readable, code is produced with both "-vt=2" and "-vtc=2":
# perltidy -lp -vt=2 -vtc=2 $init->add( mysprintf( "(void)find_threadsv(%s);", cstring( $threadsv_names[ $op->targ ] ) ) );
Notice how the code in all of these examples collapses vertically as -vt increases, but the indentation remains unchanged. This is because perltidy implements the -vt parameter by first formatting as if -vt=0, and then simply overwriting one output line on top of the next, if possible, to achieve the desired vertical tightness. The -lp indentation style has been designed to allow this vertical collapse to occur, which is why it is required for the -vt parameter.
The -vt=n and -vtc=n parameters apply to each type of container token. If desired, vertical tightness controls can be applied independently to each of the closing container token types.
The parameters for controlling parentheses are -pvt=n or --paren-vertical-tightness=n, and -pcvt=n or --paren-vertical-tightness-closing=n.
Likewise, the parameters for square brackets are -sbvt=n or --square-bracket-vertical-tightness=n, and -sbcvt=n or --square-bracket-vertical-tightness-closing=n.
Finally, the parameters for controlling non-code block braces are -bvt=n or --brace-vertical-tightness=n, and -bcvt=n or --brace-vertical-tightness-closing=n.
In fact, the parameter -vt=n is actually just an abbreviation for -pvt=n -bvt=n sbvt=n, and likewise -vtc=n is an abbreviation for -pvtc=n -bvtc=n sbvtc=n.
-bbvt=0 break after opening block brace (default). -bbvt=1 do not break unless this would produce more than one step in indentation in a line. -bbvt=2 do not break after opening block brace.
It is necessary to also use either -bl or -bli for this to work, because, as with other vertical tightness controls, it is implemented by simply overwriting a line ending with an opening block brace with the subsequent line. For example:
# perltidy -bli -bbvt=0 if ( open( FILE, "< $File" ) ) { while ( $File = <FILE> ) { $In .= $File; $count++; } close(FILE); } # perltidy -bli -bbvt=1 if ( open( FILE, "< $File" ) ) { while ( $File = <FILE> ) { $In .= $File; $count++; } close(FILE); }
By default this applies to blocks associated with keywords if, elsif, else, unless, for, foreach, sub, while, until, and also with a preceding label. This can be changed with the parameter -bbvtl=string, or --block-brace-vertical-tightness-list=string, where string is a space-separated list of block types. For more information on the possible values of this string, see ``Specifying Block Types''
For example, if we want to just apply this style to "if", "elsif", and "else" blocks, we could use "perltidy -bli -bbvt=1 -bbvtl='if elsif else'".
There is no vertical tightness control for closing block braces; with one exception they will be placed on separate lines. The exception is that a cascade of closing block braces may be stacked on a single line. See -scbb.
For example:
# default $opt_c = Text::CSV_XS->new( { binary => 1, sep_char => $opt_c, always_quote => 1, } ); # -sot $opt_c = Text::CSV_XS->new( { binary => 1, sep_char => $opt_c, always_quote => 1, } );
For detailed control of individual closing tokens the following controls can be used:
-sop or --stack-opening-paren -sohb or --stack-opening-hash-brace -sosb or --stack-opening-square-bracket -sobb or --stack-opening-block-brace
The flag -sot is an abbreviation for -sop -sohb -sosb.
The flag -sobb is an abbreviation for -bbvt=2 -bbvtl='*'. This will case a cascade of opening block braces to appear on a single line, although this an uncommon occurrence except in test scripts.
For example:
# default $opt_c = Text::CSV_XS->new( { binary => 1, sep_char => $opt_c, always_quote => 1, } ); # -sct $opt_c = Text::CSV_XS->new( { binary => 1, sep_char => $opt_c, always_quote => 1, } );
The -sct flag is somewhat similar to the -vtc flags, and in some cases it can give a similar result. The difference is that the -vtc flags try to avoid lines with leading opening tokens by ``hiding'' them at the end of a previous line, whereas the -sct flag merely tries to reduce the number of lines with isolated closing tokens by stacking them but does not try to hide them. For example:
# -vtc=2 $opt_c = Text::CSV_XS->new( { binary => 1, sep_char => $opt_c, always_quote => 1, } );
For detailed control of the stacking of individual closing tokens the following controls can be used:
-scp or --stack-closing-paren -schb or --stack-closing-hash-brace -scsb or --stack-closing-square-bracket -scbb or --stack-closing-block-brace
The flag -sct is an abbreviation for stacking the non-block closing tokens, -scp -schb -scsb.
Stacking of closing block braces, -scbb, causes a cascade of isolated closing block braces to be combined into a single line as in the following example:
# -scbb: for $w1 (@w1) { for $w2 (@w2) { for $w3 (@w3) { for $w4 (@w4) { push( @lines, "$w1 $w2 $w3 $w4\n" ); } } } }
To simplify input even further for the case in which both opening and closing non-block containers are stacked, the flag -sac or --stack-all-containers is an abbreviation for -sot -sct.
Please note that if both opening and closing tokens are to be stacked, then the newer flag -weld-nested-containers may be preferable because it insures that stacking is always done symmetrically. It also removes an extra level of unnecessary indentation within welded containers. It is able to do this because it works on formatting globally rather than locally, as the -sot and -sct flags do.
This flag does not prevent perltidy from eliminating existing line breaks; see --freeze-newlines to completely prevent changes to line break points.
-wba=s or --want-break-after=s, and
-wbb=s or --want-break-before=s.
These parameters are each followed by a quoted string, s, containing a list of token types (separated only by spaces). No more than one of each of these parameters should be specified, because repeating a command-line parameter always overwrites the previous one before perltidy ever sees it.
By default, perltidy breaks after these token types:
% + - * / x != == >= <= =~ !~ < > | &
= **= += *= &= <<= &&= -= /= |= >>= ||= //= .= %= ^= x=
And perltidy breaks before these token types by default:
. << >> -> && || //
To illustrate, to cause a break after a concatenation operator, '.', rather than before it, the command line would be
-wba="."
As another example, the following command would cause a break before math operators '+', '-', '/', and '*':
-wbb="+ - / *"
These commands should work well for most of the token types that perltidy uses (use --dump-token-types for a list). Also try the -D flag on a short snippet of code and look at the .DEBUG file to see the tokenization. However, for a few token types there may be conflicts with hardwired logic which cause unexpected results. One example is curly braces, which should be controlled with the parameter bl provided for that purpose.
WARNING Be sure to put these tokens in quotes to avoid having them misinterpreted by your command shell.
Two additional parameters are available which, though they provide no further capability, can simplify input are:
-baao or --break-after-all-operators,
-bbao or --break-before-all-operators.
The -baao sets the default to be to break after all of the following operators:
% + - * / x != == >= <= =~ !~ < > | & = **= += *= &= <<= &&= -= /= |= >>= ||= //= .= %= ^= x= . : ? && || and or err xor
and the -bbao flag sets the default to break before all of these operators. These can be used to define an initial break preference which can be fine-tuned with the -wba and -wbb flags. For example, to break before all operators except an = one could use --bbao -wba='=' rather than listing every single perl operator except = on a -wbb flag.
A very simple way to prevent perltidy from changing the line breaks within a comma-separated list of values is to insert a blank line, comment, or side-comment anywhere between the opening and closing parens (or braces or brackets). This causes perltidy to skip over its list formatting logic. (The reason is that any of these items put a constraint on line breaks, and perltidy needs complete control over line breaks within a container to adjust a list layout). For example, let us consider
my @list = (1, 1, 1, 1, 2, 1, 1, 3, 3, 1, 1, 4, 6, 4, 1,);
The default formatting, which allows a maximum line length of 80, will flatten this down to one line:
# perltidy (default) my @list = ( 1, 1, 1, 1, 2, 1, 1, 3, 3, 1, 1, 4, 6, 4, 1, );
This formatting loses important information. If we place a side comment on one of the lines, for example, we get the following result with with default formatting parameters:
my @list = ( 1, # a side comment, comment, or blank keeps list intact 1, 1, 1, 2, 1, 1, 3, 3, 1, 1, 4, 6, 4, 1, );
We could achieve the same result with a blank line or full comment anywhere between the opening and closing parens.
For another possibility see the -fs flag in ``Skipping Selected Sections of Code''.
# perltidy -boc -lp -pt=2 -vt=1 -vtc=1 my @list = (1, 1, 1, 1, 2, 1, 1, 3, 3, 1, 1, 4, 6, 4, 1,);
A disadvantage of this flag is that all tables in the file must already be nicely formatted.
# perltidy -mft=2 @month_of_year = ( 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' );
n=0 break at all commas after => n=1 stable: break at all commas after => if container is open, EXCEPT FOR one-line containers n=2 break at all commas after =>, BUT try to form the maximum one-line container lengths n=3 do not treat commas after => specially at all n=4 break everything: like n=0 but ALSO break a short container with a => not followed by a comma when -vt=0 is used n=5 stable: like n=1 but ALSO break at open one-line containers when -vt=0 is used (default)
For example, given the following single line, perltidy by default will not add any line breaks because it would break the existing one-line container:
bless { B => $B, Root => $Root } => $package;
Using -cab=0 will force a break after each comma-arrow item:
# perltidy -cab=0: bless { B => $B, Root => $Root } => $package;
If perltidy is subsequently run with this container broken, then by default it will break after each '=>' because the container is now broken. To reform a one-line container, the parameter -cab=2 could be used.
The flag -cab=3 can be used to prevent these commas from being treated specially. In this case, an item such as ``01'' => 31 is treated as a single item in a table. The number of fields in this table will be determined by the same rules that are used for any other table. Here is an example.
# perltidy -cab=3 my %last_day = ( "01" => 31, "02" => 29, "03" => 31, "04" => 30, "05" => 31, "06" => 30, "07" => 31, "08" => 31, "09" => 30, "10" => 31, "11" => 30, "12" => 31 );
Most of the parameters in this section would only be required for a one-time conversion of a script from short container lengths to longer container lengths. The opposite effect, of converting long container lengths to shorter lengths, can be obtained by temporarily using a short maximum line length.
Please note that this flag does not duplicate old logical breakpoints. They are merely used as a hint with this flag that a statement should remain broken. Without this flag, perltidy will normally try to combine relatively short expressions into a single line.
For example, given this snippet:
return unless $cmd = $cmd || ($dot && $Last_Shell) || &prompt('|'); # perltidy -bol [default] return unless $cmd = $cmd || ( $dot && $Last_Shell ) || &prompt('|'); # perltidy -nbol return unless $cmd = $cmd || ( $dot && $Last_Shell ) || &prompt('|');
my $q = $rs ->related_resultset('CDs') ->related_resultset('Tracks') ->search({ 'track.id' => {-ident => 'none_search.id'}, })->as_query;
It will keep these breaks, rather than become this:
my $q = $rs->related_resultset('CDs')->related_resultset('Tracks')->search({ 'track.id' => {-ident => 'none_search.id'}, })->as_query;
This flag will also look for and keep a 'cuddled' style of calls, in which lines begin with a closing paren followed by a call arrow, as in this example:
my $q = $rs->related_resultset( 'CDs' )->related_resultset( 'Tracks' )->search( { 'track.id' => { -ident => 'none_search.id' }, } )->as_query;
You may want to include the -weld-nested-containers flag in this case to keep nested braces and parens together, as in the last line.
$z = sqrt($x**2 + $y**2) ;
The default formatting will be:
$z = sqrt( $x**2 + $y**2 );
The result using perltidy -bos keeps the isolated semicolon:
$z = sqrt( $x**2 + $y**2 ) ;
The default is not to do this, -nbos.
my @field : field : Default(1) : Get('Name' => 'foo') : Set('Name');
If the attributes are on a single line in the source code then they will remain on a single line if possible.
To prevent this, and thereby always form longer lines, use -nboa.
-kbb=s or --keep-old-breakpoints-before=s, and
-kba=s or --keep-old-breakpoints-after=s
These parameters are each followed by a quoted string, s, containing a list of token types (separated only by spaces). No more than one of each of these parameters should be specified, because repeating a command-line parameter always overwrites the previous one before perltidy ever sees it.
For example, -kbb='=>' means that if an input line begins with a '=>' then the output script should also have a line break before that token.
For example, given the script:
method 'foo' => [ Int, Int ] => sub { my ( $self, $x, $y ) = ( shift, @_ ); ...; }; # perltidy [default] method 'foo' => [ Int, Int ] => sub { my ( $self, $x, $y ) = ( shift, @_ ); ...; }; # perltidy -kbb='=>' method 'foo' => [ Int, Int ] => sub { my ( $self, $x, $y ) = ( shift, @_ ); ...; };
dbmclose(%verb_delim); undef %verb_delim; dbmclose(%expanded); undef %expanded;
The default is to break after each statement, giving
dbmclose(%verb_delim); undef %verb_delim; dbmclose(%expanded); undef %expanded;
With perltidy -kis the multiple statements are retained:
dbmclose(%verb_delim); undef %verb_delim; dbmclose(%expanded); undef %expanded;
The statements are still subject to the specified value of maximum-line-length and will be broken if this maximum is exceeded.
The requested number of blanks statement will be inserted regardless of the value of --maximum-consecutive-blank-lines=n (-mbl=n) with the exception that if -mbl=0 then no blanks will be output.
This parameter interacts with the value k of the parameter --maximum-consecutive-blank-lines=k (-mbl=k) as follows:
1. If -mbl=0 then no blanks will be output. This allows all blanks to be suppressed with a single parameter. Otherwise,
2. If the number of old blank lines in the script is less than n then additional blanks will be inserted to make the total n regardless of the value of -mbl=k.
3. If the number of old blank lines in the script equals or exceeds n then this parameter has no effect, however the total will not exceed value specified on the -mbl=k flag.
This parameter interacts with the value k of the parameter --maximum-consecutive-blank-lines=k (-mbl=k) in the same way as described for the previous item -blbs=n.
Likewise, -nbbs or --noblanks-before-subs is equivalent to -blbp=0 and -blbs=0.
This is the default. The intention of this option is to introduce some space within dense coding. This is negated with -nbbb or --noblanks-before-blocks.
Please see the note below on using the -blao and -blbc options.
For example, suppose that for some reason we decide to introduce one blank space at the beginning and ending of all blocks. We could do this using
perltidy -blao=2 -blbc=2 -blaol='*' -blbcl='*' filename
Now suppose the script continues to be developed, but at some later date we decide we don't want these spaces after all. we might expect that running with the flags -blao=0 and -blbc=0 will undo them. However, by default perltidy retains single blank lines, so the blank lines remain.
We can easily fix this by telling perltidy to ignore old blank lines by including the added parameter -kbl=0 and rerunning. Then the unwanted blank lines will be gone. However, this will cause all old blank lines to be ignored, perhaps even some that were added by hand to improve formatting. So please be cautious when using these parameters.
This flag obviously does not apply to pod sections, here-documents, and quotes.
The possible values of n are:
n=0 ignore all old blank lines n=1 stable: keep old blanks, but limited by the value of the B<-mbl=n> flag n=2 keep all old blank lines, regardless of the value of the B<-mbl=n> flag
The default is n=1.
Controls for blank lines around lines of consecutive keywords
The parameters in this section provide some control over the placement of blank lines within and around groups of statements beginning with selected keywords. These blank lines are called here keyword group blanks, and all of the parameters begin with --keyword-group-blanks*, or -kgb* for short. The default settings do not employ these controls but they can be enabled with the following parameters:
-kgbl=s or --keyword-group-blanks-list=s; s is a quoted string of keywords
-kgbs=s or --keyword-group-blanks-size=s; s gives the number of keywords required to form a group.
-kgbb=n or --keyword-group-blanks-before=n; n = (0, 1, or 2) controls a leading blank
-kgba=n or --keyword-group-blanks-after=n; n = (0, 1, or 2) controls a trailing blank
-kgbi or --keyword-group-blanks-inside is a switch for adding blanks between subgroups
-kgbd or --keyword-group-blanks-delete is a switch for removing initial blank lines between keywords
-kgbr=n or --keyword-group-blanks-repeat-count=n can limit the number of times this logic is applied
In addition, the following abbreviations are available to for simplified usage:
-kgb or --keyword-group-blanks is short for -kgbb=2 -kgba=2 kgbi
-nkgb or --nokeyword-group-blanks, is short for -kgbb=1 -kgba=1 nkgbi
Before describing the meaning of the parameters in detail let us look at an example which is formatted with default parameter settings.
print "Entering test 2\n"; use Test; use Encode qw(from_to encode decode encode_utf8 decode_utf8 find_encoding is_utf8); use charnames qw(greek); my @encodings = grep( /iso-?8859/, Encode::encodings() ); my @character_set = ( '0' .. '9', 'A' .. 'Z', 'a' .. 'z' ); my @source = qw(ascii iso8859-1 cp1250); my @destiny = qw(cp1047 cp37 posix-bc); my @ebcdic_sets = qw(cp1047 cp37 posix-bc); my $str = join( '', map( chr($_), 0x20 .. 0x7E ) ); return unless ($str);
using perltidy -kgb gives:
print "Entering test 2\n"; <----------this blank controlled by -kgbb use Test; use Encode qw(from_to encode decode encode_utf8 decode_utf8 find_encoding is_utf8); use charnames qw(greek); <---------this blank controlled by -kgbi my @encodings = grep( /iso-?8859/, Encode::encodings() ); my @character_set = ( '0' .. '9', 'A' .. 'Z', 'a' .. 'z' ); my @source = qw(ascii iso8859-1 cp1250); my @destiny = qw(cp1047 cp37 posix-bc); my @ebcdic_sets = qw(cp1047 cp37 posix-bc); my $str = join( '', map( chr($_), 0x20 .. 0x7E ) ); <----------this blank controlled by -kgba return unless ($str);
Blank lines have been introduced around the my and use sequences. What happened is that the default keyword list includes my and use but not print and return. So a continuous sequence of nine my and use statements was located. This number exceeds the default threshold of five, so blanks were placed before and after the entire group. Then, since there was also a subsequence of six my lines, a blank line was introduced to separate them.
Finer control over blank placement can be achieved by using the individual parameters rather than the -kgb flag. The individual controls are as follows.
-kgbl=s or --keyword-group-blanks-list=s, where s is a quoted string, defines the set of keywords which will be formed into groups. The string is a space separated list of keywords. The default set is s=``use require local 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 BC. To include static block comments (which normally begin with '##'), include the symbol SBC.
-kgbs=s or --keyword-group-blanks-size=s, where s is a string describing the number of consecutive keyword statements forming a group. If s is an integer then it is the minimum number required for a group. A maximum value may also be given with the format s=min.max, where min is the minimum number and max is the maximum number, and the min and max values are separated by one or more dots. No groups will be found if the maximum is less than the minimum. The maximum is unlimited if not given. The default is s=5. Some examples:
s min max number for group 3 3 unlimited 3 or more 1.1 1 1 1 1..3 1 3 1 to 3 1.0 1 0 (no match)
-kgbb=n or --keyword-group-blanks-before=n specifies whether a blank should appear before the first line of the group, as follows:
n=0 => (delete) an existing blank line will be removed n=1 => (stable) no change to the input file is made [DEFAULT] n=2 => (insert) a blank line is introduced if possible
-kgba=n or --keyword-group-blanks-after=n likewise specifies whether a blank should appear after the last line of the group, using the same scheme (0=delete, 1=stable, 2=insert).
-kgbi or --keyword-group-blanks-inside controls the insertion of blank lines between the first and last statement of the entire group. If there is a continuous run of a single statement type with more than the minimum threshold number (as specified with -kgbs=s) then this switch causes a blank line be inserted between this subgroup and the others. In the example above this happened between the use and my statements.
-kgbd or --keyword-group-blanks-delete controls the deletion of any blank lines that exist in the the group when it is first scanned. When statements are initially scanned, any existing blank lines are included in the collection. Any such orignial blank lines will be deleted before any other insertions are made when the parameter -kgbd is set. The default is not to do this, -nkgbd.
-kgbr=n or --keyword-group-blanks-repeat-count=n specifies n, the maximum number of times this logic will be applied to any file. The special value n=0 is the same as n=infinity which means it will be applied to an entire script [Default]. A value n=1 could be used to make it apply just one time for example. This might be useful for adjusting just the use statements in the top part of a module for example.
-kgb or --keyword-group-blanks is an abbreviation equivalent to setting -kgbb=1 -kgba=1 -kgbi. This turns on keyword group formatting with a set of default values.
-nkgb or --nokeyword-group-blanks is equivalent to -kgbb=0 -kgba nkgbi. This flag turns off keyword group blank lines and is the default setting.
Here are a few notes about the functioning of this technique.
In particular, note that deleting old blank lines with -kgbd is an irreversible operation so it should be applied with care. Existing blank lines may be serving an important role in controlling vertical alignment.
For example, if the flags --freeze-blank-lines, or --keep-old-blank-lines=2, are set, then they have priority over any blank line deletion implied by the -kgb flags of this section, so no blank lines will be deleted.
For another example, if a keyword group ends at a sub and the flag kgba=0 requests no blank line there, but we also have --blank-lines-before-subs=2, then two blank lines will still be introduced before the sub.
-lp -bl -noll -pt=2 -bt=2 -sbt=2 -icp
-l=78 -i=4 -ci=4 -st -se -vt=2 -cti=0 -pt=1 -bt=1 -sbt=1 -bbt=1 -nsfs -nolq -wbb="% + - * / x != == >= <= =~ !~ < > | & = **= += *= &= <<= &&= -= /= |= >>= ||= //= .= %= ^= x="
Please note that this parameter set includes -st and -se flags, which make perltidy act as a filter on one file only. These can be overridden by placing -nst and/or -nse after the -pbp parameter.
Also note that the value of continuation indentation, -ci=4, is equal to the value of the full indentation, -i=4. It is recommended that the either (1) the parameter -ci=2 be used instead, or the flag -xci be set. This will help show structure, particularly when there are ternary statements. The following snippet illustrates these options.
# perltidy -pbp $self->{_text} = ( !$section ? '' : $type eq 'item' ? "the $section entry" : "the section on $section" ) . ( $page ? ( $section ? ' in ' : '' ) . "the $page$page_ext manpage" : ' elsewhere in this document' ); # perltidy -pbp -ci=2 $self->{_text} = ( !$section ? '' : $type eq 'item' ? "the $section entry" : "the section on $section" ) . ( $page ? ( $section ? ' in ' : '' ) . "the $page$page_ext manpage" : ' elsewhere in this document' ); # perltidy -pbp -xci $self->{_text} = ( !$section ? '' : $type eq 'item' ? "the $section entry" : "the section on $section" ) . ( $page ? ( $section ? ' in ' : '' ) . "the $page$page_ext manpage" : ' elsewhere in this document' );
The -xci flag was developed after the -pbp parameters were published so you need to include it separately.
where the contents within the curly braces is short enough to fit on a single line.
With few exceptions, perltidy retains existing one-line blocks, if it is possible within the line-length constraint, but it does not attempt to form new ones. In other words, perltidy will try to follow the one-line block style of the input file.
If an existing one-line block is longer than the maximum line length, however, it will be broken into multiple lines. When this happens, perltidy checks for and adds any optional terminating semicolon (unless the -nasc option is used) if the block is a code block.
The main exception is that perltidy will attempt to form new one-line blocks following the keywords "map", "eval", and "sort", because these code blocks are often small and most clearly displayed in a single line.
One-line block rules can conflict with the cuddled-else option. When the cuddled-else option is used, perltidy retains existing one-line blocks, even if they do not obey cuddled-else formatting.
Occasionally, when one-line blocks get broken because they exceed the available line length, the formatting will violate the requested brace style. If this happens, reformatting the script a second time should correct the problem.
Sometimes it might be desirable to convert a script to have one-line blocks whenever possible. Although there is currently no flag for this, a simple workaround is to execute perltidy twice, once with the flag -noadd-newlines and then once again with normal parameters, like this:
cat infile | perltidy -nanl | perltidy >outfile
When executed on this snippet
if ( $? == -1 ) { die "failed to execute: $!\n"; } if ( $? == -1 ) { print "Had enough.\n"; die "failed to execute: $!\n"; }
the result is
if ( $? == -1 ) { die "failed to execute: $!\n"; } if ( $? == -1 ) { print "Had enough.\n"; die "failed to execute: $!\n"; }
This shows that blocks with a single statement become one-line blocks.
n=0 remove terminal semicolons in one-line blocks having a single statement n=1 stable; keep input file placement of terminal semicolons [DEFAULT ] n=2 add terminal semicolons in all one-line blocks
Note that the n=2 option has no effect if adding semicolons is prohibited with the -nasc flag. Also not that while n=2 adds missing semicolons to all one-line blocks, regardless of complexity, the n=0 option only removes ending semicolons which terminate one-line blocks containing just one semicolon. So these two options are not exact inverses.
foreach (@list) { if ($_ eq $asked_for) { last } ++$found }
The default behavior is to break such lines into multiple lines, but this behavior can be controlled with this flag. The values of n are:
n=0 break nested one-line blocks into multiple lines [DEFAULT] n=1 stable: keep existing nested-one line blocks intact
For the above example, the default formatting (-olbn=0) is
foreach (@list) { if ( $_ eq $asked_for ) { last } ++$found; }
If the parameter -olbn=1 is given, then the line will be left intact if it is a single line in the source, or it will be broken into multiple lines if it is broken in multiple lines in the source.
$data = $pkg->new( PeerAddr => join( ".", @port[ 0 .. 3 ] ), PeerPort => $port[4] * 256 + $port[5], Proto => 'tcp' );
Vertical alignment can be completely turned off using -novalign, a flag mainly intended for debugging. However, vertical alignment can be forced to stop and restart by selectively introducing blank lines. For example, a blank has been inserted in the following code to keep somewhat similar things aligned.
%option_range = ( 'format' => [ 'tidy', 'html', 'user' ], 'output-line-ending' => [ 'dos', 'win', 'mac', 'unix' ], 'character-encoding' => [ 'none', 'utf8' ], 'block-brace-tightness' => [ 0, 2 ], 'brace-tightness' => [ 0, 2 ], 'paren-tightness' => [ 0, 2 ], 'square-bracket-tightness' => [ 0, 2 ], );
Vertical alignment is implemented by locally increasing an existing blank space to produce alignment with an adjacent line. It cannot occur if there is no blank space to increase. So if a particular space is removed by one of the existing controls then vertical alignment cannot occur. Likewise, if a space is added with one of the controls, then vertical alignment might occur.
For example,
# perltidy -nwls='=>' $data = $pkg->new( PeerAddr=> join( ".", @port[ 0 .. 3 ] ), PeerPort=> $port[4] * 256 + $port[5], Proto=> 'tcp' );
The command -dp or --delete-pod will remove all pod documentation (but not comments).
Two commands which remove comments (but not pod) are: -dbc or --delete-block-comments and -dsc or --delete-side-comments. (Hanging side comments will be deleted with side comments here.)
The negatives of these commands also work, and are the defaults. When block comments are deleted, any leading 'hash-bang' will be retained. Also, if the -x flag is used, any system commands before a leading hash-bang will be retained (even if they are in the form of comments).
The command -tac or --tee-all-comments will write all comments and all pod documentation.
The command -tp or --tee-pod will write all pod documentation (but not comments).
The commands which write comments (but not pod) are: -tbc or --tee-block-comments and -tsc or --tee-side-comments. (Hanging side comments will be written with side comments here.)
The negatives of these commands also work, and are the defaults.
These other locations are system-dependent, and may be displayed with the command "perltidy -dpro". Under Unix systems, it will first look for an environment variable PERLTIDY. Then it will look for a .perltidyrc file in the home directory, and then for a system-wide file /usr/local/etc/perltidyrc, and then it will look for /etc/perltidyrc. Note that these last two system-wide files do not have a leading dot. Further system-dependent information will be found in the INSTALL file distributed with perltidy.
Under Windows, perltidy will also search for a configuration file named perltidy.ini since Windows does not allow files with a leading period (.). Use "perltidy -dpro" to see the possible locations for your system. An example might be C:\Documents and Settings\All Users\perltidy.ini.
Another option is the use of the PERLTIDY environment variable. The method for setting environment variables depends upon the version of Windows that you are using. Instructions for Windows 95 and later versions can be found here:
http://www.netmanage.com/000/20021101_005_tcm21-6336.pdf
Under Windows NT / 2000 / XP the PERLTIDY environment variable can be placed in either the user section or the system section. The later makes the configuration file common to all users on the machine. Be sure to enter the full path of the configuration file in the value of the environment variable. Ex. PERLTIDY=C:\Documents and Settings\perltidy.ini
The configuration file is free format, and simply a list of parameters, just as they would be entered on a command line. Any number of lines may be used, with any number of parameters per line, although it may be easiest to read with one parameter per line. Comment text begins with a #, and there must also be a space before the # for side comments. It is a good idea to put complex parameters in either single or double quotes.
Here is an example of a .perltidyrc file:
# This is a simple of a .perltidyrc configuration file # This implements a highly spaced style -se # errors to standard error output -w # show all warnings -bl # braces on new lines -pt=0 # parens not tight at all -bt=0 # braces not tight -sbt=0 # square brackets not tight
The parameters in the .perltidyrc file are installed first, so any parameters given on the command line will have priority over them.
To avoid confusion, perltidy ignores any command in the .perltidyrc file which would cause some kind of dump and an exit. These are:
-h -v -ddf -dln -dop -dsn -dtt -dwls -dwrs -ss
There are several options may be helpful in debugging a .perltidyrc file:
perltidy -dpro
newword { -opt1 -opt2 }
where newword is the abbreviation, and opt1, etc, are existing parameters or other abbreviations. The main syntax requirement is that the new abbreviation along with its opening curly brace must begin on a new line. Space before and after the curly braces is optional. For a specific example, the following line
airy {-bl -pt=0 -bt=0 -sbt=0}
could be placed in a .perltidyrc file, and then invoked at will with
perltidy -airy somefile.pl
(Either "-airy" or "--airy" may be used).
Please note: do not use this flag unless you are sure your script needs it. Parsing errors can occur if it does not have a hash-bang, or, for example, if the actual first hash-bang is in a here-doc. In that case a parsing error will occur because the tokenization will begin in the middle of the here-doc.
perltidy --mangle myfile.pl -st | perltidy -o myfile.pl.new
This will form the maximum possible number of one-line blocks (see next section), and can sometimes help clean up a badly formatted script.
A similar technique can be used with --extrude instead of --mangle to make the minimum number of one-line blocks.
Another use for --mangle is to combine it with -dac to reduce the file size of a perl script.
--dump-cuddled-block-list or -dcbl will dump to standard output the internal hash of cuddled block types created by a -cuddled-block-list input string.
--dump-defaults or -ddf will write the default option set to standard output and quit
--dump-profile or -dpro will write the name of the current configuration file and its contents to standard output and quit.
--dump-options or -dop will write current option set to standard output and quit.
--dump-long-names or -dln will write all command line long names (passed to Get_options) to standard output and quit.
--dump-short-names or -dsn will write all command line short names to standard output and quit.
--dump-token-types or -dtt will write a list of all token types to standard output and quit.
--dump-want-left-space or -dwls will write the hash %want_left_space to standard output and quit. See the section on controlling whitespace around tokens.
--dump-want-right-space or -dwrs will write the hash %want_right_space to standard output and quit. See the section on controlling whitespace around tokens.
--no-memoize or -nmem will turn of memoizing. Memoization can reduce run time when running perltidy repeatedly in a single process. It is on by default but can be deactivated for testing with -nmem.
--no-timestamp or -nts will eliminate any time stamps in output files to prevent differences in dates from causing test installation scripts to fail. There are just a couple of places where timestamps normally occur. One is in the headers of html files, and another is when the -cscw option is selected. The default is to allow timestamps (--timestamp or -ts).
--file-size-order or -fso will cause files to be processed in order of increasing size, when multiple files are being processed. This is useful during program development, when large numbers of files with varying sizes are processed, because it can reduce virtual memory usage.
--maximum-file-size-mb=n or -maxfs=n specifies the maximum file size in megabytes that perltidy will attempt to format. This parameter is provided to avoid causing system problems by accidentally attempting to format an extremely large data file. Most perl scripts are less than about 2 MB in size. The integer n has a default value of 10, so perltidy will skip formatting files which have a size greater than 10 MB. The command to increase the limit to 20 MB for example would be
perltidy -maxfs=20
This only applies to files specified by filename on the command line.
--maximum-level-errors=n or -maxle=n specifies the maximum number of indentation level errors are allowed before perltidy skips formatting and just outputs a file verbatim. The default is n=1. This means that if the final indentation of a script differs from the starting indentation by more than 1 levels, the file will be output verbatim. To avoid formatting if there are any indentation level errors use -maxle=0. To skip this check you can either set n equal to a large number, such as n=100, or set n=-1.
For example, the following script has level error of 3 and will be output verbatim
Input and default output: {{{ perltidy -maxle=100 { { {
--maximum-unexpected-errors=n or -maxue=n specifies the maximum number of unexpected tokenization errors are allowed before formatting is skipped and a script is output verbatim. The intention is to avoid accidentally formatting a non-perl script, such as an html file for example. This check can be turned off by setting n=0.
A recommended value is n=3. However, the default is n=0 (skip this check) to avoid causing problems with scripts which have extended syntaxes.
-DEBUG will write a file with extension .DEBUG for each input file showing the tokenization of all lines of code.
If the AutoLoader module is used, perltidy will continue formatting code after seeing an __END__ line. Use --nolook-for-autoloader, or -nlal, to deactivate this feature.
Likewise, if the SelfLoader module is used, perltidy will continue formatting code after seeing a __DATA__ line. Use --nolook-for-selfloader, or -nlsl, to deactivate this feature.
There is no way to override these rules.
perltidy -html somefile.pl
will produce a syntax-colored html file named somefile.pl.html which may be viewed with a browser.
Please Note: In this case, perltidy does not do any formatting to the input file, and it does not write a formatted file with extension .tdy. This means that two perltidy runs are required to create a fully reformatted, html copy of a script.
NOTE: Perltidy counts the number of "=cut" lines, and either moves the pod text to the top of the html file if there is one "=cut", or leaves the pod text in its original order (interleaved with code) otherwise.
Most of the flags accepted by pod2html may be included in the perltidy command line, and they will be passed to pod2html. In some cases, the flags have a prefix "pod" to emphasize that they are for the pod2html, and this prefix will be removed before they are passed to pod2html. The flags which have the additional "pod" prefix are:
--[no]podheader --[no]podindex --[no]podrecurse --[no]podquiet --[no]podverbose --podflush
The flags which are unchanged from their use in pod2html are:
--backlink=s --cachedir=s --htmlroot=s --libpods=s --title=s --podpath=s --podroot=s
where 's' is an appropriate character string. Not all of these flags are available in older versions of Pod::Html. See your Pod::Html documentation for more information.
The alternative, indicated with -npod, is not to use Pod::Html, but rather to format pod text in italics (or whatever the stylesheet indicates), without special html markup. This is useful, for example, if pod is being used as an alternative way to write comments.
MyModule.pm.html - the frame MyModule.pm.toc.html - the table of contents MyModule.pm.src.html - the formatted source code
Obviously this file naming scheme requires that output be directed to a real file (as opposed to, say, standard output). If this is not the case, or if the file extension is unknown, the -frm option will be ignored.
An alternative to an embedded style sheet is to create a page with a link to an external style sheet. This is indicated with the -css=filename, where the external style sheet is filename. The external style sheet filename will be created if and only if it does not exist. This option is useful for controlling multiple pages from a single style sheet.
To cause perltidy to write a style sheet to standard output and exit, use the -ss, or --stylesheet, flag. This is useful if the style sheet could not be written for some reason, such as if the -pre flag was used. Thus, for example,
perltidy -html -ss >mystyle.css
will write a style sheet with the default properties to file mystyle.css.
The use of style sheets is encouraged, but a web page without a style sheets can be created with the flag -nss. Use this option if you must to be sure that older browsers (roughly speaking, versions prior to 4.0 of Netscape Navigator and Internet Explorer) can display the syntax-coloring of the html files.
Syntax colors may be changed from their default values by flags of the either the long form, -html-color-xxxxxx=n, or more conveniently the short form, -hcx=n, where xxxxxx is one of the following words, and x is the corresponding abbreviation:
Token Type xxxxxx x ---------- -------- -- comment comment c number numeric n identifier identifier i bareword, function bareword w keyword keyword k quite, pattern quote q here doc text here-doc-text h here doc target here-doc-target hh punctuation punctuation pu parentheses paren p structural braces structure s semicolon semicolon sc colon colon co comma comma cm label label j sub definition name subroutine m pod text pod-text pd
A default set of colors has been defined, but they may be changed by providing values to any of the following parameters, where n is either a 6 digit hex RGB color value or an ascii name for a color, such as 'red'.
To illustrate, the following command will produce an html file somefile.pl.html with ``aqua'' keywords:
perltidy -html -hck=00ffff somefile.pl
and this should be equivalent for most browsers:
perltidy -html -hck=aqua somefile.pl
Perltidy merely writes any non-hex names that it sees in the html file. The following 16 color names are defined in the HTML 3.2 standard:
black => 000000, silver => c0c0c0, gray => 808080, white => ffffff, maroon => 800000, red => ff0000, purple => 800080, fuchsia => ff00ff, green => 008000, lime => 00ff00, olive => 808000, yellow => ffff00 navy => 000080, blue => 0000ff, teal => 008080, aqua => 00ffff,
Many more names are supported in specific browsers, but it is safest to use the hex codes for other colors. Helpful color tables can be located with an internet search for ``HTML color tables''.
Besides color, two other character attributes may be set: bold, and italics. To set a token type to use bold, use the flag --html-bold-xxxxxx or -hbx, where xxxxxx or x are the long or short names from the above table. Conversely, to set a token type to NOT use bold, use --nohtml-bold-xxxxxx or -nhbx.
Likewise, to set a token type to use an italic font, use the flag --html-italic-xxxxxx or -hix, where again xxxxxx or x are the long or short names from the above table. And to set a token type to NOT use italics, use --nohtml-italic-xxxxxx or -nhix.
For example, to use bold braces and lime color, non-bold, italics keywords the following command would be used:
perltidy -html -hbs -hck=00FF00 -nhbk -hik somefile.pl
The background color can be specified with --html-color-background=n, or -hcbg=n for short, where n is a 6 character hex RGB value. The default color of text is the value given to punctuation, which is black as a default.
Here are some notes and hints:
1. If you find a preferred set of these parameters, you may want to create a .perltidyrc file containing them. See the perltidy man page for an explanation.
2. Rather than specifying values for these parameters, it is probably easier to accept the defaults and then edit a style sheet. The style sheet contains comments which should make this easy.
3. The syntax-colored html files can be very large, so it may be best to split large files into smaller pieces to improve download times.
The keyword sub indicates a named sub. For anonymous subs, use the special keyword asub.
For example, the following parameter specifies "sub", labels, "BEGIN", and "END" blocks:
-cscl="sub : BEGIN END"
(the meaning of the -cscl parameter is described above.) Note that quotes are required around the list of block types because of the spaces. For another example, the following list specifies all block types for vertical tightness:
-bbvtl='*'
For example, suppose the file is somefile.pl. For "-bext=old", a '.' is added to give somefile.pl.old. For "-bext=.old", no additional '.' is added, so again the backup file is somefile.pl.old. For "-bext=~", then no dot is added, and the backup file will be somefile.pl~ .
D anl asbl asc ast asu aws b baa baao bar bbao bbb bbc bbs bl bli boa boc bok bol bom bos bot cblx ce conv csc cscb cscw dac dbc dcbl dcsc ddf dln dnl dop dp dpro dsc dsm dsn dtt dwls dwrs dws f fll frm fs fso gcs hbc hbcm hbco hbh hbhh hbi hbj hbk hbm hbn hbp hbpd hbpu hbq hbs hbsc hbv hbw hent hic hicm hico hih hihh hii hij hik him hin hip hipd hipu hiq his hisc hiv hiw hsc html ibc icb icp iob isbc iscl kgb kgbd kgbi kis lal log lop lp lsl mem nib ohbr okw ola olc oll olq opr opt osbc osbr otr ple pod pvl q sac sbc sbl scbb schb scp scsb sct se sfp sfs skp sob sobb sohb sop sosb sot ssc st sts t tac tbc toc tp tqw trp ts tsc tso vmll w wn x xci xs
Equivalently, the prefix 'no' or 'no-' on the corresponding long names may be used.
The main current limitation is that perltidy does not scan modules included with 'use' statements. This makes it necessary to guess the context of any bare words introduced by such modules. Perltidy has good guessing algorithms, but they are not infallible. When it must guess, it leaves a message in the log file.
If you encounter a bug, please report it.
A exit value of 0 indicates that perltidy ran to completion with no error messages.
A non-zero exit value indicates some kind of problem was detected.
An exit value of 1 indicates that perltidy terminated prematurely, usually due to some kind of errors in the input parameters. This can happen for example if a parameter is misspelled or given an invalid value. Error messages in the standard error output will indicate the cause of any problem. If perltidy terminates prematurely then no output files will be produced.
An exit value of 2 indicates that perltidy was able to run to completion but there there are (1) warning messages in the standard error output related to parameter errors or problems and/or (2) warning messages in the perltidy error file(s) relating to possible syntax errors in one or more of the source script(s) being tidied. When multiple files are being processed, an error detected in any single file will produce this type of exit condition.
To report a new bug or problem, use the link on this page.
The source code repository is at <https://github.com/perltidy/perltidy>.
Please refer to the file ``COPYING'' for details.
See the ``GNU General Public License'' for more details.