in reply to When does '123' become the number 123.0?
what is actually stored in $a[$i]? Is it the string "123" or the number 123.0?
split returns strings.
And does it make any difference to the perl programmer?
No. It works as intended in practically all cases.
The only time I can think of where it makes a difference is when you use |, & or ^ on it with a string as the other operand.
>perl -le"($x,$y)=map 0+$_, @ARGV; print $x|$y;" 2 8 10 >perl -le"($x,$y)=map ''.$_, @ARGV; print $x|$y;" 2 8 :
I'm using the results entirely in numerical calculations, so I want to do all conversion of string-to-number (if any) at the time I read the file and get it over with early.
It's not needed, but you could use
my @nums = map 0+$_, split /,/;
I've been using the perl debugger to print these variables but I can't (yet) tell whether it's printing a string or a number.
That just shows you how much it matters.
Devel::Peek will show you. pPOK = string, pIOK, pUOK or pNOK = number. Can be both.
>perl -e"use Devel::Peek; my $x=123; Dump($x);" SV = IV(0x1826628) at 0x225350 REFCNT = 1 FLAGS = (PADBUSY,PADMY,IOK,pIOK) <-- signed int IV = 123 >perl -e"use Devel::Peek; my $x='123'; Dump($x);" SV = PV(0x226154) at 0x225350 REFCNT = 1 FLAGS = (PADBUSY,PADMY,POK,pPOK) <-- string PV = 0x18227c4 "123"\0 CUR = 3 LEN = 4 >perl -e"use Devel::Peek; my $x=123; qq{$x}; Dump($x);" SV = PVIV(0x22718c) at 0x225358 REFCNT = 1 FLAGS = (PADBUSY,PADMY,IOK,POK,pIOK,pPOK) <-- signed int AND string IV = 123 PV = 0x18227cc "123"\0 CUR = 3 LEN = 4
|
|---|