On Thu, Feb 21, 2013 at 12:31 PM, Aristotle Pagaltzis <
paga...@gmx.de> wrote:
> * demerphq <
deme...@gmail.com> [2013-02-20 04:20]:
>> I am worried about cases like this:
>>
>> $x= "0 but true";
>> print 0+$x;
>>
>> I want to be able to know that this needs to be serialized as C<"0 but
>> true"> and not C<0>.
>
> Right, the issue isn’t treating perfect equivalents like 1 and '1' as
> different things, it is that imperfect conversions that lose information
> can attach non-canonical values to SVs a side effect of other operations
> and thereafter there is no way to know which value was canonical.
This is not generally true; "0 but true" is just a special case that
does get SVf_IOK set when used as a number. In general, only the
private SVp_IOK or SVp_NOK flags will be set if the conversion is
in-exact, to indicate that the PV is the canonical representation:
$ perl -MDevel::Peek -e '$a="0"; $b=$a+1; Dump $a'
SV = PVIV(0x100832bf0) at 0x100831cd8
REFCNT = 1
FLAGS = (IOK,POK,pIOK,pPOK)
IV = 0
PV = 0x100205db0 "0"\0
CUR = 1
LEN = 16
$ perl -MDevel::Peek -e '$a="0 but"; $b=$a+1; Dump $a'
SV = PVNV(0x100806330) at 0x100831cd8
REFCNT = 1
FLAGS = (POK,pIOK,pNOK,pPOK)
IV = 0
NV = 0
PV = 0x100205db0 "0 but"\0
CUR = 5
LEN = 16
$ perl -MDevel::Peek -e '$a="0 but true"; $b=$a+1; Dump $a'
SV = PVIV(0x100832bf0) at 0x100831cd8
REFCNT = 1
FLAGS = (IOK,POK,pIOK,pPOK)
IV = 0
PV = 0x100205dc0 "0 but true"\0
CUR = 10
LEN = 16
There are other special cases for "Inf" and "NaN" too, but it looks
like they will only get SVp_IOK set when they start out as strings,
not as numbers:
$ perl -MDevel::Peek -e '$a="Inf"; $b=$a+1; Dump $a'
SV = PVNV(0x100806330) at 0x100831cd8
REFCNT = 1
FLAGS = (NOK,POK,pIOK,pNOK,pPOK,IsUV)
UV = 18446744073709551615
NV = inf
PV = 0x100205db0 "Inf"\0
CUR = 3
LEN = 16
$ perl -MDevel::Peek -e '$a=2**9999; $b="$a"; Dump $a'
SV = PVNV(0x100806330) at 0x100831cd8
REFCNT = 1
FLAGS = (NOK,POK,pNOK,pPOK)
IV = 0
NV = inf
PV = 0x100214f00 "inf"\0
CUR = 3
LEN = 48
I think though that the SVp_IOK difference may be an accidental
implementation detail, so it would need tests to make sure we don't
accidentally change it in the future.
Another problem with NaN and Inf is that their string representation
is currently platform dependent (inherited from C RTL).
Cheers,
-Jan