in reply to Re^2: Hash value typing
in thread Hash value typing
Why do the Perl developers (or others for that matter) consider it OK to change the internal respresentation of data (albeit the narrow context of Hash values) when it is referenced?
Because that's what loose typing is all about - and perl works that way, sorry. In your example, you did not reference/dereference anything, but coerced the numeric value to a string, by treating it as a string (taking the length from it). Instead of creating a temporary variable as a typecast copy of the number value, perl stores that string value in the PV slot of the variable. The NV (numeric value) and/or IV (integer value) slots of the variables remain unaffected.
Apparently, for the JSON::XS code the PV slot has priority over the NV and IV slots. If you want numbers to remain numbers in your JSON output, you would need to inspect the values and numify them, e.g.
#!/usr/bin/perl %x=(a=>1); print length($x{a}),"~", encode_json( { map { $_ => $x{$_} =~ /^[\d.]+$/ ? 0+$x{$_} : $_ } keys %x } ),"\n"; __END__ 1~{"a":1}
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Hash value typing
by waynerasm (Initiate) on Jun 11, 2018 at 01:10 UTC |