From what I read, perl doesn't have a real type for integer or string, for him any $variable is scalar.
Recently I had a lot of trouble with an old script that was generating JSON objects required by another process for which values inside JSON must be integers, after some debug I found that because a simple print function:
JSON::encode_json
was generating a string instead an integer, here's my example:
use strict;
use warnings;
use JSON;
my $score1 = 998;
my $score2 = 999;
print "score1: ".$score1."\n";
my %hash_object = ( score1 => $score1, score2 => $score2 );
my $json_str = encode_json(\%hash_object); # This will work now
print "$json_str";
And it outputs:
score1: 998
{"score1":"998","score2":999}
Somehow perl variables have a type or at least this is how JSON::encode_json thinks.
Is there a way to find this type programmatically and why this type is changed when making an operation like the concatenation above?
%arris a terrible name for a variable. First of all, it's a hash, not an array, and secondly the identifier should reflect a variables purpose rather than its nature. The%says that it's a hash, so%hashis superfluous and slightly ridiculous. But sometimes there is little to say, especially about aggregate data structures, and I have often resorted to%datawhich I think is fine - Borodinmy %hash_object = ( score1 => int($score1), score2 => $score2 );- Steven Carlson