As part of the promotional package for Effective Perl Programming, I wrote Process HTML with a Perl Module for InformIT. It’s just like the Items that you see here, but on another site.
Category: book
Use /gc and \G in matches to separate alternations in separate, smaller patterns
Perl keeps track of the last position in a string where it had a successful global match (using the /g
flag). You can access this position with the pos
operator. With Perl 5.10, you can use the /p
switch to get the per-match variable ${^MATCH}
instead of the performance-dampening $&
: Continue reading “Use /gc and \G in matches to separate alternations in separate, smaller patterns”
Know the difference between regex and match operator flags
The match and substitution operators, as well as regex quoting with qr//
, use flags to signal certain behavior of the match or interpretation of the pattern. The flags that change the interpretation of the pattern are listed in the documentation for qr//
in perlop (and maybe in other places in earlier versions of the documentation): Continue reading “Know the difference between regex and match operator flags”
Install scripts from distributions
Perl’s distribution system is quite powerful and supported by a variety of tools that can make life easier for you. Most people tend to think that “distributions” are synonymous with modules, but that’s only one of the uses for distributions. Continue reading “Install scripts from distributions”
Make deep copies
When you want to make a completely disconnected copy of a hash or an array, it’s not enough to merely assign it to a new variable name, at least in the general case: Continue reading “Make deep copies”
Process XML data with XML::Twig
People often reach for regular expressions to extract and rearrange information in XML documents. Those usually only work for the limited test cases people specifically target, but are really little time-bombs waiting to go off when the data or the format changes even slightly. The bomb often explodes after the original programmer has disappeared. Continue reading “Process XML data with XML::Twig”
Make links to per-version tools
In Item 110: Compile and install your own perl
s, we showed you how to compile and install several versions of perl
so that they don’t conflict with each other and you can use them simultaneously. Since they don’t install their programs, they are left in their $prefix/bin directories. With several perl
s, each of which has their own modules directories, using tools such as cpan
and perldoc
can get confusing. Which version of those tools are you using and which perl
are they trying to use? Continue reading “Make links to per-version tools”
Avoid accidently creating methods from module exports
Perl’s object system is fuzzy. Methods are really just subroutines and classes are just packages, which means that any subroutine in a package is also a method in that class. Your class might have subroutines that you’ve never even noticed, so you end up with methods that you didn’t want in your interface. Continue reading “Avoid accidently creating methods from module exports”
Know how Perl handles scientific notation in string to number conversions.
A recent question on Stackoverlow asked about the difference between the same floating numbers being stored in scientific notation and written out. Why does 0.76178
come out differently than 7.6178E-01
When Perl stores them, they can come out as slightly different numbers. This is related to the perlfaq answer to Why am I getting long decimals (eg, 19.9499999999999) instead of the numbers I should be getting (eg, 19.95)?, but a bit more involved. You’ll see how to skip the whole mess at the end, but be patient. Continue reading “Know how Perl handles scientific notation in string to number conversions.”
Slides for “Effective Perl: Unicode” at Frozen Perl 2010
At Frozen Perl I did a quick presentation about Unicode and Perl. I had to do some work on the slides before releasing them publicly, but here they are… Be sure to look at the author notes if you want more detailed information.