Tải bản đầy đủ (.pdf) (891 trang)

www.it-ebooks.info.www.it-ebooks.info.Developing Web Applications with Perl, memcached, MySQL® and ApacheForeword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxv Intr potx

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (6.15 MB, 891 trang )

www.it-ebooks.info
Galbraith cag.tex V2 - 06/02/2009 11:44am Page ii
www.it-ebooks.info
Galbraith cag.tex V2 - 06/02/2009 4:43pm Page i
Developing Web Applications with Perl,
memcached, MySQL
®
and Apache
Foreword xxv
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxvii
Chapter 1: LAMMP, Now with an Extra M . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Chapter 2: MySQL. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Chapter 3: Advanced MySQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
Chapter 4: Perl Primer. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
Chapter 5: Object-Oriented Perl. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211
Chapter 6: MySQL and Perl . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245
Chapter 7: Simple Database Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285
Chapter 8: memcached . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313
Chapter 9: libmemcached . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359
Chapter 10: Memcached Functions for MySQL . . . . . . . . . . . . . . . . . . . . . . . . . . 383
Chapter 11: Apache . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 417
Chapter 12: Contact List Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 503
Chapter 13: mod_perl . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 565
Chapter 14: Using mod_perl Handlers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 601
Chapter 15: More mod_perl . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 633
Chapter 16: Perl and Ajax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 707
Chapter 17: Search Engine Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 739
Appendix A: Installing MySQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 793
Appendix B: Configuring MySQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 811
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 831
www.it-ebooks.info


Galbraith cag.tex V2 - 06/02/2009 11:44am Page ii
www.it-ebooks.info
Galbraith ffirs.tex V3 - 06/03/2009 4:22pm Page iii
Developing Web Applications with Perl,
memcached, MySQL
®
and Apache
Patrick Galbraith
Wiley Publishing, Inc.
www.it-ebooks.info
Galbraith ffirs.tex V3 - 06/03/2009 4:22pm Page iv
Developing Web Applications with Perl, memcached, MySQL
®
and Apache
Published by
Wiley Publishing, Inc.
10475 Crosspoint Boulevard
Indianapolis, IN 46256
www.wiley.com
Copyright © 2009 by Wiley Publishing, Inc., Indianapolis, Indiana
Published simultaneously in Canada
ISBN: 978-0-470-41464-4
Manufactured in the United States of America
10987654321
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-8600. Requests to the Publisher for permission should be
addressed to the Permissions Department, John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030, (201)

748-6011, fax (201) 748-6008, or online at
/>.
Limit of Liability/Disclaimer of Warranty: The publisher and the author make no representations or warranties with
respect to the accuracy or completeness of the contents of this work and specifically disclaim all warranties, including
without limitation warranties of fitness for a particular purpose. No warranty may be created or extended by sales or
promotional materials. The advice and strategies contained herein may not be suitable for every situation. This work
is sold with the understanding that the publisher is not engaged in rendering legal, accounting, or other professional
services. If professional assistance is required, the services of a competent professional person should be sought.
Neither the publisher nor the author shall be liable for damages arising herefrom. The fact that an organization or
Web site is referred to in this work as a citation and/or a potential source of further information does not mean that
the author or the publisher endorses the information the organization or Web site may provide or recommendations
it may make. Further, readers should be aware that Internet Web sites listed in this work may have changed or
disappeared between when this work was written and when it is read.
For general information on our other products and services please contact our Customer Care Department within the
United States at (877) 762-2974, outside the United States at (317) 572-3993 or fax (317) 572-4002.
Library of Congress Control Number: 2009927343
Trademarks: Wiley, the Wiley logo, Wrox, the Wrox logo, Wrox Programmer to Programmer, and related trade dress
are trademarks or registered trademarks of John Wiley & Sons, Inc. and/or its affiliates, in the United States and
other countries, and may not be used without written permission. MySQL is a registered trademark of MySQL AB.
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.
Wiley also publishes its books in a variety of electronic formats. Some content that appears in print may not be
available in electronic books.
www.it-ebooks.info
Galbraith ffirs.tex V3 - 06/03/2009 4:22pm Page v
To my wonderful wife, Ruth, whom I have known for 27 years and who has stood by me while writing
this book, even when I couldn’t give her the time she deserved. Also, to my dear friend Krishna,
who gave me inspiration every day.
www.it-ebooks.info
Galbraith fcre.tex V3 - 06/02/2009 11:44am Page vi

