in reply to When does '123' become the number 123.0?

You could consider the contents of a Perl scalar to be a quantum superposition of string and numeric states which are resolved when you look at them. :-)


Perl is environmentally friendly - it saves trees
  • Comment on Re: When does '123' become the number 123.0?

Replies are listed 'Best First'.
Re^2: When does '123' become the number 123.0?
by quester (Vicar) on Jun 17, 2008 at 06:03 UTC

    But unlike real quanta, reading a Perl variable doesn't force it to collapse into only one state.

    To make this more concrete, note that converting a number (either integer or floating point) to a string leaves both the numeric AND string values marked as valid:

    $ perl -MDevel::Peek -wle '$x=12345; print Dump $x; print "$x"; print +Dump $x' SV = IV(0x1004f958) at 0x1004f958 REFCNT = 1 FLAGS = (IOK,pIOK) IV = 12345 12345 SV = PVIV(0x10023810) at 0x1004f958 REFCNT = 1 FLAGS = (IOK,POK,pIOK,pPOK) IV = 12345 PV = 0x10040c08 "12345"\0 CUR = 5 LEN = 8
    The same thing works for string to numeric conversions, so
    $x="12345.67"; $y=$x+1;
    leaves $x with a floating point value and a string value which are both valid.

    Not only is the Perl implementation Doing What I Mean, by caching the results before it even occurs to me that it would be a good idea it's Doing What I Should Have Meant!