I contributed to CPAN!

Posted at February 20, 2016

I spend a lot of time dealing with Perl at $WORK (as you can tell by the word “$WORK”). One of my projects is updating our legacy code, which mostly runs on Perl 5.8. When I tried running things under Perl 5.20, some unit tests broke because as of Perl 5.18, the return order of keys and values from hashes are randomized. The code was generating links, such as

my $cgiParams = {
    foo => 1,
    bar => 2,
my $url = "http://yarr.com/?" . hashToParams($cgiParams);

# And in the unit test...

is($url, "http://yarr.com/?foo=1&bar=2", "Hit expected URL");

Thanks to the randomization, the order of “foo” and “bar” is unknown. Which is good! But rather than rewriting all the unit tests, I decided to write a Test::Deep helper to handle unordered parameters for me. Thus Test::Deep::URI! Now that test can be written like

use Test::Deep;
use Test::Deep::URI;

# Now we can do

cmp_deeply($url, uri("http://yarr.com/?bar=2&foo=1"), "Hit expected URL");

And just because, I added support for partial URI matching, so it’d also match uri("/?foo=1&bar=2") or uri("//yarr.com/?bar=2&foo=1").

It’s nothing major, but it feels good to give something back. Plus now I can use it at work, and that problem is solved. :D