Beginning Perl. Third Edition. Apress. JAMES LEE with SIMON COZENS

Similar documents
1. Introduction. 2. Scalar Data

Perl (5 Days Content)

Hands-On Perl Scripting and CGI Programming

Perl Scripting. Students Will Learn. Course Description. Duration: 4 Days. Price: $2295

PERL Scripting - Course Contents

Learning C# 3.0. Jesse Liberty and Brian MacDonald O'REILLY. Beijing Cambridge Farnham Köln Sebastopol Taipei Tokyo

"Charting the Course... Intermediate PHP & MySQL Course Summary

A Web-Based Introduction

CERTIFICATE IN WEB PROGRAMMING

Preface... (vii) CHAPTER 1 INTRODUCTION TO COMPUTERS

Learning Perl Objects, References, and Modules

Microsoft. Microsoft Visual C# Step by Step. John Sharp

Contents. Figures. Tables. Examples. Foreword. Preface. 1 Basics of Java Programming 1. xix. xxi. xxiii. xxvii. xxix

Excel Programming with VBA (Macro Programming) 24 hours Getting Started

SAP' ABAP. Handbook. Kogent Learning Solutions, Inc. Sudbury, Massachusetts JONES AND BARTLETT PUBLISHERS BOSTON TORONTO LONDON SINUAPORI:

Table of Contents. Preface... xxi

CHAPTER 1: INTRODUCING C# 3

This course is designed for web developers that want to learn HTML5, CSS3, JavaScript and jquery.

PYTHON. p ykos vtawynivis. Second eciitiovl. CO Ve, WESLEY J. CHUN

Table of Contents at a Glance

Advanced R. V!aylor & Francis Group. Hadley Wickham. ~ CRC Press

Murach s Beginning Java with Eclipse

COMS 3101 Programming Languages: Perl. Lecture 5

