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