Credits
Acquisitions Editor
Jenny Watson
Project Editor
Maureen Spears
Technical Editor
John Bokma
Production Editor
Rebecca Coleman
Copy Editor
Sara E. Wilson
Editorial Manager
Mary Beth Wakefield
Production Manager
Tim Tate
Vice President and Executive Group
Publisher
Richard Swadley
Vice President and Executive Publisher
Barry Pruett
Associate Publisher
Jim Minatel
Project Coordinator, Cover
Lynsey Stanford
Proofreader
Corina Copp, Word One
Indexer
Robert Swanson
www.it-ebooks.info
Galbraith fauth.tex V3 - 06/02/2009 11:46am Page vii

About the Author
Patrick Galbraith lives up in the sticks of southwestern New Hampshire near Mt. Monadnock with
his wife, Ruth. Since 1993, he has been using and developing open source software. He has worked on
various open source projects, including MySQL, Federated storage engine, Memcached Functions for
MySQL, Drizzle, and Slashcode, and is the maintainer of DBD::mysql. He has worked at a number of
companies throughout his career, including MySQL AB, Classmates.com, OSDN/Slashdot. He currently
works for Lycos. He is also part owner of a wireless broadband company, Radius North, which provides
Internet service to underserved rural areas of New Hampshire. His web site, which comes by way of a
5.8GHz Alvarion access unit up in a pine tree, is

.
About the Technical Editor
John Bokma is a self-employed Perl programmer and consultant from the Netherlands. He has been
working professionally in software development since 1994, moving his primary focus more and
more toward the Perl programming language. John and his wife, Esmeralda, currently live in the
state of Veracruz, Mexico, with their daughter Alice. John’s other two children, Jim and Laurinda,
live with their mother in New Zealand. For more information or to contact John, visit his web site at
/>.
www.it-ebooks.info
Galbraith fauth.tex V3 - 06/02/2009 11:46am Page viii
www.it-ebooks.info
Galbraith fack.tex V3 - 06/02/2009 11:46am Page ix
Acknowledgments
One weekend in 1993, I had the chance to go on a getaway to San Diego. Instead, I opted to stay home
and download, onto 26 floppies, Slackware Linux, which I promptly installed onto my Packard Bell 386.
I could never get the built-in video card to work with X, so I ended up buying a separate video card and
had to edit my XConfig file to get it to work. How much more interesting this was to do than editing a
config.sys and an autoexec.bat! From then on, I was hooked. I worked at Siemens Ultrasound Group in
Issaquah, Washington, at the time. An engineer there named Debra, when asked what was a good thing
to learn, said something I’ll never forget: ‘‘Learn Perl.’’ Debra — you were right!

I always wanted to be a C++ graphics programmer. That didn’t happen because of this thing called the
World Wide Web. I remember Ray Jones and Randy Bentson of Celestial Software showing me a program
called Mosaic, which allowed you to view text over the Internet. Images would be launched using XV.
Everywhere I worked, I had to write programs that ran on the Web, which required me to write CGI in
Perl. So much for my goal of being a C++ programmer — but I consider this a great trade for a great
career. (I did eventually get to write C++ for MySQL!)
I would first like to thank my editor, Maureen Spears, who is not only a great editor, but also a friend.
She gave me much-needed encouragement throughout the writing of this book.
A special thanks goes to John Bokma for his meticulous attention to detail and great knowledge of
Perl — particularly w ith regard to Perl programming style and convention that I didn’t realize had
changed over the last several years. I was somewhat set in my ways!
Thank you to Jenny Watson, who gave me the opportunity to write this book in the first place!
Thanks to Monty Widenius for creating MySQL and for being a mentor as well as a good friend, and
thanks, Monty, for looking over Chapters 1, 2, and 3! Thanks also to Brian Aker for being another great
mentor and friend, as well as being a software-producing machine with a scrolling page full of open
source software projects that he’s created, including Drizzle and libmemcached. Thanks to Sheeri Kritzer
for her encouragement and for listening to me — she finished her book not too long before I finished
mine, so she understood completely what I was going through.
I’d like to thank my friend, Wes Moran, head of design for Sourceforge, for providing the nice, clean,
simple HTML design I used for many of the examples in this book.
Thanks to Eric Day for his excellent input and review of chapters pertaining to Gearman.
A special thanks to Joaqu
´
ın Ruiz of Gear 6, who provided a lot of input on Chapter 1, as well as Jeff
Freund of Clickability and Edwin Desouza and Jimmy Guerrero of Sun, who put me in touch with others
and were great sources of memcached information.
I would like to thank my current colleagues at Lycos, and former colleagues at Grazr and MySQL, as
well as the team members of Drizzle, for their part in my professional development, which gave me the
ability to write this book. Thanks also to anyone I forgot to mention!
www.it-ebooks.info