Chapters and Appendix F are PDF documents posted online at the book s Companion Website (located at

"Charting the Course... Java Programming Language. Course Summary

Microsoft Visual C# Step by Step. John Sharp

C# Programming: From Problem Analysis to Program Design. Fourth Edition

Oracle Data Integrator: Administration and Development Volume I Student Guide

Practical C++ Programming

CP150 - Advanced Perl Programming

Acknowledgments Introduction. Chapter 1: Introduction to Access 2007 VBA 1. The Visual Basic Editor 18. Testing Phase 24

Oracle BPM 10g R3 Programming 1 Essentials

Fundamentals of the Java Programming Language

Regular expressions and case insensitivity

Python Scripting for Computational Science

Data Structures in C++ Using the Standard Template Library

Contents. Acknowledgments

Contents. iii Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services August 1998, Revision B

Course Outline. Advanced Perl Programming. Course Description: Pre-requisites: Course Content: Tel: +44 (0) Fax: +44 (0)

VB.NET. Exercise 1: Creating Your First Application in Visual Basic.NET

CONTENTS. PART 1 Structured Programming 1. 1 Getting started 3. 2 Basic programming elements 17

Intermediate Perl Table of Contents Intermediate Perl Foreword Preface Structure of This Book Conventions Used in This Book Using Code Examples

Contents. Illustrations. 1 Introduction to Computers, the Internet, the Web and C# 1

Tilak Maharashtra University

"Charting the Course... MOC C: Querying Data with Transact-SQL. Course Summary

An Introduction to JavaScript & Bootstrap Basic concept used in responsive website development Form Validation Creating templates

Who This Book Is For What This Book Covers How This Book Is Structured What You Need to Use This Book. Source Code

Introduction to JavaScript p. 1 JavaScript Myths p. 2 Versions of JavaScript p. 2 Client-Side JavaScript p. 3 JavaScript in Other Contexts p.

Contents in Detail. Foreword by Xavier Noria

Perl Dbi Insert Hash Into Table >>>CLICK HERE<<<

object/relational persistence What is persistence? 5

Regular expressions and case insensitivity

Installing and Administering a Satellite Environment

Java Fundamentals p. 1 The Origins of Java p. 2 How Java Relates to C and C++ p. 3 How Java Relates to C# p. 4 Java's Contribution to the Internet p.

CITY UNIVERSITY OF NEW YORK. Creating a New Project in IRBNet. i. After logging in, click Create New Project on left side of the page.

KLiC C++ Programming. (KLiC Certificate in C++ Programming)

Computer Programming C++ (wg) CCOs

Java Programming with Eclipse

Beginning PHP. and MySQL. Fourth Edition. From Novice to Professional. W. Jason Gilmore. mmm

Outline. CS3157: Advanced Programming. Feedback from last class. Last plug

APPENDIX B. The Future Of Perl & CGI Programming OVERVIEW

OVERVIEW ENVIRONMENT PROGRAM STRUCTURE BASIC SYNTAX DATA TYPES TYPE CONVERSION

Object-Oriented Programming in C# (VS 2015)

IT441. Network Services Administration. Perl: File Handles

Perl and Python ESA 2007/2008. Eelco Schatborn 27 September 2007

Coding Faster: Getting More Productive with Microsoft Visual

Object-Oriented Programming in C# (VS 2012)

Object-Oriented Programming and Data Structures

UNIX Shell Programming

Java 1.8 Programming

foreword to the first edition preface xxi acknowledgments xxiii about this book xxv about the cover illustration

"Charting the Course to Your Success!" MOC D Querying Microsoft SQL Server Course Summary

M Introduction to Visual Basic.NET Programming with Microsoft.NET 5 Day Course

Web Development & SEO (Summer Training Program) 4 Weeks/30 Days

MySQL and PHP - Developing Dynamic Web Applications

CITY UNIVERSITY OF NEW YORK. i. Visit:

Chapters are PDF documents posted online at the book s Companion Website (located at

CORE PHP CURRICULUM. Introductory Session Web Architecture Overview of PHP Platform Origins of PHP in the open source community

Contents. Using Interpreters... 5 Using Compilers... 5 Program Development Life Cycle... 6

Python Scripting for Computational Science

"Charting the Course... MOC A: Developing with the SharePoint Framework. Course Summary

Perl. Interview Questions and Answers

PHP WITH ANGULAR CURRICULUM. What you will Be Able to Achieve During This Course

Welcome to Teach Yourself Acknowledgments Fundamental C++ Programming p. 2 An Introduction to C++ p. 4 A Brief History of C++ p.

Developing Web Applications

Scripting Languages Perl Basics. Course: Hebrew University

System Administration of PTC Windchill 11.0

C++ (Non for C Programmer) (BT307) 40 Hours

Perl. Perl. Beginning. Beginning. James Lee Lee. Perl Perl 5.10 THE EXPERT S VOICE IN OPEN SOURCE

Verilog HDL. A Guide to Digital Design and Synthesis. Samir Palnitkar. SunSoft Press A Prentice Hall Title

CS 105 Perl: Modules and Objects

2559 : Introduction to Visual Basic.NET Programming with Microsoft.NET

WITH C+ + William Ford University of the Pacific. William Topp University of the Pacific. Prentice Hall, Englewood Cliffs, New Jersey 07632

Get in Touch Module 1 - Core PHP XHTML

Learn Windows PowerShell 3 in a Month of Lunches

CLASSIC DATA STRUCTURES IN JAVA

Table of Contents. Introduction... xxi

ABOUT WEB TECHNOLOGY COURSE SCOPE:

Swami Ramanand Teerth Marathwada University, Nanded

Exercise Solutions. Chapter 1. Chapter 2 APPENDIX. #!/usr/bin/perl -w # chap01ex1. pl. print "Hi Mom.\nThis is my second program.

Transcription:

Beginning Perl Third Edition JAMES LEE with SIMON COZENS Apress

About the Author... About the Technical Reviewers Acknowledgements Suitrod yetion «. xvi xvii xviii «xix. Chapter 1: First Steps in Perl.. 1 Programming Languages 1 Our First Perl Program 2 Program Structure 6 Character Sets 8 Escape Sequences -8 Whitespace 9 Number Systems 9 The Perl Debugger 11 Summary 11 Exercises 12 Chapter 2: Scalars 13 Types of Data 13 Numbers 14 Strings 17 Here-Documents 20 Converting Between Numbers and Strings 21 v

Operators 22 Numeric Operators 22 String Operators 32 Operators to Be Covered Later 36 Operator Precedence 37 Variables 38 Scoping 43 Variable Names 46 Variable Interpolation 46 Currency Converter 48 Two Miscellaneous Functions 50 The exit() Function 50 The die() Function 51 Summary 52 Exercises 52 Chapter 3: Control Flow Constructs...53 The if Statement 54 Operators Revisited 55 Multiple Choice: if... else 61 The unless Statement 64 Expression Modifiers 65 Using Short-Circuited Evaluation 65 Looping Constructs 66 The while Loop 66 while (<STDIN>) 67 Infinite Loops 69 Looping Until 70 The for Loop 71 vi

Theforeach Loop '"I do..while and do..until 72 Loop Control Constructs 74 Breaking Out 74 Going On to the Next 75 Reexecuting the Loop 76 Loop Labels 77 goto 79 Summary 79 Exercises 79 Chapter 4: Lists and Arrays -81 Lists 81 Simple Lists 82 More Complex Lists 83 Creating Lists Easily with qw// 84 Accessing List Values 87 Arrays 91 Assigning Arrays 91 Scalar vs. List Context 94 Adding to an Array 95 Accessing an Array 95 Summary 114 Exercises 114 Chapter 5: Hashes 115 Creating a Hash 115 Working with Hash Values 117 Hash in List Context 119

Hash in Scalar Context 120 Hash Functions 121 The keys() Function 121 The valuesq Function 122 The each() Function 123 The delete() Function 123 The exists() Function 124 Hash Examples 125 Creating Readable Variables 125 "Reversing" Information 125 Counting Things 126 Summary 129 Exercises 129 Chapter 6: Subroutines/Functions.131 Understanding Subroutines 132 Defining a Subroutine 132 Invoking a Subroutine 133 Order of Declaration and Invoking Functions 134 Passing Arguments into Functions 137 Return Values 139 The return Statement 141 Understanding Scope 142 Global Variables 142 Introduction to Packages 144 Lexical Variables (aka Local Variables) 146 Some Important Notes on Passing Arguments 147 Function Arguments Passed by Reference 147 Lists Are One-Dimensional 149 viii

Default Argument Values 150 Named Parameters 151 Summary 152 Exercises...152 Chapter 7: Regular Expressions -153 What Are They? 153 Patterns 154 Working with Regexes 170 Substitution 170 Changing Delimiters 172 Modifiers 173 The split() Function 174 The join() Function 175 Common Blunders 175 Summary 176 Exercises 177 Chapter 8: Files and Data 179 Filehandles 179 The open() Function 179 The close() Function 180 Three Ways to Open a File 181 Read Mode 182 Reading in Scalar Context 183 Reading with the Diamond 185 @ARGV: The Command-Line Arguments 187 @ARGV and <> 189 SARGV 190 Reading in List Context 190 L\

Writing to Files 192 Buffering 195 Opening Pipes 196 Receiving Piped Data from a Process 196 Sending Piped Data to Another Process 198 Bidirectional Pipes 200 File Tests 200 Summary 205 Exercises 205 Chapter 9: String Processing 207 Character Position 207 String Functions 208 The lengthtj Function 208 The index() Function 208 The rindex() Function 210 The substrf) Function 210 Transliteration 212 Summary 213 Exercises 213 Chapter 10: Interfacing to the Operating System 215 The %ENV Hash 215 Working with Files and Directories 217 File Globbing with glob() 217 Reading Directories 220 Functions to Work with Files and Directories 221 Executing External Programs 225 The system() Function 225

Backquotes 227 There's More 228 Summary 228 Exercises 229 Chapter 11: References 231 What Is a Reference? 231 Anonymity 232 The Life Cycle of a Reference 232 Reference Creation 232 Reference Modification 239 Reference Counting and Destruction 243 Using References for Complex Data Structures 244 Matrices 245 Autovivification 245 Trees 250 Summary 255 Exercises 255 Chapter 12: Modules 257 Why Do We Need Them? 257 Creating a Module 258 Including Other Files with use 260 do 260 require 261 use 262 Changing @INC 262 Package Hierarchies 263 Exporters 265

The Perl Standard Modules - 267 Online Documentation 268 Data::Dumper 268 File-Find 270 Getopt::Std 6etopt::Long 271 272 File::Spec 273 Benchmark 275 Win32 276 CPAN 278 Installing Modules with PPM 280 Installing a Module Manually 281 The CPAN Module 281 Bundles 284 Submitting Your Own Module to CPAN 285 Summary 286 Chapter 13: Object-Oriented Perl....287 00 Buzzwords 287 Objects 287 Attributes 288 Methods 288 Classes 289 Polymorphism 290 Encapsulation 290 Inheritance 290 Constructors 291 Destructors 292 An Example 292 xii

Rolling Your Own Classes 295 Bless You, My Reference 296 Storing Attributes 298 The Constructor 298 Creating Methods 301 Do You Need 00?..-313 Are Your Subroutines Tasks? 314 Do You Need Persistence? 314 Do You Need Sessions? 314 Do You Need Speed? 314 Do You Want the User to Be Unaware of the Object? 314 Are You Still Unsure? 314 Summary 315 Exercises 315 Chapter. 14: Introduction to CGI 317 We Need a Web Server, 318 Creating a CGI Directory 318 Writing CGI Programs 318 "hello, world!" in CGI 319 The CGI Environment 321 Generating HTML 323 Introducing CGI.pm 325 Conventional Style of Calling Methods 331 CGI.pm Methods 332 Methods That Generate Several Tags 332 Methods That Generate One Tag 333 Processing Form Data 333 The param() Method 335 xiii

Dynamic CGI 336 Let's Play Chess! 338 Improvements We Can Make 346 What We Did Not Talk About 347 Summary 348 Exercises 348 Chapter 15: Perl and DBI 349 Introduction to Relational Databases 349 We Need an SQL Server MySQL 353 Testing the MySQL Server 353 Creating a Database 354 Creating a Non-root User with the GRANT Command 357 The INSERT Command 358 The SELECT Command 361 Table Joins 367 Introduction to DBI 368 Installing DBI and the DBD::mysql 368 Connecting to the MySQL Database 369 Executing an SQL Query with DBI 370 A More Complex Example 372 Use Placeholders 375 DBI and Table Joins 377 Perl, DBI, and CGI 378 What We Didn't Talk About 385 Summary 386 Exercises 386 Appendix: Exercise Solutions 387 xiv

Chapter 1 387 Chapter 2 387 Chapter 3 389 Chapter 4 390 Chapter 5 391 Chapter 6 393 Chapter 7 395 Chapter 8 396 Chapter 9 398 Chapter 10 399 Chapter 11, 400 Chapter 13 404 Chapter Chapter 14 405 15 406 Index 409 XV