in reply to Need some wisdom on strings to numbers

This seems to be a weird artifact of Data::Dumper's processing:

#!/usr/bin/perl use Data::Dumper; use Devel::Peek; my $store = {}; my $numString2 = '123.10'; my $num2 = $numString2+0; Dump $num2; #1 $store->{'num2'}=$num2; Dump $store->{'num2'}; #2 print Dumper $store; #3 Dump $store->{'num2'}; #4 __END__ SV = NV(0x636e40) at 0x604fd0 #1 REFCNT = 1 FLAGS = (PADBUSY,PADMY,NOK,pNOK) NV = 123.1 SV = NV(0x636e50) at 0x604410 #2 REFCNT = 1 FLAGS = (NOK,pNOK) NV = 123.1 $VAR1 = { #3 'num2' => '123.1' }; SV = PVNV(0x6b42f8) at 0x604410 #4 REFCNT = 1 FLAGS = (NOK,POK,pNOK,pPOK) IV = 0 NV = 123.1 PV = 0x6eb260 "123.1"\0 <-- side effect of dumping it with Data::D +umper CUR = 5 LEN = 40

As you can see in Devel::Peek's output, $num2 initially is a number only (no PV), but after dumping it with Data::Dumper, the string representation has been added to the scalar. And apparently, Data::Dumper then also prefers the string representation for its output...

Replies are listed 'Best First'.
Re^2: Need some wisdom on strings to numbers
by almut (Canon) on Oct 07, 2009 at 14:55 UTC

    swampyankee asked whether I would consider this a bug to be filed against Data::Dumper.  As I have no decided opinion on the matter, I'd like to pass the question on to all of you...   Essentially, I see two issues:

    (1) Data::Dumper promises

    (...) The return value can be "eval"ed to get back an identical copy of the original reference structure.

    However:

    use Data::Dumper; use Devel::Peek; my $foo = { num => 123.1 }; Dump $foo->{num}; my $serialised = Dumper $foo; $foo = eval $serialised; # deserialise Dump $foo->{num}; __END__ SV = NV(0x669d40) at 0x63c430 REFCNT = 1 FLAGS = (NOK,pNOK) NV = 123.1 <-- originally, it's a number SV = PV(0x63d918) at 0x742c80 REFCNT = 1 FLAGS = (POK,pPOK) PV = 0x6650d0 "123.1"\0 <- it's a string now, when deserialised... CUR = 5 LEN = 8

    But what would you consider "identical"...? I.e. does internal representation really matter, as Perl would automagically convert under most circumstances, anyway?

    (2) It's not exactly good manners to silently modify data structures under the hood, when being passed them just for dumping purposes (this could cause unexpected issues with copy-on-write semantics with forked processes — such as when trying to preload libs/data to be shared across processes, in order to keep memory usage low (mod_perl comes to mind...)).  OTOH, this is not the only case in Perl where such things do happen...

    So, whuttaya think?

      Personally, I would consider silently modifying data structures to be a bug. I realize there may be reasons for Data::Dumper treating floats as strings, mostly because Perl relies on the math library used by the C compiler used to build Perl's executable, and it may be distracting to see 123.1 show up as 123.09999… in Data::Dumper's output. The most obvious fix is to modify the documentation ("Data::Dumper outputs all floating point values as strings so that 123.1 doesn't appear as 123.0999…"). This has the obvious advantage of avoiding the need to change any code.


      Information about American English usage here and here. Floating point issues? Please read this before posting. — emc