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

Discussion in 'Perl Misc' started by Liang Wang, Feb 2, 2008.

  1. Liang Wang

    Liang Wang Guest

    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?
    Liang Wang, Feb 2, 2008
    #1
    1. Advertising

  2. Liang Wang wrote:
    > 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
    --
    Perl isn't a toolbox, but a small machine shop where you
    can special-order certain sorts of tools at low cost and
    in short order. -- Larry Wall
    John W. Krahn, Feb 2, 2008
    #2
    1. Advertising

  3. Liang Wang <> wrote:
    >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
    Jürgen Exner, Feb 2, 2008
    #3
  4. Liang Wang

    Liang Wang Guest

    On Feb 2, 11:19 am, "John W. Krahn" <> wrote:
    > Liang Wang wrote:
    > > 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
    > --
    > Perl isn't a toolbox, but a small machine shop where you
    > can special-order certain sorts of tools at low cost and
    > in short order. -- Larry Wall


    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.
    Liang Wang, Feb 2, 2008
    #4
  5. Liang Wang

    Ben Morrow Guest

    Quoth 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?


    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
    Ben Morrow, Feb 2, 2008
    #5
  6. Liang Wang wrote:
    > On Feb 2, 11:19 am, "John W. Krahn" <> wrote:
    >> Liang Wang wrote:
    >>> 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.

    >
    > 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
    --
    Perl isn't a toolbox, but a small machine shop where you
    can special-order certain sorts of tools at low cost and
    in short order. -- Larry Wall
    John W. Krahn, Feb 2, 2008
    #6
  7. Liang Wang <> wrote:

    > 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.


    --
    Tad McClellan
    email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
    Tad J McClellan, Feb 2, 2008
    #7
  8. Ben Morrow wrote:
    > Quoth 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?

    >
    > 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
    --
    Perl isn't a toolbox, but a small machine shop where you
    can special-order certain sorts of tools at low cost and
    in short order. -- Larry Wall
    John W. Krahn, Feb 2, 2008
    #8
  9. Liang Wang

    Ben Morrow Guest

    Quoth "John W. Krahn" <>:
    > Ben Morrow wrote:
    > >
    > > ~% 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


    Heh :). Yes, sorry.

    Ben
    Ben Morrow, Feb 2, 2008
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page