Program and Subroutine ParametersThe main part of any Perl script is sometimes known as package main, and just like any Perl package it can have subroutines. Calling these routines is straightforward. An example, displayed in Figure A-3, is broken down as follows:
Figure A-3. @ARGV and @_Environmental Variable AccessA Perl script can also make use of environmental variables. These are stored within the built-in %ENV hash: $old_oracle_home = $ENV{ORACLE_HOME}; # Store latest ORACLE_HOME $ENV{ORACLE_HOME} = "C:\ORANT"; # Now set new ORACLE_HOME Variable TypesProduction Perl code generally starts off with the following line: use strict; This pragma disciplines the naming of Perl's two main types of variable:
Think of package variables as being the major chess pieces, one set for the black package and one set for the white, with $black::king being entirely different from $white::king. Think of the lexical my variables as being more like pawns, useful to a particular package but generally disposable. However, as we'll see later in our discussion of object orientation, even humble my variables can be vital for object orientation - in the same way that a lowly pawn can decide chess games by reaching the opposite package's back line and becoming a knight or a queen. The our prefix, introduced in Perl 5.6, mimics the my syntax, but defines globals rather than lexically scoped variables. It's a way of disguising package variables from the discipline of the use strict pragma, often to make your code look cleaner by avoiding full package name qualification. Aside from instances like this, where it is assumed that you know what you're doing, the use strict pragma will insist that you employ either fully qualified package variable names or lexically declared variables. Think of our as being like a bishop disguised as a pawn. Because it takes other pieces diagonally, a bishop is sometimes used to hold up pawn defenses, but is still a major piece possessing lethal power.[2]
Taint ModeAs well as use strict, you can also run your program with extra warnings to detect syntax ambiguities, unused variables, and that sort of thing. You can turn these warnings on via either the -w flag, or (in Perl Version 5.6 onwards) the use warnings pragma. For instance, the following code at the top of a program will turn on extra warnings: #!/usr/local/bin/perl -w use strict; Alternatively, use the more modern form: #!/usr/local/bin/perl
use warnings;
use strict;
To go beyond warnings in certain classes of programs, you must use taint mode. This mode works on the simple principle that nothing derived from outside your program should be allowed to change anything else held outside your program. All data is checked in taint mode, and the tainted variety usually includes @ARGV program parameters, %ENV environmental variables, and any file input. Anything else that uses tainted data also becomes tainted. You turn taint mode on with the following -T switch: #!/usr/local/bin/perl -T There are many mechanisms within Perl for laundering tainted data, but they all work on the basic assumption that you know what you're doing before you untaint such data. All CGI scripts should use taint mode, as should any other program being accessed remotely, especially via the Internet. You should also consider taint mode for any kind of daemon, or indeed any other kind of program that deals with external users or sensitive data. |