Galbraith fack.tex V3 - 06/02/2009 11:46am Page x
Acknowledgments
Finally, I would like to thank the entire Open Source community. My life would not be the same without
open source software.
There’s a verse in an ancient book, the Bhagavad Gita, that aptly describes how people like Monty
Widenius, Linus Torvalds, Larry Wall, Brian Aker and other leaders within the Open Source community
inspire the rest of us:
‘‘Whatever action a great man performs, common men follow. And whatever standards he sets by exemplary acts,
all the world pursues.’’
x
www.it-ebooks.info
Galbraith ftoc.tex V2 - 06/02/2009 4:42pm Page xi
Contents
Foreword xxv
Introduction xxvii
Chapter 1: LAMMP, Now with an Extra
M
1
Linux 2
Apache 3
MySQL 4
memcached 5
Gear6 6
Clickability 6
GaiaOnline 7
How memcached Can Work for You 7
Perl 8
Other Technologies 10
Sphinx 10
Gearman 11

The New Picture 11
The Future of Open-Source Web Development and Databases 12
Projects to Watch! 13
Summary 13
Chapter 2: MySQL 15
How CGI and PHP Changed the Web Dramatically 15
About MySQL 16
MySQL Programs 19
Client Programs 20
Utility Programs 25
MySQL Daemon and Startup Utilities 27
Working with Data 28
Creating a Schema and Tables 29
Inserting Data 35
Querying Data 38
Updating Data 50
Deleting Data 52
www.it-ebooks.info
Galbraith ftoc.tex V2 - 06/02/2009 4:42pm Page xii
Contents
Replacing Data 56
INSERT ON DUPLICATE KEY UPDATE 57
Operators 58
Functions 59
Using Help 70
User-Defined Variables in MySQL 72
MySQL Privileges 74
MySQL Access Control Privilege System 75
MySQL Global System User 75
MySQL System Schema Grant Tables 76

Account Management 80
Summary 84
Chapter 3: Advanced MySQL 85
SQL Features 85
Stored Procedures and Functions 86
Triggers 94
Views 102
User Defined Functions 105
Storage Engines 111
Commonly Used Storage Engines 112
Storage Engine Abilities 113
Using Storage Engines 113
MyISAM 115
InnoDB 118
Archive 123
The Federated Storage Engine 125
Tina/CSV Storage Engine 130
Blackhole Storage Engine 132
Replication 133
Replication Overview 133
Replication schemes 134
Replication Command Options 137
Setting Up Replication 139
Searching Text 148
When to Use Sphinx 161
Summary 162
Chapter 4: Perl Primer 163
What Exactly Is Perl? 163
Perl Primer 165
xii

www.it-ebooks.info
Galbraith ftoc.tex V2 - 06/02/2009 4:42pm Page xiii
Contents
Perl Data Types 165
Scalars 165
Arrays 167
Hashes 167
File Handles 168
Type Globs 168
Subroutines 168
Variable Usage 168
References 169
Scalar Usage 173
Array Usage and Iteration 174
Working with Hashes 179
Writing to Files 184
STDOUT and STDERR 184
File Handles to Processes 185
Subroutines 186
Variable Scope 189
Packages 192
Perl Modules 193
Writing a Perl Module 194
@ISA array 197
Documenting Your Module 197
Making Your Module Installable 201
Testing 201
Adding a MANIFEST file 204
CPAN 205
Regex One-Liners 206

Storing Regular Expressions in Variables 207
Regex Optimizations 208
Perl 6 Tidbits 208
Summary 210
Chapter 5: Object-Oriented Perl 211
About Object Orientation 212
Object Orientation in Perl 213
Writing a Perl Class 213
Adding Methods 217
On-Demand Method Manifestation Using AUTOLOAD 221
Other Methods 231
Making Life Easier: Moose 240
Summary 244
xiii
www.it-ebooks.info
Galbraith ftoc.tex V2 - 06/02/2009 4:42pm Page xiv
Contents
Chapter 6: MySQL and Perl 245
Perl DBI 245
DBI and DBD 246
Installation 247
DBI API 247
Connect 249
$dsn 249
$username and $password 253
$attributes 253
connect_cached 254
Statement Handles 255
Writing Data 256
Reading Data 258

