ebook img

The Definitive Guide to Catalyst: Writing Extensible, Scalable, and Maintainable Perl-Based Web Applications PDF

346 Pages·2009·2.18 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview The Definitive Guide to Catalyst: Writing Extensible, Scalable, and Maintainable Perl-Based Web Applications

The Definitive Guide to Catalyst Writing Extensible, Scalable, and Maintainable Perl-Based Web Applications Kieren Diment and Matt S Trout with Eden Cardim, Jay Kuri, and Jess Robinson The Definitive Guide to Catalyst: Writing Extensible, Scalable, and Maintainable Perl-Based Web Applications Copyright © 2009 by Kieren Diment and Matt S Trout All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. ISBN-13 (pbk): 978-1-4302-2365-8 ISBN-13 (electronic): 978-1-4302-2366-5 Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1 Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. Lead Editor: Duncan Parkes Technical Reviewer: Jacinta Richardson Editorial Board: Clay Andres, Steve Anglin, Mark Beckner, Ewan Buckingham, Tony Campbell, Gary Cornell, Jonathan Gennick, Michelle Lowman, Matthew Moodie, Jeffrey Pepper, Frank Pohlmann, Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh Project Manager: Denise Santoro Lincoln Copy Editor: Nicole Flores Associate Production Director: Kari Brooks-Copony Production Editor: Kelly Winquist Compositor: Patrick Cunningham Proofreader: April Eddy Indexer: Carol Burbo Artist: April Milne Cover Designer: Kurt Krames Manufacturing Director: Tom Debolski Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax 201-348-4505, e-mail (cid:107)(cid:110)(cid:96)(cid:97)(cid:110)(cid:111)(cid:41)(cid:106)(cid:117)(cid:60)(cid:111)(cid:108)(cid:110)(cid:101)(cid:106)(cid:99)(cid:97)(cid:110)(cid:41)(cid:111)(cid:94)(cid:105)(cid:42)(cid:95)(cid:107)(cid:105), or visit (cid:100)(cid:112)(cid:112)(cid:108)(cid:54)(cid:43)(cid:43)(cid:115)(cid:115)(cid:115)(cid:42)(cid:111)(cid:108)(cid:110)(cid:101)(cid:106)(cid:99)(cid:97)(cid:110)(cid:107)(cid:106)(cid:104)(cid:101)(cid:106)(cid:97)(cid:42)(cid:95)(cid:107)(cid:105). For information on translations, please contact Apress directly at 2855 Telegraph Avenue, Suite 600, Berkeley, CA 94705. Phone 510-549-5930, fax 510-549-5939, e-mail (cid:101)(cid:106)(cid:98)(cid:107)(cid:60)(cid:93)(cid:108)(cid:110)(cid:97)(cid:111)(cid:111)(cid:42)(cid:95)(cid:107)(cid:105), or visit (cid:100)(cid:112)(cid:112)(cid:108)(cid:54)(cid:43)(cid:43)(cid:115)(cid:115)(cid:115)(cid:42) (cid:93)(cid:108)(cid:110)(cid:97)(cid:111)(cid:111)(cid:42)(cid:95)(cid:107)(cid:105). Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Special Bulk Sales–eBook Licensing web page at (cid:100)(cid:112)(cid:112)(cid:108)(cid:54)(cid:43)(cid:43)(cid:115)(cid:115)(cid:115)(cid:42)(cid:93)(cid:108)(cid:110)(cid:97)(cid:111)(cid:111)(cid:42)(cid:95)(cid:107)(cid:105)(cid:43)(cid:101)(cid:106)(cid:98)(cid:107)(cid:43)(cid:94)(cid:113)(cid:104)(cid:103)(cid:111)(cid:93)(cid:104)(cid:97)(cid:111). The information in this book is distributed on an “as is” basis, without warranty. Although every precau- tion has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indi- rectly by the information contained in this work. The source code for this book is available to readers at (cid:100)(cid:112)(cid:112)(cid:108)(cid:54)(cid:43)(cid:43)(cid:115)(cid:115)(cid:115)(cid:42)(cid:93)(cid:108)(cid:110)(cid:97)(cid:111)(cid:111)(cid:42)(cid:95)(cid:107)(cid:105). For Lorna, Freya, and Hunter Contents at a Glance Foreword ........................................................................ xv About the Authors ................................................................xix About the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxi Acknowledgments ...............................................................xxiii Introduction .................................................................... xxv CHAPTER 1 Introducing the Catalyst Web Application Framework .............1 CHAPTER 2 Catalyst Setup and Background Knowledge .....................19 CHAPTER 3 Your First Catalyst Application ..................................53 CHAPTER 4 Extending LolCatalyst-Lite ......................................81 CHAPTER 5 Deployment ...................................................109 CHAPTER 6 Database Models ..............................................135 CHAPTER 7 The Catalyst Dispatcher .......................................167 CHAPTER 8 Authentication and Authorization ..............................189 CHAPTER 9 Interacting with Web Services in Your Applications .............219 CHAPTER 10 Extending Catalyst ............................................233 CHAPTER 11 Catalyst Cookbook ............................................247 CHAPTER 12 The Reaction Component UI Framework ........................303 APPENDIX A Compiling Your Own Perl ......................................315 APPENDIX B Making Your Own CPAN Mirror with CPAN::Mini or minicpan.sh ................................317 INDEX .......................................................................319 v Contents Foreword ........................................................................ xv About the Authors ................................................................xix About the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxi Acknowledgments ...............................................................xxiii Introduction .................................................................... xxv CHAPTER 1 Introducing the Catalyst Web Application Framework ....1 Catalyst’s Usage and History .......................................2 The Model/View/Controller Pattern ..................................2 URL Path Dispatch Logic ......................................6 High-Level Session Management ..............................7 Authentication and Authorization ..............................7 Logging ....................................................7 Templates ..................................................8 Testing .....................................................8 Deployment .................................................8 Catalyst vs. CGI ..................................................9 Introducing Persistence FastCGI ..............................12 mod_perl ..................................................12 Catalyst vs. Other Frameworks ....................................13 URI Routing via External File vs. Self-Contained Controllers ......13 Catalyst vs. CGI::Application ..................................14 Catalyst vs. Jifty ............................................16 Summary .......................................................17 CHAPTER 2 Catalyst Setup and Background Knowledge ..............19 Installing Catalyst ................................................19 Choosing Hardware and Software .............................19 Obtaining Perl ..............................................21 Setting Up CPAN ...........................................23 Installation .................................................24 vii viii (cid:78)CONTENTS Catalyst Background Knowledge ..................................24 Hypertext Transfer Protocol ..................................24 Object-Oriented Programming with Moose .....................28 Making the Most of CPAN ........................................44 Search Strategies for CPAN ..................................45 Basic CPAN Usage ..........................................47 Summary .......................................................52 CHAPTER 3 Your First Catalyst Application .............................53 Sample Application Overview .....................................53 Files and Directories .............................................54 Top-Level Directory .........................................54 lib Directory ................................................55 root Directory ..............................................55 script Directory ............................................55 t Directory .................................................56 Getting Started Writing Our Application .............................57 Writing the Application ...........................................59 A Brief Explanation of Views ......................................61 Our First Template ...............................................62 A Brief Introduction to Catalyst Models .............................65 Adding a Different View ..........................................66 Testing .........................................................67 Automated Testing ..........................................69 Fixing the Plan .............................................71 Checking and Fixing the JSON Request ........................72 Adding Authentication for the Web Portion of Our Application ..........74 Adding a New User .........................................75 Requiring Authentication in the Application .........................75 Requiring Authentication for Web Services Requests Only ........75 Requiring Authentication for the Whole Application ..............79 Summary .......................................................79 CHAPTER 4 Extending LolCatalyst-Lite .................................81 First Comes the Model ...........................................82 New Model Capabilities. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .82 Application As Pure UI .......................................82 Measure Twice, Code Once ..................................82 Always Run the First Code from a Test ........................83 (cid:78)CONTENTS ix Extracting the Model .............................................83 Moving the Model Logic into an Application-Independent Class ...84 Extending the Model .............................................87 A Driver Model for Generic Translation ........................88 Fixing Up the Makefile.PL ....................................89 Enforcing the Interface Using a Role ...........................93 Interlude: LolCatalyst::Lite::Model::SnippetStore ................94 Another Interlude: Spotting Business Logic Errors ...............96 Extending the Application .........................................96 First New Features ..........................................97 A Chained Controller ........................................97 Adding Templates .........................................100 Back to the Test Cycle. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .101 Interlude: Converting the Snippets to Be Object Based ..........101 Thin Controller, Smart Model .....................................102 Adding Tests for the New View Functionality ..................105 Summary ......................................................108 CHAPTER 5 Deployment .................................................109 Using the Built-in Server ........................................109 Deploying with the Built-in Server ...........................110 HTTP::Prefork Engine Behind a Front-End Proxy ...............111 FastCGI Deployment ............................................117 Deployment on Shared Hosting ..............................118 FastCGI Deployment on Your Own Web Server .................118 Deployment on mod_perl .......................................123 Using Apache As a Back End to a Front-End Proxy .............125 Getting Your Code to the Server ..................................125 Rsync ....................................................125 Version Control Checkout ...................................126 make dist .................................................126 To make install or Not to make install, That Is the Question .....127 Dependency Installation .........................................128 The cpan . Command ......................................130 make installdeps ..........................................131 make catalyst_par .........................................132 Summary ......................................................133 x (cid:78)CONTENTS CHAPTER 6 Database Models ...........................................135 Choosing and Connecting to a Database ...........................135 Popular Database Systems .................................135 Connecting to the Database from Catalyst ....................136 Designing and Creating the Sample Application Database ...........137 Creating the Catalyst Model ......................................139 Exploring the DBIx::Class Schema Files. . . . . . . . . . . . . . . . . . . . . . . . . . . .141 The Schema File ...........................................141 The Result Files ...........................................142 Deploying to a Different Database System .........................147 Dealing with Data ..............................................149 Simple CRUD ..............................................150 Creating a User ............................................151 Displaying Users ...........................................155 Updating Users ............................................157 Adding Roles ..............................................159 Adding Roles to Users ......................................160 Removing a User ..........................................163 Listing All Users and Their Roles .............................164 Summary ......................................................166 CHAPTER 7 The Catalyst Dispatcher ...................................167 How Catalyst Prepares Before Dispatch ...........................169 The Basic Dispatch Process ......................................169 Two Special Dispatch Types: begin and auto .......................171 LocalRegex and Regex Actions ..............................175 Internal Request Flow Control: $c->forward, $c->detach, $c->go, and $c->visit ............................177 forward and detach ........................................178 visit and go ...............................................180 The Chained Dispatch Type ......................................181 Overriding the Public Dispatch Paths with Configuration .............187 Summary ......................................................188 (cid:78)CONTENTS xi CHAPTER 8 Authentication and Authorization ........................189 The Three Parts of Authentication ................................189 Authentication Realms .....................................190 Persistence ...............................................191 Authorization with Roles ....................................192 Installing the Required Modules ..................................193 Catalyst::Plugin::Authentication ..............................193 Catalyst::Authentication::Store::DBIx::Class ...................193 Catalyst::Plugin::Authorization::Roles .........................194 Catalyst::Plugin::Session ....................................194 Our Example Application: SneakyCat ..............................194 Creating and Configuring SneakyCat .........................195 The SimpleDB Option ......................................200 A Side Trip into Application Setup ............................200 Using Authentication .......................................203 Working with Authentication After Login ......................207 Trying Out the Application ..................................209 Adding Finer-Grained Access Control to SneakyCat with Authorization ..........................211 Working with the Authenticated User .........................216 Summary ......................................................218 CHAPTER 9 Interacting with Web Services in Your Applications .....219 Consuming a Simple REST Service ...............................219 Setting Up OpenCalais in a Catalyst Application ....................220 Creating Web Services with REST ................................224 Implementing the REST Controller ...........................224 Changing the Data Type for Serialization ......................228 Remaining Options for Web Services .........................229 Summary ......................................................231 CHAPTER 10 Extending Catalyst .........................................233 Base Controllers ................................................233 A Simple SerialiseStashKey View .................................234 Action Classes .................................................237 xii (cid:78)CONTENTS Adding Attributes to Controller Actions ............................237 A Simple Example .........................................238 A More Sophisticated Example ..............................238 Adding More Than One New Attribute ........................240 Cleaning Up Multiple Attributes with Roles ....................241 Extending Catalyst with Plug-ins .................................242 Helper Methods in MyApp.pm ...............................242 Catalyst::Plugin::SmartURI ..................................244 Catalyst::Plugin::Static::Simple ..............................244 Catalyst::Plugin::Server .....................................245 Summary ......................................................245 CHAPTER 11 Catalyst Cookbook .........................................247 Model/View/Controller ...........................................247 Import Existing Schemas to DBIx::Class ......................248 Use DBIx::Class::Tree and Display with JavaScript and the Template Toolkit .................................249 Catalyst::Model::Factory and Catalyst::Model:: Factory::PerRequest .....................................254 A Way to Quickly Resolve Browser Compatibility Issues .........255 Output CSV from a Controller Action ..........................258 Dynamically Generate Graphics with Catalyst::View::GD ........259 Dynamically Generate Barcodes from Catalyst .................260 Produce Graphs with Chart::Clicker and Catalyst:: View::Graphics::Primitive .................................262 Alternative Templating Systems .............................264 Easy Spam Protection with Catalyst::Controller::reCAPTCHA .....267 Catalyst::View::Component::SubInclude. . . . . . . . . . . . . . . . . . . . . . .269 Handle Legacy CGI Scripts in Catalyst ........................270 Intelligent Use of Base Controllers ...........................272 Catalyst::View::Email::Template .............................275 Miscellaneous Recipes ..........................................277 Run Parts of an Application As a Stand-Alone Script ............277 MojoMojo: The Catalyst Wiki ................................278 Persistent Login and API Authentication ......................281 Multirealm and Progressive Authentication ....................286 Deploy with a Cache .......................................289

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.