How to use perl v5.40's boolean builtins in Mojo::Pg queries

Perl v5.40 introduced native true and false keywords. Unfortunately not all CPAN modules are ready to use them. One of those not yet ready is Mojo::Pg.

Normally you'd want to pass booleans to your queries as just 1's and 0's. However, since Mojo::JSON's true & false stringify to 1 and 0, my 5.38-using codebase is full of Mojo::Pg queries with Mojo::JSON's true and false as arguments.

This is a problem if I want to upgrade the perl interpreter of that project to Perl v5.40, because if I write "use v5.40;" in the file that contains those boolean keywords, Perl's builtin booleans will be used instead, which don't stringify to 1 and 0, but to 1 and the empty string, which can't be used by DBD::Pg in boolean fields and makes DBD::Pg throw an exception.

The solution I found was to subclass Mojo::Pg::Database, and wrap the query method, so that if Perl's builtin booleans are found, they are replaced in the query with native Pg booleans.

The source of the module and a lot more information can be found in my blogpost, here.

Leave a comment

About karjala

user-pic I'm a Perl developer.