msbalaji has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks, I have a doubt about print statement as,

#!/usr/bin/perl use warnings; use strict; print 25_000_000, " ", -4, "\n"; expected output 25_000_000 -4 but getting 25000000 -4

What's happening?

Thanks in advance,
Balaji M.

Replies are listed 'Best First'.
Re: Print Statement
by izut (Chaplain) on Feb 17, 2006 at 11:41 UTC
    Perl treats 25_000_000 as integer. To print it correctly, you should do something like this:
    print "25_000_000", " ", -4, "\n";

    Igor 'izut' Sutton
    your code, your rules.

Re: Print Statement
by ikegami (Patriarch) on Feb 17, 2006 at 15:58 UTC
    1225.5, 1225.50, 12255E-1 and 1_225.5 are all stored indentically internally, and print will print them all as 1225.5 because that is how print displays the value 1225.5. print has no idea how the number came to be; it just knows its value. If you wish to preserve the underscores, you want to treat the number as a string instead of as a number, so put quotes around it.
      I was trying to do :
      perl -e 'print 0100101."\n"'
      As expected I got the decimal equivalent of octal as:
      32833
      However, doing
      perl -e 'print 100101."\n"'
      returned
      String found where operator expected at -e line 1, near "100101."\n"" (Missing operator before "\n"?) syntax error at -e line 1, near "100101."\n"" Execution of -e aborted due to compilation errors.
      Any idea why this fuss about a zero? Not a burning problem or something. Just curious.

        The "." is part of the number.

        print 100101. "\n" ^ missing operator

        Add a space before the ".".

Re: Print Statement
by Fletch (Bishop) on Feb 17, 2006 at 13:57 UTC

    a) You have a "question", not a "doubt";
    b) see perldoc perldata in the section "Scalar value constructors" where it covers that an underscore may be used in a numeric literal for readability (but perl will silently discard them after parsing your source).

      a) You have a "question", not a "doubt";

      The OP's original language is not English. Doubt is a reasonable translation from several languages.

        Yes, and I'm trying to help them become more idiomatic speakers of English; but I'll be sure to remember that the next time I see someone taking doubts from reporters at a press conference.

        Update: And I'm not the only person around here it annoys either (c.f. "Question" vs "Doubt").