Fetch Methods, One Row at a Time 258
Fetch Methods — the Whole Shebang 259
Binding Methods 261
Binding Input Parameters 262
Binding Output Parameters 263
Other Statement Handle Methods 264
rows 264
dump_results 265
Statement Handle Attributes 265
MySQL-Specific Statement Handle Attributes 267
Multistep Utility Methods 269
do 270
selectall_arrayref 270
selectall_hashref 272
selectcol_arrayref 273
selectrow_array 273
selectrow_arrayref 274
selectrow_hashref 274
Other Database Handle Methods 274
last_insert_id 275
ping 275
clone 276
Transactional Methods — begin_work, commit, rollback 276
Stored Procedures 277
Error Handling 279
Server Admin 281
Summary 283
xiv
www.it-ebooks.info
Galbraith ftoc.tex V2 - 06/02/2009 4:42pm Page xv

Contents
Chapter 7: Simple Database Application 285
Planning Application Functionality 285
Schema Design 286
Writing Up a Wire-Frame 286
Declarations, Initializations 287
Program Entry Point 290
Table Creation Subroutine 292
Using information_schema 293
Listing Contacts 294
Editing a Contact 297
Inserting a Contact 301
Updating a Contact 302
Deleting a Contact 303
Testing update_contact, insert_contact, and delete_contact 304
Editing a Contact 304
Adding a Contact 305
Deleting a Contact 306
Lookup of a Contact 309
Testing Lookup of a Contact 310
Summary 312
Chapter 8: memcached 313
What Is memcached? 313
How memcached Is Used 315
What Is Gearman? 317
Caching Strategies 318
Installing memcached 318
Starting memcached 321
Startup Scripts 322
Installing the Cache::Memcached Perl Module 323

Using Cache::Memcached 323
Connecting, Instantiation 324
Memcached Operations 325
Cache::Memcached API 325
Simple Examples 328
Storing a Scalar 328
Complex Data Types 329
Add and Replace 330
A More Practical Example 331
User Application 331
Data Design 332
xv
www.it-ebooks.info
Galbraith ftoc.tex V2 - 06/02/2009 4:42pm Page xvi
Contents
UserApp Package 334
Instantiation 334
Database Connector Method 334
Data Retrieval Methods 335
Simple Accessor Methods 338
Data Modification Methods 339
Using UserApp 342
Memcached Connector Method 344
Caching Implementation Plan 345
Where to Add Caching? 345
Caching Key Scheme 346
Precaching 346
Precaching Cities 347
Precaching States 347
Using Instantiation for Precaching Method Calls 348

Modifying Accessor Methods to Use Cache 348
User Data Caching — Set Method Modifications 350
User Data Caching — Get Method Modifications 351
UserApp Now Has Caching! 352
Other Caching Issues 352
Summary 357
Chapter 9: libmemcached 359
What Is libmemcached? 359
libmemcached Features 360
Libmemcache Utility Programs 360
Installing libmemcached 360
libmemcached Utility Programs 361
memcat 361
memflush 362
memcp 362
memstat 362
memrm 363
memslap 363
memerror 364
libmemcached Perl Driver 364
Installation 365
Memcached::libmemcached and libmemcached API using Memcached::libmemcached 365
Connection Functions 366
libmemcached Behavioral Functions 366
Functions for Setting Values 369
xvi
www.it-ebooks.info
Galbraith ftoc.tex V2 - 06/02/2009 4:42pm Page xvii
Contents
Data Retrieval (get) Functions 370

Increment, Decrement, and Delete 371
Informational and Utility Functions 372
Object-Oriented Interface 373
Procedure Memcached::libmemcached Program Example 373
Object-Oriented Memcached::libmemcached Program Example 374
Cache::memcached::libmemcached 375
Performance Comparisons 376
Writing Your Own Comparison Script 377
Summary 380
Chapter 10: Memcached Functions for MySQL 383
What Are Memcached Functions for MySQL? 383
How Do the Memcached Functions for MySQL Work? 384
Install the Memcached Functions for MySQL 385
Prerequisites 385
Configure the Source 385
Build the Source 386
Install the UDF 386
Checking Installation 387
Using the Memcached Functions for MySQL 388
Establishing a Connection to the memcached Server 388
Setting Values 389
Fetching, Incrementing, and Decrementing Functions 395
Behavioral Functions 397
Statistical Functions 400
Version Functions 401
Using memcached UDFs 402
Single Database Handle Example 403
FunwithTriggers(andUDFs) 409
Read-Through Caching with Simple Select Statements 412
Updates, Too! 415

