Debian Codegraph @

upstream/debian-unstable/man1/perlstyle.1

raw
1 .\" -*- mode: troff; coding: utf-8 -*-
2 .\" Automatically generated by Pod::Man 5.0102 (Pod::Simple 3.45)
3 .\"
4 .\" Standard preamble:
5 .\" ========================================================================
6 .de Sp \" Vertical space (when we can't use .PP)
7 .if t .sp .5v
8 .if n .sp
9 ..
10 .de Vb \" Begin verbatim text
11 .ft CW
12 .nf
13 .ne \\$1
14 ..
15 .de Ve \" End verbatim text
16 .ft R
17 .fi
18 ..
19 .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>.
20 .ie n \{\
21 . ds C` ""
22 . ds C' ""
23 'br\}
24 .el\{\
25 . ds C`
26 . ds C'
27 'br\}
28 .\"
29 .\" Escape single quotes in literal strings from groff's Unicode transform.
30 .ie \n(.g .ds Aq \(aq
31 .el .ds Aq '
32 .\"
33 .\" If the F register is >0, we'll generate index entries on stderr for
34 .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
35 .\" entries marked with X<> in POD. Of course, you'll have to process the
36 .\" output yourself in some meaningful fashion.
37 .\"
38 .\" Avoid warning from groff about undefined register 'F'.
39 .de IX
40 ..
41 .nr rF 0
42 .if \n(.g .if rF .nr rF 1
43 .if (\n(rF:(\n(.g==0)) \{\
44 . if \nF \{\
45 . de IX
46 . tm Index:\\$1\t\\n%\t"\\$2"
47 ..
48 . if !\nF==2 \{\
49 . nr % 0
50 . nr F 2
51 . \}
52 . \}
53 .\}
54 .rr rF
55 .\" ========================================================================
56 .\"
57 .IX Title "PERLSTYLE 1"
58 .TH PERLSTYLE 1 2025-11-16 "perl v5.40.1" "Perl Programmers Reference Guide"
59 .\" For nroff, turn off justification. Always turn off hyphenation; it makes
60 .\" way too many mistakes in technical documents.
61 .if n .ad l
62 .nh
63 .SH NAME
64 perlstyle \- Perl style guide
65 .SH DESCRIPTION
66 .IX Header "DESCRIPTION"
67 Each programmer will, of course, have his or her own preferences in
68 regards to formatting, but there are some general guidelines that will
69 make your programs easier to read, understand, and maintain.
70 .PP
71 The most important thing is to use strict and warnings in all your
72 code or know the reason why not to. You may turn them off explicitly for
73 particular portions of code via \f(CW\*(C`no warnings\*(C'\fR or \f(CW\*(C`no strict\*(C'\fR, and this
74 can be limited to the specific warnings or strict features you wish to
75 disable. The \fB\-w\fR flag and \f(CW$^W\fR variable should not be used for this
76 purpose since they can affect code you use but did not write, such as
77 modules from core or CPAN.
78 .PP
79 A concise way to arrange for this is to use the
80 \&\f(CW\*(C`use VERSION\*(C'\fR syntax, requesting a version 5.36
81 or above, which will enable both the \f(CW\*(C`strict\*(C'\fR and \f(CW\*(C`warnings\*(C'\fR pragmata (as
82 well as several other useful named features).
83 .PP
84 .Vb 1
85 \& use v5.36;
86 .Ve
87 .PP
88 Regarding aesthetics of code layout, about the only thing Larry
89 cares strongly about is that the closing curly bracket of
90 a multi-line BLOCK should line up with the keyword that started the construct.
91 Beyond that, he has other preferences that aren't so strong:
92 .IP \(bu 4
93 4\-column indent.
94 .IP \(bu 4
95 Opening curly on same line as keyword, if possible, otherwise line up.
96 .IP \(bu 4
97 Space before the opening curly of a multi-line BLOCK.
98 .IP \(bu 4
99 One-line BLOCK may be put on one line, including curlies.
100 .IP \(bu 4
101 No space before the semicolon.
102 .IP \(bu 4
103 Semicolon omitted in "short" one-line BLOCK.
104 .IP \(bu 4
105 Space around most operators.
106 .IP \(bu 4
107 Space around a "complex" subscript (inside brackets).
108 .IP \(bu 4
109 Blank lines between chunks that do different things.
110 .IP \(bu 4
111 Uncuddled elses.
112 .IP \(bu 4
113 No space between function name and its opening parenthesis.
114 .IP \(bu 4
115 Space after each comma.
116 .IP \(bu 4
117 Long lines broken after an operator (except \f(CW\*(C`and\*(C'\fR and \f(CW\*(C`or\*(C'\fR).
118 .IP \(bu 4
119 Space after last parenthesis matching on current line.
120 .IP \(bu 4
121 Line up corresponding items vertically.
122 .IP \(bu 4
123 Omit redundant punctuation as long as clarity doesn't suffer.
124 .PP
125 Larry has his reasons for each of these things, but he doesn't claim that
126 everyone else's mind works the same as his does.
127 .PP
128 Here are some other more substantive style issues to think about:
129 .IP \(bu 4
130 Just because you \fICAN\fR do something a particular way doesn't mean that
131 you \fISHOULD\fR do it that way. Perl is designed to give you several
132 ways to do anything, so consider picking the most readable one. For
133 instance
134 .Sp
135 .Vb 1
136 \& open(my $fh, \*(Aq<\*(Aq, $foo) || die "Can\*(Aqt open $foo: $!";
137 .Ve
138 .Sp
139 is better than
140 .Sp
141 .Vb 1
142 \& die "Can\*(Aqt open $foo: $!" unless open(my $fh, \*(Aq<\*(Aq, $foo);
143 .Ve
144 .Sp
145 because the second way hides the main point of the statement in a
146 modifier. On the other hand
147 .Sp
148 .Vb 1
149 \& print "Starting analysis\en" if $verbose;
150 .Ve
151 .Sp
152 is better than
153 .Sp
154 .Vb 1
155 \& $verbose && print "Starting analysis\en";
156 .Ve
157 .Sp
158 because the main point isn't whether the user typed \fB\-v\fR or not.
159 .Sp
160 Similarly, just because an operator lets you assume default arguments
161 doesn't mean that you have to make use of the defaults. The defaults
162 are there for lazy systems programmers writing one-shot programs. If
163 you want your program to be readable, consider supplying the argument.
164 .Sp
165 Along the same lines, just because you \fICAN\fR omit parentheses in many
166 places doesn't mean that you ought to:
167 .Sp
168 .Vb 2
169 \& return print reverse sort num values %array;
170 \& return print(reverse(sort num (values(%array))));
171 .Ve
172 .Sp
173 When in doubt, parenthesize. At the very least it will let some poor
174 schmuck bounce on the % key in \fBvi\fR.
175 .Sp
176 Even if you aren't in doubt, consider the mental welfare of the person
177 who has to maintain the code after you, and who will probably put
178 parentheses in the wrong place.
179 .IP \(bu 4
180 Don't go through silly contortions to exit a loop at the top or the
181 bottom, when Perl provides the \f(CW\*(C`last\*(C'\fR operator so you can exit in
182 the middle. Just "outdent" it a little to make it more visible:
183 .Sp
184 .Vb 7
185 \& LINE:
186 \& for (;;) {
187 \& statements;
188 \& last LINE if $foo;
189 \& next LINE if /^#/;
190 \& statements;
191 \& }
192 .Ve
193 .IP \(bu 4
194 Don't be afraid to use loop labels\-\-they're there to enhance
195 readability as well as to allow multilevel loop breaks. See the
196 previous example.
197 .IP \(bu 4
198 Avoid using \f(CWgrep()\fR (or \f(CWmap()\fR) or `backticks` in a void context, that is,
199 when you just throw away their return values. Those functions all
200 have return values, so use them. Otherwise use a \f(CWforeach()\fR loop or
201 the \f(CWsystem()\fR function instead.
202 .IP \(bu 4
203 For portability, when using features that may not be implemented on
204 every machine, test the construct in an eval to see if it fails. If
205 you know what version or patchlevel a particular feature was
206 implemented, you can test \f(CW$]\fR (\f(CW$PERL_VERSION\fR in \f(CW\*(C`English\*(C'\fR) to see if it
207 will be there. The \f(CW\*(C`Config\*(C'\fR module will also let you interrogate values
208 determined by the \fBConfigure\fR program when Perl was installed.
209 .IP \(bu 4
210 Choose mnemonic identifiers. If you can't remember what mnemonic means,
211 you've got a problem.
212 .IP \(bu 4
213 While short identifiers like \f(CW$gotit\fR are probably ok, use underscores to
214 separate words in longer identifiers. It is generally easier to read
215 \&\f(CW$var_names_like_this\fR than \f(CW$VarNamesLikeThis\fR, especially for
216 non-native speakers of English. It's also a simple rule that works
217 consistently with \f(CW\*(C`VAR_NAMES_LIKE_THIS\*(C'\fR.
218 .Sp
219 Package names are sometimes an exception to this rule. Perl informally
220 reserves lowercase module names for "pragma" modules like \f(CW\*(C`integer\*(C'\fR and
221 \&\f(CW\*(C`strict\*(C'\fR. Other modules should begin with a capital letter and use mixed
222 case, but probably without underscores due to limitations in primitive
223 file systems' representations of module names as files that must fit into a
224 few sparse bytes.
225 .IP \(bu 4
226 You may find it helpful to use letter case to indicate the scope
227 or nature of a variable. For example:
228 .Sp
229 .Vb 3
230 \& $ALL_CAPS_HERE constants only (beware clashes with perl vars!)
231 \& $Some_Caps_Here package\-wide global/static
232 \& $no_caps_here function scope my() or local() variables
233 .Ve
234 .Sp
235 Function and method names seem to work best as all lowercase.
236 E.g., \f(CW\*(C`$obj\->as_string()\*(C'\fR.
237 .Sp
238 You can use a leading underscore to indicate that a variable or
239 function should not be used outside the package that defined it.
240 .IP \(bu 4
241 If you have a really hairy regular expression, use the \f(CW\*(C`/x\*(C'\fR or \f(CW\*(C`/xx\*(C'\fR
242 modifiers and put in some whitespace to make it look a little less like
243 line noise.
244 Don't use slash as a delimiter when your regexp has slashes or backslashes.
245 .IP \(bu 4
246 Use the \f(CW\*(C`and\*(C'\fR and \f(CW\*(C`or\*(C'\fR operators to avoid having to parenthesize
247 list operators so much, and to reduce the incidence of punctuation
248 operators like \f(CW\*(C`&&\*(C'\fR and \f(CW\*(C`||\*(C'\fR. Call your subroutines as if they were
249 functions or list operators to avoid excessive ampersands and parentheses.
250 .IP \(bu 4
251 Use here documents instead of repeated \f(CWprint()\fR statements.
252 .IP \(bu 4
253 Line up corresponding things vertically, especially if it'd be too long
254 to fit on one line anyway.
255 .Sp
256 .Vb 4
257 \& $IDX = $ST_MTIME;
258 \& $IDX = $ST_ATIME if $opt_u;
259 \& $IDX = $ST_CTIME if $opt_c;
260 \& $IDX = $ST_SIZE if $opt_s;
261 \&
262 \& mkdir $tmpdir, 0700 or die "can\*(Aqt mkdir $tmpdir: $!";
263 \& chdir($tmpdir) or die "can\*(Aqt chdir $tmpdir: $!";
264 \& mkdir \*(Aqtmp\*(Aq, 0777 or die "can\*(Aqt mkdir $tmpdir/tmp: $!";
265 .Ve
266 .IP \(bu 4
267 Always check the return codes of system calls. Good error messages should
268 go to \f(CW\*(C`STDERR\*(C'\fR, include which program caused the problem, what the failed
269 system call and arguments were, and (VERY IMPORTANT) should contain the
270 standard system error message for what went wrong. Here's a simple but
271 sufficient example:
272 .Sp
273 .Vb 1
274 \& opendir(my $dh, $dir) or die "can\*(Aqt opendir $dir: $!";
275 .Ve
276 .IP \(bu 4
277 Line up your transliterations when it makes sense:
278 .Sp
279 .Vb 2
280 \& tr [abc]
281 \& [xyz];
282 .Ve
283 .IP \(bu 4
284 Think about reusability. Why waste brainpower on a one-shot when you
285 might want to do something like it again? Consider generalizing your
286 code. Consider writing a module or object class. Consider making your
287 code run cleanly with \f(CW\*(C`use strict\*(C'\fR and \f(CW\*(C`use warnings\*(C'\fR in
288 effect. Consider giving away your code. Consider changing your whole
289 world view. Consider... oh, never mind.
290 .IP \(bu 4
291 Try to document your code and use Pod formatting in a consistent way. Here
292 are commonly expected conventions:
293 .RS 4
294 .IP \(bu 4
295 use \f(CW\*(C`C<>\*(C'\fR for function, variable and module names (and more
296 generally anything that can be considered part of code, like filehandles
297 or specific values). Note that function names are considered more readable
298 with parentheses after their name, that is \f(CWfunction()\fR.
299 .IP \(bu 4
300 use \f(CW\*(C`B<>\*(C'\fR for commands names like \fBcat\fR or \fBgrep\fR.
301 .IP \(bu 4
302 use \f(CW\*(C`F<>\*(C'\fR or \f(CW\*(C`C<>\*(C'\fR for file names. \f(CW\*(C`F<>\*(C'\fR should
303 be the only Pod code for file names, but as most Pod formatters render it
304 as italic, Unix and Windows paths with their slashes and backslashes may
305 be less readable, and better rendered with \f(CW\*(C`C<>\*(C'\fR.
306 .RE
307 .RS 4
308 .RE
309 .IP \(bu 4
310 Be consistent.
311 .IP \(bu 4
312 Be nice.