I am using Perl 5.22.1 (Ubuntu 16.04 LTS) and am trying to understand why referencing a hash value changes the internal type of the value and how to identify what the type is in the first place. By way of example:


perl -e'use JSON::XS; %x=(a=>1);print $x{a},"~",encode_json(\%x),"\n"'

outputs 1~{"a":1}


perl -e'use JSON::XS; %x=(a=>"1"); print $x{a},"~",encode_json(\%x),"\n"'

outputs 1~{"a":"1"}


However,


perl -e'use JSON::XS; %x=(a=>1); print length($x{a}),"~",encode_json(\%x),"\n"'

outputs 1~{"a":"1"} NB {a=>1} at start


yet ostensibly no explicit change was made to $x{a} further more the reverse does not apply i.e. I can't change it back via reference only; such as print length($x{a}),$x{a}+0,... does not lead to encode_json(\%x) returning {"a":1}

So BUG, lack of understanding on my part? And how does one determin that $x{a} is a number verses a string?

As always, thanks in advance for any understanding shared.
For those that ask why do you want to know, becasue I am writting JSON to a field in MySQL Database which will be consumed via R (amongst other avenues) and in R "1"+1 -> ERROR. I need to ensure the JSON value pairs are consistent so it appears I need to be very careful (or use another version or Perl?) between generating the Hash and encoding it before writting to the Database.


In reply to Hash value typing by waynerasm

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.