Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Did the JSON module change?

by Corion (Patriarch)
on Mar 01, 2018 at 17:46 UTC ( [id://1210172]=note: print w/replies, xml ) Need Help??


in reply to Did the JSON module change?

Perl has no fixed distinction between "string" and "number". This is a bit problematic for modules like JSON that try to guess such concepts from circumstantial evidence.

My approach is to try to explicitly state whether I want a value to be treated as string or number by performing either string concatenation on it or an addition:

my $string = 3; my $number = "42"; my $data = { string => "".$string, number => 0+$number, };

Replies are listed 'Best First'.
Re^2: Did the JSON module change?
by nilesOien (Novice) on Mar 01, 2018 at 23:59 UTC

    I agree explicit typing (if "typing" is the right word, I'm not even sure if perl has types as such, but I'll talk about "typing" because I think you know what I mean) is probably best. What surprised us is that we didn't think something like this :

    my $var = "3"; my $otherVar = $var + 0;

    Would change the typing on $var. It's as if in the past, perl created a temporary variable, a copy of $var that was treated as a number, and used that in the math without messing with $var, and now that's not the case? Or maybe the new JSON module is looking at something different to determine variable type? As you say it must be hard for JSON to figure that out, so maybe they're clutching at a different straw in that determination now. I kind of suspect that's the case, and I'd be interested to know. Is there a way to contact the JSON module author(s)?

      It's as if in the past, perl created a temporary variable, a copy of $var that was treated as a number, and used that in the math without messing with $var, and now that's not the case?

      All that's changing are the variable's flags, and also the contents of its IV (integer) and NV (float) slots . You can see those internals using the Devel::Peek module - eg:
      use Devel::Peek; $var = "3"; Dump $var; print "^^^^^^^^^^\n"; $x = $var + 0; Dump $var; print "^^^^^^^^^^\n"; $var += 0; Dump $var; print "^^^^^^^^^^\n";
      For me, that outputs:
      SV = PV(0x4fae04) at 0x612d94 REFCNT = 1 FLAGS = (POK,pPOK) PV = 0x5ffdcc "3"\0 CUR = 1 LEN = 12 ^^^^^^^^^^ SV = PVIV(0x610f84) at 0x612d94 REFCNT = 1 FLAGS = (IOK,POK,pIOK,pPOK) IV = 3 PV = 0x5ffdcc "3"\0 CUR = 1 LEN = 12 ^^^^^^^^^^ SV = PVIV(0x610f84) at 0x612d94 REFCNT = 1 FLAGS = (IOK,pIOK) IV = 3 PV = 0x5ffdcc "3"\0 CUR = 1 LEN = 12 ^^^^^^^^^^
      and you can see that the FLAGS have changed from each step to the next.
      Those changes can have a bearing on the values that are printed out.

      Additionally, the flags that are currently set, and the ways in which they change have differed over the years - so if JSON is looking at the flags then it is possible that behaviour may change from one version of perl to another.
      And if JSON has actually changed the way it treats values based on the flags then that could also change behaviour.

      For example, wrt $var in the middle of the script where the flags are (IOK,POK,pIOK,pPOK), it may well have once been the case that the value would be displayed surrounded by quotes if the POK and pPOK flags were set, else no quotes. (This would make sense).
      Subsequently, the condition could have changed to not surrounding the value with quotes if the IOK and pIOK flags were set, else surround with double quotes. (This would also make sense.)
      Two different treatments - both sane.

      The changes to the flags are all part of perl's commitment to "least surprise". It generally works quite well, but there's no way of guaranteeing what programmers will do with the flags.

      Cheers,
      Rob

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1210172]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2024-04-19 01:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found