Spot the error

use Data::Rmap qw(:all);
use JSON;
use Data::Dump;
use Clone;
use boolean;

my $arg = from_json(q{{"1":true,"2":false}});
# convert JSON booleans to boolean's booleans
rmap_all { bless $_,"boolean" if ref($_) =~ /^JSON::(XS|PP)::Boolean$/ }, $arg;
dd $arg;

Hint: it's one character long.

In fact, this piece of code is full of Perl's traps (from Perl's lack of booleans obviously, to less obviously having to clone and rmap not working), it disgusts me.

6 Comments

I say it's the comma after the rmap_all block.

You are also relying on the internals of both boolean and JSON's boolean values, and breaking them in the process. JSON (PP or XS) uses singleton objects for the booleans, which means you are trying to rebless the constants $JSON::true and $JSON::false. With JSON::XS, this gives errors about modifying read only values. With JSON::PP, it works but breaks the is_bool sub.

A more correct conversion would be:
rmap_all { JSON::is_bool($_) ? ( $_ ? true : false ) : $_ } $arg;

Or actually:

rmap_all { $_ = $_ ? true : false if JSON::is_bool($_) } $arg;

Probably should have checked the docs on Data::Rmap before replying.

Leave a comment

About Steven Haryanto

user-pic A programmer (mostly Perl 5 nowadays). My CPAN ID: SHARYANTO. I'm sedusedan on perlmonks. My twitter is stevenharyanto (but I don't tweet much). Follow me on github: sharyanto.