Thursday, December 20, 2007

Perl is a relatively unstructured language. Although it does, of course, have rules,
most of the restrictions and rules that you may be used to in other languages are
not so heavily enforced. For example, you don’t need to worry too much about
telling Perl what you are expecting to do with your program (or script or application),
or what variable’s subroutines or other elements you are either going to use or introduce.
This approach leads to what is called “There Is More Than One Way to Do It”
(TIMTOWTDI, or tim toady for short) syndrome—which refers to the fact that there
are different ways of achieving the same result, all of which are legally valid.
In fact, a Perl program is as easy as
print "Hello World\n";
Note that there’s nothing before that statement to tell Perl what it needs in order to
print out that message. Compare it to a similar C program:
#include
int main()
{
printf("Hello World\n");
}
In Perl there is no “main” (well, not in the same sense as there is in C)—execution
of a Perl script starts with the first statement in the list and continues until the end of
the file. We don’t need to tell Perl to explicitly exit the program, or give a return value
to the caller; Perl will handle all of that for us.
The rest of this chapter is given over to describing how to create Perl scripts and
use Perl to execute scripts, and to describing the basic components that make up a Perl
program. The rest of this section of the book is devoted to giving more detail on each of
these elements (and more) so that you will have a complete understanding of how to
write basic Perl programs. The rest of the book looks at more advanced topics, such as
object orientation, networking, and interface and web development.
Installing and Using Perl
Perl is available for a huge array of platforms, but the chances are that you are using Perl
on one of the main three—Unix, Windows, and Mac OS. The use of Perl under all these
platforms varies slightly, so we’ll look at the Perl implementation on each one in turn.
As a basic rule, however, Perl works the same way on every platform—you create
a text file (using your favorite editor: vi, emacs, kedit, Notepad, WordPad, SimpleText,
BBEdit, Pepper), and then use Perl to execute the statements within that file. You don’t
have to worry about compiling the file into another format first—Perl executes the
statements directly from the raw text.Writing a Perl Script
Ignoring the platform-specific issues for a moment, producing a Perl script is not as
difficult as it sounds. Perl scripts are just text files, so in order to actually “write” the
script, all you need to do is create a text file using your favorite text editor. Once you’ve
written the script, you tell Perl to execute the text file you created.
Under Unix, you would use
$ perl myscript.pl
and the same works under Windows:
C:\> perl myscript.pl
Under Mac OS, you need to drag and drop the file onto the MacPerl application.
In each case, Perl reads the contents of your text file, interpreting the file as Perl
statements and expressions.
The file-naming system is part convention and part rule. Generally, Perl scripts
have a .pl extension, even under Mac OS and Unix. This helps to identify what the file
is by name, although it’s actually a requirement. Other extensions you may come
across include .pm for a Perl module, .ph for a Perl header file, and .pod for a Perl
documentation file (POD stands for Plain Old Documentation).
Perl Under Unix
Because much of Perl’s early development originated on a Unix platform, it is not
surprising that this is still one of the most strongly supported Perl environments.
Perl under Unix is available in a number of formats and distributions. The main,
or “core,” distribution comes from the main Perl developers and is available in
precompiled binary and source format. There is also a distribution of Perl that comes
from ActiveState—the original developers of the Windows port of Perl—that comes
as a binary bundled with some additional extensions and the Perl Package Manager
(PPM), an alternative to the CPAN module distributed with the core release. Currently
the ActiveState release is available only for Linux (x86), Solaris, and, of course, the
original Windows.
Installation
Perl is available for Unix in both precompiled binary and source format. Precompiled
binaries can be downloaded, extracted, and then installed without any need to compile
the source code. They are available both as compressed tar archives, RPM (RedHat
Package Manager) packages, and Solaris packages. The best place to get Perl is from the
main Perl website, www. perl.com. You should find links to most binaries on that site.
If you want to compile from the sources (useful if you want to enable certain extensions
and options), then you need to download the source and then configure and compile it.You’ll need a C compiler installed on your system—both commercial environments such as
Sun Microsystem’s Forte for C/C++ and free systems such as GNU CC should work fine.
Once you’ve downloaded the source from www.perl.com, do the following:
1. Extract the source code from the archive using tar and gunzip, for example:
$ $ gunzip -c perl.tar.gz | tar xvf -
2. Change to the newly created directory. It’s worth checking the README and
INSTALL files, which contain general Perl information and specific details on
the installation process, respectively.
3. Run the configuration script:
$ ./configure.gnu
This is, in fact, a GNU-style execution of the real Configure script. The standard
Perl Configure script is interactive, requiring input from you on a number of
questions. The GNU-compatible execution answers the questions automatically
for you, making a number of assumptions about your system, though it still
shows the process going on behind the scenes.
The former GNU style-configuration script will probably install Perl into
/usr/local, with the interpreter and support scripts ending up in /usr/local/
bin and the Perl library being placed into /usr/local/lib/perl5. This obviously
requires suitable access privileges to install the files to this location. You can
change the install directory by using the --prefix command line option:
$ ./configure.gnu --prefix=/home/mc/local
4. Run make to build the application:
$ make
The application and support files have now been compiled. It’s a good idea
at this point to run make test, which will run a standard selection of tests to
ensure that Perl has compiled properly. If there are any problems, you want
to check the build process to see if anything failed. On the mainstream systems,
such as Linux and Solaris, it’s unlikely that you will notice any test failures.
5. Once the build has completed, install the application, scripts, and modules
using make:
$ make install
Remember that the installation prefix will by default be /usr/local/, although
the exact setting will depend on your OS. Providing you didn’t specify
different directories, the usual directory specification will install Perl into the
/usr/local/bin and /usr/local/lib/perl5 directories. You will need to add
/usr/local/bin or the installation directory you chose (specified by the
$installation_prefix/bin variable in the makefile) to your $PATH environment
variable, if it is not already in it.Executing Scripts
There are two ways of executing a Perl script under Unix. You can run the Perl application,
supplying the script’s name on the command line, as in the first example, or you can
place the second example on the first line of the file (called the shebang line),
$ perl myscript.pl #!/usr/local/bin/perl
where the path given is the path to the Perl application. You must then change the file
mode of the script to be executable (usually 0755). You can change the mode using the
chmod command:
$ chmod 755 myscript.pl
Note that it is common to have different versions of Perl on your system. In this
case, the latest version will always have been installed as /user/local/bin/perl, which
is linked to the version-specific file, for example, /user/local/bin/perl5.6.0.
The Perl libraries and support files are installed in $prefix/lib/perl5. Since version
5.004, each version of Perl has installed its own subdirectory such that the actual location
becomes /user/local/lib/perl5/5.6.0/, or whatever the version number is. User-installed
(site-specific) scripts should be placed into /user/local/lib/perl5/site-perl/5.6.0.
Whenever a script is run, unless it has been redirected, standard input, output, and
errors are sent via the terminal or window, the same as in the shell environment, except
in the case of CGI scripts, where standard input is taken from the web server, standard
output is sent back to the browser, and standard error is sent to the web server’s log file.
Installing Third-Party Modules
For most modules (particularly those from CPAN), the installation process is fairly
straightforward:
1. Download the module, and extract it using tar and gunzip, for example:
$ gunzip -c module.tar.gz | tar xf -
This should create a new directory with the module contents.
2. Change to the module directory.
3. Type
$ perl5 Makefile.PL
This will check that the module contents are complete and that the necessary
prerequisite modules are already installed. It will also create a makefile that
will compile (if necessary) and install the module.
As in the original installation process, a make test will verify that the
compilation and configuration of the package works before you come to install
it. You should report any problems to the package’s author.4. To install the module, type
$ make install
This will copy the modules and any required support files into the appropriate
directories.
A better, and less interactive, solution is to use the CPAN module to do
the downloading, building, and installation for you. See Web Appendix B at
www.osborne.com for information on how to use the CPAN module.
Perl Under Windows
Perl has been supported under Windows for some time. Originally, development
concentrated on providing a Windows-compatible version from the core libraries,
and then the development split as it became apparent that providing a lot of the
built-in support for certain functions (notably fork) was unattainable. This lead to
a “core” port and a separate development handled by a company called ActiveWare.
ActiveWare worked on providing not only Perl, but also a suite of extensions that
allowed you to perform most operations normally handled by the built-in functions
that were only supported under Unix.
ActiveWare later became ActiveState, and their changes were rolled back into the
core release. Now there is only one version of the Perl interpreter that is valid on both
platforms, but there are now two distributions. The “core” distribution is identical to
that under Unix, so it comes with the standard Perl library but not the extension set
originally developed under the original ActiveWare development.
ActiveState still provides a prepackaged version of Perl for Windows that includes
the core Perl interpreter and an extended set of modules that include the Perl Package
Manager, a number of Win32-specific modules (see Table 2-1), and some general
extensions like Graham Barr’s libnet bundle and Gisle Aas’s LWP (libwww-perl)
bundle. The main ActiveState Perl distribution is called ActivePerl (and is now also
available under Solaris and Linux x86), but they also supply a number of extras, such
as the Perl Development Kit, which provides a visual package installer and debugger,
and PerlEx, which speeds up execution of Perl scripts when used under Microsoft’s
Internet Information Server.Module Description
Archive::Tar A toolkit for opening and using Unix tar files.
Compress::Zlib An interface for decompressing information entirely
within Perl.LWP Gisle Aas’s Lib WWW Perl (LWP) toolkit. This includes
modules for processing HTML, URLs, and MIMEencoded
information, and the necessary code for
downloading files by HTTP and FTP.
Win32::ChangeNotify Interface to the NT Change/Notify system for monitoring
the status of files and directories transparently.
Win32::Clipboard Access to the global system clipboard. You can add and
remove objects from the clipboard directory.
Win32::Console Terminal control of an MSDOS or Windows NT
command console.
Win32::Event Interface to the Win32 event system for IPC.
Win32::EventLog Interface to reading from and writing to the Windows
NT event log system.
Win32::File Allows you to access and set the attributes of a file.
Win32::FileSecurity Interface to the extended file security options under
Windows NT.
Win32::Internet Interface to Win32’s built-in Internet access system for
downloading files. For a cross-platform solution see
Net::FTP, Net:HTTP or the LWP modules elsewhere
in this appendix.
Win32::IPC Base methods for the different IPC techniques
supported under Win32.
Win32::Mutex Interface to the Mutex (Mutual/Exclusive) locking and
access mechanism.
Win32::NetAdmin Network administration functions for individual
machines and entire domains.
Win32::NetResource Provides a suite of Perl functions for accessing and
controlling the individual Net resources.
Win32::ODBC ODBC interface for accessing databases. See also the
DBI and DBD toolkits.
Win32::OLE Interface to OLE automation.
Win32::PerfLib Supports an interface to the Windows NT
performance system.Module Description
Win32::Pipe Named pipes and assorted functions.
Win32::Process Allows you to create manageable Win32 processes
within Perl.
Win32::Registry Provides an interface to the Windows registry. See the
Win32API::Registry module and the Win32::TieRegistry
module for a tied interface.
Win32::Semaphore Interface to the Win32 semaphores.
Win32::Service Allows the control and access of Windows NT services.
Win32::Shortcut Access (and modification) of Win32 shortcuts.
Win32::Sound Allows you to play .WAV and other file formats within
a Perl script.
Win32::TieRegistry A tied interface to the Win32 registry system.
Win32::WinError Access to the Win32 error system.
Win32API::Net Provides a complete interface to the underlying
C++ functions for managing accounts with the
NT LanManager.
Win32API::Registry Provides a low-level interface to the core API used for
manipulating the registry.Installation
There are two ways of installing Perl—the best and recommended way is to download
the ActivePerl installer from www.activestate.com, run the installer, and then reboot your
machine. This will do everything required to get Perl working on your system, including
installing the Perl binary, its libraries and modules, and modifying your PATH so that
you can find Perl in a DOS window or at the command prompt. If you are running Perl
under Windows NT or Windows 2000, or are using Microsoft’s Personal Web Server for
Windows 95/98/Me, then the installer will also set up the web server to support Perl as
a scripting host for web development. Finally, under Windows NT and Windows 2000,
the ActivePerl installer will also modify the configuration of your machine to allow Perl
scripts ending in .pl to be executed directly—that is, without the need to pass the script
names to Perl beforehand.The alternative method is to compile Perl from the core distribution. Although
some people prefer this version, it’s important to note that core distribution does not
come with any of the Win32-specific modules. You will need to download and install
those modules separately.
If you want to install a version of the Perl binary based on the latest source code,
you will need to find a C compiler capable of compiling the application. It’s then a case
of following the instructions relevant to your C and development environment. The
supported C compilers are described here. Other versions and C compilers may work,
but it’s not guaranteed.
 Borland C++, version 5.02 or later: With the Borland C++ compiler, you
