Commit | Line | Data |
---|---|---|
920dae64 AT |
1 | .\" Automatically generated by Pod::Man v1.37, Pod::Parser v1.32 |
2 | .\" | |
3 | .\" Standard preamble: | |
4 | .\" ======================================================================== | |
5 | .de Sh \" Subsection heading | |
6 | .br | |
7 | .if t .Sp | |
8 | .ne 5 | |
9 | .PP | |
10 | \fB\\$1\fR | |
11 | .PP | |
12 | .. | |
13 | .de Sp \" Vertical space (when we can't use .PP) | |
14 | .if t .sp .5v | |
15 | .if n .sp | |
16 | .. | |
17 | .de Vb \" Begin verbatim text | |
18 | .ft CW | |
19 | .nf | |
20 | .ne \\$1 | |
21 | .. | |
22 | .de Ve \" End verbatim text | |
23 | .ft R | |
24 | .fi | |
25 | .. | |
26 | .\" Set up some character translations and predefined strings. \*(-- will | |
27 | .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left | |
28 | .\" double quote, and \*(R" will give a right double quote. | will give a | |
29 | .\" real vertical bar. \*(C+ will give a nicer C++. Capital omega is used to | |
30 | .\" do unbreakable dashes and therefore won't be available. \*(C` and \*(C' | |
31 | .\" expand to `' in nroff, nothing in troff, for use with C<>. | |
32 | .tr \(*W-|\(bv\*(Tr | |
33 | .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' | |
34 | .ie n \{\ | |
35 | . ds -- \(*W- | |
36 | . ds PI pi | |
37 | . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch | |
38 | . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch | |
39 | . ds L" "" | |
40 | . ds R" "" | |
41 | . ds C` "" | |
42 | . ds C' "" | |
43 | 'br\} | |
44 | .el\{\ | |
45 | . ds -- \|\(em\| | |
46 | . ds PI \(*p | |
47 | . ds L" `` | |
48 | . ds R" '' | |
49 | 'br\} | |
50 | .\" | |
51 | .\" If the F register is turned on, we'll generate index entries on stderr for | |
52 | .\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index | |
53 | .\" entries marked with X<> in POD. Of course, you'll have to process the | |
54 | .\" output yourself in some meaningful fashion. | |
55 | .if \nF \{\ | |
56 | . de IX | |
57 | . tm Index:\\$1\t\\n%\t"\\$2" | |
58 | .. | |
59 | . nr % 0 | |
60 | . rr F | |
61 | .\} | |
62 | .\" | |
63 | .\" For nroff, turn off justification. Always turn off hyphenation; it makes | |
64 | .\" way too many mistakes in technical documents. | |
65 | .hy 0 | |
66 | .if n .na | |
67 | .\" | |
68 | .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). | |
69 | .\" Fear. Run. Save yourself. No user-serviceable parts. | |
70 | . \" fudge factors for nroff and troff | |
71 | .if n \{\ | |
72 | . ds #H 0 | |
73 | . ds #V .8m | |
74 | . ds #F .3m | |
75 | . ds #[ \f1 | |
76 | . ds #] \fP | |
77 | .\} | |
78 | .if t \{\ | |
79 | . ds #H ((1u-(\\\\n(.fu%2u))*.13m) | |
80 | . ds #V .6m | |
81 | . ds #F 0 | |
82 | . ds #[ \& | |
83 | . ds #] \& | |
84 | .\} | |
85 | . \" simple accents for nroff and troff | |
86 | .if n \{\ | |
87 | . ds ' \& | |
88 | . ds ` \& | |
89 | . ds ^ \& | |
90 | . ds , \& | |
91 | . ds ~ ~ | |
92 | . ds / | |
93 | .\} | |
94 | .if t \{\ | |
95 | . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" | |
96 | . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' | |
97 | . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' | |
98 | . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' | |
99 | . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' | |
100 | . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' | |
101 | .\} | |
102 | . \" troff and (daisy-wheel) nroff accents | |
103 | .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' | |
104 | .ds 8 \h'\*(#H'\(*b\h'-\*(#H' | |
105 | .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] | |
106 | .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' | |
107 | .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' | |
108 | .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] | |
109 | .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] | |
110 | .ds ae a\h'-(\w'a'u*4/10)'e | |
111 | .ds Ae A\h'-(\w'A'u*4/10)'E | |
112 | . \" corrections for vroff | |
113 | .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' | |
114 | .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' | |
115 | . \" for low resolution devices (crt and lpr) | |
116 | .if \n(.H>23 .if \n(.V>19 \ | |
117 | \{\ | |
118 | . ds : e | |
119 | . ds 8 ss | |
120 | . ds o a | |
121 | . ds d- d\h'-1'\(ga | |
122 | . ds D- D\h'-1'\(hy | |
123 | . ds th \o'bp' | |
124 | . ds Th \o'LP' | |
125 | . ds ae ae | |
126 | . ds Ae AE | |
127 | .\} | |
128 | .rm #[ #] #H #V #F C | |
129 | .\" ======================================================================== | |
130 | .\" | |
131 | .IX Title "PERLSTYLE 1" | |
132 | .TH PERLSTYLE 1 "2006-01-07" "perl v5.8.8" "Perl Programmers Reference Guide" | |
133 | .SH "NAME" | |
134 | perlstyle \- Perl style guide | |
135 | .SH "DESCRIPTION" | |
136 | .IX Header "DESCRIPTION" | |
137 | Each programmer will, of course, have his or her own preferences in | |
138 | regards to formatting, but there are some general guidelines that will | |
139 | make your programs easier to read, understand, and maintain. | |
140 | .PP | |
141 | The most important thing is to run your programs under the \fB\-w\fR | |
142 | flag at all times. You may turn it off explicitly for particular | |
143 | portions of code via the \f(CW\*(C`no warnings\*(C'\fR pragma or the \f(CW$^W\fR variable | |
144 | if you must. You should also always run under \f(CW\*(C`use strict\*(C'\fR or know the | |
145 | reason why not. The \f(CW\*(C`use sigtrap\*(C'\fR and even \f(CW\*(C`use diagnostics\*(C'\fR pragmas | |
146 | may also prove useful. | |
147 | .PP | |
148 | Regarding aesthetics of code lay out, about the only thing Larry | |
149 | cares strongly about is that the closing curly bracket of | |
150 | a multi-line \s-1BLOCK\s0 should line up with the keyword that started the construct. | |
151 | Beyond that, he has other preferences that aren't so strong: | |
152 | .IP "\(bu" 4 | |
153 | 4\-column indent. | |
154 | .IP "\(bu" 4 | |
155 | Opening curly on same line as keyword, if possible, otherwise line up. | |
156 | .IP "\(bu" 4 | |
157 | Space before the opening curly of a multi-line \s-1BLOCK\s0. | |
158 | .IP "\(bu" 4 | |
159 | One-line \s-1BLOCK\s0 may be put on one line, including curlies. | |
160 | .IP "\(bu" 4 | |
161 | No space before the semicolon. | |
162 | .IP "\(bu" 4 | |
163 | Semicolon omitted in \*(L"short\*(R" one-line \s-1BLOCK\s0. | |
164 | .IP "\(bu" 4 | |
165 | Space around most operators. | |
166 | .IP "\(bu" 4 | |
167 | Space around a \*(L"complex\*(R" subscript (inside brackets). | |
168 | .IP "\(bu" 4 | |
169 | Blank lines between chunks that do different things. | |
170 | .IP "\(bu" 4 | |
171 | Uncuddled elses. | |
172 | .IP "\(bu" 4 | |
173 | No space between function name and its opening parenthesis. | |
174 | .IP "\(bu" 4 | |
175 | Space after each comma. | |
176 | .IP "\(bu" 4 | |
177 | Long lines broken after an operator (except \f(CW\*(C`and\*(C'\fR and \f(CW\*(C`or\*(C'\fR). | |
178 | .IP "\(bu" 4 | |
179 | Space after last parenthesis matching on current line. | |
180 | .IP "\(bu" 4 | |
181 | Line up corresponding items vertically. | |
182 | .IP "\(bu" 4 | |
183 | Omit redundant punctuation as long as clarity doesn't suffer. | |
184 | .PP | |
185 | Larry has his reasons for each of these things, but he doesn't claim that | |
186 | everyone else's mind works the same as his does. | |
187 | .PP | |
188 | Here are some other more substantive style issues to think about: | |
189 | .IP "\(bu" 4 | |
190 | Just because you \fI\s-1CAN\s0\fR do something a particular way doesn't mean that | |
191 | you \fI\s-1SHOULD\s0\fR do it that way. Perl is designed to give you several | |
192 | ways to do anything, so consider picking the most readable one. For | |
193 | instance | |
194 | .Sp | |
195 | .Vb 1 | |
196 | \& open(FOO,$foo) || die "Can't open $foo: $!"; | |
197 | .Ve | |
198 | .Sp | |
199 | is better than | |
200 | .Sp | |
201 | .Vb 1 | |
202 | \& die "Can't open $foo: $!" unless open(FOO,$foo); | |
203 | .Ve | |
204 | .Sp | |
205 | because the second way hides the main point of the statement in a | |
206 | modifier. On the other hand | |
207 | .Sp | |
208 | .Vb 1 | |
209 | \& print "Starting analysis\en" if $verbose; | |
210 | .Ve | |
211 | .Sp | |
212 | is better than | |
213 | .Sp | |
214 | .Vb 1 | |
215 | \& $verbose && print "Starting analysis\en"; | |
216 | .Ve | |
217 | .Sp | |
218 | because the main point isn't whether the user typed \fB\-v\fR or not. | |
219 | .Sp | |
220 | Similarly, just because an operator lets you assume default arguments | |
221 | doesn't mean that you have to make use of the defaults. The defaults | |
222 | are there for lazy systems programmers writing one-shot programs. If | |
223 | you want your program to be readable, consider supplying the argument. | |
224 | .Sp | |
225 | Along the same lines, just because you \fI\s-1CAN\s0\fR omit parentheses in many | |
226 | places doesn't mean that you ought to: | |
227 | .Sp | |
228 | .Vb 2 | |
229 | \& return print reverse sort num values %array; | |
230 | \& return print(reverse(sort num (values(%array)))); | |
231 | .Ve | |
232 | .Sp | |
233 | When in doubt, parenthesize. At the very least it will let some poor | |
234 | schmuck bounce on the % key in \fBvi\fR. | |
235 | .Sp | |
236 | Even if you aren't in doubt, consider the mental welfare of the person | |
237 | who has to maintain the code after you, and who will probably put | |
238 | parentheses in the wrong place. | |
239 | .IP "\(bu" 4 | |
240 | Don't go through silly contortions to exit a loop at the top or the | |
241 | bottom, when Perl provides the \f(CW\*(C`last\*(C'\fR operator so you can exit in | |
242 | the middle. Just \*(L"outdent\*(R" it a little to make it more visible: | |
243 | .Sp | |
244 | .Vb 7 | |
245 | \& LINE: | |
246 | \& for (;;) { | |
247 | \& statements; | |
248 | \& last LINE if $foo; | |
249 | \& next LINE if /^#/; | |
250 | \& statements; | |
251 | \& } | |
252 | .Ve | |
253 | .IP "\(bu" 4 | |
254 | Don't be afraid to use loop labels\*(--they're there to enhance | |
255 | readability as well as to allow multilevel loop breaks. See the | |
256 | previous example. | |
257 | .IP "\(bu" 4 | |
258 | Avoid using \f(CW\*(C`grep()\*(C'\fR (or \f(CW\*(C`map()\*(C'\fR) or `backticks` in a void context, that is, | |
259 | when you just throw away their return values. Those functions all | |
260 | have return values, so use them. Otherwise use a \f(CW\*(C`foreach()\*(C'\fR loop or | |
261 | the \f(CW\*(C`system()\*(C'\fR function instead. | |
262 | .IP "\(bu" 4 | |
263 | For portability, when using features that may not be implemented on | |
264 | every machine, test the construct in an eval to see if it fails. If | |
265 | you know what version or patchlevel a particular feature was | |
266 | implemented, you can test \f(CW$]\fR (\f(CW$PERL_VERSION\fR in \f(CW\*(C`English\*(C'\fR) to see if it | |
267 | will be there. The \f(CW\*(C`Config\*(C'\fR module will also let you interrogate values | |
268 | determined by the \fBConfigure\fR program when Perl was installed. | |
269 | .IP "\(bu" 4 | |
270 | Choose mnemonic identifiers. If you can't remember what mnemonic means, | |
271 | you've got a problem. | |
272 | .IP "\(bu" 4 | |
273 | While short identifiers like \f(CW$gotit\fR are probably ok, use underscores to | |
274 | separate words in longer identifiers. It is generally easier to read | |
275 | \&\f(CW$var_names_like_this\fR than \f(CW$VarNamesLikeThis\fR, especially for | |
276 | non-native speakers of English. It's also a simple rule that works | |
277 | consistently with \f(CW\*(C`VAR_NAMES_LIKE_THIS\*(C'\fR. | |
278 | .Sp | |
279 | Package names are sometimes an exception to this rule. Perl informally | |
280 | reserves lowercase module names for \*(L"pragma\*(R" modules like \f(CW\*(C`integer\*(C'\fR and | |
281 | \&\f(CW\*(C`strict\*(C'\fR. Other modules should begin with a capital letter and use mixed | |
282 | case, but probably without underscores due to limitations in primitive | |
283 | file systems' representations of module names as files that must fit into a | |
284 | few sparse bytes. | |
285 | .IP "\(bu" 4 | |
286 | You may find it helpful to use letter case to indicate the scope | |
287 | or nature of a variable. For example: | |
288 | .Sp | |
289 | .Vb 3 | |
290 | \& $ALL_CAPS_HERE constants only (beware clashes with perl vars!) | |
291 | \& $Some_Caps_Here package-wide global/static | |
292 | \& $no_caps_here function scope my() or local() variables | |
293 | .Ve | |
294 | .Sp | |
295 | Function and method names seem to work best as all lowercase. | |
296 | E.g., \f(CW\*(C`$obj\->as_string()\*(C'\fR. | |
297 | .Sp | |
298 | You can use a leading underscore to indicate that a variable or | |
299 | function should not be used outside the package that defined it. | |
300 | .IP "\(bu" 4 | |
301 | If you have a really hairy regular expression, use the \f(CW\*(C`/x\*(C'\fR modifier and | |
302 | put in some whitespace to make it look a little less like line noise. | |
303 | Don't use slash as a delimiter when your regexp has slashes or backslashes. | |
304 | .IP "\(bu" 4 | |
305 | Use the new \f(CW\*(C`and\*(C'\fR and \f(CW\*(C`or\*(C'\fR operators to avoid having to parenthesize | |
306 | list operators so much, and to reduce the incidence of punctuation | |
307 | operators like \f(CW\*(C`&&\*(C'\fR and \f(CW\*(C`||\*(C'\fR. Call your subroutines as if they were | |
308 | functions or list operators to avoid excessive ampersands and parentheses. | |
309 | .IP "\(bu" 4 | |
310 | Use here documents instead of repeated \f(CW\*(C`print()\*(C'\fR statements. | |
311 | .IP "\(bu" 4 | |
312 | Line up corresponding things vertically, especially if it'd be too long | |
313 | to fit on one line anyway. | |
314 | .Sp | |
315 | .Vb 4 | |
316 | \& $IDX = $ST_MTIME; | |
317 | \& $IDX = $ST_ATIME if $opt_u; | |
318 | \& $IDX = $ST_CTIME if $opt_c; | |
319 | \& $IDX = $ST_SIZE if $opt_s; | |
320 | .Ve | |
321 | .Sp | |
322 | .Vb 3 | |
323 | \& mkdir $tmpdir, 0700 or die "can't mkdir $tmpdir: $!"; | |
324 | \& chdir($tmpdir) or die "can't chdir $tmpdir: $!"; | |
325 | \& mkdir 'tmp', 0777 or die "can't mkdir $tmpdir/tmp: $!"; | |
326 | .Ve | |
327 | .IP "\(bu" 4 | |
328 | Always check the return codes of system calls. Good error messages should | |
329 | go to \f(CW\*(C`STDERR\*(C'\fR, include which program caused the problem, what the failed | |
330 | system call and arguments were, and (\s-1VERY\s0 \s-1IMPORTANT\s0) should contain the | |
331 | standard system error message for what went wrong. Here's a simple but | |
332 | sufficient example: | |
333 | .Sp | |
334 | .Vb 1 | |
335 | \& opendir(D, $dir) or die "can't opendir $dir: $!"; | |
336 | .Ve | |
337 | .IP "\(bu" 4 | |
338 | Line up your transliterations when it makes sense: | |
339 | .Sp | |
340 | .Vb 2 | |
341 | \& tr [abc] | |
342 | \& [xyz]; | |
343 | .Ve | |
344 | .IP "\(bu" 4 | |
345 | Think about reusability. Why waste brainpower on a one-shot when you | |
346 | might want to do something like it again? Consider generalizing your | |
347 | code. Consider writing a module or object class. Consider making your | |
348 | code run cleanly with \f(CW\*(C`use strict\*(C'\fR and \f(CW\*(C`use warnings\*(C'\fR (or \fB\-w\fR) in | |
349 | effect. Consider giving away your code. Consider changing your whole | |
350 | world view. Consider... oh, never mind. | |
351 | .IP "\(bu" 4 | |
352 | Try to document your code and use Pod formatting in a consistent way. Here | |
353 | are commonly expected conventions: | |
354 | .RS 4 | |
355 | .IP "\(bu" 4 | |
356 | use \f(CW\*(C`C<>\*(C'\fR for function, variable and module names (and more | |
357 | generally anything that can be considered part of code, like filehandles | |
358 | or specific values). Note that function names are considered more readable | |
359 | with parentheses after their name, that is \f(CW\*(C`function()\*(C'\fR. | |
360 | .IP "\(bu" 4 | |
361 | use \f(CW\*(C`B<>\*(C'\fR for commands names like \fBcat\fR or \fBgrep\fR. | |
362 | .IP "\(bu" 4 | |
363 | use \f(CW\*(C`F<>\*(C'\fR or \f(CW\*(C`C<>\*(C'\fR for file names. \f(CW\*(C`F<>\*(C'\fR should | |
364 | be the only Pod code for file names, but as most Pod formatters render it | |
365 | as italic, Unix and Windows paths with their slashes and backslashes may | |
366 | be less readable, and better rendered with \f(CW\*(C`C<>\*(C'\fR. | |
367 | .RE | |
368 | .RS 4 | |
369 | .RE | |
370 | .IP "\(bu" 4 | |
371 | Be consistent. | |
372 | .IP "\(bu" 4 | |
373 | Be nice. |