Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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.


In reply to Re: Data::Dumper turns floating points numbers into strings by ig
in thread Data::Dumper turns floating points numbers into strings by Hue-Bond

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (5)
As of 2024-04-25 14:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found