Perl 5.12.1 is out, which is the sign that it’s time for normal users to pay attention to it: that first point release should have sanded down all the rough edges. As usual, the complete list of major changes is in the perldelta5.12.0 documentation, we’ll cover some more of the interesting features in The Effective Perler in the coming weeks. Our initial list of user-interesting features include: Continue reading “Perl 5.12 new features”
Author: brian d foy
Use B::Deparse to see what perl thinks the code is.
We used B::Deparse in Item 7. Know which values are false and test them accordingly, but we didn’t say much about that module. The B
namespace has many modules that do various nasty black magic things with the perl parse tree. Continue reading “Use B::Deparse to see what perl thinks the code is.”
Compare dates as strings when you can.
Just because you find a module that does something doesn’t mean that you have to use it. There are many excellent date and time modules on CPAN, including most people’s favorite, DateTime. In your heady rush for program purity and elegance, don’t think that you always have to use objects to do your work. Sometimes the overhead of objects, which have to call (perhaps many) subroutines to do their work, is too expensive. Continue reading “Compare dates as strings when you can.”
Effective Perl free sample chapter: Files and Filehandles
Addison-Wesley converted our chapter on “Files and Filehandles” to HTML and put it online for as a free sample chapter. I selected this chapter as the free sample because it was the most fun to write but also the most valuable to new Perl programmers. Filehandles are the way you interact with the world, and using them wisely can give your program quite a bit of flexibility and make many tasks much easier.
Here’s the list of Items from that chapter, each of which you can read for free online:
- Item 51. Don’t ignore the file test operators.
- Item 52. Always use the three-argument open.
- Item 53. Consider different ways of reading from a stream.
- Item 54. Open filehandles to and from strings.
- Item 55. Make flexible output.
- Item 56. Use File::Spec or Path::Class to work with paths
- Item 57. Leave most of the data on disk to save memory.
We’ve also added more Items for “Files and Filehandles” in this blog, which you can also read for free. However, don’t forget about that Donate button on the right had side of the page if you find this site valuable. Or, buy our book and encourage all your friends to buy our book. Donations and book sales give us a little motivational boost to keep going. :)
Know what your the last evaluated expression actually is.
In Perl, a subroutine or other block structure that returns a value gives back the last evaluated expression, but if you’re not careful you might not recognize what that last evaluation actually is. It’s not necessarily the last statement in the block; it’s just the last one that you actually execute. For this Item, forget about the best practice of using explicit returns. You should do that for precisely the reasons you will see here, but you can’t learn about the problem by avoiding it. Continue reading “Know what your the last evaluated expression actually is.”
Report any errors you find in Effective Perl Programming
Now that you should be able to get Effective Perl Programming, let us know if you find a problem. We’re going to keep a list of errata on this site. Make sure you check the current list before you spend your time reporting it to us. When you do find something, you can send it to errata at effectiveperlprogramming dot com.
Effective Perl Programming is now in stock at Amazon!
Josh and I got hard copies of the Effective Perl Programming, Second Edition last week, and Amazon says they are in stock.
Effective Perl Programming’s table of contents
Here is the final table of contents for Effective Perl Programming, 2nd Edition. The “Item” references in our blog entries refer to the items in the book. We also have a map from the Item numbers in the first edition to those in the second, but we’ll have to do a little work to make those look nice for the blog. Continue reading “Effective Perl Programming’s table of contents”
InformIT has an Effective Perl Item: Process HTML with a Perl Module
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.
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”