Friday, 17 Dec 2004 [updated]

Chris Nandor inspired me to write hl, a little Perl script you can use to highlight pattern matches in input data.

Usage: hl [ -c colour ] [ -x ] pattern [ file... ] [ < input ]

You can use capturing parens in the pattern. In that case, you can supply multiple colours separated by commas, which will be used to individually colour the submatches.

-x will supress lines without matches.

Due to the semantics of Perl’s @- and @+ arrays (or @LAST_MATCH_START and @LAST_MATCH_END), my first stab was a horrible monster and incredibly difficult to debug, far harder to write than it promised to be. These arrays hold offsets into the string being matched against – one holds the indices of the start of each submatch, the other holds the indices of the end of each submatch. The entries at index 0 are special, however: they indicate the start and end of the entire match. This required terrible contortions to take into account.

And, surprise surprise, it was buggy.

In fixing my bug, I realized that the proper special case looked almost like a common case. And then I realized that by appending a phantom zero-length match to the arrays and changing index 0 to instead signify a phantom zero-length 0th match, both special cases disappear.

Lesson: when implementing the semantics turns your brain to mush, change the semantics.