IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 2359


Ignore:
Timestamp:
Nov 12, 2004, 4:57:30 PM (22 years ago)
Author:
jhoblitt
Message:

fixes for formatting, grammar, spelling, and missing bits

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/doc/misc/perlCodeConventions.tex

    r2356 r2359  
    1 %%% $Id: perlCodeConventions.tex,v 1.10 2004-11-12 22:31:58 jhoblitt Exp $
     1%%% $Id: perlCodeConventions.tex,v 1.11 2004-11-13 02:57:30 jhoblitt Exp $
    22\documentclass[panstarrs]{panstarrs}
    33
     
    3030%%% References here.
    3131\textbf{\PS{} ID} & \textbf{Title} & \textbf{Author} \\
     32\hline
    3233430-004 &Pan-STARRS IPP C Code Standards    &Robert Lupton\\
    3334\hline
     
    6667\subsection{Limitations}
    6768
    68 Although Perl's syntax is in many ways similar to that of C or Java, there are
    69 many signification deviations.  This has lead to a coding style within the Perl
     69Although Perl's syntax is similar to that of C or Java, there are many
     70signification deviations.  This has lead to a coding style within the Perl
    7071community that is generally significantly different then that used for C or
    7172Java (See the \code{perlstyle} Pod in the standard Perl documentation for an
     
    7374common with Perl programmers.  Instead, it is an attempt to make the C and Perl
    7475coding styles as consistent as is reasonably feasible within the Pan-STARRS
    75 project.
     76project.  This document also only contains brief discussion of Perl's
     77Object-Oriented features and none of Perl's more advanced features.
     78
     79\subsection{Applicability}
     80
     81This document is intended to be relevant with Perl 5.8.0 or later.
    7682
    7783\subsection{To whom should I Complain?}
     
    131137\subsubsection{Perl Scripts}
    132138
    133 Include files should have the following order:
     139Script files should have the following order:
    134140
    135141\begin{itemize}
    136142\item
    137143A Bourne shell "she-bang" for perl.  \code{#!/usr/bin/perl}.
     144
    138145\item
    139146A Copyright notice followed by a blank commented line.  Then one commented line
    140147per CVS keyword to be expanded.
     148
     149\item
     150A minimum perl version requirement in long form (vStrings are not allowed).
     151Where \code(requirement = revision + version / 1000 + subversion /
     1521\_000\_000).  E.g. A minimum requirement of Perl 5.8.5 would be expressed as
     153\code{use 5.008005}
     154
     155\item
     156A \code{use strict} and \code{use warnings} declaration.
     157
     158\item
     159Any base classes.
     160
     161\item
     162Any constant declarations.
     163
     164\item
     165Any script level "globals" declared as lexical variables.  E.g. \code{my $foo}
     166
     167\item
     168Any subroutines.
     169
     170\item
     171A \code{__END__} token to instruct the perl parser to stop looking for
     172executable code.
     173
     174\item
     175Any Pod documentation about the script.
     176\end{itemize}
     177
     178e.g. for file \file{foo.pl}:
     179
     180\scriptsize
     181\code{#!/usr/bin/perl}\\
     182\\
     183\code{# Copyright (C) 2004  Joshua Hoblitt}\\
     184\code{#}\\
     185\code{# $}\code{Id$}\\
     186\normalsize
     187\begin{verbatim}
     188use 5.008005;   # optional
     189
     190use strict;     # not optional
     191use warnings;   # not optional
     192
     193use Foo qw( $bar );
     194
     195use constant MAXFOO => 3;
     196
     197my $baz;
     198
     199sub fuu
     200{
     201
     202}
     203
     204__END__
     205
     206...
     207\end{verbatim}
     208
     209\subsubsection{Perl Modules}
     210
     211Perl Module/Namespaces deviate slightly from the standard naming conventions in
     212that the first character is always capitalized.
     213
     214\begin{itemize}
     215\item
     216A Copyright notice followed by a blank commented line.  Then one commented line
     217per CVS keyword to be expanded.
     218
     219\item
     220A Perl \code{package} declaration.
     221
    141222\item
    142223A minimum perl version requirement in long form (vStrings are not allowed).
     
    144225E.g. A minimum requirement of Perl 5.8.5 would be expressed as \code{use
    1452265.008005}
     227
    146228\item
    147229A \code{use strict} and \code{use warnings} declaration.
     230
    148231\item
    149232Any modules to include.
     233
     234\item
     235Any constant declarations.
     236
    150237\item
    151238Any script level "globals" declared as lexical variables.  E.g. \code{my $foo}
     239
    152240\item
    153241Any subroutines.
     242
     243\item
     244A \code{1} on a line by itself so when the module is parsed it always ends with
     245a true expression.
     246
    154247\item
    155248A \code{__END__} token to instruct the perl parser to stop looking for
    156249executable code.
    157 \item Any Pod documentation about the script.
     250
     251\item
     252Any Pod documentation about the module.
    158253\end{itemize}
    159254
    160 e.g. for file \file{foo.pl}:
    161 
    162 \begin{verbatim}
    163 #!/usr/bin/perl
    164 
    165 # Copyright (C) 2004  Joshua Hoblitt
    166 #
    167 # $Id: perlCodeConventions.tex,v 1.10 2004-11-12 22:31:58 jhoblitt Exp $
     255e.g. for file \file{Foo.pm}:
     256
     257\scriptsize
     258\code{# Copyright (C) 2004  Joshua Hoblitt}\\
     259\code{#}\\
     260\code{# $}\code{Id$}\\
     261\normalsize
     262\begin{verbatim}
     263package Foo;
    168264
    169265use 5.008005;   # optional
     
    172268use warnings;   # not optional
    173269
     270use base qw( Baz );
     271
    174272use Foo qw( $bar );
     273
     274use constant MAXFOO => 3;
    175275
    176276my $baz;
     
    181281}
    182282
     2831;
     284
    183285__END__
    184 \end{verbatim}
    185 
    186 \subsubsection{Perl Modules}
    187 
    188 Perl module names deviate slightly from the other naming conventions in that
    189 the first character is always capitalized.
    190 
    191 \begin{itemize}
    192 \item
    193 A Copyright notice followed by a blank commented line.  Then one commented line
    194 per CVS keyword to be expanded.
    195 \item
    196 A Perl Package declaration.  \code{package Foo}
    197 \item
    198 A minimum perl version requirement in long form (vStrings are not allowed).
    199 Where \code(requirement = revision + version / 1000 + subversion / 1\_000\_000).
    200 E.g. A minimum requirement of Perl 5.8.5 would be expressed as \code{use
    201 5.008005}
    202 \item
    203 A \code{use strict} and \code{use warnings} declaration.
    204 \item
    205 Any modules to include.
    206 \item
    207 Any script level "globals" declared as lexical variables.  E.g. \code{my $foo}
    208 \item
    209 Any subroutines.
    210 \item
    211 A \code{1} on a line by itself so when the module is parsed it always ends with
    212 a true expression.
    213 \item
    214 A \code{__END__} token to instruct the perl parser to stop looking for
    215 executable code.
    216 \item Any Pod documentation about the module.
    217 \end{itemize}
    218 
    219 e.g. for file \file{Foo.pm}:
    220 
    221 \begin{verbatim}
    222 # Copyright (C) 2004  Joshua Hoblitt
    223 #
    224 # $Id: perlCodeConventions.tex,v 1.10 2004-11-12 22:31:58 jhoblitt Exp $
    225 
    226 package Foo;
    227 
    228 use 5.008005;   # optional
    229 
    230 use strict;     # not optional
    231 use warnings;   # not optional
    232 
    233 use Foo qw( $bar );
    234 
    235 my $baz;
    236 
    237 sub fuu
    238 {
    239 
    240 }
    241 
    242 1;
    243 
    244 __END__
     286
     287...
    245288\end{verbatim}
    246289
     
    328371=cut
    329372\end{verbatim}
     373
    330374
    331375%------------------------------------------------------------------------------
     
    410454\end{verbatim}
    411455
     456
    412457%------------------------------------------------------------------------------
    413458
     
    421466
    422467Example of deactivating a single line code.
     468
    423469\begin{verbatim}
    424470    if ($foo) {
     
    432478
    433479Example of deactivating multiple lines of code.
     480
    434481\begin{verbatim}
    435482if (0) {
     
    475522
    476523Example of a single-line comment.
     524
    477525\begin{verbatim}
    478526# some words
     
    486534\label{col41}
    487535
    488 Very short comments can appear on the same line as the code they
    489 describe, and should be indented to column 41\footnote{In emacs, this
    490 may be achieved with \code{ESC;}}. If the code extends beyond this
    491 column, the comment should be separated from the closing semi-colon by
    492 a single space.
     536Very short comments can appear on the same line as the code they describe, and
     537should be indented to column 41\footnote{In emacs, this may be achieved with
     538\code{ESC;}}. If the code extends beyond this column, the comment should be
     539separated from the closing semi-colon by a single space.
    493540
    494541Here's an example of a trailing comment in Perl code:
     542
    495543\begin{verbatim}
    496544if ($a == 2) {
     
    532580multi-line comment  immediately \textit{before} the code (as exampled above).
    533581
     582
    534583%------------------------------------------------------------------------------
    535584
    536585\section{Declarations}
    537586
    538 All globally-visible symbols must be declared in a suitable header
    539 (.h) file.  Global private symbols should not appear in public header
    540 files, but rather in separate, private header files.
    541 
    542587\subsection{Lexical Variables}
    543588
    544 One declaration per line with a single place between 'my' and the identifier.
     589One declaration per line with a single place between \code{my} and the identifier.
    545590
    546591\begin{verbatim}
     
    560605Do not put different types on the same line. Example:
    561606\begin{verbatim}
    562 my ($foo,  @fooArray);                  # WRONG!
     607my ($foo, @fooArray);                  # WRONG!
    563608\end{verbatim}
    564609
     
    614659\item
    615660The first line after the opening brace should declare a list of lexical
    616 variables for input parameters and assign \code{@\_} to it.
     661variables for input parameters and assign \code{@_} to it.
    617662
    618663\item
     
    634679}
    635680\end{verbatim}
     681
    636682
    637683%------------------------------------------------------------------------------
     
    679725\begin{verbatim}
    680726return undef;                           # Correct
    681 return;                                 # Wrong!
     727return;                                 # Avoid!
    682728
    683729return $myDisk->size;
     
    687733
    688734Note:  In absence of an explicit return statement Perl will return the results
    689 of the last statement executed.  A null return statement \code{return;} also
    690 causes the result of the last statement executed to be returned.
     735of the last statement executed.  A null \code{return} statement causes the
     736result of the last statement executed to be returned.
    691737
    692738\subsection{if, if-else, if elsif else Statements}
     
    747793the loop (for the update clause).
    748794
     795\subsection{Perl style for, foreach Statements}
     796
     797\begin{verbatim}
     798\end{verbatim}
     799
    749800\subsection{while Statements}
    750801
    751802A \code{while} statement should have the following form:
     803
    752804\begin{verbatim}
    753805while (condition) {
     
    755807}
    756808\end{verbatim}
     809
    757810An empty \code{while} statement should have the following form:
     811
    758812\begin{verbatim}
    759813while (condition) {}
     
    763817
    764818A \code{do-while} statement should have the following form:
     819
    765820\begin{verbatim}
    766821do {
     
    773828Code labels should be indented to align with the previous level of
    774829indentation.
     830
     831\begin{verbatim}
     832MYLABEL: while (condition) {
     833    if (condition) {
     834        next;
     835    }
     836}
     837\end{verbatim}
     838
    775839
    776840%------------------------------------------------------------------------------
     
    816880\item
    817881Binary operators should be separated from their operands by spaces.  Blank
    818 spaces should never separate unary operators such as a type cast, unary minus,
    819 increment (\code{++}), and decrement (\code{--}) from their operands. Examples:
     882spaces should never separate unary operators such as a unary minus, increment
     883(\code{++}), and decrement (\code{--}) from their operands. Examples:
    820884
    821885\begin{verbatim}
     
    850914\section{Naming Conventions}
    851915
    852 Naming conventions make programs more understandable by making them
    853 easier to read. They can also give information about the subroutine of
    854 the identifier -- for example, whether it's a constant, a subroutine, or
    855 a type -- which can be helpful in understanding the code.  Remember
    856 these are guidelines for improving readability; clarity should trump
    857 rigid adherence to the guideline.
     916Naming conventions make programs more understandable by making them easier to
     917read. They can also give information about the subroutine of the identifier --
     918for example, whether it's a constant or a subroutine -- which can be helpful in
     919understanding the code.  Remember these are guidelines for improving
     920readability; clarity should trump rigid adherence to the guideline.
    858921 
    859922{ \small
     
    876939\code{runFast();}\hfil\break
    877940\code{getBackground();}\hfil\break
     941\code{_keepHandsOff();}\hfil\break
     942\code{_notForPublicUse();}\hfil\break
    878943
    879944\\
     
    881946Variables &
    882947
    883 Except for variables, all instance, class, and class constants are in mixed
    884 case with Variable names should not start with underscore \code{_} as these
    885 names are, under some circumstances, reserved by Perl community convention.
    886 
    887 Variable names should be short yet meaningful. The choice of a variable name
    888 should be mnemonic- that is, designed to indicate to the casual observer the
    889 intent of its use. One-character variable names should be avoided except for
    890 temporary \textit{throwaway} variables. Common names for temporary variables
    891 are \code{i}, \code{j}, \code{k}, \code{m}, and \code{n} for integers;
    892 \code{c}, \code{d}, and \code{e} for characters.
     948Variable names should be in mixed case and be short yet meaningful. The choice
     949of a variable name should be mnemonic- that is, designed to indicate to the
     950casual observer the intent of its use. One-character variable names should be
     951avoided except for temporary \textit{throwaway} variables. Common names for
     952temporary variables are \code{$i}, \code{$j}, \code{$k}, \code{$m}, and \code{$n}
     953for integers; \code{$c}, \code{$d}, and \code{$e} for characters.
    893954
    894955&
     
    905966Constants &
    906967
    907 Perl's \code{constant} pragma should not be used as it merely generates
    908 subroutines with null prototypes.  'Constants' created this way can not be
    909 interpolated instead of strings.  Instead, use Lexical variables but with all
    910 capital letters.
     968Perl's \code{constant} pragma should be used to declare constants.  Note:
     969'constants' created this way can not be interpolated inside of double quoted
     970strings.
    911971
    912972&
    913 \code{my $MAXLEN = 40;}\hfil\break
    914 \code{my @LENGTH = (2, 4, 8);}\hfil\break
    915 \code{my %CLASS = (raw => 1, processed => 2);}\hfil\break
     973\code{use constant MAXLEN => 40;}\hfil\break
     974\code{use constant LENGTH => qw(2, 4, 8);}\hfil\break
     975\code{use constant CLASSES => (raw => 1, processed => 2);}\hfil\break
    916976
    917977\\
     
    924984&
    925985
    926 \code{Class::Data::Inheritable}\hfil\break
    927 \code{Time::HiRes}\hfil\break
    928 \code{XML::SimpleObject}\hfil\break
     986\code{package Class::Data::Inheritable;}\hfil\break
     987\code{package Time::HiRes;}\hfil\break
     988\code{package XML::SimpleObject;}\hfil\break
    929989
    930990\\
     
    932992\end{tabular}
    933993}
     994
    934995
    935996%------------------------------------------------------------------------------
     
    10121073\end{verbatim}
    10131074
    1014 There are some famous problems with precedence in C.  In particular,
    1015 expressions involving the combinations of \code{||} and \code{&&} should be
    1016 fully parenthesized, as should all expressions containing bitwise operators:
     1075In particular, expressions involving the combinations of \code{||} and
     1076\code{&&} should be fully parenthesized, as should all expressions containing
     1077bitwise operators:
    10171078
    10181079\begin{verbatim}
     
    10701131\end{verbatim}
    10711132
     1133\end{document}
    10721134%------------------------------------------------------------------------------
    10731135\appendix                               %Begin Appendices
Note: See TracChangeset for help on using the changeset viewer.