Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

277 lines
7.9 KiB

  1. =head1 NAME
  2. perlstyle - Perl style guide
  3. =head1 DESCRIPTION
  4. Each programmer will, of course, have his or her own preferences in
  5. regards to formatting, but there are some general guidelines that will
  6. make your programs easier to read, understand, and maintain.
  7. The most important thing is to run your programs under the B<-w>
  8. flag at all times. You may turn it off explicitly for particular
  9. portions of code via the C<use warnings> pragma or the C<$^W> variable
  10. if you must. You should
  11. also always run under C<use strict> or know the reason why not.
  12. The C<use sigtrap> and even C<use diagnostics> pragmas may also prove
  13. useful.
  14. Regarding aesthetics of code lay out, about the only thing Larry
  15. cares strongly about is that the closing curly bracket of
  16. a multi-line BLOCK should line up with the keyword that started the construct.
  17. Beyond that, he has other preferences that aren't so strong:
  18. =over 4
  19. =item *
  20. 4-column indent.
  21. =item *
  22. Opening curly on same line as keyword, if possible, otherwise line up.
  23. =item *
  24. Space before the opening curly of a multi-line BLOCK.
  25. =item *
  26. One-line BLOCK may be put on one line, including curlies.
  27. =item *
  28. No space before the semicolon.
  29. =item *
  30. Semicolon omitted in "short" one-line BLOCK.
  31. =item *
  32. Space around most operators.
  33. =item *
  34. Space around a "complex" subscript (inside brackets).
  35. =item *
  36. Blank lines between chunks that do different things.
  37. =item *
  38. Uncuddled elses.
  39. =item *
  40. No space between function name and its opening parenthesis.
  41. =item *
  42. Space after each comma.
  43. =item *
  44. Long lines broken after an operator (except "and" and "or").
  45. =item *
  46. Space after last parenthesis matching on current line.
  47. =item *
  48. Line up corresponding items vertically.
  49. =item *
  50. Omit redundant punctuation as long as clarity doesn't suffer.
  51. =back
  52. Larry has his reasons for each of these things, but he doesn't claim that
  53. everyone else's mind works the same as his does.
  54. Here are some other more substantive style issues to think about:
  55. =over 4
  56. =item *
  57. Just because you I<CAN> do something a particular way doesn't mean that
  58. you I<SHOULD> do it that way. Perl is designed to give you several
  59. ways to do anything, so consider picking the most readable one. For
  60. instance
  61. open(FOO,$foo) || die "Can't open $foo: $!";
  62. is better than
  63. die "Can't open $foo: $!" unless open(FOO,$foo);
  64. because the second way hides the main point of the statement in a
  65. modifier. On the other hand
  66. print "Starting analysis\n" if $verbose;
  67. is better than
  68. $verbose && print "Starting analysis\n";
  69. because the main point isn't whether the user typed B<-v> or not.
  70. Similarly, just because an operator lets you assume default arguments
  71. doesn't mean that you have to make use of the defaults. The defaults
  72. are there for lazy systems programmers writing one-shot programs. If
  73. you want your program to be readable, consider supplying the argument.
  74. Along the same lines, just because you I<CAN> omit parentheses in many
  75. places doesn't mean that you ought to:
  76. return print reverse sort num values %array;
  77. return print(reverse(sort num (values(%array))));
  78. When in doubt, parenthesize. At the very least it will let some poor
  79. schmuck bounce on the % key in B<vi>.
  80. Even if you aren't in doubt, consider the mental welfare of the person
  81. who has to maintain the code after you, and who will probably put
  82. parentheses in the wrong place.
  83. =item *
  84. Don't go through silly contortions to exit a loop at the top or the
  85. bottom, when Perl provides the C<last> operator so you can exit in
  86. the middle. Just "outdent" it a little to make it more visible:
  87. LINE:
  88. for (;;) {
  89. statements;
  90. last LINE if $foo;
  91. next LINE if /^#/;
  92. statements;
  93. }
  94. =item *
  95. Don't be afraid to use loop labels--they're there to enhance
  96. readability as well as to allow multilevel loop breaks. See the
  97. previous example.
  98. =item *
  99. Avoid using grep() (or map()) or `backticks` in a void context, that is,
  100. when you just throw away their return values. Those functions all
  101. have return values, so use them. Otherwise use a foreach() loop or
  102. the system() function instead.
  103. =item *
  104. For portability, when using features that may not be implemented on
  105. every machine, test the construct in an eval to see if it fails. If
  106. you know what version or patchlevel a particular feature was
  107. implemented, you can test C<$]> (C<$PERL_VERSION> in C<English>) to see if it
  108. will be there. The C<Config> module will also let you interrogate values
  109. determined by the B<Configure> program when Perl was installed.
  110. =item *
  111. Choose mnemonic identifiers. If you can't remember what mnemonic means,
  112. you've got a problem.
  113. =item *
  114. While short identifiers like $gotit are probably ok, use underscores to
  115. separate words. It is generally easier to read $var_names_like_this than
  116. $VarNamesLikeThis, especially for non-native speakers of English. It's
  117. also a simple rule that works consistently with VAR_NAMES_LIKE_THIS.
  118. Package names are sometimes an exception to this rule. Perl informally
  119. reserves lowercase module names for "pragma" modules like C<integer> and
  120. C<strict>. Other modules should begin with a capital letter and use mixed
  121. case, but probably without underscores due to limitations in primitive
  122. file systems' representations of module names as files that must fit into a
  123. few sparse bytes.
  124. =item *
  125. You may find it helpful to use letter case to indicate the scope
  126. or nature of a variable. For example:
  127. $ALL_CAPS_HERE constants only (beware clashes with perl vars!)
  128. $Some_Caps_Here package-wide global/static
  129. $no_caps_here function scope my() or local() variables
  130. Function and method names seem to work best as all lowercase.
  131. E.g., $obj-E<gt>as_string().
  132. You can use a leading underscore to indicate that a variable or
  133. function should not be used outside the package that defined it.
  134. =item *
  135. If you have a really hairy regular expression, use the C</x> modifier and
  136. put in some whitespace to make it look a little less like line noise.
  137. Don't use slash as a delimiter when your regexp has slashes or backslashes.
  138. =item *
  139. Use the new "and" and "or" operators to avoid having to parenthesize
  140. list operators so much, and to reduce the incidence of punctuation
  141. operators like C<&&> and C<||>. Call your subroutines as if they were
  142. functions or list operators to avoid excessive ampersands and parentheses.
  143. =item *
  144. Use here documents instead of repeated print() statements.
  145. =item *
  146. Line up corresponding things vertically, especially if it'd be too long
  147. to fit on one line anyway.
  148. $IDX = $ST_MTIME;
  149. $IDX = $ST_ATIME if $opt_u;
  150. $IDX = $ST_CTIME if $opt_c;
  151. $IDX = $ST_SIZE if $opt_s;
  152. mkdir $tmpdir, 0700 or die "can't mkdir $tmpdir: $!";
  153. chdir($tmpdir) or die "can't chdir $tmpdir: $!";
  154. mkdir 'tmp', 0777 or die "can't mkdir $tmpdir/tmp: $!";
  155. =item *
  156. Always check the return codes of system calls. Good error messages should
  157. go to STDERR, include which program caused the problem, what the failed
  158. system call and arguments were, and (VERY IMPORTANT) should contain the
  159. standard system error message for what went wrong. Here's a simple but
  160. sufficient example:
  161. opendir(D, $dir) or die "can't opendir $dir: $!";
  162. =item *
  163. Line up your transliterations when it makes sense:
  164. tr [abc]
  165. [xyz];
  166. =item *
  167. Think about reusability. Why waste brainpower on a one-shot when you
  168. might want to do something like it again? Consider generalizing your
  169. code. Consider writing a module or object class. Consider making your
  170. code run cleanly with C<use strict> and C<use warnings> (or B<-w>) in effect
  171. Consider giving away
  172. your code. Consider changing your whole world view. Consider... oh,
  173. never mind.
  174. =item *
  175. Be consistent.
  176. =item *
  177. Be nice.
  178. =back