| Subcribe via RSS

Well. That Was… Educational.

August 29th, 2017 Posted in CPAN, Perl

I got a great lesson recently in the dangers of making assumptions about other people’s code. One could almost call it an object lesson. (A really bad pun that will become more evident as you read further.)

I’ve had a small flurry of coding activity recently, fixing some issues in YASF and writing a new module, one that I’m calling “JSON-WithComments“. (Note to self: need to update my avatar pic on that site.) The new module is actually very simple; the manual page is longer than the code itself. It sub-classes the JSON module in order to allow you to have comments (either Perl/shell-style, or JavaScript-style) in JSON data (presumably from a file that is being used as the configuration for a larger tool of some sort). I came up with the need for this based on a project at my day job, but decided to flesh it out and develop it at home so that I could share it on CPAN.

(A little while into the project, I discovered that the JSON module will actually support Perl-style comments directly, via the relaxed method. I decided to continue with the development of the module anyway, as I was already supporting multiple comment styles. But if you only need Perl-style comments, you can manage with just the JSON module.)

Anyway, so I spent lots of time studying the main JSON.pm file, determined that the objects were hash-reference-based, and wrote my code to extend the objects with a new property to track which comment-style to use. I wrote some (admittedly sparse) tests and some docs, and Sunday night I uploaded version 0.001 to CPAN.

Monday morning, I woke up to 70+ failure notifications from the automated testing corps.

See, JSON is really just a front-end that chooses between JSON::PP and JSON::XS back-ends. But no big deal, I have both of those installed, so my code must be working with both back-ends, right? Right?

Wrong. I did not have JSON-XS installed, which (being much faster) is the preferred back-end. And this back-end does not, in fact, implement the underlying object as a blessed hash reference. It uses a blessed scalar. So when my code cheerfully tried to store and retrieve a hash key from the reference, it didn’t just not work, it died immediately. Taking the test suite with it.

Once I finally figured out what the cause was (by which I mean, I figured out that I didn’t have JSON-XS installed after all and once installed it broke on my machine too), a fix was pretty easy to implement and last night I uploaded version 0.002. But getting a 70+ flurry of failures on my initial upload was a lesson I’ll not soon forget.

So be cautious about making assumptions when sub-classing someone else’s class. Lest you, too, receive the same “object lesson” that I just did…

Leave a Reply