CLI Perl syntax highlighter

Last night, someone posed a problem in #perl-help. They wanted to have syntax highlighting in their shell for Perl code. I immediately suggested that they use Pygments - I knew about it because I'd used it before, and I had experimented with the command line tool. But I was surprised there wasn't already a Perl solution.

In this website's previous incarnation, I used Syntax::Highlight::Perl::Improved to provide syntax highlighting of the Perl script, and it gave me good results on most of my code. So, why not hook that up with Term::ANSIColor to provide syntax highlighting on the command line. Surely if it was that simple, someone would have done it already... but apparently not. Enter stage right.

I first experimented with a one-liner:

perl -MText::Highlight -E '$h=Text::Highlight->new(ansi=>1); my $text=do{local $/; open my $fh, "< ", $ARGV[0]; <$fh>}; say $h->highlight("Perl", $text);' lib/My/

This used Text::Highlight, and while it was OK, the readability was pretty poor, and... actually, not much got highlighted. And it wasn't very easy to reuse. So, I started with App::perl::highlight. (As an aside, I use the lower-case letters to match the name of the app that'll be installed. I know upper case is Good Form™, so perhaps the elder Perl hackers will let me know if this isn't an acceptable exception)

Less than an hour later, I was mostly done. Readability isn't great yet because there's something not quite right with the colour combinations I'm using, so more experimentation is needed there. But it works! For now, the code is on Github, and a 0.001 release will come to the CPAN in a few days' time. If you feel like it, try it out and give me some help with the colours and readability.

Update: Version 0.001 was released to the CPAN in the wee hours as App::perlhl. I also discovered that there is a viewperl script shipped with Syntax::Highlight::Perl::Improved that doesn't get installed with the module. Yes, I filed a bug.

Pingback from Human usability testers wanted » - April 26, 2011 at 6:43 pm

[...] I wrote App::perlhl a few weeks ago, I was mostly concerned with getting it working. I picked colours more-or-less at random, and I [...]