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 values because they are a single item in both the non-reference and reference form. Continue reading “Use scalar references to pass large data without copying.”
Category: chapters
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.”
Use DBI_TRACE to follow DBI’s work.
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 a built-in tracing feature to make it easier though. Continue reading “Use DBI_TRACE to follow DBI’s work.”
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”
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”