IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 2539


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

add subroutine prototypes and Params::Validate

File:
1 edited

Legend:

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

    r2484 r2539  
    1 %%% $Id: perlCodeConventions.tex,v 1.38 2004-11-25 03:39:17 jhoblitt Exp $
     1%%% $Id: perlCodeConventions.tex,v 1.39 2004-11-30 22:27:21 jhoblitt Exp $
    22\documentclass[panstarrs]{panstarrs}
    33
     
    11801180\end{verbatim}
    11811181
     1182\subsubsection{Subroutine Prototypes}
     1183
     1184Do not use subroutine prototypes.  If you need to validate subroutine
     1185parameters see \code{Params::Validate} (\S\ref{Params::Validate}).
     1186
     1187\begin{verbatim}
     1188    sub myopen (*;$)                    # AVOID!
     1189\end{verbatim}
     1190
     1191
    11821192%------------------------------------------------------------------------------
    11831193\appendix                               %Begin Appendices
     
    12111221\verbatiminput{.perltidyrc}
    12121222
     1223\section{Params::Validate}
     1224\label{Params::Validate}
     1225
     1226\code{Params::Validate}\footnote{Params::Validate -
     1227http://search.cpan.org/~drolsky/Params-Validate/} is a Perl module that allows
     1228you to perform rigorous subroutine parameter validation.  It is highly
     1229configurable and validation may be deactivated at runtime
     1230
     1231The basic premise is that a validation specification is passed to one of
     1232several ``validating'' subroutines along with the parameters the subroutine was
     1233called with.
     1234
     1235An example \code{Params::Validate} named parameters validation spec taken from
     1236the \code{DateTime} module.
     1237
     1238\begin{verbatim}
     1239    my $BasicValidate =
     1240        { year   => { type => SCALAR },
     1241          month  => { type => SCALAR, default => 1,
     1242                      callbacks =>
     1243                      { 'is between 1 and 12' =>
     1244                        sub { $_[0] >= 1 && $_[0] <= 12 }
     1245                      },
     1246                    },
     1247          day    => { type => SCALAR, default => 1,
     1248                      callbacks =>
     1249                      { 'is a possible valid day of month' =>
     1250                        sub { $_[0] >= 1 && $_[0] <= 31 }
     1251                      },
     1252                    },
     1253          hour   => { type => SCALAR, default => 0,
     1254                      callbacks =>
     1255                      { 'is between 0 and 23' =>
     1256                        sub { $_[0] >= 0 && $_[0] <= 23 },
     1257                      },
     1258                    },
     1259          minute => { type => SCALAR, default => 0,
     1260                      callbacks =>
     1261                      { 'is between 0 and 59' =>
     1262                        sub { $_[0] >= 0 && $_[0] <= 59 },
     1263                      },
     1264                    },
     1265          second => { type => SCALAR, default => 0,
     1266                      callbacks =>
     1267                      { 'is between 0 and 61' =>
     1268                        sub { $_[0] >= 0 && $_[0] <= 61 },
     1269                      },
     1270                    },
     1271          nanosecond => { type => SCALAR, default => 0,
     1272                          callbacks =>
     1273                          { 'cannot be negative' =>
     1274                            sub { $_[0] >= 0 },
     1275                          }
     1276                        },
     1277          locale    => { type => SCALAR | OBJECT,
     1278                         default => undef },
     1279          language  => { type => SCALAR | OBJECT,
     1280                         optional => 1 },
     1281        };
     1282\end{verbatim}
     1283
     1284An example of how a validation spec is used in the \code{DateTime} module.
     1285
     1286\begin{verbatim}
     1287    use Params::Validate qw( validate validate_pos SCALAR BOOLEAN HASHREF OBJECT );
     1288    .
     1289    .
     1290
     1291    sub new
     1292        {
     1293            my $class = shift;
     1294            my %p = validate( @_, $NewValidate );
     1295    .
     1296    .
     1297\end{verbatim}
     1298
     1299Please see the \code{Params::Validate} Pod\footnote{Params::Validate Pod -
     1300http://search.cpan.org/~drolsky/Params-Validate/lib/Params/Validate.pm} for
     1301further details.
     1302
    12131303\bibliographystyle{plain}
    12141304\bibliography{panstarrs}
Note: See TracChangeset for help on using the changeset viewer.