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 | |
by Anonymous Monk on Jul 21, 2015 at 19:12 UTC | |
by misterperl (Friar) on Jul 21, 2015 at 19:19 UTC |