Summary 416
Chapter 11: Apache 417
Understanding Apache: An Overview 417
Understanding the Apache Modules API 419
Apache 2.2 Changes Since Apache 1.3 420
Apache 2.2 Request Phases 421
New and Modified Modules 423
xvii
www.it-ebooks.info
Galbraith ftoc.tex V2 - 06/02/2009 4:42pm Page xviii
Contents
Installing Apache 424
Installing Apache on Windows 425
Installing Apache and mod_perl on a Working UNIX System 427
Installing Apache on Apple OS X (10.5) 429
Apache Source Install on UNIX 429
Installing mod_perl from Source 433
Installing libapreq2 from Source 434
Apache Configuration 435
Configuration Section Container Directives 436
Basic Directives 440
Server Tuning Directives 444
Logging Directives 446
Error Directives 448
Access Control, Authentication, and Authorization 449
.htaccess File Directives 453
Indexing Directives 454
CGI Directives 457
VirtualHost Directives 459
Handler and Filter Directives 460

Client Handling 462
SSL Directives 463
Clickstream Analysis 466
Rewriting URLs 468
Conditional Pattern 471
Apache Reverse Proxying 478
Enabling mod_proxy 480
mod_proxy Directives 481
Apache Server Control 483
Apache Configuration Schemes 483
Source Install 484
Ubuntu/Debian 484
Centos/Redhat Variants 486
SUSE 487
Windows 489
Common Apache Tasks 492
Configuring a Name-Based Virtual Host 493
Setting Up HTTP Basic Authentication 495
Setting Up Digest Authentication 496
xviii
www.it-ebooks.info
Galbraith ftoc.tex V2 - 06/02/2009 4:42pm Page xix
Contents
Configuring a Secure Server 497
Settin Up a Secure Server with a Valid Secure Certificate 498
Setting up a Reverse Proxy with Two Virtual Hosts 499
Summary 501
Chapter 12: Contact List Application 503
Using MySQL and memcached Together 503
A CGI Program 504

CGI Apache Setup 504
Your Basic CGI Program, and Then Some 504
User Interface 506
Database Storage Requirements 513
Program Flow 515
First Things First 515
Program Implementation 516
WebApp Class Methods 529
Instantiation with the new() Method 531
Connection to MySQL 532
Connection to memcached 533
The getUsers() Method 534
The getUser() Method 537
The saveUser() Method 538
Database Methods 542
The insertUser() Method 542
The updateUser() Method 543
The deleteUsers() Method 545
The userExists() Method 547
Caching Methods 549
The saveUserToCache() Method 549
The cacheUsers() Method 550
The getUsersFromCache() Method 552
The userExistsInCache() Method 553
The deleteUserFromCache Method 554
The setMemcUIDList() Method 556
The updateMemcUIDList Method 556
The deleteMemcUIDList() Method 558
The getMemcUIDList Method 559
xix

www.it-ebooks.info
Galbraith ftoc.tex V2 - 06/02/2009 4:42pm Page xx
Contents
Other Methods 560
The getStates() Method 560
The getState() Method 561
The encodeUserData() Method 562
Testing 563
Summary 564
Chapter 13: mod_perl 565
New mod_perl 2.0 Features 566
Configuring mod_perl 566
mod_perl Configuration Directives 569
<Perl> Sections 569
PerlModule 570
PerlLoadModule 571
SetHandler perl-script 571
SetHandler modperl 571
PerlSetEnv 571
PerlPassEnv 572
PerlSetVar 572
PerlAddVar 572
PerlPostConfigRequire 573
PerlRequire 573
PerlOptions 573
PerlSwitches 574
POD 574
mod_perl Handler Directives 575
Handler Scope 575
Handler Type 575

