22,99 €
In the days before personal computers, BASIC was the easy programming language to learn, and serious programmers learned FORTRAN or COBOL to do "real work." Today, many people have discovered that Perl is both a great beginning programming language and one that enables them to write powerful programs with little effort. If you're interested in discovering how to program (or how others program), Perl For Dummies, 4th Edition, is for you. If you already know something about programming (but not about Perl), this book is also for you. If you're already an expert programmer, you're still welcome to read this book; you can just skip the basic stuff (you never know what kind of new tips and tricks you'll pick up). This reference guide shows you how to use Perl under many different operating systems, such as UNIX, many flavors of Windows (Windows 95/98, Windows NT, Windows 2000, Windows Me, and Windows XP), and Macintosh OS 9 and OS X; in fact, Perl runs on many more operating systems than these. Here's a sampling of what Perl For Dummies, 4th Edition, has to offer: * Installing Perl on various platforms * Nailing down the basics of building Perl programs * Working with text and numbers * Constructing lists and working with them * Creating conditionals and loops * Delving into more advanced features such as operators and functions * Reading and writing files and directories * Using subroutines for modularity * Demystifying Web server programs * Creating your own Internet clients The Perl programming language enables you to write fully working computer programs with just a few steps. It's particularly good at common programming tasks, such as reading and writing text files, but it also excels at reducing the work that programmers have to do. Perl For Dummies, 4th Edition, shows you how to do all of that and how to modify programs to your heart's content. After all, one of the common phrases in the world of Perl programmers is, "There's more than one way to do it."
Sie lesen das E-Book in den Legimi-Apps auf:
Seitenzahl: 535
Veröffentlichungsjahr: 2011
by Paul Hoffman
Perl For Dummies®, 4th Edition
Published by Wiley Publishing, Inc.909 Third Avenue New York, NY 10022
www.wiley.com
Copyright © 2003 by Wiley Publishing, Inc., Indianapolis, Indiana
Published by Wiley Publishing, Inc., Indianapolis, Indiana
Published simultaneously in Canada
No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under Sections 107 or 108 of the 1976 United States Copyright Act, without either the prior written permission of the Publisher, or authorization through payment of the appropriate per-copy fee to the Copyright Clearance Center, 222 Rosewood Drive, Danvers, MA 01923, (978) 750-8400, fax (978) 646-8700. Requests to the Publisher for permission should be addressed to the Legal Department, Wiley Publishing, Inc., 10475 Crosspoint Blvd., Indianapolis, IN 46256, (317) 572-3447, fax (317) 572-4447, e-mail: [email protected].
Trademarks: Wiley, the Wiley Publishing logo, For Dummies, the Dummies Man logo, A Reference for the Rest of Us!, The Dummies Way, Dummies Daily, The Fun and Easy Way, Dummies.com and related trade dress are trademarks or registered trademarks of Wiley Publishing, Inc., in the United States and other countries, and may not be used without written permission. All other trademarks are the property of their respective owners. Wiley Publishing, Inc., is not associated with any product or vendor mentioned in this book.
LIMIT OF LIABILITY/DISCLAIMER OF WARRANTY: While the publisher and author have used their best efforts in preparing this book, they make no representations or warranties with respect to the accuracy or completeness of the contents of this book and specifically disclaim any implied warranties of merchantability or fitness for a particular purpose. No warranty may be created or extended by sales representatives or written sales materials. The advice and strategies contained herein may not be suitable for your situation. You should consult with a professional where appropriate. Neither the publisher nor author shall be liable for any loss of profit or any other commercial damages, including but not limited to special, incidental, consequential, or other damages. FULFILLMENT OF EACH COUPON OFFER IS THE SOLE RESPONSIBILITY OF THE OFFEROR.
For general information on our other products and services or to obtain technical support, please contact our Customer Care Department within the U.S. at 800-762-2974, outside the U.S. at 317-572-3993, or fax 317-572-4002.
Wiley also publishes its books in a variety of electronic formats. Some content that appears in print may not be available in electronic books.
Library of Congress Control Number: 2003101830
ISBN: 0-7645-3750-4
Manufactured in the United States of America
12 11 10 9 8 7 6 5 4 3
4B/QW/QT/QT/IN
Paul E. Hoffman has written more than a dozen computer books, many of them about the Internet (including Netscape Communicator For Dummies and The Internet, the official book of the Public Television presentation “The Internet Show,” both published by Wiley Publishing, Inc.). In fact, he’s been active on the Internet for more than 20 years. He is the founder of the VPN Consortium, and has written many of the programs used on that group’s Web site. He lives in Santa Cruz, California.
The Perl world is full of very helpful people who answer questions for just about anyone. Perl’s main developer, Larry Wall, started this trend when he released Perl, and now hundreds of people exist on various mailing lists and Web sites who keep the spirit of cooperation alive. Many of my ideas in this book have come from things that these people have said in different forums over the past many years. Tens of thousands of hours have been spent on making Perl free and useful, and I’m grateful for all this effort by Perl enthusiasts all over the world (and you should be, too!). It is my hope that readers of this book will become enamored not only with Perl, but with creating Perl software that will be freely available.
This is my fourth edition of Perl For Dummies, and I’d like to repeat what I said in my very first book. Writing for computer novices is always easier if you have a few of them around asking you questions. My circle of novices and ex-novices keeps me on my toes and reminds me what is not obvious, how frustrating it can be when the system is designed by know-it-all dweebs, and what parts are fun. I am indebted to my friends, my family, and my family-by-choice.
Special thanks to Kala Schrager, the project editor, who spent long hours improving this edition of the book, and for this I am very grateful. Many other folks at Wiley Publishing (and you can find out who they are on the next page) also helped to bring this book to your hands, and I am thankful for their effort and patience.
We’re proud of this book; please send us your comments through our online registration form located at www.dummies.com/register/.
Some of the people who helped bring this book to market include the following:
Acquisitions, Editorial, and Media Development
Project Editor: Kala Schrager
Acquisitions Editor: Terri Varveris
Copy Editor: Rebekah Mancilla
Technical Editor: Steve Seuhring
Editorial Manager: Kevin Kirschner
Media Development Manager: Laura VanWinkle
Media Development Supervisor: Richard Graves
Editorial Assistant: Amanda Foxworth
Cartoons: Rich Tennant (www.the5thwave.com)
Production
Project Coordinator: Nancee Reeves
Layout and Graphics: Seth Conley, Carrie Foster, Michael Kruzil, Jeremey Unger
Proofreaders: Laura Albert, John Greenough; TECHBOOKS Production Services, Inc.
Indexer: TECHBOOKS Production Services, Inc.
Publishing and Editorial for Technology Dummies
Richard Swadley, Vice President and Executive Group Publisher
Andy Cummings, Vice President and Publisher
Mary C. Corder, Editorial Director
Publishing for Consumer Dummies
Diane Graves Steele, Vice President and Publisher
Joyce Pepple, Acquisitions Director
Composition Services
Gerry Fahey, Vice President of Production Services
Debbie Stailey, Director of Composition Services
Title
Introduction
Who You Are
What You Need to Know First
What You’ll Find in This Book
A Word about Program Examples
Icons Used in This Book
Part I : Getting Started with Perl
Chapter 1: Perl’s Place in the Programming World
Understanding the Purpose of Programming
Making Computers Compute
Translating Your Language into Computer Language
Designing Computer Languages
Appreciating the Benefits of Perl
Many Versions, One Perl
Differentiating Between Perl 5 and Perl 5.6
Exploring the New Features of Perl 5.8
Chapter 2: Running Perl on Your Computer
Perl Isn’t Just for UNIX Anymore
UNIX: Perl’s First Home
ActivePerl on Windows
Perl for Mac Users
Working with Perl on the Command Line
Other Perls
Utilizing the Comprehensive Perl Archive Network
Working with the ActivePerl Package Manager
Getting Help
Chapter 3: Diving into the Guts of Perl
Grabbing a Text Editor
Examining Some Basic Components of a Perl Program
Creating Perl Programs Using Statements
Keeping Tabs on Statements: Indenting and Spacing
A Five-Minute Guide to Numbers and Text
Working with Variables and Literals
Chapter 4: A Perl Program Tour, Times Two
Examining the Inside of a Sample Program
Exploring a Program with More Features
Avoiding the Most Common Oversights
Part II : The Basic Perl Programming Ingredients
Chapter 5: Terrific Text
Checking Your Work: The print Statement
Defining Strings
Differentiating between Visible Characters and Character Values
Quoting Text
Manipulating Strings
Performing Simple String Transformations
Getting Information from Strings
Writing for the World in Perl 5.6 and Perl 5.8
Chapter 6: Nifty Numbers
Contrasting Integers and Real Numbers
Using Perl’s Basic Math Operators
Doing Math and an Assignment at the Same Time
Autoincrementing and Autodecrementing
Employing Simple Numeric Functions
Converting Numbers into Strings
Working with Really Big Numbers
Counting in Nondecimal Number Systems
Chapter 7: Learning to Love Lists and Hashes
Listing the Basics
Specifying Elements and Slices of Lists
Using Lists to Simplify Basic Tasks
Using the print Statement with Lists
Adding and Removing List Elements
Splitting Strings with the split Function
Using Scalar Variables
Employing Existing Lists
Distinguishing Context Types
Using List::Util in Perl 5.8
Understanding the Basics of Working with Hashes
Writing Hash Data to a File
Creating Multidimensional Lists
Creating Hashes Consisting of Lists
Writing the Files for Lists within Lists
Chapter 8: Creating Cool Conditionals and Lovely Loops
Exploring Basic Logic Statements
Working with Conditional Statements
Going Around in Loops
Creating switch structures in Perl 5.8
Using Expressions as Logical Values
Caveat Emptor: Jumping around Programs by Using Labels
Chapter 9: Doing Advanced Stuff with Operators and Functions
Using Undefined Variables
Looking for Strings within Strings
Applying Some Fancy Formatting to Text and Numbers
Tracking Time
Picking Random Numbers
Shifting Bits with Bit-Level Operators
Converting Lists to Strings
Rearranging Lists
Part III : The Nuts and Bolts of Perl
Chapter 10: The Files Go In, The Files Go Out
Understanding Perl File Types
Opening and Closing Files
Specifying Filenames and DirectoryPaths for Various Operating Systems
Reading from a Text File the Easy Way
Writing Text to a File the Easy Way
Running Tests to Get Information on a File
Moving File Streams in UNIX: Standard In, Standard Out, and Standard Error
Chapter 11: Beyond File and Directory Basics
Reading from and Writing to Binary Files
Changing File Characteristics
Grouping Filenames by Matching Characters
File Features Strictly for UNIX Users
Getting the Nitty-Gritty Stats on Files
Working with Directories of Files
Chapter 12: Stringing Along: Pattern Matching and Regular Expressions
Working with Regular Expressions
Making Inexact Matches in Regular Expressions
Searching the Beginning or End of a String
Using Quantifiers for Numerical Matches
Getting the Match Results
More Programming Magic with the m// Operator
Using the grep Function
Doing Simple Substitutions with the s/// Operator
Using the split Function with More-Advanced Regular Expressions
Using the tr/// Operator
Chapter 13: Looking Like a Pro: Subroutines, Imported Code, and Graceful Exits
Saving Steps with Subroutines
Importing Prewritten Perl Code into Your Programs
Ending Perl Programs Gracefully
Part IV : Advanced Perl Demystified
Chapter 14: Perl and CGI: Web Server Programs Demystified
Writing CGI Programs in Perl
Using Environment Variables
Gathering Information from HTML Forms
Using GET and POST to Collect Form Data
Displaying Output to the User
Creating an HTML Form
Using Redirection to Link to Other Sites
Exploring a Few Other Environment Variables
Working with Apache and Perl
Handling CGI and HTML with Mason
Chapter 15: Perl Gets Cozy with the Web, E-Mail, and the Rest of the Internet
Using the libwww-perl Library
Getting Your E-Mail
Pinging with Perl
Using Net::FTP to Grab Files
Delving into Domain Names with Perl
Chapter 16: Using Perl as a Gateway to XML and Web Services
Exploring the Role of Perl and XML in Web Programming
Combining XML and Perl
Checking a Document for Accuracy
How Do You Want to Read It?
Writing an XML Document
Reading and Writing XML in Other Ways
Looking into Web Services
Connecting Web Services and Perl
Chapter 17: Dancing with Databases
Managing Databases Using Hashes
Opening Databases
Closing Databases
Tips on Opening and Closing Databases
Chapter 18: Controlling Your Computer from Perl
Getting with the System
Controlling a Windows or Mac Computer with Perl
Using UNIX-Only System Functions
Chapter 19: Object-Oriented Perl
Understanding the Advantages Object-Oriented Programming
Defining Objects, Methods, and Arguments
Perl’s Objects Made Easy
Considering a Real-World Object Example
Part V : The Part of Tens
Chapter 20: Ten Guidelines for Programming with Style
Examining the Perl Creator’s Style Preferences
Choosing Your Operator
Parentheses Are Your Friends
Flexible Ordering for Conditionals
Commenting about Comments
Standout Line Treatments
Comments Cohabitating with Code
Naming Your Variables Descriptively
Capital Offense, or Not?
The Beauty of Blank Lines
Chapter 21: Ten Really Short, Really Useful Perl Programs
Collecting Unique Lines
Getting Rid of Multiple Blank Lines
Printing Lines from a File in Sorted Order
Printing a Range of Lines from a File
Listing Just the Files in a Directory
Listing a Directory by Size
Sorting Directories by Extension
Creating a Simple Calculator
Randomizing a List
Generating Random Mnemonic Passwords
Part VI : Appendix
Appendix: The Great Perl Reference
Perl Operators
Perl’s Operator Hierarchy
Perl’s Functions and Statements
Perl’s Special Variables
Perl’s Predefined File Handles
A ssuming that you want to venture into programming, your first step is to pick a specific programming language to start out with. Because you’re reading this book, I’m guessing that you’re seriously thinking about programming with Perl. I applaud your choice. Like human languages, some programming languages are easier to learn than others. Perl is one of the easiest programming languages to work with, yet it’s as powerful as many languages that are much more difficult to master.
The Perl programming language allows you to create fully working computer programs with just a few steps. It’s particularly good at common programming tasks, such as reading and writing text files, but it also excels at reducing the work that programmers have to do — more so than other popular languages, such as C and Java.
Your reasons for wanting to learn a programming language probably boil down to the following:
A fascination with how computers work
A desire to create your own programs to control your computer
For professional development (for example, you want to advance to programmer status)
You may also want to get acquainted with a programming language because you work with programmers and you wonder what it is that they do all day. Understanding programming definitely helps you to appreciate what it takes to be a good programmer, even if you don’t become one yourself.
If you’re interested in discovering how to program (or how others program), Perl For Dummies, 4th Edition is for you. If you already know something about programming (but not about Perl), this book is also for you. If you’re already an expert programmer, you’re still welcome to read this book; you can just skip the basic stuff (you never know what kind of new tips and tricks you’ll pick up).
After finding out how to program with Perl, you may decide to go on and become someone who makes a living as a Perl programmer. Or, you may decide that, although programming is interesting, it isn’t something that you want to do full time. And, a chance exists (although a slight one) that after you learn how to program in Perl, you may discover that you like programming, but you dislike Perl.
Fortunately, you can also apply most of the Perl programming skills that you can pick up from this book to another programming language later on. Perl is simpler to work with than most other programming languages — and Perl uses many of the same concepts as other languages do, which makes it an excellent introductory language.
This book covers how to use Perl under many different operating systems, such as UNIX, many flavors of Windows (Windows 9 5/98, Windows NT, Windows 2000, Windows Me, and Windows XP), and Macintosh OS 9 and OS X; in fact, Perl runs on many more operating systems than these. To use this book to learn Perl programming, you need to know at least one of these operating systems. You don’t need to know more than one operating system, and very few folks out there know how to use all four. If you’re one of those people who’s familiar with all four operating systems, you can enjoy the title of Total Computer Dweeb and be comfortable in the fact that few others in the world share your unique skills.
In short, you don’t need to know anything about programming up front, or even what Perl is, to get lots of value from this book. However, you do need to know the basics of using your computer system so that you can load Perl and create text files that Perl can use as programs.
Because Perl For Dummies, 4th Editionis meant for both programming novices and folks who already know something about programming, I arranged the book so that both groups of people can find what they want quickly. You may want to read this book straight through from beginning to end, or you may be one of those folks who enjoys skipping to just what you need. In either case, you need to know how this book is organized.
Part I tells you what programming is all about, what Perl is, what makes it different from other programming languages, and what it means “to program.” This part contains some example programs so that you can see what they look like and how they are put together. You also get some help in installing Perl and running Perl on various operating systems.
In Part II, I cover the basic elements of programming with Perl — including techniques for working with text and numbers, plus in-the-trenches programming stuff like lists, strings, operators, loops, and conditional statements. When you’re done perusing this part, you can probably write a few short programs of your own.
This part of the book shows you how to structure a complete Perl program, put together larger and more complex programs, tell Perl to read and write files on your computer, and write programs that know how to talk to a Web server.
After you’re comfortable with Perl and feeling adventurous, you can dip into this part of the book, which covers a number of advanced Perl topics. Novice programmers aren’t expected to use these features immediately, but if you zoom ahead to these chapters (maybe you’re saying, “Nah, that’s not me,” but you may surprise yourself later), you’ll find lots of useful information to rev up your Perl programs.
This part of the book contains some nice, quick lists of interesting information about Perl. If you like round numbers, you’ll be happy to know that they’re all organized in groups of ten.
Part VI, as you may have guessed, contains the appendix. I list some handy Perl references, such as operators, special variables, and predefined file handles.
Throughout the book, you can find examples of Perl code that look like the following line:
print “Hello, world!\n”;
You will also see examples of this same kind of typestyle in the body text like this to indicate that this text is a part of a Perl program. You can also find multiple lines of code neatly enclosed in gray boxes. The single lines of code are usually Perl statements (or portions of a statement) and the multiple lines are generally Perl programs. (Yes, you can write an entire program in Perl with just three or four lines.) Not to worry, though. This books tells you all about the difference between statements and programs (and functions and strings and other programming conventions) that are displayed in code font.
One of the fun things about writing a For Dummies book is that you get to use these clever icons to point out items of special interest.
Hopefully, you’ll find everything in the book really useful, but these items are especially noteworthy.
This icon alerts you to something that you should definitely watch out for. A couple of important “gotchas” are buried in Perl, and these warnings can help steer you clear of them.
This icon appears next to anything you need to remember later on when you’re doing other kinds of stuff with Perl.
Stop here for a side note about something especially high-end. This icon points out technical information that isn’t critical to know, but handy nonetheless.
In this part. . .
You’ve got to start somewhere, and the beginning is the best place, is it not? In these first few chapters, you find out what a programming language is, what Perl is, how to get Perl up and running on your particular operating system, and what a couple of substantial Perl programs look like.
Understanding programming languages
Comparing Perl to other languages
Using Perl with emerging technologies
M any people want to know exactly what makes their computer programs tick. They have a nagging itch to find out what’s “under the hood” of their computers, beyond the chips and wires. They want to find out how they can create new and better programs and how it is that programmers can make so much money yet look like they dress in the dark.
Don’t assume that everyone who programs or writes books about programming is a young whippersnapper. Without getting too specific, let’s just say that my college days are way, way behind me. When I worked with computers back then, I often used punched cards and paper tape — ah, those wonderful pre-PC days.
In pre-PC days, BASIC was the easy language to learn, and serious programmers learned FORTRAN or COBOL to do “real work.” But many people discovered that you could accomplish a lot with plain old BASIC. Today, many people have discovered that Perl is a great beginning programming language because it’s simple to use and yet you can create powerful programs with little effort. Sure, you can immerse yourself in more difficult languages, such as C++ or Java, but you can probably learn Perl faster and find yourself doing what you want to do just as well, if not better.
If you’re new to the concept of programming, you may be asking yourself, “Why am I learning how to program?” Sure, programs control computers, but that doesn’t mean that everyone should become a programmer.
When personal computers appeared on the scene about 25 years ago, almost no commercial software existed, and the software that did exist was pretty primitive. If you wanted to do anything special, you had to learn to program so that you could create programs to meet your specific needs. By 1981, when the IBM PC came out, plenty of commercial programs were available and users no longer needed to program their computers — the software did it for them.
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!