O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Embedding Perl in HTML with Mason

Book Description

Mason doesn't aim to be the one true Perl-based templating system for building web sites, but it's led many programmers to abandon their custom solutions when they've seen how much easier using Mason can be. It's a powerful, open source, Perl-based web site development and delivery engine, with features that make it an ideal backend for high load sites serving dynamic content. Mason uses a concept called components: a mix of HTML, Perl, and special Mason commands. These components can be entire web pages, or bits of HTML that can be embedded in top-level components. Shared and reusable, these components greatly simplify site maintenance: when you change a shared component, you instantly change all pages that refer to it.Although using Mason isn't difficult, creating a Mason-based site can be tricky. Embedding Perl in HTML with Mason, written by members of Mason's core development team, shows you how to take advantage of Mason's strengths while avoiding the obstacles that inexperienced users may encounter. Mason's unique features, when used properly, can streamline the design of a web site or application. This concise book covers these features from several angles, and includes a study of the authors' sample site where these features are used.Embedding Perl in HTML with Mason shows you how to create large, complex, dynamically driven web sites that look good and are a snap to maintain. You'll learn how to visualize multiple Mason-based solutions to any given problem and select among them. The book covers the latest line of Mason development 1.1x, which has many new features, including line number reporting based on source files, sub-requests, and easier use as a CGI. The only book to cover this important tool, Embedding Perl in HTML with Mason is essential reading for any Perl programmer who wants to simplify web site design. Learn how to use Mason, and you'll spend more time making things work, and less time reinventing the wheel.

