You can write your own mini (or micro) debuggers to watch your program run. You might want to do this when the other Perl debuggers are too heavyweight (or even too interactive) for your immediate problem. Continue reading “Trace your Perl programs”
Author: brian d foy
Use Data::Printer to debug data structures
You can use several different Perl modules to inspect data structures. Many of these modules, however, are really two tools in one. Besides showing a data structure as a string, they also serialize the data as Perl code so you can reconstruct the data structure. That second job often makes things hard for you. If you don’t need the serialization job, don’t use a module that insists on it. Continue reading “Use Data::Printer to debug data structures”
Make grep-like syntax
To create grep– or map-like syntax, you need to use Perl’s prototypes, despite whatever we told you in Understand why you probably don’t need prototypes. Perl needs the special hints that prototypes to parse a block as an argument to a subroutine. Continue reading “Make grep-like syntax”
Profile with Devel::NYTProf
Profile before you decide where to optimize—you might be surprised where you’re losing all of your performance.
We won’t go into all the details of profiling in this Item, but you can read about those in Mastering Perl. In short, profilers count something then report the results. They can track any of the things that you might care about. The Devel::NYTProf module, like most profilers, tracks time, counting the statements you run and how long they take. Continue reading “Profile with Devel::NYTProf”
Use lookarounds to split to avoid special cases
There are some regular expression tricks that can help you deal with balanced delimiters in a string. The split command takes a pattern, removes the parts of a string that match that pattern, and give you a list of the parts of the string between those separators. Said another way, split works when the parts you don’t need are between the values. Continue reading “Use lookarounds to split to avoid special cases”
Understand why you probably don’t need prototypes
You should understand how Perl’s prototypes work, not so you’ll use them but so you won’t be tempted to use them. Although prototypes can solve some problems, they don’t solve the problems most people want. Continue reading “Understand why you probably don’t need prototypes”
Return error objects instead of throwing exceptions
Programmers generally consider two types of error communication: the “modern” and shiny exception throwing, and the old and decrepit return values. When they consider these, they choose one and forsake the other. One is good, and the other is bad. Programmers won’t agree on which is which though. Continue reading “Return error objects instead of throwing exceptions”
A Chinese translation of Effective Perl Programming
I mentioned a long time ago that a Chinese translation of Effective Perl Programming was in the works, and apparently it’s done. Someone sent me a copy of the Chinese version of the book. I can’t tell you who did it (if it’s you, let me know) and I don’t know where you can buy it (if you know, let me know). Also, I don’t know what I want to do with the copy that I have. I don’t read Chinese, so I can’t really read the book to see how well it translates, and I don’t want to keep the book as a trophy. Does someone else want the book? Is there a Chinese Perl event that would like to give it away as a prize? I’ll get Josh and I to sign it and send it along.
2021 Update A reader wrote to me to note that the name of the book is “Perl高效编程” from Pearson Education, Inc. and Post&Telecom Press publishing as Addison Wesley. It’s on TaoBao from various sellers.
Use lookarounds to eliminate special cases in split
The split built-in takes a string and turns it into a list, discarding the separators that you specify as a pattern. This is easy when the separator is simple, but seems hard if the separator gets more tricky. Continue reading “Use lookarounds to eliminate special cases in split”
Enchant closures for better debugging output
When you’re using code references heavily, you’re going to have a problem figuring out which one of them is having a problem. You define them in possibly several and far-flung parts of your program, but when it comes to using them, you don’t know which one you are using. You can’t really print its value like you would for a scalar, making it more difficult for you to debug things. You can dereference a scalar or an array to see what it is, but you can’t dereference a code reference without making it do something. Continue reading “Enchant closures for better debugging output”