Product Details
Advanced Perl Programming

Advanced Perl Programming
By Simon Cozens

List Price: £30.99
Price: £15.52 & eligible for FREE Super Saver Delivery on orders over £5. Details

Availability: Usually dispatched within 24 hours
Dispatched from and sold by Amazon.co.uk

23 new or used available from £11.50

Average customer review:

Product Description

With a worldwide community of users and more than a million dedicated programmers, Perl has proven to be the most effective language for the latest trends in computing and business. Every programmer must keep up with the latest tools and techniques. This updated version of "Advanced Perl Programming" from O'Reilly gives you the essential knowledge of the modern Perl programmer. Whatever your current level of Perl expertise, this book will help you push your skills to the next level and become a more accomplished programmer. O'Reilly's most high-level Perl tutorial to date, "Advanced Perl Programming", Second Edition teaches you all the complex techniques for production-ready Perl programs. This completely updated guide clearly explains concepts such as introspection, overriding built-ins, extending Perl's object-oriented model, and testing your code for greater stability. Other topics include: Complex data structures; Parsing; Templating toolkits; Working with natural language data; Unicode; and Interaction with C and other languages. In addition, this guide demystifies once complex topics like object-relational mapping and event-based development-arming you with everything you need to completely upgrade your skills. Praise for the Second Edition: "Sometimes the biggest hurdle to problem solving isn't the subject itself but rather the sheer number of modules Perl provides. "Advanced Perl Programming" walks you through Perl's TMTOWTDI ("There's More Than One Way To Do It") forest, explaining and comparing the best modules for each task so you can intelligently apply them in a variety of situations." - Rocco Caputo, lead developer of POE. "It has been said that sufficiently advanced Perl code is indistinguishable from magic. This book of spells goes a long way to unlocking those secrets. It has the power to transform the most humble programmer into a Perl wizard." - Andy Wardley. "The information here isn't theoretical. It presents tools and techniques for solving real problems cleanly and elegantly." - Curtis 'Ovid' Poe. " "Advanced Perl Programming" collects hard-earned knowledge from some of the best programmers in the Perl community, and explains it in a way that even novices can apply immediately." - chromatic, Editor of Perl.com.


Product Details

  • Amazon Sales Rank: #54149 in Books
  • Published on: 2005-06-28
  • Original language: English
  • Number of items: 1
  • Binding: Paperback
  • 281 pages

Editorial Reviews

From the Publisher
O'Reilly's most high-level Perl tutorial to date, Advanced Perl Programming, Second Edition teaches you all the complex techniques for production-ready Perl programs. This completely updated guide clearly explains concepts such as introspection, overriding built-ins, extending Perl's object-oriented model, and testing your code for greater stability. Whatever your current level of Perl expertise, this book will help you push your skills to the next level and become a more accomplished programmer.

About the Author
Simon Cozens is an Open Source programmer and author. He has released over a hundred Perl modules including Email::Simple, Mail::Audit, Maypole, Plucene, and B::Generate. He's the co-author of Beginning Perl (Wrox) and Extending and Embedding Perl (Manning) and was the managing editor of Perl.com from 2001 to 2004. A graduate in Japanese from Oxford University, he now lives in Wales and enjoys Japanese and Greek food, bizarre music and fine typography.

Excerpted from Advanced Perl Programming by Simon Cozens. Copyright © 2005. Reprinted by permission. All rights reserved.
CHAPTER 3 Templating Tools

A recent thread on comp.lang.perl.moderated enumerated the Perl rites of passage— the perfectly good wheels that every journeyman Perl programmer reinvents. These were found to be a templating system, a database abstraction layer, an HTML parser, a processor for command-line arguments, and a time/date handling module.

See if you recognize yourself in the following story: you need to produce a form letter of some description. You’ve got a certain amount of fixed content, and a certainamount that changes. So you set up a template a little like this:

my $template = q{
Dear $name,

We have received your request for a quote for $product, and have
calculated that it can be delivered to you by $date at a cost of
approximately $cost.

Thank you for your interest,

Acme Integrated Foocorp.
};

Then you struggle with some disgusting regular expression along the lines of s/(\$\w+)/$1/eeg, and eventually you get something that more or less does the job.

