Different behavior between eval "07" and eval "08"

L

Liang Wang

eval "07" gets 7 but eval "08" gets an undefined value. I tested perl
5.8.8 on fedora 8 and ubuntu.

Is there any difference between those two eval statements? Is this a
bug?
 
J

John W. Krahn

Liang said:
eval "07" gets 7 but eval "08" gets an undefined value. I tested perl
5.8.8 on fedora 8 and ubuntu.

Is there any difference between those two eval statements? Is this a
bug?

No, there is no difference in the eval statements. The problem is that
"08" is not a valid octal number.


John
 
J

Jürgen Exner

Liang Wang said:
eval "07" gets 7 but eval "08" gets an undefined value. I tested perl
5.8.8 on fedora 8 and ubuntu.

Is there any difference between those two eval statements?

Yes. The digit 7 is a valid octal digit, 8 is not a valid octal digit.

jue
 
L

Liang Wang

No, there is no difference in the eval statements. The problem is that
"08" is not a valid octal number.

John

Got it. Thanks.

I've tested "06", but not "09". What I really want to do is to
transform string "08" to integer 8. But in this case, eval doesn't
treat "08" as string, as you said.
 
B

Ben Morrow

Quoth Liang Wang said:
eval "07" gets 7 but eval "08" gets an undefined value. I tested perl
5.8.8 on fedora 8 and ubuntu.

Is there any difference between those two eval statements? Is this a
bug?

Nope. Both 07 and 08 *as literals* (i.e. either specified directly in
your source or passed to eval STRING) are interpreted as octal numbers.
07 is a valid octal representation of the number 7; this number can also
be written 0x7 if you like hex, or 0b111 if you like binary :). 08 is
not a valid octal representation of any number, since 8 is not an octal
digit.

Why are you trying to eval a string consisting of a single number
anyway? You do know Perl will autoconvert a string to a number if you
use it as one, and in *this* case it always uses decimal? So "08" + 1
evaluates to 9, as you'd expect. If you're trying to 'normalise' the
representation of the number (get rid of the leading zeros, etc.) you
can force a numeric conversion by adding 0.

~% perl -le'print 0 + "08"'
9

Ben
 
J

John W. Krahn

Liang said:
Got it. Thanks.

I've tested "06", but not "09". What I really want to do is to
transform string "08" to integer 8. But in this case, eval doesn't
treat "08" as string, as you said.

The string "08" is already the integer 8.

$ perl -le' $x = "08"; print $x; print $x + 1'
08
9



John
 
T

Tad J McClellan

Liang Wang said:
What I really want to do is to
transform string "08" to integer 8.


Why do you think you need to transform string "08" to integer 8?

Values in Perl are both strings and numbers simultaneously.

You really should review the "Scalar values" and
"Scalar value constructors" sections in perldata.pod.
 
J

John W. Krahn

Ben said:
Nope. Both 07 and 08 *as literals* (i.e. either specified directly in
your source or passed to eval STRING) are interpreted as octal numbers.
07 is a valid octal representation of the number 7; this number can also
be written 0x7 if you like hex, or 0b111 if you like binary :). 08 is
not a valid octal representation of any number, since 8 is not an octal
digit.

Why are you trying to eval a string consisting of a single number
anyway? You do know Perl will autoconvert a string to a number if you
use it as one, and in *this* case it always uses decimal? So "08" + 1
evaluates to 9, as you'd expect. If you're trying to 'normalise' the
representation of the number (get rid of the leading zeros, etc.) you
can force a numeric conversion by adding 0.

~% perl -le'print 0 + "08"'
9

I think your version of Perl has a bug in it. Mine produces a different
result.

$ perl -le'print 0 + "08"'
8



John
 
B

Ben Morrow

Quoth "John W. Krahn said:
I think your version of Perl has a bug in it. Mine produces a different
result.

$ perl -le'print 0 + "08"'
8

Heh :). Yes, sorry.

Ben
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top