IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Nov 15, 2004, 6:58:16 PM (22 years ago)
Author:
jhoblitt
Message:

fix grammar, spelling, section naming, example corrections and clarifications
specified inline documentation for .pl and separate pods for .pm

File:
1 edited

Legend:

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

    r2363 r2373  
    1 %%% $Id: perlCodeConventions.tex,v 1.15 2004-11-13 03:39:25 jhoblitt Exp $
     1%%% $Id: perlCodeConventions.tex,v 1.16 2004-11-16 04:58:16 jhoblitt Exp $
    22\documentclass[panstarrs]{panstarrs}
    33
     
    6969Although Perl's syntax is similar to that of C or Java, there are many
    7070signification deviations.  This has lead to a coding style within the Perl
    71 community that is generally significantly different then that used for C or
     71community that is generally significantly different than that used for C or
    7272Java (See the \code{perlstyle} Pod in the standard Perl documentation for an
    7373example of this).  This document does not attempt to define the coding style
     
    8686complaints it may generate.
    8787
    88 \code{rhl@astro.princeton.edu}.
     88\code{<rhl@astro.princeton.edu>}.
    8989
    9090%------------------------------------------------------------------------------
     
    101101\textbf{File Type}& \textbf{Suffix}\\
    102102\hline
    103 Perl scripts&       \code{.pl}\\
    104 Perl modules&       \code{.pm}\\
    105 Perl documentation& \code{.pod}\\
     103Perl scripts&               \code{.pl}\\
     104Perl modules&               \code{.pm}\\
     105Plain old documentation&    \code{.pod}\\
    106106\end{tabular}
    107107\end{center}
     
    133133Source File Example (\S\ref{SourceExample}).
    134134
     135``Scripts'' have user documentation included at the end of the file.
     136
     137``Modules'' have documentation included in a companion \code{.pod}.
     138
    135139\subsection{Source Files}
    136140
     
    186190\normalsize
    187191\begin{verbatim}
    188 use 5.008005;   # optional
    189 
    190 use strict;     # not optional
    191 use warnings;   # not optional
    192 
    193 use Foo qw( $bar );
    194 
    195 use constant MAXFOO => 3;
    196 
    197 my $baz;
     192use 5.008005;                           # optional
     193
     194use strict;                             # not optional
     195use warnings;                           # not optional
     196
     197use Foo qw( $bar );                     # import $bar into our namespace
     198
     199use constant MAXFOO => 3;               # maximum number of foos
     200
     201my $baz;                                # is a lexical variable in the top level scope
    198202
    199203sub fuu
     
    236240
    237241\item
    238 Any script level "globals" declared as lexical variables.  E.g. \code{my $foo}
     242Any script level ``globals'' declared as lexical variables.  E.g. \code{my
     243$foo}
    239244
    240245\item
     
    248253A \code{__END__} token to instruct the perl parser to stop looking for
    249254executable code.
    250 
    251 \item
    252 Any Pod documentation about the module.
    253255\end{itemize}
    254256
     
    263265package Foo;
    264266
    265 use 5.008005;   # optional
    266 
    267 use strict;     # not optional
    268 use warnings;   # not optional
    269 
    270 use base qw( Baz );
    271 
    272 use Foo qw( $bar );
    273 
    274 use constant MAXFOO => 3;
    275 
    276 my $baz;
     267use 5.008005;                           # optional
     268
     269use strict;                             # not optional
     270use warnings;                           # not optional
     271
     272use base qw( Baz );                     # become a subclass of Baz
     273
     274use Foo qw( $bar );                     # import $bar into our namespace
     275
     276use constant MAXFOO => 3;               # maximum number of foos
     277
     278my $baz;                                # is a lexical variable in the top level scope
    277279
    278280sub fuu
     
    284286
    285287__END__
    286 
    287 ...
    288 \end{verbatim}
    289 
    290 \subsubsection{Perl Documentation}
    291 
    292 In most cases documentation should be included as ether in-line Pod or after
    293 the \code{__END__} token in a \code{.pl} file.  The format for documentation
    294 embedded in a \code{.pl} file after the \code{__END__} token or as a stand
    295 alone file is as follows:
     288\end{verbatim}
     289
     290\subsubsection{Plain Old Documentation}
     291
     292This is the format for documentation in a \code{.pl} file after the
     293\code{__END__} token and the \code{.pod} file that should accompany a
     294a module (\code{.pm}).
    296295
    297296e.g. for file Foo.pod:
     
    412411\item Align the new line with the beginning of the expression at the
    413412same level on the previous line\footnote{For emacs users, this means
    414 the indentation that \code{<tab>} produces}.
     413the indentation that \code{<tab>} generally produces}.
    415414
    416415\item If the above rules lead to confusing code or to code that's
     
    432431\end{verbatim}
    433432
    434 Following are two examples of breaking an arithmetic expression. The
    435 first is preferred, since the break occurs outside the parenthesized
    436 expression, which is at a higher level.
     433Following are two examples of breaking an arithmetic expression. The first is
     434preferred, since the break occurs outside the parenthesized expression, which
     435is at a higher level.  The second is demonstrating a statement being broken at
     436too low a level.
    437437
    438438\begin{verbatim}
    439439    $longName1 = $longName2*($longName3 + $longName4 - $longName5) +
    440         4*$longname6;                    # PREFER
     440        4*$longname6;                   # PREFER
    441441   
    442442    $longName1 = $longName2*($longName3 + $longName4 -
     
    445445
    446446Here are three acceptable ways to format ternary expressions:
     447
    447448\begin{verbatim}
    448449$alpha = (aLongBooleanExpression) ? $beta : $gamma; 
     
    506507code to make it clearer.
    507508
    508 Comments should not be enclosed in large boxes drawn with asterisks or
    509 other characters.
     509Comments should not be enclosed in large boxes drawn with hashes or other
     510characters.
    510511\hfil\break
    511512Comments should never include special characters such as form-feed and
     
    531532\subsubsection{Multi-Line Comments}
    532533
    533 Merely a repetition of single-line comments.
     534Merely a repetition of single-line comments.  Logically distinct but adjoining
     535comments should be separated by an empty comment line.
     536
     537\begin{verbatim}
     538# this is the start of a multi-line comment that continues across a couple of
     539# lines then has a logical separation before another comment
     540#
     541# this is an adjoined comment
     542\end{verbatim}
    534543
    535544\subsubsection{Trailing Comments}
     
    607616Do not put different types on the same line. Example:
    608617\begin{verbatim}
    609 my ($foo, @fooArray);                  # WRONG!
     618my ($foo, @fooArray);                  # AVOID!
    610619\end{verbatim}
    611620
     
    621630sub mySubroutine
    622631{
    623     local $count;
     632    local $count;                       # AVOID!
    624633    ...
    625634}
     
    664673
    665674\item
    666 Closing brace \CODE.}. starts a line by itself indented to match its
    667 corresponding opening statement, except when it is a null statement the
    668 \CODE.}. should appear immediately after the \CODE.{..
     675Closing brace \CODE.}. starts a line by itself, except when it is a null
     676statement the \CODE.}. should appear immediately after the \CODE.{..
    669677
    670678\item
    671679The last line before the closing brace must explicitly declare a return value.
     680
     681\item
     682
     683Do not nest named subroutines.
    672684\end{itemize}
    673685
     
    695707\end{verbatim}
    696708
    697 An example of a reasonably two-statement line is:
     709An example of a reasonable two-statement line is:
    698710\begin{verbatim}
    699711$foo++; $bar--;
     
    713725indented to the beginning of the compound statement.
    714726
    715 \item Braces are used around all statements, even single statements,
    716 when they are part of a control structure, such as a \code{if-else} or
    717 \code{for} statement. This makes it easier to add statements without
    718 accidentally introducing bugs due to forgetting to add braces.
    719 \end{itemize}
    720 
    721727\subsection{return Statements}
    722728
     
    734740\end{verbatim}
    735741
    736 Note:  In absence of an explicit return statement Perl will return the results
    737 of the last statement executed.  A null \code{return} statement causes the
    738 result of the last statement executed to be returned.
    739 
    740 \subsection{if, if-else, if elsif else Statements}
    741 
    742 \code{if} statements must always use braces \{\} around statements and parenthesis
     742Note:  In the absence of an explicit return statement Perl will return the
     743results of the last statement executed.  A null \code{return} statement also
     744causes the result of the last statement executed to be returned.  Relying on
     745this behavior is likely to confuse maintenance programmers and therefore should
     746be avoided.
     747
     748\subsection{if, if-else, if-elsif-else Statements}
     749
     750\code{if} statements must always use braces \{\} around statements and parentheses
    743751() around conditionals.
    744752
     
    777785
    778786\begin{verbatim}
    779 for (my $i =0; $i < 100; $i++) {
     787for (my $i = 0; $i < 100; $i++) {
    780788    print "$i\n";
    781789}
     
    841849\end{verbatim}
    842850
    843 \subsection{label Statements}
     851\subsection{Label Statements}
    844852
    845853Code labels should be indented to align with the previous level of
     
    983991
    984992Constant names should be in all capital letters and highly descriptive.  The
    985 \code{constant} pragma should be used to declare constants.  Note: 'constants'
     993\code{constant} pragma should be used to declare constants.  Note: `constants'
    986994created this way can not be interpolated inside of double quoted strings.
    987995
     
    989997\code{use constant PI => 3.14159265;}\hfil\break
    990998\code{use constant LENGTHS => qw(2, 4, 8);}\hfil\break
    991 \code{use constant CLASSES => (raw => 1, processed => 2);}\hfil\break
     999\code{use constant CLASSES => (a => 1, b => 2);}\hfil\break
    9921000
    9931001\\
     
    10001008&
    10011009
    1002 \code{package Class::Data::Inheritable;}\hfil\break
    1003 \code{package Time::HiRes;}\hfil\break
    1004 \code{package XML::SimpleObject;}\hfil\break
     1010\code{package PS::Metadata;}\hfil\break
     1011\code{package PS::Modules::Debias;}\hfil\break
     1012\code{package PS::Modules::FlatField;}\hfil\break
    10051013
    10061014\\
     
    10091017
    10101018Label names should be in all capital letters and short.  Generally they should
    1011 attempt to describe the 'thing' being operated on by the loop they are labeling.
     1019attempt to describe the `thing' being operated on by the loop they are labeling.
    10121020
    10131021&
     
    10981106
    10991107\begin{verbatim}
    1100 if ($a & $b || $c & $d)                 # AVOID!
    1101 if (($a & $b) || ($c & $d))             # RIGHT
     1108if ($a & $b || $c & $d) {}              # AVOID!
     1109if (($a & $b) || ($c & $d)) {}          # RIGHT
    11021110\end{verbatim}
    11031111
     
    11071115
    11081116\begin{verbatim}
    1109 if (($a == $b && $c == $d) || $e == $f)
     1117if (($a == $b && $c == $d) || $e == $f) {}
    11101118$l << ($j + $k)
    11111119($l << $j) + $k
Note: See TracChangeset for help on using the changeset viewer.