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: Continue reading “Download most Mastering Perl programs”

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! Continue reading “Mastering Perl Kindle screen shots”

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. Continue reading “New to “Lightweight Persistence””

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.

New to “Advanced Regular Expressions”

This chapter has lots of new material and much of the old text is gone. I wasn’t looking forward to working on this chapter because I knew it was going to be mostly new text.

My goal of Mastering Perl has always been to present material not already well covered in some other book. This chapter did that when I first wrote it, but I then moved some of it into other books.

The stuff about regular expression references moved into Intermediate Perl, so I assume in Mastering Perl that readers already know that stuff. There went a third of the chapter.

I had a medium-sized section on YAPE::Regex::Explain, a module that turned a pattern into an english description of the pattern. That module hasn’t kept up with post-v5.10 features and has been abandoned. That’s gone.

Curiously, neither Learning Perl nor Intermediate Perl had covered non-capturing parentheses. I fixed that, so that section is gone from Mastering Perl.

But, this left room for much more exciting and advanced things.

Randal Schwartz meditated on a wickedly tight minimal JSON parser that he wrote for a particular client situation. He used advanced features including regex grammars, code execution with (?{...}), and data structure bootstrapping with $^R. I wanted to explain that regex but I needed to talk about all of the features in it. I think I did a pretty good job in the normal camelid trilogy style: I start with a simple program, find an edge case that causes a problem, modify, and repeat. I used the example of matching nested quotes to cover, one feature at a time:

  • (?PARNO) to refer to earlier capture groups as independent patterns
  • Recursion with (?PARNO), which allows matching balanced text
  • (?(DEFINE)...) to create and name sub patterns for use later
  • (?{...}) to watch what’s happening in a regex
  • $^N inside (?{...}) to get the text of the previous capture buffer
  • bootstrapping a data structure with $^R inside (?{...})

Once I cover all of those, I can show off Randal’s regex. Instead of explaining it, though, I leave that to the reader. By the time I get there, they should understand all of the features he uses.

That stuff was a bit of a bear to figure out. The docs aren’t great and there are very few examples out there. I even typed out a long StackOverflow question about it that led me to answering my own question.

Besides that, I pull out \K, another v5.10 feature, to fix my broken money commifying example. I’m not actually the person who fixed it though; I lifted that regex from Michael Carmen’s answer to my StackOverflow question about it.

After all that, I end the chapter with an expanded section on regular expression debugging, including Damian Conway’s Regexp::Debugger. That’s not very exciting in a static book, so I’ll have to make some screencasts about it here.

You can read this chapter in O’Reilly Atlas.