When does the difference between a string and a number matter in Perl 5?

后端 未结 4 1748
逝去的感伤
逝去的感伤 2020-12-17 19:30

If a string in Perl 5 passes looks_like_number, it might as well be a number. For instance,

my $s = \"10\" + 5;

results in $s

4条回答
  •  情书的邮戳
    2020-12-17 20:18

    An equivalent number and string behave differently in hash keys -- or, more generally, any time we stringify a largish number:

    my (%g, %h);
    $g{ 1234000000000000 } = undef;  # '1.234e+015'       => undef
    $h{'1234000000000000'} = undef;  # '1234000000000000' => undef
    

    Note that we are still within the range where Perl can store the number precisely:

    > perl -e 'printf qq{%.f\n}, 1234000000000000 + $_ for +1, 0, -1'
    1234000000000001
    1234000000000000
    1233999999999999
    

提交回复
热议问题