Learning Perl through code golf

Before I started with my current employer almost six years ago I moved from a small, privately-owned, web development company with a few developers working in the languages we chose (or at least, could make a good enough argument for) and with tools recommended by each other or the latest and greatest libraries we’d recently discovered. This was great fun and whilst we dabbled in other languages, learning Ruby and Python, tinkering with shell scripts and Haskell, there were things we didn’t touch because we didn’t have to. That was, until another developer that was no longer with us, created a Perl script that needed updating. The fear in our eyes when that script came up, or when it needed to be used again for another client…

When I started the new job, I was hired as a PHP developer, but knowing I’d have to learn Perl, although there was some PHP work and a lot of frontend work, which I was very familiar and comfortable with and this wasn’t seen as a problem. I spent my first week or so reading through the Perl book and becoming familiar with the kinds of tasks I’d need to do on a day-to-day basis, the one that caused most people problems was de-referencing hash references. I still learn new things all the time that I thought I knew about how this works! But when I was starting to get to grips with Perl, I started to run out of material to work on. There wasn’t any module work I could take on during the day that would stretch me and force me to read more of the manual, and rewriting modules I’ve made in the past wasn’t what I was looking for. I needed something to keep me engaged.

I’d been aware of the term ‘code golf’ and seen some examples that were impressive and totally beyond comprehension to me at the time “How do they know all these obscure variables exist, and what data they contain!”. I started lurking on Programming Puzzles and Code Golf and completing challenges in Perl and JavaScript (but not sharing them as I didn’t think they were ‘good enough’. I started reading all the older highly rated questions and reading the answers written in Perl to try and understand some of the more obscure parts of the language and learn about the various limits of the syntax.

Whilst learning via code golf might not produce eloquent, well-written code to be used in production, it allowed me to become more and more comfortable with the language itself and use it both in my day job and in my spare time. I read, re-read and read again the perlvar man page to be able to (ab)use the ‘magic’ vars. I brushed up on my perlre to be able to capitalise on the terse text parsing capabilities, but no amount of reading perlrun was as useful as real-world examples of other peoples work. Like seeing long-time ‘golfers making use of the fact that execution with perl -pe literally wraps your code with while (<>) {;} meaning you can break out of this loop within your code and tricks like using the last ; in the generated code like s;x;y (instead of s/x/y/, saving a whole byte!)

All this time spent playing with Perl, observing code of other golfers, playing with, and understanding, the structure (and execution) of the many esolangs that pop up (many with clever, single-byte answers, to seemingly complex problems!), through to seeing the loopholes exploited in the question/specification, have all helped me understand more of the programming process I use every day, highlighted alternative approaches and ingenious ways to simplify complicated problems with terse solution design.

Whilst code golf itself might not have a practical use directly in my day-to-day life, it’s helped me become more confident in a language I thought (mostly due to ignorance) was boring and only used by a few hardcore, stuck in the past, programmers. This approach might not work for everyone, but it’s helped me so much. I use Perl almost every day for shell tasks that I might have tackled manually in the past and now happily list it as one of my favourite languages.

Feel free to nose through my submissions, some of my favourites are:

Trick or Treat Polyglot
Generate a GitHub Avatar
Create Rainbow Text
Markov Chain Quine
Faux Source Code
One Ring to rule them all. One String to contain them all


If you want to learn the ins and outs of a language, play with it. Lots. Programming Puzzles and Code Golf might help you do that!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.