will need to use a different make command, since the one supplied does
not work very well and certainly doesn’t support MakeMaker extensions.
The documentation recommends the dmake application, available from
http://www-personal.umich.edu/~gsar/dmake-4.1-win32.zip.
 Microsoft Visual C++, version 4.2 or later: You can use the nmake that comes
with Visual C++ to build the distribution correctly.
 Mingw32 with EGCS, versions 1.0.2 and 1.1, or Mingw32 with GCC, version
2.8.1: Both EGCS and GCC supply their own make command. You can
download a copy of the EGCS version (preferred) from ftp://ftp.xraylith.
wisc.edu/pub/khan/gnu-win32/mingw32/. The GCC version is available
from http://agnes.dida.physik.uni-essen.de/~janjaap/mingw32/.
Also, be aware that Windows 95/98 as a compilation platform is not supported.
This is because the command shell available under Windows 95/98 is not capable of
working properly with the scripts and make commands required during the building
process. The best platforms for building from the core source code are Windows NT or
Windows 2000 using the standard cmd shell.
In all cases, ignore the Configure utility that you would normally use when compiling
under Unix and Unix-like operating systems. Instead, change to the win32 directory
and run the make command for your installation. For example:
c:\perl\win32> dmake
For Microsoft’s Visual C++, you will need to execute the VCVARS32.BAT batch file,
which sets up the environment for using Visual C++ on the command line; for example:
c:\perlsrc\win32>c:\progra~1\micros~1\vc98\bin\vcvars32.bat
You may need to increase the environment memory on your command.com for
this batch file to work properly—you can do this by modifying the properties for theMS-DOS Prompt shortcut. Select the shortcut within the Start menu, and then choose
the Program tab. You should modify the “Cmd Line” field to read
C:\WINDOWS\COMMAND.COM /E:4096
This boosts the environment memory for the command prompt up to 4K—more than
enough for all the variables you should need.
Remember that compiling and installing Perl from the source distribution does not
give you the integration facilities or modules that are included as standard within the
ActiveState version.
You will need to manually update your PATH variable so that you have access to
the Perl interpreter on the command line. You can do this within Windows 95/98 by
modifying the AUTOEXEC.BAT file. You will need to add a line like
SET PATH=C:\PERL\BIN\;%PATH%
This will update your search path without replacing the preexisting contents. The
C:\PERL\BIN\ is the default install location; you should modify this to wherever
you have installed the binary.
On Windows NT/2000, you will need to update the PATH variable by using the
System control panel.
Executing Scripts
Once installed correctly, there are two basic ways of executing a Perl script. You can
either type
C:\> perl hello.pl
in a command window, or you can double-click on a script in Windows Explorer. The
former method allows you to specify command line arguments; the latter method will
require that you ask the user for any required information.
Under Windows NT, if you want a more Unix-like method of executing scripts,
you can modify the PATHEXT environment variable (in the System control panel)
to include .pl as a recognized extension. This allows you to call a script just like any
other command on the command line, but with limitations. The following will work:
C:\> hello readme.txt
However, redirection and pipes to the Perl script will not work. This means that the
following examples, although perfectly valid under Unix, will not work under Windows:C:\> hello C:\> hello readme.txt|more
The other alternative, which works on all Windows platforms, is to use the pl2bat
utility. This wraps the call to your Perl script within a Windows batch file. For
example, we could use it to convert our hello.pl utility:
C:\> pl2bat hello.pl
C:\> hello
The big advantage here is that because we are using batch file, it works on any Windows
platform, and we can even add command line options to the Perl interpreter within the
batch file to alter the behavior. Furthermore, pipes and redirection work correctly with
batch files, which therefore also means the options work with our Perl script.
If you want to specify any additional command line options, you can use the normal
“shebang” line (#!) to specify these options. Although Windows will ignore this line,
the Perl interpreter still has to read the file, and so it will extract any information from
the line that it needs. So, for example, to turn warnings on within a script, you might
use a line such as
#!perl -w
Note that you must still comment out the line using a leading hash character.
Installing Third-Party Modules
Although it’s possible to use the CPAN module to do the installation for you, it requires
access to the make command and often a C compiler in order for it to work properly.
Instead, ActivePerl comes with the Perl Package Manager (PPM). This works along the
same basic premise as the CPAN module, except that PPM modules are precompiled
and ready to be installed—all the PPM tool actually does is copy the files downloaded
in a given package into their required location.
Using PPM is very easy. You start PPM from the command line:
C:\> ppm
PPM interactive shell (1.1.1) - type 'help' for available commands.
PPM>
Once there, you use search to find a suitable package, and install to install it.
For example, to install the Tk interface module,
C:\> ppm
PPM interactive shell (1.1.1) - type 'help' for available commands.
PPM> install TkAnd you then let PPM install the files for you. The number of PPM files is smaller than
CPAN, largely because the modules on CPAN are uncompiled, and those for use under
PPM need to be precompiled. To add to the headaches for developers many of the
CPAN packages rely on libraries and functions only available under Unix.
PPM packages are stored in a number of repositories. The main repository is at
ActiveState, but others are available.

No comments: