in reply to Leading Zeros confound eval() ?

Perl treats literal numbers differently from strings converted to numbers, and as others have said literals beginning with zero are octal numbers:

$ perl -wMstrict -le 'print 0+"010"' 10 $ perl -wMstrict -le 'print 0+ 010 ' 8

When you write my $x = "016"; $y = eval("$x * 10");, the $x is evaluated into the string before the eval and it is the equivalent of asking Perl to evaluate 016 * 10:

$ perl -wMstrict -le 'my $x = "016"; print eval("$x * 10");' 140 $ perl -wMstrict -le 'print 016 * 10;' 140

Replies are listed 'Best First'.
Re^2: Leading Zeros confound eval() ?
by misterperl (Friar) on Jul 21, 2015 at 19:08 UTC
    thank-you that explanation makes sense. Its a odd side-effect of eval, IMHO, but I see why it's working that way..

    Voted you a ++

      BTW, official Perl documentation on this is here. Another difference is that underscores are not handled when strings are converted to numbers:

      $ perl -wMstrict -le 'print 0+ 1_234 ' 1234 $ perl -wMstrict -le 'print 0+"1_234"' Argument "1_234" isn't numeric in addition (+) at -e line 1. 1
        wow, I'm surprised by THAT also. Thanks again, very interesting results.