Perl by Example [3rd Edition]
9780132381826, 0132381826
This tutorial/reference is the ideal guide for UNIX professionals who want to (or must) learn Perl (Practical Extraction
330
82
8MB
English
Pages 1008
Year 2007
Report DMCA / Copyright
DOWNLOAD PDF FILE
Table of contents :
Cover......Page 1
Contents......Page 8
Preface......Page 28
1.1 What Is Perl?......Page 32
1.2 What Is an Interpreted Language?......Page 33
1.3.1 Which Perl?......Page 34
1.3.2 What Is Perl 6?......Page 35
1.4 Where to Get Perl......Page 36
1.4.1 What Version Do I Have?......Page 38
1.5 What Is CPAN?......Page 40
1.6.1 Perl Man Pages......Page 41
1.6.2 HTML Documentation......Page 42
1.8 What's Next?......Page 43
2.1.2 A Note to Non-Programmers......Page 44
2.1.3 Perl Syntax and Constructs......Page 45
2.3 What's Next?......Page 60
3.1 Script Setup......Page 62
3.2.1 Startup......Page 63
3.2.4 Statements, Whitespace, and Linebreaks......Page 65
3.2.6 Perl Statements......Page 66
3.2.8 Executing the Script......Page 67
3.2.9 Sample Script......Page 68
3.2.10 What Kinds of Errors to Expect......Page 69
3.3 Perl at the Command Line......Page 70
3.3.2 The -n Switch......Page 71
3.3.3 The -c Switch......Page 73
3.5 What's Next?......Page 74
EXERCISE 3 Getting with It Syntactically......Page 75
4.2 Words......Page 76
4.3 The print Function......Page 77
4.3.1 Quotes......Page 78
4.3.2 Literals (Constants)......Page 80
4.3.3 Printing Literals......Page 82
4.3.4 The warnings Pragma and the -w Switch......Page 86
4.3.5 The diagnostics Pragma......Page 88
4.3.6 The strict Pragma and Words......Page 89
4.4 The printf Function......Page 90
4.4.1 The sprintf Function......Page 93
4.4.2 Printing without Quotes—The here document......Page 94
4.6 What's Next?......Page 97
EXERCISE 4 A String of Perls......Page 98
5.1.2 Scope and the Package......Page 100
5.1.3 Naming Conventions......Page 101
5.1.4 Assignment Statements......Page 102
5.1.5 Quoting Rules......Page 103
5.2.1 Scalar Variables......Page 108
5.2.2 Arrays......Page 111
5.2.3 Hashes......Page 118
5.2.4 Complex Data Structures......Page 122
5.3.1 Assigning Input to a Scalar Variable......Page 125
5.3.2 The chop and chomp Functions......Page 126
5.3.3 The read Function......Page 127
5.3.4 The getc Function......Page 128
5.3.5 Assigning Input to an Array......Page 129
5.3.6 Assigning Input to a Hash......Page 130
5.4.1 The chop and chomp Functions (with Lists)......Page 131
5.4.3 The delete Function......Page 132
5.4.5 The join Function......Page 133
5.4.6 The map Function......Page 134
5.4.7 The pack and unpack Functions......Page 136
5.4.8 The pop Function......Page 138
5.4.10 The shift Function......Page 139
5.4.11 The splice Function......Page 140
5.4.12 The split Function......Page 141
5.4.13 The sort Function......Page 145
5.4.14 The reverse Function......Page 147
5.4.15 The unshift Function......Page 148
5.5.1 The keys Function......Page 149
5.5.3 The each Function......Page 150
5.5.4 Sorting a Hash......Page 151
5.5.5 The delete Function......Page 157
5.5.6 The exists Function......Page 158
5.6.1 Loading a Hash from a File......Page 159
5.6.2 Special Hashes......Page 160
5.6.3 Context......Page 162
5.7 What You Should Know......Page 163
5.8 What's Next?......Page 164
EXERCISE 5 The Funny Characters......Page 165
6.1 About Perl Operators......Page 168
6.2 Mixing Data Types......Page 169
6.3 Precedence and Associativity......Page 170
6.3.1 Assignment Operators......Page 172
6.3.2 Relational Operators......Page 174
6.3.3 Equality Operators......Page 177
6.3.4 Logical Operators (Short-Circuit Operators)......Page 180
6.3.5 Logical Word Operators......Page 182
6.3.6 Arithmetic Operators......Page 184
6.3.7 Autoincrement and Autodecrement Operators......Page 185
6.3.8 Bitwise Logical Operators......Page 187
6.3.9 Conditional Operators......Page 190
6.3.10 Range Operator......Page 192
6.3.11 Special String Operators and Functions......Page 193
6.3.12 Arithmetic Functions......Page 195
6.5 What's Next?......Page 199
EXERCISE 6 Operator, Operator......Page 200
7.1 Control Structures, Blocks, and Compound Statements......Page 202
7.1.1 Decision Making—Conditional Constructs......Page 203
7.2.1 The while Loop......Page 208
7.2.2 The until Loop......Page 210
7.2.3 The for Loop......Page 213
7.2.4 The foreach Loop......Page 215
7.2.5 Loop Control......Page 219
7.2.6 The switch Statement......Page 227
7.4 What's Next?......Page 231
EXERCISE 7 What Are Your Conditions?......Page 232
8.1 What Is a Regular Expression?......Page 234
8.2.1 Conditional Modifiers......Page 235
8.2.2 The DATA Filehandle......Page 236
8.2.3 Looping Modifiers......Page 240
8.3.1 The m Operator and Matching......Page 241
8.3.2 The s Operator and Substitution......Page 247
8.3.3 Pattern Binding Operators......Page 257
8.5 What's Next?......Page 263
EXERCISE 8 A Match Made in Heaven......Page 265
9.1 Regular Expression Metacharacters......Page 266
9.1.1 Metacharacters for Single Characters......Page 269
9.1.2 Whitespace Metacharacters......Page 278
9.1.3 Metacharacters to Repeat Pattern Matches......Page 281
9.1.4 The tr or y Function......Page 307
9.2.1 Perl and Unicode......Page 312
9.4 What's Next?......Page 314
EXERCISE 9 And the Search Goes On.........Page 315
10.1.1 Opening Files—The open Function......Page 316
10.1.2 Open for Reading......Page 317
10.1.3 Open for Writing......Page 322
10.1.4 Win32 Binary Files......Page 323
10.1.5 Open for Appending......Page 324
10.1.6 The select Function......Page 325
10.1.7 File Locking with flock......Page 326
10.1.8 The seek and tell Functions......Page 327
10.1.9 Open for Reading and Writing......Page 332
10.1.10 Open for Pipes......Page 333
10.2.1 The ARGV Array......Page 341
10.2.2 ARGV and the Null Filehandle......Page 342
10.2.3 The eof Function......Page 346
10.2.4 The -i Switch—Editing Files in Place......Page 348
10.3 File Testing......Page 350
10.4 What You Should Know......Page 352
10.5 What's Next?......Page 353
EXERCISE 10 Getting a Handle on Things......Page 354
11.1 Subroutines/Functions......Page 356
11.1.1 Defining and Calling a Subroutine......Page 357
11.2 Passing Arguments......Page 361
11.2.1 Prototypes......Page 369
11.2.2 Return Value......Page 371
11.2.3 Context and Subroutines......Page 373
11.3.1 Symbolic References—Typeglobs......Page 375
11.3.2 Hard References—Pointers......Page 380
11.3.3 Autoloading......Page 385
11.3.4 BEGIN and END Subroutines (Startup and Finish)......Page 388
11.4 What You Should Know......Page 389
11.5 What's Next?......Page 390
EXERCISE 11 I Can't Seem to Function without Subroutines......Page 391
12.1.2 An Analogy......Page 394
12.1.3 Definition......Page 395
12.1.4 The Symbol Table......Page 396
12.2 The Standard Perl Library......Page 401
12.2.1 The @INC Array......Page 402
12.2.2 Packages and .pl Files......Page 405
12.2.3 Modules and .pm Files......Page 409
12.2.4 Exporting and Importing......Page 410
12.2.5 How to "use" a Module from the Standard Perl Library......Page 416
12.2.6 Using Perl to Create Your Own Module......Page 419
12.3 Modules from CPAN......Page 421
12.3.1 Using PPM......Page 424
12.5 What's Next?......Page 429
EXERCISE 12 I Hid All My Perls in a Package......Page 430
13.1.1 Symbolic versus Hard References......Page 432
13.1.2 Hard References, Pointers......Page 434
13.1.3 References and Anonymous Variables......Page 437
13.1.4 Nested Data Structures......Page 439
13.1.5 References and Subroutines......Page 445
13.1.6 Filehandle References......Page 448
13.1.7 The ref Function......Page 449
13.3 What's Next?......Page 451
EXERCISE 13 It's Not Polite to Point!......Page 452
14.1.1 Packages and Modules Revisited......Page 454
14.1.2 Some Object-Oriented Lingo......Page 455
14.2.1 Real World......Page 456
14.2.3 Classes and Privacy......Page 457
14.2.4 Objects......Page 459
14.2.5 The bless Function......Page 460
14.2.6 Methods......Page 462
14.2.7 What an Object-Oriented Module Looks Like......Page 464
14.2.8 Polymorphism and Dynamic Binding......Page 476
14.2.9 Destructors and Garbage Collection......Page 482
14.3.1 What Is a Closure?......Page 484
14.3.2 Closures and Objects......Page 486
14.4.1 The @ISA Array and Calling Methods......Page 491
14.4.2 $AUTOLOAD, sub AUTOLOAD, and UNIVERSAL......Page 493
14.4.3 Derived Classes......Page 496
14.4.5 Overriding a Parent Method......Page 502
14.5.1 pod Files......Page 505
14.5.2 pod Commands......Page 507
14.5.4 Translating pod Documentation into Text......Page 508
14.6.1 Another Look at the Standard Perl Library......Page 510
14.6.2 An Object-Oriented Module from the Standard Perl Library......Page 512
14.6.3 Using a Module with Objects from the Standard Perl Library......Page 514
14.7 What You Should Know......Page 515
14.8 What's Next?......Page 516
EXERCISE 14 What's the Object of This Lesson?......Page 517
15.1.1 The tie Function......Page 524
15.1.3 Tying a Scalar......Page 525
15.1.4 Tying an Array......Page 528
15.1.5 Tying a Hash......Page 531
15.2 DBM Files......Page 536
15.2.1 Creating and Assigning Data to a DBM File......Page 537
15.2.2 Retrieving Data from a DBM File......Page 539
15.2.3 Deleting Entries from a DBM File......Page 541
15.4 What's Next?......Page 543
16.1 Static and Dynamic Web Pages......Page 544
16.2.1 Internet Communication between Client and Server......Page 547
16.3 Creating a Web Page with HTML......Page 553
16.4 How HTML and CGI Work Together......Page 557
16.4.1 A Simple CGI Script......Page 558
16.4.2 Error Log Files......Page 561
16.5.1 CGI Environment Variables......Page 562
16.6 CGI and Forms......Page 566
16.6.1 Input Types for Forms......Page 567
16.6.2 Creating an HTML Form......Page 568
16.6.3 The GET Method......Page 572
16.6.4 Processing the Encoded Data......Page 575
16.6.5 Putting It All Together......Page 579
16.6.6 The POST Method......Page 582
16.6.7 Handling E-mail......Page 586
16.7.1 Introduction......Page 590
16.7.3 Two Styles of Programming with CGI.pm......Page 591
16.7.4 An Important Warning!......Page 593
16.7.5 HTML Form Methods......Page 595
16.7.6 How CGI.pm Works with Forms......Page 603
16.7.7 CGI.pm Form Elements......Page 608
16.7.8 Methods Defined for Generating Form Input Fields......Page 610
16.7.9 Error Handling......Page 624
16.7.10 HTTP Header Methods......Page 627
EXERCISE 16 Surfing for Perls......Page 630
17.1 Introduction......Page 634
17.2.1 Client/Server Databases......Page 635
17.2.2 Components of a Relational Database......Page 636
17.2.3 Talking to the Database with SQL (the Structured Query Language)......Page 641
17.3.2 Installing MySQL......Page 644
17.3.3 Connecting to MySQL......Page 645
17.3.4 Graphical User Tools......Page 647
17.3.5 Finding the Databases......Page 650
17.3.6 Getting Started with Basic Commands......Page 652
17.4 What Is the Perl DBI?......Page 669
17.4.1 Installing the DBI......Page 670
17.4.2 The DBI Class Methods......Page 676
17.4.3 How to Use DBI......Page 678
17.4.4 Connecting to and Disconnecting from the Database......Page 679
17.4.6 Preparing a Statement Handle and Fetching Results......Page 681
17.4.7 Handling Quotes......Page 685
17.4.8 Getting Error Messages......Page 686
17.5.1 The do() method......Page 697
17.6 Transactions......Page 701
17.7 Using CGI and the DBI to Select and Display Entries......Page 703
17.8 What's Left?......Page 709
17.10 What's Next?......Page 710
EXERCISE 17 Select * from Chapter......Page 711
18.1 System Calls......Page 716
18.1.1 Directories and Files......Page 718
18.1.2 Directory and File Attributes......Page 720
18.1.3 Finding Directories and Files......Page 723
18.1.4 Creating a Directory—The mkdir Function......Page 726
18.1.5 Removing a Directory—The rmdir Function......Page 727
18.1.6 Changing Directories—The chdir Function......Page 728
18.1.7 Accessing a Directory via the Directory Filehandle......Page 729
18.1.8 Permissions and Ownership......Page 732
18.1.9 Hard and Soft Links......Page 736
18.1.10 Renaming Files......Page 739
18.1.11 Changing Access and Modification Times......Page 740
18.1.12 File Statistics......Page 741
18.1.13 Low-Level File I/O......Page 743
18.1.14 Packing and Unpacking Data......Page 746
18.2.1 UNIX Processes......Page 752
18.2.3 The Environment (UNIX and Windows)......Page 754
18.2.4 Processes and Filehandles......Page 756
18.2.5 Process Priorities and Niceness......Page 759
18.2.6 Password Information......Page 761
18.2.7 Time and Processes......Page 765
18.2.8 Process Creation UNIX......Page 770
18.2.9 Process Creation Win32......Page 775
18.3.1 The syscall Function and the h2ph Script......Page 778
18.3.2 Command Substitution—The Backquotes......Page 779
18.3.3 The Shell.pm Module......Page 780
18.3.4 The system Function......Page 781
18.3.5 here documents......Page 783
18.3.6 Globbing (Filename Expansion and Wildcards)......Page 784
18.4.1 The die Function......Page 786
18.4.3 The eval Function......Page 788
18.5 Signals......Page 791
18.6 What You Should Know......Page 795
18.7 What's Next?......Page 796
19.1.1 Steps in Defining the Template......Page 798
19.1.2 Changing the Filehandle......Page 801
19.1.3 Top-of-the-Page Formatting......Page 802
19.1.4 The select Function......Page 807
19.1.5 Multiline Fields......Page 809
19.1.6 Filling Fields......Page 810
19.1.7 Dynamic Report Writing......Page 812
19.3 What's Next?......Page 814
EXERCISE 19 Pretty as a Picture!......Page 815
20.3 Network Protocols (TCP/IP)......Page 816
20.3.4 User Datagram Protocol (UDP)......Page 817
20.4.3 Port Numbers......Page 818
20.4.4 Perl Protocol Functions......Page 819
20.4.5 Perl's Server Functions......Page 821
20.4.6 Perl's Host Information Functions......Page 823
20.5 Sockets......Page 825
20.5.2 Socket Domains......Page 826
20.5.3 Creating a Socket......Page 827
20.5.5 Creating a Socket Queue......Page 828
20.5.7 Establishing a Socket Connection......Page 829
20.5.8 Socket Shutdown......Page 830
20.6.1 Connection-Oriented Sockets on the Same Machine......Page 831
20.6.2 Connection-Oriented Sockets on Remote Machines (Internet Clients and Servers)......Page 835
20.7 The Socket.pm Module......Page 839
20.8 What You Should Know......Page 844
A.1 Perl Functions......Page 846
A.2 Special Variables......Page 876
A.3 Perl Pragmas......Page 879
A.4 Perl Modules......Page 881
A.5 Command-Line Switches......Page 887
A.6.2 The Perl Debugger......Page 889
A.6.3 Entering and Exiting the Debugger......Page 890
A.6.4 Debugger Commands......Page 891
B.1 What Is SQL?......Page 894
B.1.2 Executing SQL Statements......Page 895
B.1.3 About SQL Commands/Queries......Page 896
B.1.4 SQL and the Database......Page 899
B.1.5 SQL Database Tables......Page 900
B.2.1 The SELECT Command......Page 902
B.2.2 The INSERT Command......Page 916
B.2.3 The UPDATE Command......Page 917
B.2.4 The DELETE Statement......Page 918
B.3.1 Creating the Database......Page 919
B.3.2 SQL Data Types......Page 920
B.3.3 Creating a Table......Page 922
B.3.4 Creating a Key......Page 924
B.3.5 Relations......Page 927
B.3.6 Altering a Table......Page 930
B.4 SQL Functions......Page 932
B.4.1 Numeric Functions......Page 933
B.4.2 String Functions......Page 936
B.4.3 Date and Time Functions......Page 937
B.6 What You Should Know......Page 941
EXERCISE B......Page 943
C.2 A Little Background on DNA......Page 946
C.3 Some Perl Examples......Page 948
C.4 What Is BioPerl?......Page 950
C.5 Resources......Page 954
D.1 What Is mod_perl?......Page 956
D.2 The mod_perl Web Site......Page 958
D.3 Installing mod_perl......Page 959
D.4 Resources......Page 969
Index......Page 970
A......Page 971
B......Page 972
C......Page 973
D......Page 976
E......Page 977
F......Page 978
H......Page 983
I......Page 984
L......Page 985
M......Page 986
O......Page 989
P......Page 990
R......Page 993
S......Page 995
T......Page 999
V......Page 1000
W......Page 1001
Z......Page 1002