Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Data::Dumper turns floating points numbers into strings

by ig (Vicar)
on Jun 14, 2011 at 18:43 UTC ( #909626=note: print w/ replies, xml ) Need Help??


in reply to Data::Dumper turns floating points numbers into strings

Note that this effect isn't unique to Data::Dumper. Any operation that stringifies a number or integer will change how JSON (the pure Perl implementation, I didn't look at the XS) represents it.

use strict; use warnings; use JSON; my $json = JSON->new; my $data = { foo => 'bar', qux => 0.42 }; printf "before: '%s'\n", $json->encode($data); print "$data->{qux}\n"; printf "after: '%s'\n", $json->encode($data);

gives

before: '{"qux":0.42,"foo":"bar"}' 0.42 after: '{"qux":"0.42","foo":"bar"}'

This problem isn't unique to JSON either. I have seen similar issues in Win32::OLE, for example. Anything that inspects the scalar type and behaves differently depending on what it finds must resolve the ambiguity, shown by ikegami / Devel::Peek, that sometimes exists. When using such modules, you must be careful with your data.

One way to avoid the problem would be to make a copy of your data and stringify the copy.


Comment on Re: Data::Dumper turns floating points numbers into strings
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2014-08-30 16:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (293 votes), past polls