Changeset 2378 for trunk/doc/misc/perlCodeConventions.tex
- Timestamp:
- Nov 16, 2004, 1:40:04 PM (22 years ago)
- File:
-
- 1 edited
-
trunk/doc/misc/perlCodeConventions.tex (modified) (46 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/doc/misc/perlCodeConventions.tex
r2377 r2378 1 %%% $Id: perlCodeConventions.tex,v 1. 19 2004-11-16 22:15:07jhoblitt Exp $1 %%% $Id: perlCodeConventions.tex,v 1.20 2004-11-16 23:40:04 jhoblitt Exp $ 2 2 \documentclass[panstarrs]{panstarrs} 3 3 … … 56 56 57 57 \begin{verbatim} 58 Adapted with permission from59 CODE CONVENTIONS FOR THE JAVA^TM PROGRAMMING LANGUAGE.60 Copyright 1995-1999 Sun61 Microsysytems, Inc. All rights reserved.58 Adapted with permission from 59 CODE CONVENTIONS FOR THE JAVA^TM PROGRAMMING LANGUAGE. 60 Copyright 1995-1999 Sun 61 Microsysytems, Inc. All rights reserved. 62 62 \end{verbatim} 63 63 … … 193 193 \normalsize 194 194 \begin{verbatim} 195 use 5.008005; # optional196 197 use strict; # not optional198 use warnings; # not optional199 200 use Foo qw( $bar ); # import $bar into our namespace201 202 use constant MAXFOO => 3; # maximum number of foos203 204 my $baz; # is a lexical variable in the top level scope205 206 sub fuu207 {208 209 }210 211 __END__212 213 ...195 use 5.008005; # optional 196 197 use strict; # not optional 198 use warnings; # not optional 199 200 use Foo qw( $bar ); # import $bar into our namespace 201 202 use constant MAXFOO => 3; # maximum number of foos 203 204 my $baz; # is a lexical variable in the top level scope 205 206 sub fuu 207 { 208 209 } 210 211 __END__ 212 213 ... 214 214 \end{verbatim} 215 215 … … 267 267 \normalsize 268 268 \begin{verbatim} 269 package Foo;270 271 use 5.008005; # optional272 273 use strict; # not optional274 use warnings; # not optional275 276 use base qw( Baz ); # become a subclass of Baz277 278 use Foo qw( $bar ); # import $bar into our namespace279 280 use constant MAXFOO => 3; # maximum number of foos281 282 my $baz; # is a lexical variable in the top level scope283 284 sub fuu285 {286 287 }288 289 1;269 package Foo; 270 271 use 5.008005; # optional 272 273 use strict; # not optional 274 use warnings; # not optional 275 276 use base qw( Baz ); # become a subclass of Baz 277 278 use Foo qw( $bar ); # import $bar into our namespace 279 280 use constant MAXFOO => 3; # maximum number of foos 281 282 my $baz; # is a lexical variable in the top level scope 283 284 sub fuu 285 { 286 287 } 288 289 1; 290 290 291 291 __END__ … … 301 301 302 302 \begin{verbatim} 303 =pod 304 305 =head1 NAME 306 307 Foo - Does something with Baz. 308 309 =head1 SYNOPSIS 310 311 use Foo; 303 =pod 304 305 =head1 NAME 306 307 Foo - Does something with Baz. 308 309 =head1 SYNOPSIS 310 311 use Foo; 312 313 ... 314 315 =head1 DESCRIPTION 316 317 More details about what this modules does with Baz. 318 319 =head1 USAGE 320 321 =head2 Import Parameters 322 323 This module accepts no arguments to it's C<import> method and exports no 324 I<symbols>. 325 326 =head2 Methods 327 328 =head3 Class Methods 329 330 =over 4 331 332 =item * methodOne 333 334 details of methodOne 335 336 =item * methodTwo 337 338 details of methodTwo 312 339 313 340 ... 314 341 315 =head1 DESCRIPTION 316 317 More details about what this modules does with Baz. 318 319 =head1 USAGE 320 321 =head2 Import Parameters 322 323 This module accepts no arguments to it's C<import> method and exports no 324 I<symbols>. 325 326 =head2 Methods 327 328 =head3 Class Methods 329 330 =over 4 331 332 =item * methodOne 333 334 details of methodOne 335 336 =item * methodTwo 337 338 details of methodTwo 339 340 ... 341 342 =back 343 344 =head1 SUPPORT 345 346 Where to get help. 347 348 =head1 AUTHOR 349 350 Principle authors and contact info. 351 352 =head1 COPYRIGHT 353 354 Copyright (C) 2004 Robert Lupton. All rights reserved. 355 356 This program is free software; you can redistribute it and/or modify it under 357 the terms of the GNU General Public License as published by the Free Software 358 Foundation; either version 2 of the License, or (at your option) any later 359 version. 360 361 This program is distributed in the hope that it will be useful, but WITHOUT ANY 362 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 363 PARTICULAR PURPOSE. See the GNU General Public License for more details. 364 365 You should have received a copy of the GNU General Public License along with 366 this program; if not, write to the Free Software Foundation, Inc., 59 Temple 367 Place - Suite 330, Boston, MA 02111-1307, USA. 368 369 The full text of the license can be found in the LICENSE file included with 370 this module, or in the L<perlgpl> Pod as supplied with Perl 5.8.1 and later. 371 372 =head1 SEE ALSO 373 374 L<Bar::Baz> 375 376 =cut 342 =back 343 344 =head1 SUPPORT 345 346 Where to get help. 347 348 =head1 AUTHOR 349 350 Principle authors and contact info. 351 352 =head1 COPYRIGHT 353 354 Copyright (C) 2004 Robert Lupton. All rights reserved. 355 356 This program is free software; you can redistribute it and/or modify it under 357 the terms of the GNU General Public License as published by the Free Software 358 Foundation; either version 2 of the License, or (at your option) any later 359 version. 360 361 This program is distributed in the hope that it will be useful, but WITHOUT ANY 362 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 363 PARTICULAR PURPOSE. See the GNU General Public License for more details. 364 365 You should have received a copy of the GNU General Public License along with 366 this program; if not, write to the Free Software Foundation, Inc., 59 Temple 367 Place - Suite 330, Boston, MA 02111-1307, USA. 368 369 The full text of the license can be found in the LICENSE file included with 370 this module, or in the L<perlgpl> Pod as supplied with Perl 5.8.1 and later. 371 372 =head1 SEE ALSO 373 374 L<Bar::Baz> 375 376 =cut 377 377 \end{verbatim} 378 378 … … 404 404 405 405 \begin{verbatim} 406 if ($a < $b ||407 $a > 2*$b) {408 $x = ($a + $b + $c) +409 sin($z);410 }406 if ($a < $b || 407 $a > 2*$b) { 408 $x = ($a + $b + $c) + 409 sin($z); 410 } 411 411 \end{verbatim} 412 412 … … 427 427 428 428 \begin{verbatim} 429 someSub(longExpression1, longExpression2, longExpression3,430 longExpression4, longExpression5);431 432 $var = someSub(longExpression1,433 someSub(longExpression2,434 longExpression3));429 someSub(longExpression1, longExpression2, longExpression3, 430 longExpression4, longExpression5); 431 432 $var = someSub(longExpression1, 433 someSub(longExpression2, 434 longExpression3)); 435 435 \end{verbatim} 436 436 … … 451 451 452 452 \begin{verbatim} 453 $alpha = (aLongBooleanExpression) ? $beta : $gamma;454 455 $alpha = (aLongBooleanExpression) ? $beta456 : $gamma;457 458 $alpha = (aLongBooleanExpression)459 ? $beta460 : $gamma;453 $alpha = (aLongBooleanExpression) ? $beta : $gamma; 454 455 $alpha = (aLongBooleanExpression) ? $beta 456 : $gamma; 457 458 $alpha = (aLongBooleanExpression) 459 ? $beta 460 : $gamma; 461 461 \end{verbatim} 462 462 … … 475 475 476 476 \begin{verbatim} 477 if ($foo) {478 # print("foo");479 }477 if ($foo) { 478 # print("foo"); 479 } 480 480 \end{verbatim} 481 481 … … 487 487 488 488 \begin{verbatim} 489 if (0) {490 if ($foo) {491 print("foo");492 }493 }489 if (0) { 490 if ($foo) { 491 print("foo"); 492 } 493 } 494 494 \end{verbatim} 495 495 … … 531 531 532 532 \begin{verbatim} 533 # some words533 # some words 534 534 \end{verbatim} 535 535 … … 540 540 541 541 \begin{verbatim} 542 # this is the start of a multi-line comment that continues across a couple of543 # lines then has a logical separation before another comment544 #545 # this is an adjoined comment542 # this is the start of a multi-line comment that continues across a couple of 543 # lines then has a logical separation before another comment 544 # 545 # this is an adjoined comment 546 546 \end{verbatim} 547 547 … … 557 557 558 558 \begin{verbatim} 559 if ($a == 2) {560 return TRUE; # special case561 } else {562 return isPrime($a); # works only for odd a563 }559 if ($a == 2) { 560 return TRUE; # special case 561 } else { 562 return isPrime($a); # works only for odd a 563 } 564 564 \end{verbatim} 565 565 … … 574 574 575 575 \begin{verbatim} 576 =item * reallyCoolSubroutine577 578 Accepts a real number and a string. Does some really cool stuff with it and579 returns a really cool thing.580 581 =cut582 583 sub reallyCoolSubroutine584 {585 my ($aNumber, $aString) = @_;586 587 # Do really cool stuff588 589 return $aThing;590 }576 =item * reallyCoolSubroutine 577 578 Accepts a real number and a string. Does some really cool stuff with it and 579 returns a really cool thing. 580 581 =cut 582 583 sub reallyCoolSubroutine 584 { 585 my ($aNumber, $aString) = @_; 586 587 # Do really cool stuff 588 589 return $aThing; 590 } 591 591 \end{verbatim} 592 592 … … 605 605 606 606 \begin{verbatim} 607 my $level; # indentation level608 my $size; # size of table607 my $level; # indentation level 608 my $size; # size of table 609 609 \end{verbatim} 610 610 … … 612 612 613 613 \begin{verbatim} 614 my ($level, $size);614 my ($level, $size); 615 615 \end{verbatim} 616 616 … … 620 620 Do not put different types on the same line. Example: 621 621 \begin{verbatim} 622 my ($foo, @fooArray); # AVOID!622 my ($foo, @fooArray); # AVOID! 623 623 \end{verbatim} 624 624 … … 630 630 631 631 \begin{verbatim} 632 my $count; 633 ... 634 sub mySubroutine 635 { 636 local $count; # AVOID! 632 my $count; 637 633 ... 638 } 634 sub mySubroutine 635 { 636 local $count; # AVOID! 637 ... 638 } 639 639 \end{verbatim} 640 640 … … 689 689 690 690 \begin{verbatim} 691 sub subName692 {693 my ($param1, $param2, ...) = @_;694 ...695 696 return $val;697 }691 sub subName 692 { 693 my ($param1, $param2, ...) = @_; 694 ... 695 696 return $val; 697 } 698 698 \end{verbatim} 699 699 … … 705 705 706 706 Each line should usually contain only one statement. Example: 707 \begin{verbatim} 708 $x = sqrt($x2); # Correct 709 $i++; # Correct 710 $x = sqrt($x2); $i++; # Avoid! 707 708 \begin{verbatim} 709 $x = sqrt($x2); # Correct 710 $i++; # Correct 711 $x = sqrt($x2); $i++; # Avoid! 711 712 \end{verbatim} 712 713 713 714 An example of a reasonable two-statement line is: 714 \begin{verbatim} 715 $foo++; $bar--; 716 \end{verbatim} 715 716 \begin{verbatim} 717 $foo++; $bar--; 718 \end{verbatim} 719 717 720 where the two actions are intimately related. 718 721 … … 737 740 738 741 \begin{verbatim} 739 return undef; # Correct740 return; # Avoid!741 742 return $myDisk->size;743 744 return ($size ? $size : $defaultSize);742 return undef; # Correct 743 return; # Avoid! 744 745 return $myDisk->size; 746 747 return ($size ? $size : $defaultSize); 745 748 \end{verbatim} 746 749 … … 757 760 758 761 The \code{if-else} class of statements should have the following form: 759 \begin{verbatim} 760 if (condition) { 761 statements; 762 } 763 764 if (condition) { 765 statements; 766 } else { 767 statements; 768 } 769 770 if (condition) { 771 statements; 772 } elsif (condition) { 773 statements; 774 } else { 775 statements; 776 } 762 763 \begin{verbatim} 764 if (condition) { 765 statements; 766 } 767 768 if (condition) { 769 statements; 770 } else { 771 statements; 772 } 773 774 if (condition) { 775 statements; 776 } elsif (condition) { 777 statements; 778 } else { 779 statements; 780 } 777 781 \end{verbatim} 778 782 … … 782 786 783 787 \begin{verbatim} 784 for (initialization; condition; update) {785 statements;786 }788 for (initialization; condition; update) { 789 statements; 790 } 787 791 \end{verbatim} 788 792 … … 790 794 791 795 \begin{verbatim} 792 for (my $i = 0; $i < 100; $i++) {793 print "$i\n";794 }796 for (my $i = 0; $i < 100; $i++) { 797 print "$i\n"; 798 } 795 799 \end{verbatim} 796 800 … … 799 803 800 804 \begin{verbatim} 801 for (initialization; condition; update) {}805 for (initialization; condition; update) {} 802 806 \end{verbatim} 803 807 … … 814 818 815 819 \begin{verbatim} 816 foreach my $item (@workingList) {817 # do stuff with $item818 }820 foreach my $item (@workingList) { 821 # do stuff with $item 822 } 819 823 \end{verbatim} 820 824 … … 823 827 824 828 \begin{verbatim} 825 foreach (@workingList) { # Avoid!826 # do stuff with $_827 }829 foreach (@workingList) { # Avoid! 830 # do stuff with $_ 831 } 828 832 \end{verbatim} 829 833 … … 833 837 834 838 \begin{verbatim} 835 while (condition) {836 statements;837 }839 while (condition) { 840 statements; 841 } 838 842 \end{verbatim} 839 843 … … 841 845 842 846 \begin{verbatim} 843 while (condition) {}847 while (condition) {} 844 848 \end{verbatim} 845 849 … … 849 853 850 854 \begin{verbatim} 851 do {852 statements;853 } while (condition);855 do { 856 statements; 857 } while (condition); 854 858 \end{verbatim} 855 859 … … 860 864 861 865 \begin{verbatim} 862 MYLABEL: while (condition) {863 if (condition) {864 next MYLABEL;865 }866 }866 MYLABEL: while (condition) { 867 if (condition) { 868 next MYLABEL; 869 } 870 } 867 871 \end{verbatim} 868 872 … … 897 901 898 902 \begin{verbatim} 899 while (true) {900 ...901 }903 while (true) { 904 ... 905 } 902 906 \end{verbatim} 903 907 … … 916 920 $a += $c + $d; 917 921 $a = ($a + $b) / ($c * $d); 918 922 919 923 while ($d++ = $s++) { 920 924 $n++; … … 928 932 929 933 \begin{verbatim} 930 for (expr1; expr2; expr3) 934 for (expr1; expr2; expr3) {} 931 935 \end{verbatim} 932 936 … … 936 940 937 941 \begin{verbatim} 938 my @things = qw( foo bar baz );942 my @things = qw( foo bar baz ); 939 943 \end{verbatim} 940 944 \end{itemize} … … 1047 1051 1048 1052 \begin{verbatim} 1049 package someModule; 1050 1051 use base qw( Exporter ); 1052 1053 our @EXPORT_OK = qw( $foo $bar ); 1054 1053 use base qw( Exporter ); 1054 1055 our @EXPORT_OK = qw( $foo $bar ); 1055 1056 \end{verbatim} 1056 1057 … … 1067 1068 1068 1069 \begin{verbatim} 1069 $row0 = $col0 = 0;1070 $sum = $sumx = $sumy = 0;1070 $row0 = $col0 = 0; 1071 $sum = $sumx = $sumy = 0; 1071 1072 \end{verbatim} 1072 1073 … … 1075 1076 1076 1077 \begin{verbatim} 1077 if ($c++ = $d++) { # AVOID!1078 ...1079 }1078 if ($c++ = $d++) { # AVOID! 1079 ... 1080 } 1080 1081 \end{verbatim} 1081 1082 … … 1083 1084 1084 1085 \begin{verbatim} 1085 if (($c++ = $d++) != 0) {1086 ...1087 }1086 if (($c++ = $d++) != 0) { 1087 ... 1088 } 1088 1089 \end{verbatim} 1089 1090 1090 1091 Do not use embedded assignments in an attempt to improve run-time 1091 1092 performance. This is the job of the compiler. Example: 1092 \begin{verbatim} 1093 $d = ($a = $b + $c) + $r; # AVOID! 1093 1094 \begin{verbatim} 1095 $d = ($a = $b + $c) + $r; # AVOID! 1094 1096 \end{verbatim} 1095 1097 1096 1098 should be written as 1097 \begin{verbatim} 1098 $a = $b + $c; 1099 $d = $a + $r; 1099 1100 \begin{verbatim} 1101 $a = $b + $c; 1102 $d = $a + $r; 1100 1103 \end{verbatim} 1101 1104 … … 1111 1114 1112 1115 \begin{verbatim} 1113 if ($a & $b || $c & $d) {} # AVOID!1114 if (($a & $b) || ($c & $d)) {} # RIGHT1116 if ($a & $b || $c & $d) {} # AVOID! 1117 if (($a & $b) || ($c & $d)) {} # RIGHT 1115 1118 \end{verbatim} 1116 1119 … … 1120 1123 1121 1124 \begin{verbatim} 1122 if (($a == $b && $c == $d) || $e == $f) {}1123 $l << ($j + $k)1124 ($l << $j) + $k1125 ($i & $b) | $c1125 if (($a == $b && $c == $d) || $e == $f) {} 1126 $l << ($j + $k) 1127 ($l << $j) + $k 1128 ($i & $b) | $c 1126 1129 \end{verbatim} 1127 1130 … … 1131 1134 1132 1135 \begin{verbatim} 1133 if (booleanExpression) {1134 return true;1135 } else {1136 return false;1137 }1136 if (booleanExpression) { 1137 return true; 1138 } else { 1139 return false; 1140 } 1138 1141 \end{verbatim} 1139 1142 … … 1141 1144 1142 1145 \begin{verbatim} 1143 return booleanExpression;1146 return booleanExpression; 1144 1147 \end{verbatim} 1145 1148 … … 1148 1151 1149 1152 \begin{verbatim} 1150 return (booleanExpression ? true : false);1153 return (booleanExpression ? true : false); 1151 1154 \end{verbatim} 1152 1155 … … 1154 1157 1155 1158 \begin{verbatim} 1156 if (condition) { 1157 return $x; 1158 } 1159 return $y; 1160 \end{verbatim} 1159 if (condition) { 1160 return $x; 1161 } 1162 return $y; 1163 \end{verbatim} 1164 1161 1165 should be written as 1162 \begin{verbatim} 1163 return (condition ? x : y); 1166 1167 \begin{verbatim} 1168 return (condition ? x : y); 1164 1169 \end{verbatim} 1165 1170 … … 1170 1175 1171 1176 \begin{verbatim} 1172 ($x >= 0) ? $x : -$x;1177 ($x >= 0) ? $x : -$x; 1173 1178 \end{verbatim} 1174 1179
Note:
See TracChangeset
for help on using the changeset viewer.
