in reply to Need some wisdom on strings to numbers
Edit: almut beat me to it. Thats what I get for walking the dogs mid-post.
A couple of things...
First, it probably should not matter to you whether something is a 'string' or a 'number', as internally perl will convert it between strings and numbers as needed. The only use case I can think of for needing to care is if you are passing things to a serialization routine, where the output is destined for another programming language where it matters.
Second, I'm not sure I'd take the output of Data::Dumper at face value. Since from perl's perspective 123.1 is more or less the same as '123.1', it may just display the string.
If you really care to see the internal representation of a perl variable, you can use Devel::Peek.
use Devel::Peek qw| Dump |; my $x = 123.1; my $y = '123.10'; my $z = $y + 0; Dump $x; Dump $y; Dump $z;
Outputs
SV = NV(0x81ac10) at 0x801794 REFCNT = 1 FLAGS = (PADBUSY,PADMY,NOK,pNOK) NV = 123.1 SV = PVNV(0x8044d0) at 0x801770 REFCNT = 1 FLAGS = (PADBUSY,PADMY,NOK,POK,pIOK,pNOK,pPOK) IV = 123 NV = 123.1 PV = 0x301200 "123.10"\0 CUR = 6 LEN = 8 SV = NV(0x81ac20) at 0x801788 REFCNT = 1 FLAGS = (PADBUSY,PADMY,NOK,pNOK) NV = 123.1
So, as you can see, $x and $z are strictly NVs ( numeric values ) while $y is a PVNV, which has both numeric and string values contained. More information on the 'c' types for perl variables can be found in perlguts.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Need some wisdom on strings to numbers
by decebel (Acolyte) on Oct 07, 2009 at 03:53 UTC | |
by decebel (Acolyte) on Oct 07, 2009 at 03:58 UTC | |
by alexlc (Beadle) on Oct 07, 2009 at 12:02 UTC |