[Chicago-talk] Problem with JSON module

Andrew Rodland andrew at cleverdomain.org
Fri Oct 2 23:50:50 PDT 2009


"x" in the debugger doesn't show it, but I would bet that JSON is keying on 
the magical, mostly-hidden type ("OK") flags of scalars. If you do $a = 
"12.34", $a has a valid string value but not a valid numeric value. If you 
calculate $a + 0, $a now has a valid string value *and* a valid numeric value, 
until it's changed by either a string operation or a numeric operation (at 
which point the value that wasn't part of the operation is marked as no longer 
"OK".) If you do $b = 12.34, $b has a valid numeric value but not a valid 
string value. If you then print $b, $b will have valid numeric *and* string 
parts, etc. etc.

> $ perl -MDevel::Peek -le '$a = "12.34"; print "Before:"; Dump($a); 0 + $a; 
print "After:"; Dump($a)'
> Before:
> SV = PV(0xe58b68) at 0xe80ca8
>   REFCNT = 1
>   FLAGS = (POK,pPOK)
>   PV = 0xe7ad00 "12.34"\0
>   CUR = 5
>   LEN = 8
> After:
> SV = PVNV(0xe5a0b0) at 0xe80ca8
>   REFCNT = 1
>   FLAGS = (NOK,POK,pIOK,pNOK,pPOK)
>   IV = 12
>   NV = 12.34
>   PV = 0xe7ad00 "12.34"\0
>   CUR = 5
>   LEN = 8

> $ perl -MDevel::Peek -le '$b = 12.34; print "Before:"; Dump($b); "" . $b; 
print "After:"; Dump($b)'
> Before:
> SV = NV(0x15810d0) at 0x1559ca8
>   REFCNT = 1
>   FLAGS = (NOK,pNOK)
>   NV = 12.34
> After:
> SV = PVNV(0x15330b0) at 0x1559ca8
>   REFCNT = 1
>   FLAGS = (NOK,POK,pNOK,pPOK)
>   IV = 0
>   NV = 12.34
>   PV = 0x157c020 "12.34"\0
>   CUR = 5
>   LEN = 40

JSON is presumably turning JSON numeric literals into numeric scalars (storing 
0 + $val), and likewise when it gets a value that has NOK or IOK but not POK, 
it outputs a numeric value without quotes.

Andrew


More information about the Chicago-talk mailing list