Register Now for O'Reilly Open Source Convention

Sub::Install has a nice interface

I didn’t mention Sub::Install in the “Dynamic Subroutines” (or maybe the Symbol Table chapter. It was worth a mention (but only that). I show readers how to define subroutines in other packages (or even the current one, I guess) by assigning to a typeglob:

no strict 'refs';
*{ $package . "::$name" } = sub { ... };

To do this, though, I have to violate strict‘s prohibition on symbolic references and I have to construct the fully-qualified package subroutine myself. After that, I have to use the typeglob, which isn’t common; it’s not in Learning Perl or Intermediate Perl. Those can be tricky for the new Perler.

Sub::Install gets around both of those with an interface that avoids the tricky, uncommon syntax:

use Sub::Install;

    code => sub { ... },
    into  => $package,
    as     => $name,

Most of the code in that module is just the infrastructure to make the module work, but it’s otherwise very simple.

I can understand why people would want to use the module: it makes things slightly easier to read to the novice programmer. I would weigh that against creating another dependency. In this case, there are no non-core dependencies, so its not so onerous. Other modules that give simplified interfaces aren’t so lean.

Download most Mastering Perl programs

The Mastering Perl Downloads page now has the named programs I’ve extracted from the book sources. This comprises most of the programs that you can run to do something, rather than all the code segments—many of which are fragments.

I wrote the book as Pod, then converted to DocBook. Despite all the advice we give people about parsing XML properly, I did the thing we tend to tell people not to do:

my $data = do { local( @ARGV, $/ ) = $file; <> };

my @programs = $data =~ m{


Most of the interesting programs in Mastering Perl have a shebang line and a following comment to give the program a name, such as:


This makes it easy to name the files once I extract them.

Curiously, I found a mistake I made in the Advanced Regular Expression chapter. In the last days, I had to make some corrections to a few programs. When I added the new versions of the book, I forgot to remove the actual shebang I was using. In those programs, two shebangs show up:


I guess you get double perl action for your money in those programs.

Naming anonymous subroutines

In the “Dynamic Subroutines” chapter, I considered adding the undocumented __ANON__ trick to give anonymous subroutines names. ysth described this in Permonks in 2003 in Named anonymous subs and later showed up in Perl Hacks #57. Although it’s undocumented, several modules use the trick.

I can use carp in an anonymous subroutine:

package ThisPackage;
use Carp qw(cluck);

my $sub = sub {
	cluck "This is a warning";
	return 1;

package OtherPackage;

Perl doesn’t know what to label the subroutine in the warning message, so it uses ::__ANON__:

This is a warning at line 7.
	ThisPackage::__ANON__() called at line 12

This can get quite tricky if I’m making many anonymous subroutines—perhaps through some generator function—and I want to know which one is causing the problem.

Inside an anonymous subroutine, I can set the __ANON__ variable to the value that I want to see in the message:


package ThisPackage;
use Carp qw(cluck);

my $sub = sub {
	local *__ANON__ = "Here I am";
	cluck "This is a warning";
	return 1;

package OtherPackage;

Even though I don’t use a valid variable name, the warning uses that name:

This is a warning at line 8.
	ThisPackage::Here I am() called at line 13

I showed a different, more complicated way to do this in The Effective Perler as Enchant closures for better debugging output

Mastering Perl Kindle screen shots

Catalin Ciurea (CATALIN on CPAN) sent in some pictures to show what Mastering Perl looks like on a Kindle, although he doesn’t specify which device it is. It’s one of the less sophisticated ones, but I’m surprised that it looks as good as it does. I knew the body text would be find, but it looks like the code comes out nicely too. Curiously, the book is measured in time, and it looks like he’s taking longer and longer to get through it as he progresses!

These pictures show the MOBI format, and by buying an O’Reilly ebook you can get any or all electronic formats. Not only that, but you get updates for free for life.

Mastering Perl Kindle version

You can read Mastering Perl on your Kindle now. The print edition should be in the warehouses in a couple of weeks.

I don’t have a Kindle so I don’t know how the book looks in it. I don’t have much hope for the older Kindles, but what about the new Fires? Send me a screenshot if you’d like me to see what you see!

The Mastering Perl Module Index

Mastering Perl Second Edition has a module index, which is something I first tried in Programming Perl 4th Edition. Here’s just the index from the last review I just turned in (which means the book should be available in about a month).

Presentation: Parsing JSON with a single Perl regex

I’m giving a presentation from Mastering Perl while I’m in Houston next week.

Perl v5.10 added several features that take (ir)regular expressions to a new level. With grammars and recursion, a single regex can now process things such as HTML and JSON. After going through these features, I’ll show how Randal Schwartz used them in his tiny JSON parser. This discussion also appears in chapter 2 of Mastering Perl, 2nd Edition, which you can read for free through O’Reilly’s Early Release program.

New to “Lightweight Persistence”

“Lightweight Persistence” has many changes. I covered Storable in the first edition, but since then it’s security problem has become more widely know. It’s easy to create a Storable file that can run unexpected code, for a couple of reasons.

Since I first wrote this book, YAML and JSON have become much more important, so I cover those.

A new format, Sereal, has emerged from It has some interesting design goals that make it fast and small. I’ve included it and I’m hoping that it works out well. It’s always a bit iffy to include new modules because people haven’t discovered the problems yet. It’s in heavy production use at Booking, though, so I think it’s better than most probably.

You can read this chapter in O’Reilly Atlas.

New to “Perl Debuggers”

I renamed this chapter to “Perl Debuggers” from “Debugging Perl”; it’s not really about the process of debugging but the tools to do it. Most of it has stayed the same, although I had to remove the unmaintained Devel::ebug::HTTP that uses old Catalyst code. That’s one of the reasons I don’t like tying such modules to frameworks. The people who like to make the frameworks like to reinvent them, leaving all of the legacy code behind.

You can read this chapter in O’Reilly Atlas.

New to “Profiling”

Devel::NYTProf, Adam Kaplan’s original creation and Tim Bunce’s ongoing gift to the Perl community, is the new star of my “Profiling” chapter. However, the static book format doesn’t do much to show it off.

Other than that, I went through all of the code and output with v5.18 to refresh it. Not much changed other than the times getting much faster almost a decade later.

You can read this chapter in O’Reilly Atlas.