Which code to read?

Peter Seibel has written a great blog post on code reading - learning more about programming by reading good examples. The final model he describes has someone diving into a piece of code like a 19th century explorer and then reporting back to the group on what they found. This sounds like a valuable activity for a Perl Mongers group.

So my question to you is what CPAN module would you recommend as rewarding to an intrepid code explorer; and why? Of course I'm not suggesting Perl programmers should only read Perl code, but I do think CPAN is a good place to start.

4 Comments

I don't know if I could say necessarily whose modules are the best examples to learn from, but the code I like to read comes from modules that make me think, "how did they *do* that?!" For a good supply of "how did they do that ?!" modules, I recommend the book Perl Hacks. You could spend a lot of time going through the code of the modules discussed in there. One example I can think of was the code for Filter::Macro, which uses a source filter to turn the calling module into a source filter!

I found a neat SO question with some good answers.

I have also been meaning to get around to reading the 4 that Jeffrey Kiegler mentions in his The Design of Four.

Finally, I can think of two that I'm interested in learning about. The first is bignum. I mean, how do you turn every number in a Perl program into an object? The other is both base and parent, and I'm particularly curious to see why everyone hates base and loves parent (I figure that parent must have done a good job where base did a bad one).

I like reading Perl code, and these are just random examples I recently examined:

There is plenty of excellent code on CPAN servers. If you don't know what to read visit https://metacpan.org/recent and pick at random :)


I like to read code by Tokuhiro Matsuno. It's very cleanly written and there are indeed things to discover inside. I started by doing some source diving on SQL::Maker in order to figure out how to compose certain queries and ended up contributing to documentation. It's certainly more rewarding than just reading code, especially when the author is not a native English speaker.

Also, reading David Golden's code was particulary insightful. It's not overly verbose, not necessarily self-documenting but it's almost always well commented, not always easy to understand but it looks efficient. If you're allergic to postfixed ifs and fors, then avoid reading it because it has a lot of those.

Leave a comment

About Grant McLean

user-pic Perl hacker since 1995. Coordinator of Wellington.pm in New Zealand.