Table of Contents


Introduction

This book covers Perl. All of it. It begins with the basics—how to write loops, open files, and so forth—and progresses through advanced topics such as networking, generating graphics, and linking Perl and C. Over 400 self-contained and ready-to-run programs are provided.

There are 14 chapters, each of which is subdivided into eight sessions. Every session has a quiz and a programming exercise, and every chapter has four additional exercises at the end. If you read the book from start to finish, you’ll know nearly everything there is to know about Perl, including how to find other Perl resources on the Internet and how to create your own network and World Wide Web services. You’ll also know tips and tricks for manipulating e-mail, PostScript, the X window system, and database packages. You won’t find this combination in any other book.

Perl is huge and eclectic, with hundreds of functions and operators, many of which might seem arcane to some readers. To use them (and use them well), you’ll need to know a bit about how computers work. So this book is, in part, about computers in general; Perl just happens to be the vehicle for instruction.

First things first: Get a copy of Perl 5. Hey, it’s free, and exists for nearly every platform imaginable. If you have Internet access, you can grab a copy from one of the FTP or Web sites in Appendix B, including any of the CPAN (Comprehensive Perl Archive Network) sites. Be sure to get the latest version, which at the time of this writing is 5.002. If you don’t have Internet access, you can get Perl via e-mail (send a message containing the word “help” to ftpmail@decwrl.dec.com), or via CD-ROM from Walnut Creek (1-800-786-9907, or http://www.cdrom.com).

To determine what version of Perl you have, launch Perl with the -v flag. If you’re on a computer that uses a command line, type the following: perl -v to have Perl display its version number. If it’s less than 5.002, you’ll want to upgrade. If it’s less than 5.000, you’ll need to upgrade. (Perl 5 is largely a superset of Perl 4, with some additional features: object-oriented programming, dynamically loadable modules, lexical scoping, and some additional goodies. You’ll find out about all of these in this book.)

Whether this book whets or satiates your appetite depends on you. If you find yourself wanting to know more about a topic, consult the documentation bundled with the Perl distribution, and read the Perl FAQ (Frequently Asked Questions list), available from ftp://rtfm.mit.edu/pub/usenet-by-group/comp.lang.perl.misc. When all else fails, post to one of the USENET newsgroups (comp.lang.perl.misc, comp.lang.perl.modules, comp.lang.perl.guts, comp.lang.perl.tk), which are read by hundreds of helpful hackers.

Let’s get started!

Installation

Perl 5 Interactive Course is a comprehensive Perl 5 training kit, including thorough instruction in the book, resources provided on CD, and available on-line support services courtesy of The Waite Group’s eZone.

About the CD

A CD-ROM is included at the back of Perl 5 Interactive Course. It contains all the scripts for the projects developed in the book as well as some other useful tools for Perl programmers. Table I-1 depicts the root directory content and organization of the accompanying CD.

Table I-1 The Perl 5 Interactive Course CD-ROM Directory Tree
This Directory Contains
\SOURCE
The scripts and code for the projects constructed in the book. They’re organized according to Chapter and Session so that you can quickly find the ones you want.
\PERL5 The PERL 5 binaries. There are separate directories for UNIX, Windows 95/Windows NT, and DOS/Windows 3.1 versions.
\CPAN The contents of the Comprehensive Perl Archive Network, which includes tools, scripts, resources, and other valuable materials. (The contents of this directory may not be readable by DOS and/or Windows 3.1 machines.)
\NTMANAGE The contents of the NetManage Chameleon Sampler, a suite of Internet-related tools for developers and surfers.
\EZONE An interactive demo of Waite Group Press’s eZone. (You need Netscape 2 or later or Microsoft Internet Explorer 3 or later to run it.)

Be sure to check the CD for a README file, which would contain any updates affecting the CD material, the eZone, and/or the book, which was not available when Perl 5 Interactive Course went to press.

Installing the Perl 5 Binaries

Perl 5 binaries for UNIX, Windows 95, Windows NT, Windows 3.1, or DOS operating systems are included and installation information for these different versions is provided here.

UNIX Installation

The UNIX version of the Perl binaries has its own installation routine. You’ll find the compressed binaries on the CD in the \PERL5\UNIX directory. To install the binaries on a local hard drive, type

zcat/cdrom/perl5/unix/perl5.002.tar.gz | tar xvf -

The files will begin uncompressing. When they’re done, type

cd/perl5.002

Configure -d. The -d allows the configuration program to answer many, if not all, of the configuration questions automatically (you may still need to answer some questions). After the configuration program has finished, type

make

If the process fails, run Configure again, this time without the -d option. Once make has run, type

install perl

When you see the message “Perl has been installed successfully,” you are finished.

Windows 95 and Windows NT Installation

For those running Windows 95 or Windows NT, a 32-bit Windows version of the Perl binaries on the CD is in the \PERL5\WIN95-NT directory.

Windows 95

Use Windows Explorer to create a directory on your hard disk root directory called \PERL5; then drag the contents of the CD’s \PERL5\WIN95-NT subdirectory to the newly created \PERL5 directory. Now create another directory in the \PERL5 directory on your hard disk, called \BIN. Copy the following files from the hard disk \PERL5 directory to the newly created \PERL5\BIN subdirectory: perl.exe, perlglob.exe, pl2bat.bat, and mkolex.bat.

Update your Windows 95 system registry, using Regedit to create the following key (for information about using Regedit, see your Windows 95 manual):

\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Resource Kit\PERL5

Update the key with the following values:

BIN = C:\Perl\Bin
HTML-DOCS = C:\Perl\5.001\docs
PRIVLIB = C:\Perl\5.001\lib

Edit your AUTOEXEC.BAT file and add C:\PERL5\5.001 to the beginning of your PATH statement, then restart Windows 95 for your changes to take effect.

Windows NT

There is an installation batch file for Windows NT. Use the file manager to run the installer INSTALL.BAT, which is located on the CD in the \PERL5\WIN95-NT\5.001. If INSTALL.BAT is unsuccessful, create a directory off your root called \PERL\BIN. Copy the following files for Windows NT to this directory: perl.exe, perlglob.exe, pl2bat.bat, and mkolex.bat.

Windows 3.x and DOS Installation

A directory on the CD named \DOS-WIN3 contains the Perl 5 binaries for those running either DOS or Windows 3.x machines.

DOS

At the DOS Prompt, create a directory on your root drive by typing

cd\ <Enter>
md perl5<Enter>
cd perl5 <Enter>

Now copy the contents of the CD’s \PERL5\DOS-WIN3 directory into the newly created \PERL5 directory by typing

xcopy d:\perl5\dos-win3\perl5.000\*.* /v /s <Enter>

The /v and /s switches verify and copy subdirectories, respectively.

Windows 3.x

Use the File Manager to create a directory called PERL5 on the root directory of your system hard disk; then drag the contents of the CD directory \PERL5\DOS-WIN3 into the newly created \PERL5 directory on the hard drive.

Windows 3.x users need to run Perl from the DOS command line. You can get to DOS by clicking on the MS-DOS icon, which originally installs in Main program group.

Updating AUTOEXEC.BAT for Windows 3.x and DOS

Once you have copied the contents of \PERL5\DOS-WIN3 to your hard drive, you’ll need to update the system’s AUTOEXEC.BAT file. Open your AUTOEXEC.BAT file with a standard text editor, such as MS-DOS EDIT or Windows Write. At the end of your PATH statement add

C:\PERL5;

Save the change and exit the text editor, then reboot your machine.

The Chameleon Sampler

The NetManage Internet Chameleon is one of the most versatile and easy-to-use set of Internet tools in the world. Chameleon helps you sign up with an Internet provider, connect cleanly to the Internet, and access a variety of resources—including a pretty cool Web browser. The Chameleon package includes

  Custom, for connecting to the Internet
  WebSurfer, a full-featured World Wide Web browser
  Gopher, which lets you access any gopher menu worldwide
  NEWTNews, a Usenet newsreader
  Mail, a convenient way to send and receive e-mail
  Archie, which lets you search for a file over the Internet
  Telnet, for connecting to a remote computer
  FTP, for transferring files over the Internet
  FTP Server, which lets you allow others to download or upload files to your PC
  Mail Utilities, programs that help you compact or organize your mailbox files to save space
  Ping, to test if you’re connected to a remote computer
  Finger, to check if a friend is connected to the Internet
  Whois, to get information about people registered in the NIC (Network Information Center) database

You can sample the Chameleon tools for 30 days at no charge. If you like what you see, you can register everything for 50 bucks.

Installing the Chameleon


NOTE:  In the installation directions here, we assume that your hard disk is the C: drive and your CD-ROM is the D: drive. If this doesn’t match your computer, substitute C: or D: with the correct drive designation.

To copy the sampler software onto your hard disk, run the Setup program. While under Windows, select File, Run in the Program Manager. In the Run dialog box, type

d:\windows\browsers\ntmanage\disk_1\setup.exe

and then press the OK button.

The Setup program will ask you where to install the NetManage program. The default suggested is fine for most people. If you want it installed elsewhere, type in the drive and directory of your choosing and select Continue.

After a few moments, the Setup program will ask you to type in the path of the second batch of files. Select the 1 in DISK_1 and change it to 2, and select Continue.

After another few moments, the Setup program will ask you to type in the path of the third batch of files. Select the 2 in DISK_2 and change it to 3, and select Continue.

Click OK when Setup tells you that installation is complete. You are now ready to set up your Internet account!

Signing Up for an Internet Provider Account

If you don’t already have one, the Chameleon package makes it easy to sign up with one of several popular Internet providers. If you’d like to sign up using the Chameleon software, run the Automatic Internet-Click to Start icon.

To learn about a particular Internet provider, click one of the tabs (other than NetManage) in the Select Internet Provider window. Most providers give you several hours (or even a month) of free trial time. To read about the locations an Internet provider can cover, the monthly price, and other important information, click the More Info button at the bottom of the screen. If you have specific questions, contact the provider directly.

When you’re ready to begin the sign-up procedure, click the Signup button. You’ll see a registration screen similar to the one in Figure I-1. Fill in your name (as it appears on your credit card), address, phone number, and credit card information.


NOTE:  You will not actually be charged any provider fees until you officially register with the service. You can cancel the registration transaction at any time during the sign-on process. If you do decide to register, your credit card number will be sent over a secure phone line.

As you work through the sign-up process, there may be other tabs asking for additional information. If so, click these tabs and fill in the forms.

Select the Phone List button at the bottom of the screen. The Phone List dialog appears, listing possible phone numbers you can use to register. If one of the numbers is in your area code, select it. Otherwise, select the toll-free 800 number.


NOTE:  If necessary, you can edit the registration phone number. Some systems, for example, require you to dial a 9 to reach an outside line. Just type in this 9.

When you’ve typed in all your vital stats, return to the first registration tab. Click Send to dial the toll-free number and begin the registration process. The icons to the right will light up as each stage of the dialing process is completed. The set of traffic lights tell you if each stage—initializing the modem, dialing, connecting, and communicating—has worked.


You may need to click the Advanced button to specify special modem ports or commands.

Follow the instructions that appear as the registration proceeds. You will be given the option to select from various service and pricing plans. Your account information (username, e-mail address, password, dial-up number, and IP address) will automatically be configured into the Chameleon package. An interface will be created for the Custom program, which quickly and flawlessly connects you to the Internet.

That’s it! You can now reboot your system to kick-start everything.

Registering the Chameleon Software

If you already have an Internet account, you can set up the Internet Chameleon software (shown in Figure I-2) and start using it within minutes. Run the Automatic Internet-Click to Start program.

Make sure the NetManage tab is selected, and then click the Signup button. You can now activate the software for a free 30-day demonstration period. After this period, the Chameleon software will no longer work. If you decide to register the Chameleon package (for $50), your credit card will be charged and your software will be activated permanently.

Fill in all the information on both forms, as shown in Figure I-1, including your credit card number (it won’t be charged unless you complete the registration). You may need to contact your Internet provider for the Internet information on the second form.

Select the Phone button, and choose a local or toll-free phone number. Then click the Send button to dial in to NetManage and get your software activated.

Once you connect, you are given the following choices:

  Activate your software for a free 30-day demonstration.
  Purchase your software to activate it permanently.
  Configure your connection (if your Chameleon software has already been activated).

Connecting to the Internet

Now that you have selected a provider and registered your software, you can actually get hooked in to the Internet. To do this, you need to run the Custom program (Figure I-3) from Windows File Manager.

If you used the Chameleon package to sign up with your Internet provider, an automatic configuration file should have already been written for you. Otherwise, Chameleon comes with the configurations for most popular Internet providers. Select File, Open and look for the configuration file for your provider. If your provider is not listed, you’ll need to contact them and ask what the proper settings are. They may even be able to send you a prewritten Chameleon configuration file.

If you do need to enter the connection settings yourself, use the appropriate values you have obtained from your Internet provider. You can verify or edit the following information under the Setup menu:

  IP Address
  Subnet Mask
  Host Name
  Domain Name
  Port
  Modem
  Dial
  Login
  Interface Name
  BOOTP

You may also need to fill in the following under the Services menu:

  Default Gateway
  Domain Servers

Read Chapter 1 for more information about these terms.

Logging In

Once your configuration settings are in place, simply click the Connect menu to dial up your Internet provider and get connected. If all goes well, you should hear a small beep, and a program known as Newt will run. This program lets Windows communicate with the Internet. You can then minimize the Custom program and run the Internet application of your choice.

Logging Out

When you’re done using the Internet, call up the Custom program and click the Disconnect menu.

WEB BROWSING with WebSurfer

WebSurfer is a full-featured World Wide Web browser similar to Mosaic. You can read all about browsers in Chapter 2 and about Mosaic in Chapter 5. To start exploring the Web, first use the Chameleon Custom program to connect to the Internet. Then run the WebSurfer program.

Like Mosaic, WebSurfer has a toolbar (see the top of Figure I-4) that acts as a shortcut for most commands. The toolbar contains

  Show Connection Status: Shows you which links are currently being loaded.
  Go to URL: Opens a specific Web URL (defined in Chapter 1).
  Get URL: Reloads the current document.
  Hotlist: Shows the list of your favorite Web pages for you to choose from. To go to a page, just double-click on it. You can also delete pages from the list by selecting the page and clicking Remove.
  Make Hot: Adds the current Web page to your hotlist.
  Back: Revisits the Web page you just came from.
  Forward: Goes to the next Web page in the series, if applicable.
  Home: Returns to the Web page you started from.
  Cancel All: Stops the loading of the current Web page.

Loading a Web Page from the Internet

Like Mosaic, WebSurfer combines text and graphics on the same page. Any text in blue or graphics with a blue border are hypertext links to other Web pages, multimedia files, or Internet areas. To load a link, just click on it.

You can also load up a document directly. Just select Retrieve, Go To URL and type in the document’s exact URL. Alternatively, you can type a document’s URL in the Dialog bar’s URL box and press [ENTER] to load it.

If the document is a Web page, it will be displayed. If the document is a graphic, sound, or movie, the WebSurfer program will attempt to call up a viewer program to display/play it. If the document is any other type of multimedia file, WebSurfer allows you to save the document directly to your hard disk.

To find out more about the current Web document, select Retrieve, Properties.

Loading a Web Page from Your Hard Disk

If you have any Web pages on your hard disk (perhaps ones that you’ve created yourself), you can easily use WebSurfer to view them. Select Retrieve, Open Local File. Choose the file you want to view and click OK.

You can even edit the current Web document—a very handy capability for Web developers. Select Retrieve, Edit HTML. Then access the Retrieve, Refresh From Disk menu item to reload the page in a flash and see what your edits look like.


Figure I-4  The WebSurfer browser in all its glory


Figure I-3  Your customized on-ramp onto the Information Superhighway


Figure I-2  The full Chameleon package in the Internet Chameleon program group


Figure I-1  The easiest way to sign up for an Internet provider


Table of Contents