]> git.donarmstrong.com Git - perltidy.git/blob - docs/stylekey.pod
* upgrade to the 20060614 release
[perltidy.git] / docs / stylekey.pod
1 =head1 Perltidy Style Key
2
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
8 the end.
9
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
16 system.
17
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.
23
24 =head2 Use as Filter?
25
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>.  
28
29 =head2 Line Length Setting
30
31 Perltidy will set line breaks to prevent lines from exceeding the
32 maximum line length.  
33
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.
36
37 =head2 Indentation in Code Blocks
38
39 In the block below, the variable C<$anchor> is one indentation level deep
40 and is indented by 4 spaces as shown here: 
41
42     if ( $flag eq "a" ) {
43         $anchor = $header;
44     }  
45
46 If you want to change this to be a different number B<n> of spaces
47 per indentation level, use B<-i=n>.
48
49 =head2 Continuation Indentation
50
51 Look at the statement beginning with C<$anchor>:
52
53     if ( $flag eq "a" ) {
54         $anchor =
55           substr( $header, 0, 6 )
56           . substr( $char_list, $place_1, 1 )
57           . substr( $char_list, $place_2, 1 );
58     }
59
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.
66
67 =head2 Tabs
68
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.  
73
74 =head2 Opening Block Brace Right or Left?
75
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.  
82
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
85 be covered later):
86
87 If you like opening braces on the right, like this, go to 
88 L<Opening Braces Right>.
89
90     if ( $flag eq "h" ) {
91         $headers = 0;
92     }  
93
94 If you like opening braces on the left, like this, go to 
95 L<Opening Braces Left>.
96
97     if ( $flag eq "h" )
98     {
99         $headers = 0;
100     }
101
102 =head2 Opening Braces Right
103
104 In a multi-line B<if> test expression, the default is to place
105 the opening brace on the left, like this:
106
107     if ( $bigwasteofspace1 && $bigwasteofspace2
108         || $bigwasteofspace3 && $bigwasteofspace4 )
109     {
110         big_waste_of_time();
111     }
112
113 This helps to visually separate the block contents from the test
114 expression.  
115
116 An alternative is to keep the brace on the right even for
117 multiple-line test expressions, like this:
118
119     if ( $bigwasteofspace1 && $bigwasteofspace2
120         || $bigwasteofspace3 && $bigwasteofspace4 ) {
121         big_waste_of_time();
122     }
123
124 If you prefer this alternative, use B<-bar>.
125
126 =head2 Cuddled Else?
127
128 Do you prefer this B<Cuddled Else> style
129
130     if ( $flag eq "h" ) {
131         $headers = 0;
132     } elsif ( $flag eq "f" ) {
133         $sectiontype = 3;
134     } else {
135         print "invalid option: " . substr( $arg, $i, 1 ) . "\n";
136         dohelp();
137     }
138
139 instead of this default style?
140
141     if ( $flag eq "h" ) {
142         $headers = 0;
143     }  
144     elsif ( $flag eq "f" ) {
145         $sectiontype = 3;
146     } 
147     else {    
148         print "invalid option: " . substr( $arg, $i, 1 ) . "\n";
149         dohelp();
150     }
151
152 If yes, you should use B<-ce>.
153 Now skip ahead to L<Opening Sub Braces>.
154
155 =head2 Opening Braces Left
156
157 Use B<-bl> if you prefer this style:
158
159     if ( $flag eq "h" )
160     {
161         $headers = 0;
162     }
163
164 Use B<-bli> if you prefer this indented-brace style:
165
166     if ( $flag eq "h" )
167       {
168         $headers = 0;
169       }
170
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).
173
174 =head2 Opening Sub Braces
175
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>.
179
180 If you prefer an opening sub brace to be on a new line,
181 like this: 
182
183     sub message
184     {
185         # -sbl
186     }
187
188 use B<-sbl>.  If you prefer the sub brace on the right like this
189
190     sub message {
191
192         # -nsbl
193     }
194
195 use B<-nsbl>.
196
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
199 manual.
200
201 =head2 Block Brace Vertical Tightness
202
203 If you chose to put opening block braces of all types to the right, skip
204 ahead to L<Closing Block Brace Indentation>.
205
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
208 for B<-bl>):
209
210     if ( $flag eq "h" )
211       {
212         $headers = 0;
213       }
214
215 But you may also use this more compressed style if you wish:
216
217     if ( $flag eq "h" )
218       { $headers = 0;
219       }
220
221 If you do not prefer this more compressed form, go to 
222 L<Opening Sub Braces>.
223
224 Otherwise use parameter B<-bbvt=n>, where n=1 or n=2.  To decide,
225 look at this snippet:
226
227     # -bli -bbvt=1
228     sub _directives
229       {
230         {
231             'ENDIF' => \&_endif,
232                'IF' => \&_if,
233         };
234       }
235
236     # -bli -bbvt=2
237     sub _directives
238     {   {
239             'ENDIF' => \&_endif,
240             'IF'    => \&_if,
241         };
242     }
243
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.  
246
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).
250
251 =head2 Closing Block Brace Indentation
252
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:
255
256         if ($task) {
257             yyy();
258         }            # default
259
260 If you chose the B<-bli> style, however, the default closing braces will be
261 indented one continuation indentation like the opening brace:
262
263         if ($task)
264           {
265             yyy();
266           }    # -bli
267
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:
271
272         if ($task) {
273             yyy();
274             }          # -icb
275
276 use B<-icb>.
277
278 This completes the definition of the placement of code block braces.
279
280 =head2 Indentation Style for Other Containers
281
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:
286
287     $dbh = DBI->connect(
288         undef, undef, undef,
289         {
290             PrintError => 0,
291             RaiseError => 1
292         }
293     );
294
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.  
299
300 The alternate is to let the location of the opening paren (or square
301 bracket, or curly brace) define the indentation, like this:
302
303     $dbh = DBI->connect(
304                          undef, undef, undef,
305                          {
306                            PrintError => 0,
307                            RaiseError => 1
308                          }
309     );
310
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
316 which works best.
317
318 If you prefer the first (default) scheme, no parameter is needed.
319
320 If you prefer the latter scheme, use B<-lp>. 
321
322 =head2 Opening Vertical Tightness
323
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.
327
328 The default B<-lp> indentation style ends a line at the
329 opening tokens, like this:
330
331     $dbh = DBI->connect(
332                          undef, undef, undef,
333                          {
334                            PrintError => 0,
335                            RaiseError => 1
336                          }
337     );
338
339 Here is a tighter alternative, which does not end a line
340 with the opening tokens:
341
342     $dbh = DBI->connect( undef, undef, undef,
343                          { PrintError => 0,
344                            RaiseError => 1
345                          }
346     );
347
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
351 indentation style. 
352
353 If you prefer the default, skip ahead to L<Closing Token Placement>.
354
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>:
358
359     if (
360          !defined(
361                    start_slip( $DEVICE, $PHONE,  $ACCOUNT, $PASSWORD,
362                                $LOCAL,  $REMOTE, $NETMASK, $MTU
363                    )
364          )
365          && $continuation_flag
366       )
367     {
368         do_something_about_it();
369     }
370
371 And here it is again formatted with B<-lp -vt=2>:
372
373     if ( !defined( start_slip( $DEVICE, $PHONE,  $ACCOUNT, $PASSWORD,
374                                $LOCAL,  $REMOTE, $NETMASK, $MTU
375                    )
376          )
377          && $continuation_flag
378       )
379     {
380         do_something_about_it();
381     }
382
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
386 after them.  
387
388 The B<-vt=2> style does not limit itself to a single indentation step
389 per line.
390
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>.
395
396 =head2 Closing Token Placement
397
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
401 well.  
402
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.
406
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:
410
411     # perltidy
412     @month_of_year = (
413         'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
414         'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'
415     );
416
417 but it may not look very good with the B<-lp> indentation scheme:
418
419     # perltidy -lp
420     @month_of_year = (
421                        'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
422                        'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'
423     );
424
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:  
428
429     # perltidy -lp -cti=1
430     @month_of_year = (
431                        'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
432                        'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'
433                      );
434
435 Another alternative, B<-cti=3>, indents a line with leading closing
436 paren one full indentation level:
437
438     # perltidy -lp -cti=3
439     @month_of_year = (
440                        'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
441                        'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'
442                        );
443
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>. 
447
448 Finally, the question of paren indentation can be avoided by placing it
449 at the end of the previous line, like this:
450
451     @month_of_year = (
452         'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
453         'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' );
454
455 Perltidy will automatically do this to save space for very short lists but not
456 for longer lists.
457
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:
461
462 Here is B<-lp -vtc=1>:
463
464     $srec->{'ACTION'} = [
465                           $self->read_value(
466                                              $lookup->{'VFMT'},
467                                              $loc, $lookup, $fh
468                           ),
469                           $self->read_value(
470                                              $lookup->{'VFMT2'},
471                                              $loc, $lookup, $fh
472                           ) ];
473
474 Here is B<-lp -vtc=2>:
475
476     $srec->{'ACTION'} = [
477                           $self->read_value(
478                                              $lookup->{'VFMT'},
479                                              $loc, $lookup, $fh ),
480                           $self->read_value(
481                                              $lookup->{'VFMT2'},
482                                              $loc, $lookup, $fh ) ];
483
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.  
488
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.
492
493 =head2 Stack Opening Tokens
494
495 In the following snippet the opening hash brace has been placed
496 alone on a new line.  
497
498     $opt_c = Text::CSV_XS->new(
499         {
500             binary       => 1,
501             sep_char     => $opt_c,
502             always_quote => 1,
503         }
504     );
505
506 If you prefer to avoid isolated opening opening tokens by
507 "stacking" them together with other opening tokens like this:
508
509     $opt_c = Text::CSV_XS->new( {
510             binary       => 1,
511             sep_char     => $opt_c,
512             always_quote => 1,
513         }
514     );
515
516 use B<-sot>.
517
518 =head2 Stack Closing Tokens
519
520 Likewise, in the same snippet the default formatting leaves
521 the closing paren on a line by itself here:
522
523     $opt_c = Text::CSV_XS->new(
524         {
525             binary       => 1,
526             sep_char     => $opt_c,
527             always_quote => 1,
528         }
529     );
530
531 If you would like to avoid leaving isolated closing tokens by
532 stacking them with other closing tokens, like this:
533
534     $opt_c = Text::CSV_XS->new(
535         {
536             binary       => 1,
537             sep_char     => $opt_c,
538             always_quote => 1,
539         } );
540
541 use B<-sct>.
542
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.  
549
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.
552
553 =head2 Define Horizontal Tightness
554
555 Horizontal tightness parameters define how much space is included
556 within a set of container tokens.
557
558 For parentheses, decide which of the following values of B<-pt=n>
559 you prefer: 
560
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
564
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
567 one token.
568
569 For square brackets, decide which of the following values of B<-sbt=n>
570 you prefer:
571
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 
575
576 For curly braces, decide which of the following values of B<-bt=n>
577 you prefer:
578
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
582
583 For code block curly braces, decide which of the following values of
584 B<-bbt=n> you prefer: 
585
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
589
590 =head2 Spaces between function names and opening parens
591
592 The default is not to place a space after a function call:
593
594   myfunc( $a, $b, $c );    # default 
595
596 If you prefer a space:
597
598   myfunc ( $a, $b, $c );   # -sfp
599
600 use B<-sfp>.
601
602 =head2 Spaces between Perl keywords and parens
603
604 The default is to place a space between only these keywords
605 and an opening paren:
606
607    my local our and or eq ne if else elsif until unless 
608    while for foreach return switch case given when
609
610 but no others. For example, the default is:
611
612     $aa = pop(@bb);
613
614 If you want a space between all Perl keywords and an opening paren,
615
616     $aa = pop (@bb);
617
618 use B<-skp>.  For detailed control of individual keywords, see the manual.
619
620 =head2 Statement Termination Semicolon Spaces
621
622 The default is not to put a space before a statement termination
623 semicolon, like this:
624
625     $i = 1;
626
627 If you prefer a space, like this:
628
629     $i = 1 ; 
630
631 enter B<-sts>.
632
633 =head2 For Loop Semicolon Spaces
634
635 The default is to place a space before a semicolon in a for statement,
636 like this:
637
638  for ( @a = @$ap, $u = shift @a ; @a ; $u = $v ) {  # -sfs (default)
639
640 If you prefer no such space, like this:
641
642  for ( @a = @$ap, $u = shift @a; @a; $u = $v ) {    # -nsfs
643
644 enter B<-nsfs>.
645
646 =head2 Block Comment Indentation
647
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). 
652
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. 
657
658 If you never want block comments indented, use B<-nibc>.
659
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
662 B<-isbc>.
663
664 The manual shows many other options for controlling comments.
665
666 =head2 Outdenting Long Quotes
667
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:
671
672         if ($source_stream) {
673             if ( @ARGV > 0 ) {
674                 die
675  "You may not specify any filenames when a source array is given\n";
676             }
677         }
678
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:
681
682         if ($source_stream) {
683             if ( @ARGV > 0 ) {
684                 die
685                   "You may not specify any filenames when a source array is given\n";
686             }
687         }
688
689 use B<-nolq>.
690
691 =head2 Many Other Parameters
692
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
695 here.
696
697 =head2 Example F<.perltidyrc> files
698
699 Now gather together all of the parameters you prefer and enter them
700 in a file called F<.perltidyrc>.
701
702 Here are some example F<.perltidyrc> files and the corresponding style.
703
704 Here is a little test snippet, shown the way it would appear with
705 the default style.
706
707     for (@methods) {
708         push (
709             @results,
710             {
711                 name => $_->name,
712                 help => $_->help,
713             }
714         );
715     }
716
717 You do not need a F<.perltidyrc> file for this style.
718
719 Here is the same snippet
720
721     for (@methods)
722     {
723         push(@results,
724              {  name => $_->name,
725                 help => $_->help,
726              }
727             );
728     }
729
730 for a F<.perltidyrc> file containing these parameters:
731
732  -bl
733  -lp
734  -cti=1
735  -vt=1
736  -pt=2
737
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.
741
742 And here is the snippet
743
744     for (@methods) {
745         push ( @results,
746                { name => $_->name,
747                  help => $_->help,
748                } );
749     }
750
751 for a F<.perltidyrc> file containing these parameters:
752
753  -lp
754  -vt=1
755  -vtc=1
756
757 =head2 Additional Information
758
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
763
764 For an introduction to using perltidy, see the tutorial 
765 http://perltidy.sourceforge.net/tutorial.html
766
767 Suggestions for improving this document are welcome and may be sent to
768 perltidy at users.sourceforge.net
769
770 =cut