QR Encoding in Pure Perl

TL;DR

A module for doing QR Encoding in Pure Perl

I like my own mega-tool Romeo a lot and I also like adding stuff to it from time to time. That bloat seems like spoiling the real cat.

Well, it also spoils me, admittedly 😅

Anyway, I figured out a way to add support for QR codes generation, by implementing an optional plugin system that looks for stuff in some places and, when present, adds new commands under the same umbrella. This allows using the venerable Text::QRCode, which in turn uses the libqrencode library.

Alas, this has always bugged me, because the main design goal of Romeo is to produce a single-file program that can just be run wherever there’s a decent installation of Perl (v5.24 or newer). This whole plugin thing means that some compilation must happen in the target.

So well, it’s since some time that I’ve been toying with the idea of implementing QR Encoding directly in Pure Perl.

Reinventing a Wheel?

There is some prior art in CPAN for generating QR Codes, but to be honest I was a bit intimidated by it. I’m talking about GD::Barcode.

I was not too thrilled by the mix of QR Code generation and GD. Text::QRCode spoiled me into thinking that the encoding process can be thought as separated from actual image generation, and that GD was not making me too happy. Even though, admittedly, there is the following note:

From 1.14, you can use this module even if no GD (except plot method).

Looking at the code did also scare me a bit, there’s a bit too many if-s and conditions for me to confortably contribute anything.

In hindsight, anyway, what drove me away was the name: the GD part just made me skip over it thinking that it would need some compilation, which was not what I was after.

So Here’s the Wheel

So I just went to the coding board and released a couple of modules, hoping they will be useful:

  • [QRCode::Encode][] which has an interface compatible with Text::QRCode, and
  • Math::ReedSolomon::Encoder, which does the heavylifting for calculating the redundancy using Reed-Solomon codes.

Some Considerations

QR Codes are defined in ISO Standard ISO/IEC 18004. The document was basically donated by Denso Wave to ISO, I think because having something published as an ISO standard is definitely an achievement.

For all that, it has a very poor quality over cost ratio in my opinion. Take Table 9 as an example: it’s a total mess, to the point that someone wrote about it in How should I interpret Table 9 in the QR Code ISO IEC 18004 2015 Standard wrt number of blocks?. My take is that the Japanese original is much more readable, even for someone who does not know Japanese.

libqrencode was incredibly useful, I took most of the tables and part of the interface from there. I can’t say that my rating implementation is correct, though, as it is NOT specced very well (again…) and I definitely have some differences with respect to libqrencode. Anyway, it’s worked fine so far.

Back to Romeo…

So, back to Romeo… it has QR Code generation out of the box now:

Have fun!


Comments? GitHub, Reddit, or drop me a line!