1 =head1 Perltidy Style Key
3 When perltidy was first developed, the main parameter choices were the number
4 of indentation spaces and if the user liked cuddled else's. As the number of
5 users has grown so has the number of parameters. Now there are so many that it
6 can be difficult for a new user to find a good initial set. This document is
7 one attempt to help with this problem, and some other suggestions are given at
10 Use this document to methodically find a starting set of perltidy parameters to
11 approximate your style. We will be working on just one aspect of formatting at
12 a time. Just read each question and select the best answer. Enter your
13 parameters in a file named F<.perltidyrc> (examples are listed at the end).
14 Then move it to one of the places where perltidy will find it. You can run
15 perltidy with the parameter B<-dpro> to see where these places are for your
18 Before you begin, experiment using just C<perltidy filename.pl> on some
19 of your files. From the results (which you will find in files with a
20 F<.tdy> extension), you will get a sense of what formatting changes, if
21 any, you'd like to make. If the default formatting is acceptable, you
22 do not need a F<.perltidyrc> file.
26 Do you almost always want to run perltidy as a standard filter on just
27 one input file? If yes, use B<-st> and B<-se>.
29 =head2 Line Length Setting
31 Perltidy will set line breaks to prevent lines from exceeding the
34 Do you want the maximum line length to be 80 columns? If no, use
35 B<-l=n>, where B<n> is the number of columns you prefer.
37 =head2 Indentation in Code Blocks
39 In the block below, the variable C<$anchor> is one indentation level deep
40 and is indented by 4 spaces as shown here:
46 If you want to change this to be a different number B<n> of spaces
47 per indentation level, use B<-i=n>.
49 =head2 Continuation Indentation
51 Look at the statement beginning with C<$anchor>:
55 substr( $header, 0, 6 )
56 . substr( $char_list, $place_1, 1 )
57 . substr( $char_list, $place_2, 1 );
60 The statement is too long for the line length (80 characters by default), so it
61 has been broken into 4 lines. The second and later lines have some extra
62 "continuation indentation" to help make the start of the statement easy to
63 find. The default number of extra spaces is 2. If you prefer a number n
64 different from 2, you may specify this with B<-ci=n>. It is probably best if
65 it does not exceed the value of the primary indentation.
69 The default, and recommendation, is to represent leading whitespace
70 with actual space characters. However, if you prefer to entab
71 leading whitespace with one tab character for each B<n> spaces,
72 use B<-et=n>. Typically, B<n> would be 8.
74 =head2 Opening Block Brace Right or Left?
76 Opening and closing curly braces, parentheses, and square brackets are divided
77 into two separate categories and controlled separately in most cases. The two
78 categories are (1) code block curly braces, which contain perl code, and (2)
79 everything else. Basically, a code block brace is one which could contain
80 semicolon-terminated lines of perl code. We will first work on the scheme for
81 code block curly braces.
83 Decide which of the following opening brace styles you prefer for most blocks
84 of code (with the possible exception of a B<sub block brace> which will
87 If you like opening braces on the right, like this, go to
88 L<Opening Braces Right>.
94 If you like opening braces on the left, like this, go to
95 L<Opening Braces Left>.
102 =head2 Opening Braces Right
104 In a multi-line B<if> test expression, the default is to place
105 the opening brace on the left, like this:
107 if ( $bigwasteofspace1 && $bigwasteofspace2
108 || $bigwasteofspace3 && $bigwasteofspace4 )
113 This helps to visually separate the block contents from the test
116 An alternative is to keep the brace on the right even for
117 multiple-line test expressions, like this:
119 if ( $bigwasteofspace1 && $bigwasteofspace2
120 || $bigwasteofspace3 && $bigwasteofspace4 ) {
124 If you prefer this alternative, use B<-bar>.
128 Do you prefer this B<Cuddled Else> style
130 if ( $flag eq "h" ) {
132 } elsif ( $flag eq "f" ) {
135 print "invalid option: " . substr( $arg, $i, 1 ) . "\n";
139 instead of this default style?
141 if ( $flag eq "h" ) {
144 elsif ( $flag eq "f" ) {
148 print "invalid option: " . substr( $arg, $i, 1 ) . "\n";
152 If yes, you should use B<-ce>.
153 Now skip ahead to L<Opening Sub Braces>.
155 =head2 Opening Braces Left
157 Use B<-bl> if you prefer this style:
164 Use B<-bli> if you prefer this indented-brace style:
171 The number of spaces of extra indentation will be the value specified
172 for continuation indentation with the B<-ci=n> parameter (2 by default).
174 =head2 Opening Sub Braces
176 By default, the opening brace of a sub block will be treated
177 the same as other code blocks. If this is okay, skip ahead
178 to L<Block Brace Vertical Tightness>.
180 If you prefer an opening sub brace to be on a new line,
188 use B<-sbl>. If you prefer the sub brace on the right like this
197 If you wish to give this opening sub brace some indentation you can do
198 that with the parameters B<-bli> and B<-blil> which are described in the
201 =head2 Block Brace Vertical Tightness
203 If you chose to put opening block braces of all types to the right, skip
204 ahead to L<Closing Block Brace Indentation>.
206 If you chose to put braces of any type on the left, the default is to leave the
207 opening brace on a line by itself, like this (shown for B<-bli>, but also true
215 But you may also use this more compressed style if you wish:
221 If you do not prefer this more compressed form, go to
222 L<Opening Sub Braces>.
224 Otherwise use parameter B<-bbvt=n>, where n=1 or n=2. To decide,
225 look at this snippet:
244 The difference is that B<-bbvt=1> breaks after an opening brace if
245 the next line is unbalanced, whereas B<-bbvt=2> never breaks.
247 If you were expecting the 'ENDIF' word to move up vertically here, note that
248 the second opening brace in the above example is not a code block brace (it is
249 a hash brace), so the B<-bbvt> does not apply to it (another parameter will).
251 =head2 Closing Block Brace Indentation
253 The default is to place closing braces at the same indentation as the
254 opening keyword or brace of that code block, as shown here:
260 If you chose the B<-bli> style, however, the default closing braces will be
261 indented one continuation indentation like the opening brace:
268 If you prefer to give closing block braces one full level of
269 indentation, independently of how the opening brace is treated,
270 for example like this:
278 This completes the definition of the placement of code block braces.
280 =head2 Indentation Style for Other Containers
282 You have a choice of two basic indentation schemes for non-block containers.
283 The default is to use a fixed number of spaces per indentation level (the same
284 number of spaces used for code blocks, which is 4 by default). Here is an
285 example of the default:
295 In this default indentation scheme, a simple formula is used to find the
296 indentation of every line. Notice how the first 'undef' is indented 4
297 spaces (one level) to the right, and how 'PrintError' is indented 4 more
298 speces (one more level) to the right.
300 The alternate is to let the location of the opening paren (or square
301 bracket, or curly brace) define the indentation, like this:
311 The first scheme is completely robust. The second scheme often looks a little
312 nicer, but be aware that deeply nested structures it can be spoiled if the line
313 length limit is exceeded. Also, if there are comments or blank lines within a
314 complex structure perltidy will temporarily fall back on the default
315 indentation scheme. You may want to try both on large sections of code to see
318 If you prefer the first (default) scheme, no parameter is needed.
320 If you prefer the latter scheme, use B<-lp>.
322 =head2 Opening Vertical Tightness
324 The information in this section applies mainly to the B<-lp>
325 style but it also applies in some cases to the default style.
326 It will be illustrated for the B<-lp> indentation style.
328 The default B<-lp> indentation style ends a line at the
329 opening tokens, like this:
339 Here is a tighter alternative, which does not end a line
340 with the opening tokens:
342 $dbh = DBI->connect( undef, undef, undef,
348 The difference is that the lines have been compressed vertically without
349 any changes to the indentation. This can almost always be done with the
350 B<-lp> indentation style, but only in limited cases for the default
353 If you prefer the default, skip ahead to L<Closing Token Placement>.
355 Otherwise, use B<-vt=n>, where B<n> should be either 1 or 2. To help
356 decide, observe the first three opening parens in the following snippet
357 and choose the value of n you prefer. Here it is with B<-lp -vt=1>:
361 start_slip( $DEVICE, $PHONE, $ACCOUNT, $PASSWORD,
362 $LOCAL, $REMOTE, $NETMASK, $MTU
365 && $continuation_flag
368 do_something_about_it();
371 And here it is again formatted with B<-lp -vt=2>:
373 if ( !defined( start_slip( $DEVICE, $PHONE, $ACCOUNT, $PASSWORD,
374 $LOCAL, $REMOTE, $NETMASK, $MTU
377 && $continuation_flag
380 do_something_about_it();
383 The B<-vt=1> style tries to display the structure by preventing more
384 than one step in indentation per line. In this example, the first two
385 opening parens were not followed by balanced lines, so B<-vt=1> broke
388 The B<-vt=2> style does not limit itself to a single indentation step
391 Note that in the above example the function 'do_sumething_about_it'
392 started on a new line. This is because it follows an opening code
393 block brace and is governed by the flag previously set in
394 L<Block Brace Vertical Tightness>.
396 =head2 Closing Token Placement
398 You have several options for dealing with the terminal closing tokens of
399 non-blocks. In the following examples, a closing parenthesis is shown, but
400 these parameters apply to closing square brackets and non-block curly braces as
403 The default behavior for parenthesized relatively large lists is to place the
404 closing paren on a separate new line. The flag B<-cti=n> controls the amount
405 of indentation of such a closing paren.
407 The default, B<-cti=0>, for a line beginning with a closing paren, is to use
408 the indentation defined by the next (lower) indentation level. This works
409 well for the default indentation scheme:
413 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
414 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'
417 but it may not look very good with the B<-lp> indentation scheme:
421 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
422 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'
425 An alternative which works well with B<-lp> indentation is B<-cti=1>,
426 which aligns the closing paren vertically with its
427 opening paren, if possible:
429 # perltidy -lp -cti=1
431 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
432 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'
435 Another alternative, B<-cti=3>, indents a line with leading closing
436 paren one full indentation level:
438 # perltidy -lp -cti=3
440 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
441 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'
444 If you prefer the closing paren on a separate line like this,
445 note the value of B<-cti=n> that you prefer and skip ahead to
446 L<Define Horizontal Tightness>.
448 Finally, the question of paren indentation can be avoided by placing it
449 at the end of the previous line, like this:
452 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
453 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' );
455 Perltidy will automatically do this to save space for very short lists but not
458 Use B<-vtc=n> if you prefer to usually do this, where B<n> is either 1 or 2. To
459 determine B<n>, we have to look at something more complex. Observe the
460 behavior of the closing tokens in the following snippet:
462 Here is B<-lp -vtc=1>:
464 $srec->{'ACTION'} = [
474 Here is B<-lp -vtc=2>:
476 $srec->{'ACTION'} = [
479 $loc, $lookup, $fh ),
482 $loc, $lookup, $fh ) ];
484 Choose the one that you prefer. The difference is that B<-vtc=1> leaves
485 closing tokens at the start of a line within a list, which can assist in
486 keeping hierarchical lists readable. The B<-vtc=2> style always tries
487 to move closing tokens to the end of a line.
489 If you choose B<-vtc=1>,
490 you may also want to specify a value of B<-cti=n> (previous section) to
491 handle cases where a line begins with a closing paren.
493 =head2 Stack Opening Tokens
495 In the following snippet the opening hash brace has been placed
498 $opt_c = Text::CSV_XS->new(
506 If you prefer to avoid isolated opening opening tokens by
507 "stacking" them together with other opening tokens like this:
509 $opt_c = Text::CSV_XS->new( {
518 =head2 Stack Closing Tokens
520 Likewise, in the same snippet the default formatting leaves
521 the closing paren on a line by itself here:
523 $opt_c = Text::CSV_XS->new(
531 If you would like to avoid leaving isolated closing tokens by
532 stacking them with other closing tokens, like this:
534 $opt_c = Text::CSV_XS->new(
543 The B<-sct> flag is somewhat similar to the B<-vtc> flags, and in some cases it
544 can give a similar result. The difference is that the B<-vtc> flags try to
545 avoid lines with leading opening tokens by "hiding" them at the end of a
546 previous line, whereas the B<-sct> flag merely tries to reduce the number of
547 lines with isolated closing tokens by stacking multiple closing tokens
548 together, but it does not try to hide them.
550 The manual shows how all of these vertical tightness controls may be applied
551 independently to each type of non-block opening and opening token.
553 =head2 Define Horizontal Tightness
555 Horizontal tightness parameters define how much space is included
556 within a set of container tokens.
558 For parentheses, decide which of the following values of B<-pt=n>
561 if ( ( my $len_tab = length( $tabstr ) ) > 0 ) { # -pt=0
562 if ( ( my $len_tab = length($tabstr) ) > 0 ) { # -pt=1 (default)
563 if ((my $len_tab = length($tabstr)) > 0) { # -pt=2
565 For n=0, space is always used, and for n=2, space is never used. For
566 the default n=1, space is used if the parentheses contain more than
569 For square brackets, decide which of the following values of B<-sbt=n>
572 $width = $col[ $j + $k ] - $col[ $j ]; # -sbt=0
573 $width = $col[ $j + $k ] - $col[$j]; # -sbt=1 (default)
574 $width = $col[$j + $k] - $col[$j]; # -sbt=2
576 For curly braces, decide which of the following values of B<-bt=n>
579 $obj->{ $parsed_sql->{ 'table' }[0] }; # -bt=0
580 $obj->{ $parsed_sql->{'table'}[0] }; # -bt=1 (default)
581 $obj->{$parsed_sql->{'table'}[0]}; # -bt=2
583 For code block curly braces, decide which of the following values of
584 B<-bbt=n> you prefer:
586 %bf = map { $_ => -M $_ } grep { /\.deb$/ } dirents '.'; # -bbt=0 (default)
587 %bf = map { $_ => -M $_ } grep {/\.deb$/} dirents '.'; # -bbt=1
588 %bf = map {$_ => -M $_} grep {/\.deb$/} dirents '.'; # -bbt=2
590 =head2 Spaces between function names and opening parens
592 The default is not to place a space after a function call:
594 myfunc( $a, $b, $c ); # default
596 If you prefer a space:
598 myfunc ( $a, $b, $c ); # -sfp
602 =head2 Spaces between Perl keywords and parens
604 The default is to place a space between only these keywords
605 and an opening paren:
607 my local our and or eq ne if else elsif until unless
608 while for foreach return switch case given when
610 but no others. For example, the default is:
614 If you want a space between all Perl keywords and an opening paren,
618 use B<-skp>. For detailed control of individual keywords, see the manual.
620 =head2 Statement Termination Semicolon Spaces
622 The default is not to put a space before a statement termination
623 semicolon, like this:
627 If you prefer a space, like this:
633 =head2 For Loop Semicolon Spaces
635 The default is to place a space before a semicolon in a for statement,
638 for ( @a = @$ap, $u = shift @a ; @a ; $u = $v ) { # -sfs (default)
640 If you prefer no such space, like this:
642 for ( @a = @$ap, $u = shift @a; @a; $u = $v ) { # -nsfs
646 =head2 Block Comment Indentation
648 Block comments are comments which occupy a full line, as opposed to side
649 comments. The default is to indent block comments with the same
650 indentation as the code block that contains them (even though this
651 will allow long comments to exceed the maximum line length).
653 If you would like block comments indented except when this would cause
654 the maximum line length to be exceeded, use B<-olc>. This will cause a
655 group of consecutive block comments to be outdented by the amount needed
656 to prevent any one from exceeding the maximum line length.
658 If you never want block comments indented, use B<-nibc>.
660 If block comments may only be indented if they have some space
661 characters before the leading C<#> character in the input file, use
664 The manual shows many other options for controlling comments.
666 =head2 Outdenting Long Quotes
668 Long quoted strings may exceed the specified line length limit. The
669 default, when this happens, is to outdent them to the first column.
670 Here is an example of an outdented long quote:
672 if ($source_stream) {
675 "You may not specify any filenames when a source array is given\n";
679 The effect is not too different from using a here document to represent
680 the quote. If you prefer to leave the quote indented, like this:
682 if ($source_stream) {
685 "You may not specify any filenames when a source array is given\n";
691 =head2 Many Other Parameters
693 This document has only covered the most popular parameters. The manual
694 contains many more and should be consulted if you did not find what you need
697 =head2 Example F<.perltidyrc> files
699 Now gather together all of the parameters you prefer and enter them
700 in a file called F<.perltidyrc>.
702 Here are some example F<.perltidyrc> files and the corresponding style.
704 Here is a little test snippet, shown the way it would appear with
717 You do not need a F<.perltidyrc> file for this style.
719 Here is the same snippet
730 for a F<.perltidyrc> file containing these parameters:
738 You do not need to place just one parameter per line, but this may be
739 convenient for long lists. You may then hide any parameter by placing
740 a C<#> symbol before it.
742 And here is the snippet
751 for a F<.perltidyrc> file containing these parameters:
757 =head2 Additional Information
759 This document has covered the main parameters. Many more parameters are
760 available for special purposes and for fine-tuning a style. For complete
761 information see the perltidy manual
762 http://perltidy.sourceforge.net/perltidy.html
764 For an introduction to using perltidy, see the tutorial
765 http://perltidy.sourceforge.net/tutorial.html
767 Suggestions for improving this document are welcome and may be sent to
768 perltidy at users.sourceforge.net