Table of Contents

  1. Foreword
  2. Preface
    1. Intended Audience
    2. Requirements
    3. How to Read This Book
    4. Overview
    5. Other Resources
    6. Typographic Conventions
    7. We’d Like to Hear from You
    8. Open Publication License
    9. Acknowledgments
  3. 1. Introduction
    1. A First Example
    2. The Main Features of Mason
      1. Components: Modular Design Elements
      2. Object-Style Component Inheritance
      3. Intelligent Caching Mechanisms
      4. Integration with Apache and mod_perl
    3. Alternatives to Mason
      1. Embperl
      2. Apache::ASP
      3. HTML::Template
      4. Text::Template
      5. Template Toolkit
      6. PHP
    4. Philosophy
    5. Getting Started with Mason
      1. A Standalone Installation
      2. A mod_perl Installation
      3. Trying It Out
  4. 2. Components
    1. Mason from 10,000 Feet
    2. Core Concepts
    3. Basic Component Syntax
      1. Substitution Tags: <% %>
        1. Escaping substitutions
      2. Embedded Perl: % Lines and <%perl> Blocks
      3. Calling Other Components: <& &> Tags
        1. Components called with content
      4. Other Named Blocks
        1. <%init> blocks
        2. <%args> blocks
        3. <%filter> blocks
        4. <%once> blocks
        5. <%cleanup> blocks
        6. <%text> blocks
        7. <%doc> blocks
        8. <%flags> and <%attr> blocks
        9. <%def> and <%method> blocks
        10. <%shared> blocks
      5. Escaping a Newline
    4. Component Arguments
      1. <%args> Block Revisited
      2. %ARGS
      3. %ARGS Versus @_
      4. Argument Examples
      5. Arguments via Component Calls
      6. Arguments via HTTP Requests
    5. Component Return Values
    6. Special Globals
      1. $m
      2. $r
    7. Sample Component
  5. 3. Special Components: Dhandlers and Autohandlers
    1. Dhandlers
      1. Finer Control over Dhandlers
      2. D handlers and Apache Configuration
    2. Autohandlers
      1. Using Autohandlers for Initialization
      2. Using Autohandlers as Filters
      3. Inspecting the Wrapping Chain
    3. Using Autohandlers and Dhandlers Together
  6. 4. APIs
    1. Request Class and Object API
      1. Constructor Parameters
      2. Calling Other Components
      3. Aborting the Flow of Execution
      4. The Wrapping Chain
      5. Dhandler-Related Methods
      6. Miscellaneous Methods
      7. Introspection
      8. Buffer-Related Methods
      9. Caching
      10. Subrequests
      11. Methods Available Only When Using ApacheHandler
      12. Methods Available When Using ApacheHandler or CGIHandler
      13. Getting in Close with Buffers
    2. Component Object API
      1. Methods for File-based Components
    3. Buffers
  7. 5. Advanced Features
    1. Subcomponents
    2. Creating Components on the Fly
    3. Sharing Data Among Component Sections
    4. Methods and Attributes
      1. Methods
      2. Using Methods for Titles and Headers
      3. Methods with Dynamic Content
      4. Attributes
      5. Top-Down Versus Bottom-Up Inheritance
    5. Calling Components with Content Blocks
    6. Advanced Inheritance
      1. Inheritance and Multiple Component Roots
      2. An Advanced Inheritance Example
    7. Subrequests
      1. A Caution About Autohandler Inheritance
  8. 6. The Lexer, Compiler, Resolver, and Interpreter Objects
    1. Passing Parameters to Mason Classes
    2. The Lexer
    3. The Compiler
      1. Altering Every Component’s Content
      2. Compiler Methods
    4. The Resolver
    5. The Interpreter
      1. Request Parameters Passed to the Interpreter
  9. 7. Using Mason with mod_perl
    1. Configuring Mason
      1. Configuration via httpd.conf
      2. Configuration via Custom Code
    2. Document Root Versus the Component Root
    3. Not OK
    4. $r
    5. ApacheHandler Parameters
    6. To Autoflush or Not to Autoflush
    7. Generating Something Besides HTML
    8. Apache::Status and Mason
  10. 8. Building a Mason Site
    1. Functionality
    2. Directory Layout
    3. File Extensions
    4. Apache Configuration
    5. The Components
      1. The Unrestricted Parts
    6. Components with Access Controls
    7. All Done
    8. Further Directions
  11. 9. Mason and CGI
    1. CGI-Appropriate Situations
    2. CGI-Inappropriate Situations
    3. Creating a CGI-Based Site in Mason
    4. Using Mason Templates Inside Regular CGI Scripts
      1. Design Considerations
    5. Differences Between Mason Under CGI and mod_perl
  12. 10. Scalable Design
    1. Modules Versus Components
      1. The Other Side
    2. Components as Independent Units
    3. Component Layout
    4. File Naming and Directory Layout
    5. Random Advice
  13. 11. Recipes
    1. Sessions
      1. Without Touching httpd.conf
      2. Predeclaring the Global via an httpd.conf File
      3. Predeclaring the Global via a handler.pl Script
      4. Using Cache::Cache for Sessions
      5. Putting the Session ID in the URL
    2. Making Use of Autoflush
    3. User Authentication and Authorization
      1. Using Apache::AuthCookie
      2. Authentication Without Cookies
      3. Access Controls with Attributes
    4. Co-Branding Color Schemes
      1. With Stylesheets
      2. With Code
    5. Developer Environments
      1. Multiple Component Roots
        1. By path
        2. By hostname
      2. Multiple Server Configurations
      3. Managing DBI Connections
    6. Using Mason Outside of Dynamic Web Sites
      1. Generating a Static Site from Components
      2. Generating Config Files
  14. 12. Custom Mason Subclasses
    1. Class::Container as a Superclass
    2. Syntax: Your Very Own Lexer
    3. Output: Compiling to a Different Output
    4. Storage: Replacing the Resolver
    5. Request: A Request Object with a Built-in Session
    6. Argument Munging: ApacheHandler
    7. More Reader Exercises
  15. A. The Mason API
    1. Interpreter
      1. Object Properties
      2. Directories
      3. Runtime Methods
    2. Request
      1. Object Properties
      2. Altering the Request Flow
      3. Caching
      4. Introspection
      5. Content and Output
      6. Fetching/Running Components
      7. Subrequests
    3. Component
      1. Object Properties
      2. Component Relationships
      3. Inheritance
    4. Resolver
    5. ApacheHandler
    6. CGIHandler
    7. Compiler
      1. Object Properties
      2. Compilation Callbacks
    8. Lexer
  16. B. Object Constructor Parameters
    1. HTML::Mason::Interp
    2. HTML::Mason::Request
    3. HTML::Mason::Resolver::File
    4. HTML::Mason::ApacheHandler
    5. HTML::Mason::Compiler
    6. HTML::Mason::Component
    7. HTML::Mason::Buffer
    8. HTML::Mason::CGIHandler
    9. HTML::Mason::Lexer
  17. C. Text Editors That Understand Mason
    1. Emacs
    2. Vim
  18. D. Content Management with Bricolage
    1. Installing Bricolage
    2. Elements: the Building Blocks of Content
    3. Content Editing
    4. Templates
    5. Where to Learn More
  19. Glossary
  20. Index
  21. About the Authors
  22. Colophon
  23. Copyright