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