As with all projects, the specifications change two days after it goes live, so you suddenly need to extend your simple template to handle looping over arrays, conditionals, and eventually executing Perl code in the middle of the template itself. Before you realize what’s happened, you’ve created your own templating language.

Don’t worry if that’s you. Nearly everyone’s done it at least once. That’s why there’s a wide selection of modules on CPAN for templating text and HTML output, ranging from being only slightly more complex than s/(\$\w+)/$1/eeg to complete independent templating languages.

Before we start looking at these modules, though, let’s consider the built-in solution—the humble Perl format.

Formats and Text::Autoformat

Formats have been in Perl since version 1.0. They’re not used very much these days, but for a lot of what people want from text formatting, they’re precisely the right thing.

Perl formats allow you to draw up a picture of the data you want to output, and then paint the data into the format. For instance, in a recent application, I needed to display a set of IDs, dates, email addresses, and email subjects with one line per mail. If we assume that the line is fixed at 80 columns, we may need to truncate some of those fields and pad others to wider than their natural width. In pure Perl, there are basically three ways to get this sort of formatted output. There’s sprintf (or printf) and substr:

for (@mails) {
printf "%5i %10s %40s %21s\n",
$_->id,
substr($_->received,0,10),
substr($_->from_address,-40,40),
substr($_->subject,0,21);
}

Then there’s pack, which everyone forgets about (and which doesn’t give as much control over truncation):

for (@mails) {
print pack("A5 A10 A40 A21\n",
$_->id, $_->received, $_->from_address, $_->subject);
}

And then there’s the format:

format STDOUT =
@id $_->received $_->from_address $_->subject
.
for (@mails) {
write;
}

Personally, I think this is much neater and more intuitive than the other two solutions — and has the bonus that it takes the formatting away from the main loop, making the code less cluttered.*

* As it happens, I didn’t actually use formats in my code, because I wanted to have a variable-width instead of a fixed-width display. But for cases where a fixed-width output is acceptable, this solution is perfect

Formats are associated with a particular filehandle; as you can see from the example, we’ve determined that this format should apply to anything we write on standard output. The picture language of formats is pretty simple: fields begin with @ or ^ and are followed by characters specifying left, center, and right justified respectively. After each line of fields comes a line of expressions that fill those fields, one expression for each field. If we like, we could change the format to multiple lines of fields and expressions:

format STDOUT =
Id : @id
Date : @<<From : @from_address
Subject : @<<<<<<<<<<<<<<<<<<<<<<.


Customer Reviews

A very different beast to the first edition4
As other reviewers have noted, there's not a whole lot in common with the first edition of this book, either in feel or content. It's rather questionable whether this merits being called a second edition. Something like 'Problem solving with CPAN' would be a more accurate title (then again, perhaps it's just as well I don't work in the publishing industry).

It does still cover some of the material of the first edition, such as globs, closures, AUTOLOAD, the Perl class model, and some Perl internals, but it's all been compressed into one chapter.

The other chapters discuss various subjects using CPAN modules and gives some insight into how the material from the first chapter was used to solve these problems. A wide variety of issues are discussed, including serialization and object relational mapping, natural language parsing, templating and unicode. Some superficially similar material could be found in Perl Cookbook, but the discussion here is deeper (and more up to date), there's very little overlap.

Exactly how much you get out of the book will probably be dependent on how well you know the innards of CPAN and how interesting you find the topics. I liked the parsing and natural language processing chapters a lot, and the chapter on inlining code from other languages was diverting; conversely, I can't bring myself to find Unicode even remotely stimulating, and the POE (some sort of event-based framework) chapter didn't do much for me. The testing chapter is a solid addition to the material in Intermediate Perl, and I picked up some pointers to modules to check out, but it didn't feel all that advanced.

This is a book that fits in quite nicely with Intermediate Perl and Programming Perl -- it cleans up a few niggling details not well discussed in the former book, without having the intimidating heft of the latter, and also provides a wide ranging overview of several topics and the CPAN solutions for them. As such, it will bring an intermediate programmer up a few notches.

Already advanced Perl programmers may be disappointed, and those hoping for an updated version of the first edition will definitely be out of luck, but if you know what you're getting, and evaluate it on those terms, rather than what the title suggests, I think you'll enjoy it.