Handler Category 576
Apache Life Cycle Overview 577
Server Life Cycle Phase Handlers 578
Connection Cycle Phase Handlers 578
Filter Handlers 579
Perl Apache2 Modules 585
Apache2 Constants and Request Record Perl Modules 586
Apache2 Connection and Filter Record Modules 590
Apache2 Server Record Modules 591
Apache2 Configuration Modules 592
Apache2 Resource/Performance, Status, and Other Modules 594
Summary 598
xx
www.it-ebooks.info
Galbraith ftoc.tex V2 - 06/02/2009 4:42pm Page xxi
Contents
Chapter 14: Using mod_perl Handlers 601
PerlResponseHandler Example 601
Initial Handler Setup 602
Log Messages Using the Server Object and Form Parsing 602
Setting the Log Level and Printing the HTTP Header 603
Redirection 603
Print the Document Header 604
Connection mod_perl Handlers 607
PerlPreConnectionHandler Example 608
Other HTTP Request Cycle Phase Handlers 612
PerlAccessHandler Example 612
PerlAuthenHandler Example 615
PerlAuthzHandler Example 619
PerlLogHandler Example 622

Perl Filter Handler Example 627
Summary 630
Chapter 15: More mod_perl 633
mod_perl Handlers or ModPerl::Registry? 633
Using ModPerl::RegistryLoader 634
Converting a ModPerl::Registry Script to a mod_perl Handler 635
Converting a mod_perl Handler to a ModPerl::Registry Script 641
Dealing with Cookies 643
CookieTestHandler 643
Tools for Testing Cookies and Headers 649
Generic Database Methods 651
dbGetRef() 652
dbInsert() 653
dbUpdate() 654
dbDelete() 655
whereClause() 656
buildUpdate() 658
buildInsert () 659
Other Changes to WebApp 660
Session Management 662
Implementing the mod_perl Handler LoginHandler 663
Understanding the WebApp Class 667
Storing Session Data 670
xxi
www.it-ebooks.info
Galbraith ftoc.tex V2 - 06/02/2009 4:42pm Page xxii
Contents
File Upload mod_perl Handler 675
Storing Files in the Database or Not? 675
Database Table 676

mod_perl Handler Implementation 676
Methods That Need to be Added to WebApp 682
Using the mod_perl Upload Handler 685
Templating 686
Template Toolkit 686
Features 687
Plug-Ins to Template Toolkit 687
Template Toolkit Syntax 687
A mod_perl Handler Example Using Template Toolkit 690
Caching Templates 693
HTML::Template 694
Tags 694
A mod_perl Handler Example Using HTML::Template 695
HTML::Template template 697
HTML::Mason (Mason) 698
Mason Syntax 698
In-Line Perl Sections 699
Mason Objects 700
Mason Components 700
Initialization and Cleanup 702
Userlisting Page in Mason 703
Summary 704
Chapter 16: Perl and Ajax 707
What Is Ajax? 707
mod_perl Applications and Ajax 708
Basic Ajax Examples 708
More Examples Using the JSON Perl Module 713
Summary 738
Chapter 17: Search Engine Application 739
Using Gearman to Put the S earch Engine Application Together 740

Gearman 740
Installing and Running Gearman 741
Using the Gearman MySQL UDFs 744
Perl and Gearman 746
xxii
www.it-ebooks.info
Galbraith ftoc.tex V2 - 06/02/2009 4:42pm Page xxiii
Contents
The Search Engine Application 747
Database Tables for the Search Engine Application 749
Database Triggers 751
Sphinx Setup 752
Gearman Workers 756
Running the Workers 764
mod_perl Handler Web Applications 766
Search Application 766
Using the Search Application 777
URL Queue Application 778
URLHandler — AJAX Application 779
URLQueueHandler mod_perl Handler 787
URLQueueHandler handler() Subroutine 787
URLQueue Interface 790
Summary 792
Appendix A: Installing MySQL 793
Choosing a MySQL Version 793
Choosing a MySQL Package Type 794
Installing MySQL on Windows 795
Installing MySQL on RPM-based Linux Systems 804
Installing MySQL on Ubuntu 804
Installing MySQL from Source on UNIX Systems 807

Unix Post Install 809
Appendix B: Configuring MySQL 811
Running MySQL for the First Time 811
Setting Up Privileges and Creating a Schema 812
MySQL Server Configuration File 812
Basic Command Options 813
InnoDB Path and Tablespace Command Options 815
Backups 817
Replication Backup Slave 817
mysqldump 818
Scripting mysqldump Backups with Perl 818
Creating a Backup by Copying Data Files 820
mysqlhotcopy 821
Snapshots Using LVM 821
InnoDB Hotbackup, ibbackup 822
xxiii
www.it-ebooks.info

×