home *** CD-ROM | disk | FTP | other *** search
/ H4CK3R 14 / hacker14.iso / programacao / visual / perl.exe / {app} / Library / Perl / stylekey.pod < prev    next >
Encoding:
Text File  |  2002-11-23  |  15.9 KB  |  539 lines

  1. =head1 Perltidy Style Key
  2.  
  3. Use this document to quickly and methodically find a set of perltidy
  4. parameters to approximate your style.  Just read each question and
  5. select the best answer.  Enter your parameters in a file named
  6. F<.perltidyrc> (examples are listed at the end).  Then move it to one of
  7. the places where perltidy will find it.  You can run perltidy with the
  8. parameter B<-dpro> to see where these places are for your system.
  9.  
  10. Before you begin, experiment using just C<perltidy filename.pl> on some
  11. of your files.  From the results (which you will find in files with a
  12. F<.tdy> extension), you will get a sense of what formatting changes, if
  13. any, you'd like to make.  If the default formatting is acceptable, you
  14. do not need a F<.perltidyrc> file.
  15.  
  16. =head2 Use as Filter?
  17.  
  18. Do you almost always want to run perltidy as a standard filter on just
  19. one input file?  If yes, use B<-st> and B<-se>.  
  20.  
  21. =head2 Line Length Setting
  22.  
  23. Perltidy will set line breaks to prevent lines from exceeding the
  24. maximum line length.  
  25.  
  26. Do you want the maximum line length to be 80 columns?  If no, use
  27. B<-l=n>, where B<n> is the number of columns you prefer.
  28.  
  29. =head2 Indentation in Code Blocks
  30.  
  31. In the block below, the variable C<$anchor> is one indentation level deep
  32. and is indented by 4 spaces as shown here: 
  33.  
  34.     if ( $flag eq "a" ) {
  35.         $anchor = $header;
  36.     }  
  37.  
  38. If you want to change this to be a different number B<n> of spaces
  39. per indentation level, use B<-i=n>.
  40.  
  41. =head2 Continuation Indentation
  42.  
  43. Look at the statement beginning with C<$anchor>:
  44.  
  45.     if ( $flag eq "a" ) {
  46.         $anchor =
  47.           substr( $header, 0, 6 )
  48.           . substr( $char_list, $place_1, 1 )
  49.           . substr( $char_list, $place_2, 1 );
  50.     }
  51.  
  52. The statement is too long for the line length (80 characters by
  53. default), so it has been broken into 4 lines.  The second and later
  54. lines have some extra "continuation indentation" to help make the start
  55. of the statement easy to find.  The default number of extra spaces is 2.
  56. If you prefer a number n different from 2, you may specify this with
  57. B<-ci=n>.  It is best to keep this less than the value of the primary
  58. indentation.
  59.  
  60. =head2 Tabs
  61.  
  62. The default, and recommendation, is to represent leading whitespace
  63. with actual space characters.  However, if you prefer to entab
  64. leading whitespace with one tab character for each B<n> spaces,
  65. use B<-et=n>.  Typically, B<n> would be 8.  
  66.  
  67. =head2 Opening Block Brace Right or Left?
  68.  
  69. Decide which of the following opening brace styles you prefer:
  70.  
  71. If you like opening braces on the right, like this, go to 
  72. L<Braces Right>.
  73.  
  74.     if ( $flag eq "h" ) {
  75.         $headers = 0;
  76.     }  
  77.  
  78. If you like opening braces on the left, like this, go to 
  79. L<Braces Left>.
  80.  
  81.     if ( $flag eq "h" )
  82.     {
  83.         $headers = 0;
  84.     }
  85.  
  86. =head2 Braces Right
  87.  
  88. In a multi-line B<if> test expression, the default is to place
  89. the opening brace on the left, like this:
  90.  
  91.     if ( $bigwasteofspace1 && $bigwasteofspace2
  92.         || $bigwasteofspace3 && $bigwasteofspace4 )
  93.     {
  94.         big_waste_of_time();
  95.     }
  96.  
  97. This helps to visually separate the block contents from the test
  98. expression.  
  99.  
  100. An alternative is to keep the brace on the right even for
  101. multiple-line test expressions, like this:
  102.  
  103.     if ( $bigwasteofspace1 && $bigwasteofspace2
  104.         || $bigwasteofspace3 && $bigwasteofspace4 ) {
  105.         big_waste_of_time();
  106.     }
  107.  
  108. If you prefer this alternative, use B<-bar>.
  109.  
  110. =head2 Cuddled Else?
  111.  
  112. Do you prefer this B<Cuddled Else> style
  113.  
  114.     if ( $flag eq "h" ) {
  115.         $headers = 0;
  116.     } elsif ( $flag eq "f" ) {
  117.         $sectiontype = 3;
  118.     } else {
  119.         print "invalid option: " . substr( $arg, $i, 1 ) . "\n";
  120.         dohelp();
  121.     }
  122.  
  123. instead of this default style?
  124.  
  125.     if ( $flag eq "h" ) {
  126.         $headers = 0;
  127.     }  
  128.     elsif ( $flag eq "f" ) {
  129.         $sectiontype = 3;
  130.     } 
  131.     else {    
  132.         print "invalid option: " . substr( $arg, $i, 1 ) . "\n";
  133.         dohelp();
  134.     }
  135.  
  136. If yes, you should use B<-ce>.
  137.  
  138. Now skip ahead to L<Indentation Style for Other Containers>.
  139.  
  140. =head2 Braces Left
  141.  
  142. Use B<-bl> if you prefer this style:
  143.  
  144.     if ( $flag eq "h" )
  145.     {
  146.         $headers = 0;
  147.     }
  148.  
  149. Use B<-bli> if you prefer this indented-brace style:
  150.  
  151.     if ( $flag eq "h" )
  152.       {
  153.         $headers = 0;
  154.       }
  155.  
  156. The number of spaces of extra indentation will be the value specified
  157. for continuation indentation with the B<-ci=n> parameter (2 by default).
  158.  
  159. =head2 Block Brace Vertical Tightness
  160.  
  161. The default is to leave the opening brace on a line by itself, like this (shown
  162. for B<-bli>, but also true to B<-bl>):
  163.  
  164.     if ( $flag eq "h" )
  165.       {
  166.         $headers = 0;
  167.       }
  168.  
  169. But you may also use this more compressed style if you wish:
  170.  
  171.     if ( $flag eq "h" )
  172.       { $headers = 0;
  173.       }
  174.  
  175. If you do not prefer this more compressed form, go to 
  176. L<Indentation Style for Other Containers>.
  177.  
  178. Otherwise use parameter B<-bbvt=n>, where n=1 or n=2.  To decide,
  179. look at this snippet:
  180.  
  181.     # -bli -bbvt=1
  182.     sub _directives
  183.       {
  184.         {
  185.             'ENDIF' => \&_endif,
  186.                'IF' => \&_if,
  187.         };
  188.       }
  189.  
  190.     # -bli -bbvt=2
  191.     sub _directives
  192.       { {
  193.             'ENDIF' => \&_endif,
  194.                'IF' => \&_if,
  195.         };
  196.       }
  197.  
  198. The difference is that B<-bbvt=1> breaks after an opening brace if
  199. the next line is unbalanced, whereas B<-bbvt=2> never breaks.
  200.  
  201. =head2 Indentation Style for Other Containers
  202.  
  203. You have a choice of two indentation schemes for non-block containers.
  204. The default is to use a fixed number of spaces per indentation level (the
  205. same number of spaces used for code blocks).  Here is an example of the
  206. default:
  207.  
  208.     $dbh = DBI->connect(
  209.         undef, undef, undef,
  210.         {
  211.             PrintError => 0,
  212.             RaiseError => 1
  213.         }
  214.     );
  215.  
  216. The alternate is to let the location of the opening paren (or square
  217. bracket, or curly brace) define the indentation, like this:
  218.  
  219.     $dbh = DBI->connect(
  220.                          undef, undef, undef,
  221.                          {
  222.                            PrintError => 0,
  223.                            RaiseError => 1
  224.                          }
  225.     );
  226.  
  227. If you prefer the first (default) scheme, skip ahead to 
  228. L<Closing Token Placement>.
  229.  
  230. If you prefer the latter scheme, use B<-lp> and continue to the next
  231. section.  
  232.  
  233. =head2 Opening Vertical Tightness
  234.  
  235. The default B<-lp> indentation style ends a line at the
  236. opening tokens, like this:
  237.  
  238.     $dbh = DBI->connect(
  239.                          undef, undef, undef,
  240.                          {
  241.                            PrintError => 0,
  242.                            RaiseError => 1
  243.                          }
  244.     );
  245.  
  246. Here is a tighter alternative, which does not end a line
  247. with the opening tokens:
  248.  
  249.     $dbh = DBI->connect( undef, undef, undef,
  250.                          { PrintError => 0,
  251.                            RaiseError => 1
  252.                          }
  253.     );
  254.  
  255. If you prefer the default, skip ahead to L<Closing Token Placement>.
  256.  
  257. Otherwise, use B<-vt=n>, where B<n> should be either 1 or 2.  To help
  258. decide, observe the first three opening parens in the following snippet
  259. and choose the value of n you prefer.  Here it is with B<-lp -vt=1>:
  260.  
  261.     if (
  262.          !defined(
  263.                    start_slip( $DEVICE, $PHONE,  $ACCOUNT, $PASSWORD,
  264.                                $LOCAL,  $REMOTE, $NETMASK, $MTU
  265.                    )
  266.          )
  267.          && $continuation_flag
  268.       )
  269.     {
  270.         do_something_about_it();
  271.     }
  272.  
  273. And here it is again formatted with B<-lp -vt=2>:
  274.  
  275.     if ( !defined( start_slip( $DEVICE, $PHONE,  $ACCOUNT, $PASSWORD,
  276.                                $LOCAL,  $REMOTE, $NETMASK, $MTU
  277.                    )
  278.          )
  279.          && $continuation_flag
  280.       )
  281.     {
  282.         do_something_about_it();
  283.     }
  284.  
  285. The B<-vt=1> style tries to display the structure by preventing more
  286. than one step in indentation per line. In this example, the first two
  287. opening parens were not followed by balanced lines, so B<-vt=1> broke
  288. after them.  
  289.  
  290. The B<-vt=2> style does not limit itself to a single indentation step
  291. per line.
  292.  
  293. =head2 Closing Token Placement
  294.  
  295. You have several options for dealing with the terminal closing tokens of
  296. non-blocks.  In the following examples, a closing parenthesis is shown,
  297. but these parameters apply to closing square brackets and curly braces
  298. as well.  Also, the following examples show the default indentation,
  299. but they apply for B<-lp> indentation as well.
  300.  
  301. The default is to put a closing paren on a separate line, with
  302. the indentation defined by the next (lower) indentation level, like this:
  303.  
  304.     @month_of_year = (
  305.         'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
  306.         'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'
  307.     );
  308.  
  309. Skip ahead to L<Define Horizontal Tightness> if you like this.
  310.  
  311. A slight variation is to place it on a separate line but leave it
  312. indented with the previous indentation level, like this:
  313.  
  314.     @month_of_year = (
  315.         'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
  316.         'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'
  317.         );
  318.  
  319. Use B<-icp> if you prefer this and skip ahead to L<Define Horizontal Tightness>.
  320.  
  321. Finally, the question of paren indentation can be avoided by placing it
  322. at the end of the previous line, like this:
  323.  
  324.     @month_of_year = (
  325.         'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
  326.         'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' );
  327.  
  328. Use B<-vtc=n> if you prefer to do this, where n is either 1 or 2. To
  329. determine n, we have to look at something more complex.  Observe the
  330. behavior of the closing tokens in the following snippet:
  331.  
  332. Here is B<-lp -vtc=1>:
  333.  
  334.     $srec->{'ACTION'} = [
  335.                           $self->read_value(
  336.                                              $lookup->{'VFMT'},
  337.                                              $loc, $lookup, $fh
  338.                           ),
  339.                           $self->read_value(
  340.                                              $lookup->{'VFMT2'},
  341.                                              $loc, $lookup, $fh
  342.                           ) ];
  343.  
  344. Here is B<-lp -vtc=2>:
  345.  
  346.     $srec->{'ACTION'} = [
  347.                           $self->read_value(
  348.                                              $lookup->{'VFMT'},
  349.                                              $loc, $lookup, $fh ),
  350.                           $self->read_value(
  351.                                              $lookup->{'VFMT2'},
  352.                                              $loc, $lookup, $fh ) ];
  353.  
  354.  
  355. Choose the one that you prefer.  The difference is that B<-vtc=1> leaves
  356. closing tokens at the start of a line within a list, which can assist in
  357. keeping hierarchical lists readable.  The B<-vtc=2> style always tries
  358. to move closing tokens to the end of a line.
  359.  
  360. =head2 Define Horizontal Tightness
  361.  
  362. Horizontal tightness parameters define how much space is included
  363. within a set of container tokens.
  364.  
  365. For parentheses, decide which of the following values of B<-pt=n>
  366. you prefer: 
  367.  
  368.  if ( ( my $len_tab = length( $tabstr ) ) > 0 ) {  # -pt=0
  369.  if ( ( my $len_tab = length($tabstr) ) > 0 ) {    # -pt=1 (default)
  370.  if ((my $len_tab = length($tabstr)) > 0) {        # -pt=2
  371.  
  372. For n=0, space is always used, and for n=2, space is never used.  For
  373. the default n=1, space is used if the parentheses contain more than
  374. one token.
  375.  
  376. For square brackets, decide which of the following values of B<-sbt=n>
  377. you prefer:
  378.  
  379.  $width = $col[ $j + $k ] - $col[ $j ];  # -sbt=0
  380.  $width = $col[ $j + $k ] - $col[$j];    # -sbt=1 (default)
  381.  $width = $col[$j + $k] - $col[$j];      # -sbt=2 
  382.  
  383. For curly braces, decide which of the following values of B<-bt=n>
  384. you prefer:
  385.  
  386.  $obj->{ $parsed_sql->{ 'table' }[0] };    # -bt=0
  387.  $obj->{ $parsed_sql->{'table'}[0] };      # -bt=1 (default)
  388.  $obj->{$parsed_sql->{'table'}[0]};        # -bt=2
  389.  
  390. For code block curly braces, decide which of the following values of
  391. B<-bbt=n> you prefer: 
  392.  
  393.  %bf = map { $_ => -M $_ } grep { /\.deb$/ } dirents '.'; # -bbt=0 (default)
  394.  %bf = map { $_ => -M $_ } grep {/\.deb$/} dirents '.';   # -bbt=1
  395.  %bf = map {$_ => -M $_} grep {/\.deb$/} dirents '.';     # -bbt=2
  396.  
  397. =head2 Statement Termination Semicolon Spaces
  398.  
  399. The default is not to put a space before a statement termination
  400. semicolon, like this:
  401.  
  402.     $i = 1;
  403.  
  404. If you prefer a space, like this:
  405.  
  406.     $i = 1 ; 
  407.  
  408. enter B<-sts>.
  409.  
  410. =head2 For Loop Semicolon Spaces
  411.  
  412. The default is to place a space before a semicolon in a for statement,
  413. like this:
  414.  
  415.  for ( @a = @$ap, $u = shift @a ; @a ; $u = $v ) {  # -sfs (default)
  416.  
  417. If you prefer no such space, like this:
  418.  
  419.  for ( @a = @$ap, $u = shift @a; @a; $u = $v ) {    # -nsfs
  420.  
  421. enter B<-nsfs>.
  422.  
  423. =head2 Block Comment Indentation
  424.  
  425. Block comments are comments which occupy a full line, as opposed to side
  426. comments.  The default is to indent block comments with the same
  427. indentation as the code block that contains them (even though this
  428. will allow long comments to exceed the maximum line length). 
  429.  
  430. If you would like block comments indented except when this would cause
  431. the maximum line length to be exceeded, use B<-olc>.  This will cause a
  432. group of consecutive block comments to be outdented by the amount needed
  433. to prevent any one from exceeding the maximum line length. 
  434.  
  435. If you never want block comments indented, use B<-nibc>.
  436.  
  437. If block comments may only be indented if they have some space
  438. characters before the leading C<#> character in the input file, use
  439. B<-isbc>.
  440.  
  441. =head2 Outdenting Long Quotes
  442.  
  443. Long quoted strings may exceed the specified line length limit.  The
  444. default, when this happens, is to outdent them to the first column.
  445. Here is an example of an outdented long quote:
  446.  
  447.         if ($source_stream) {
  448.             if ( @ARGV > 0 ) {
  449.                 die
  450.  "You may not specify any filenames when a source array is given\n";
  451.             }
  452.         }
  453.  
  454. The effect is not too different from using a here document to represent
  455. the quote.  If you prefer to leave the quote indented, like this:
  456.  
  457.         if ($source_stream) {
  458.             if ( @ARGV > 0 ) {
  459.                 die
  460.                   "You may not specify any filenames when a source array is given\n";
  461.             }
  462.         }
  463.  
  464. use B<-nolq>.
  465.  
  466. =head2 Example F<.perltidyrc> files
  467.  
  468. Now gather together all of the parameters you prefer and enter them
  469. in a file called F<.perltidyrc>.
  470.  
  471. Here are some example F<.perltidyrc> files and the corresponding style.
  472.  
  473. Here is a little test snippet, shown the way it would appear with
  474. the default style.
  475.  
  476.     for (@methods) {
  477.         push (
  478.             @results,
  479.             {
  480.                 name => $_->name,
  481.                 help => $_->help,
  482.             }
  483.         );
  484.     }
  485.  
  486. You do not need a F<.perltidyrc> file for this style.
  487.  
  488. Here is the same snippet
  489.  
  490.     for (@methods)
  491.     {
  492.         push (@results,
  493.               { name => $_->name,
  494.                 help => $_->help,
  495.               }
  496.         );
  497.     }
  498.  
  499. for a F<.perltidyrc> file containing these parameters:
  500.  
  501.  -bl
  502.  -lp
  503.  -vt=1
  504.  -pt=2
  505.  
  506. You do not need to place just one parameter per line, but this may be
  507. convenient for long lists.  You may then hide any parameter by placing
  508. a C<#> symbol before it.
  509.  
  510. And here is the snippet
  511.  
  512.     for (@methods) {
  513.         push ( @results,
  514.                { name => $_->name,
  515.                  help => $_->help,
  516.                } );
  517.     }
  518.  
  519. for a F<.perltidyrc> file containing these parameters:
  520.  
  521.  -lp
  522.  -vt=1
  523.  -vtc=1
  524.  
  525. =head2 Additional Information
  526.  
  527. This document has covered the main parameters.  Many more parameters are
  528. available for special purposes and for fine-tuning a style.  For 
  529. complete information see the perltidy manual
  530. http://perltidy.sourceforge.net/perltidy.html
  531.  
  532. For an introduction to using perltidy, see the tutorial 
  533. http://perltidy.sourceforge.net/tutorial.html
  534.  
  535. Suggestions for improving this document are welcome and may be sent to
  536. perltidy at users.sourceforge.net
  537.  
  538. =cut
  539.