Posted by brian d foy on September 5, 2010
Perl’s regular expressions have a simple rule for capturing groups. It counts the order of left parentheses to assign capture variables. Not all capture groups must actually match parts of the string, and Perl doesn’t care if they do. Perl assigns capture groups inside an alternation consecutively, even though it knows that only one branch [...]
Posted by brian d foy on August 15, 2010
Perl 5.12 adds a feature that lets you locally delete a hash key or array element (refresh your memory of local with Item 43: Know the difference between my and local. This new feature allows you to temporarily prune a hash or an array: delete local $hash{$key}; delete local $array[$index]; This syntax has actually been [...]
Posted by brian d foy on July 25, 2010
Perl 5.12 deprecates several features, for various reasons. Some of the features were always stupid, some need to make way for future development, and some are just too ornery to maintain. All of these are listed in the perldelta5120 documentation. The new thing, however, is that Perl 5.12 will warn you about these even if [...]
Posted by brian d foy on July 11, 2010
The DBI module lets you handle errors yourself if you don’t like its built-in behavior. DBI lets you handle the errors at either the database or the statement handle level by specifying attributes: my $dbh = DBI->connect( …, …, \%attr ); my $sth = $dbh->prepare( …, \%attr ); There are several attributes that affect error [...]
Posted by brian d foy on June 27, 2010
Perl’s flip-flop operator, .., (otherwise known as the range operator in scalar context) is a simple way to choose a window on some data. It returns false until its lefthand side is true. Once the lefthand side is true, the flip-flop operator returns true until its righthand side is true. Once the righthand side is [...]
Posted by Josh McAdams on June 20, 2010
In Item 33: “Watch out for match variables, you found out that the match variable $`, $&, and $` come with a performance hit. With all of the module code that you might use, you might be using those variables even though you didn’t code with them yourself. There’s a module that can tell you [...]
Posted by brian d foy on May 30, 2010
References aren’t just for data structures, and many people overlook the benefit of references to simple scalars. With references to arrays and hashes you can keep those data structures in tact when you pass them to or return them from subroutines (Item 46: Pass references instead of copies). You don’t need to worry about scalar [...]
Posted by brian d foy on May 5, 2010
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 [...]
Posted by brian d foy on May 2, 2010
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 [...]
Posted by Josh McAdams on April 25, 2010
There is a lot going on when you use DBI to interact with a database. Tack on a few layers of abstraction from Object Relational Modelers (ORM’s) such as DBIx::Class and you can end up with a tricky maze of subroutine calls to dig through when you need to track down issues. DBI comes with [...]