I noticed that with variables declared with the Readonly
module, evaluating a variable multiple times can yield different results.
>perl -Mbigint -MReadonly -wE "Readonly my $V => 1; foreach (1..2) { say 0 + '1000000000000001' * $V };
1000000000000000
1000000000000001
Why is that?
It seems like the first time the variable is interpreted in string, the second time in numeric context. My guess is that if it's numeric, the Math::BigInteger
module will overload the '*'
operator, yielding the exact result. Is this a bug in the Readonly
module, and is there any way to avoid that?
I use perl 5.10 and Readonly 1.03 without Readonly::XS
.
I can reproduce that with
v5.10.0
onMSWin32-x86-multi-thread
(ActivePerl)v5.10.0
on linuxx86_64-linux-thread-multi
.v5.12.0
on Windows (ActivePerl)
I doesn't happen with v5.14.2
(ActivePerl), however.
I have also reproduced it with Readonly 1.04. I'm not quite sure whether this is related, but Scalar::Util::looks_like_number
behaves similarly:
>perl -MReadonly -MScalar::Util -Mbigint -wE "say $Readonly::VERSION; Readonly my $V => 1; foreach (1..2) { say Scalar::Util::looks_like_number $V; }"
1.04
0
1
v5.16.3
strawberry (1000000000000001 x2) – Imperviousv5.14.4
linux 64 bit either, Readonly 1.04. – Urinalv5.12.0
. – Immediacyv5.10.1
, see my answer. – Immediacy