Category Archives: object-oriented programming

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. The return value technique comes from older [...]

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 [...]

Hide low-level details behind an interface

Perl 5.16 makes the Perl special variable, $$, writeable, but with some magic. That’s the variable that holds the process ID. Why would you ever want to do that? There’s not much to write about with this new feature, but there’s plenty to write against it since it introduces more magic (see commit 9cdac2 on [...]

Override die with END or CORE::GLOBAL::die

Perl lets you override the effects of warn and die by redefining the signals that Perl sends when you call those functions. You probably don’t want to use the signal from die, though, since it might mean a couple of different things. You handle these special signals by setting values in the %SIG hash just [...]

Pass the empty subclass test

Is your object-oriented module subclassable? Do you know that from testing or are you just guessing? Setting aside other Perl programmers reaching into your package and redefining your subroutines, there are some basic things you can do to ensure that you’ve made life unhard for the people you want to extend your classes. If you [...]

Use the C3 method resolution order in multiple inheritance

Perl 5.10 introduced a flexible method resolution order mechanism. Instead of Perl’s default order (see Understand Perl’s default inheritance model), you can try something less stupid by using the mro pragma to specify which order perl. So far, there are only two resolution orders: dfs, which is Perl’s default depth-first search, and c3, a new [...]

Understand Perl’s default inheritance model

Perl’s default inheritance mechanism is a bit weird, and it’s not something that any language designer would want to repeat. However, it is what it is and by knowing its quirks you should have any easier time tracking down inheritance problems. Before you go further in this Item, though, you have to sit through this [...]

Use Data::Dump filters for nicer pretty-printing

Data::Dumper, a module that comes in the Standard Library, is one of the great tools knows to Perlers. You give it a big data structure and it pretty prints it for you. If you are one of those people who still believe that the best debugger in the world is print and need to get [...]

Make your methods know as little as possible

How often have you wished that one of Perl’s modules did things slightly differently? That module mostly works for you except for some hard-coded decisions in string formats, pack specifications, or other minor point that you wish that you could configure. Or, maybe you’ve been the one to write that module. When you have to [...]