Subject: | Key is magically not itself |
Testcase: http://ideone.com/9PoHhc or attachment.
When working with JSON::SL objects/hashes, keys that compare equal to a string are not actually equal to that string when used as lookup.
E.g., $key eq 'id' but using 'id' itself to access the value results in uninitialized read.
Subject: | id_isnt_id.pl |
#!/usr/bin/env perl
# -*- mode: cperl; indent-tabs-mode: nil; tab-width: 3; cperl-indent-level: 3; -*-
BEGIN { $|=1; }
use strict;
use warnings;
use utf8;
use Data::Dumper;
use JSON::SL;
my $p = JSON::SL->new;
$p->utf8(1);
$p->noqstr(1);
$p->nopath(1);
$p->set_jsonpointer(['/^']);
my $buf = <<JSON;
[{"id": "hello"}]
JSON
$p->feed($buf);
while (my $obj = $p->fetch) {
my %val = %{$obj->{Value}};
foreach my $k (sort keys %val) {
# This prints: id => hello
print "$k => $val{$k}\n";
# This prints: $VAR1 = 'id';
print Dumper($k);
if ($k eq 'id') {
# This triggers: Use of uninitialized value $val{"id"}
# So, id != id ?
print "id => $val{id}\n";
}
}
print "\n";
# Now for the fun bit: This causes 2 rows of key id
$val{'id'} = 5;
foreach my $k (sort keys %val) {
print "$k => $val{$k}\n";
print Dumper($k);
}
}
=pod
id => hello
$VAR1 = 'id';
Use of uninitialized value $val{"id"} in concatenation (.) or string at /home/tino/parsers/bin/json_sl.pl line 32.
id =>
id => hello
$VAR1 = 'id';
id => 5
$VAR1 = 'id';
=cut