locale and print()

Discussion in 'Perl Misc' started by Nico Rittner, Oct 12, 2004.

  1. Nico Rittner

    Nico Rittner Guest

    Hi,

    i am really confused about how perl locale
    deals with print() ( not printf).
    See the tiny example below.
    If the var- Declaration is quoted, print()
    behaves like expected: printf with comma,
    print with dot .. unquoted both with comma.

    If arithmetic operations are done with it,
    "print $val + 7" outputs the result with
    a comma in it. "print $val" itself outputs
    a dot like expected. What is the difference
    of perl's interpretions of quoted (string)
    and unqoted decimal values. And how the
    hell do arithmetic operations on $val
    influence print() 's behaviour ??
    Is it really true that print() will NOT
    use locale definitions?

    I have to do some kind of mathematical
    calculations, show some results (formatted)
    on screen and put the results in a mysql-table,
    which only expects dots as decimal seperators,
    i believe.
    Doing tr/,/./ or disabling locale
    before writing to mysql really can't
    be the right way to do it, i think.

    Tanks a lot
    and regards,

    Nico


    <snippet>
    use strict;
    use POSIX qw(locale_h);
    use locale; # also tried without this line.
    setlocale(LC_NUMERIC,"de_DE@EURO");

    my $val;

    $val = "6.02";
    printf ("%.2f\n",$val);
    print $val;
    print "\n";
    print $val + 7;
    print "\n\n";

    $val = 6.02;
    printf ("%.2f\n",$val);
    print $val;
    print "\n";
    print $val + 7;
    print "\n";
    </snippet>

    Output:
    6,02
    6.02
    13,02

    6,02
    6,02
    13,02
    Nico Rittner, Oct 12, 2004
    #1
    1. Advertising

  2. Nico Rittner <> wrote:

    > i am really confused about how perl locale
    > deals with print() ( not printf).
    > See the tiny example below.
    > If the var- Declaration is quoted, print()
    > behaves like expected: printf with comma,
    > print with dot .. unquoted both with comma.



    ie. in the first set of prints, $val is a string.


    > If arithmetic operations are done with it,
    > "print $val + 7" outputs the result with
    > a comma in it. "print $val" itself outputs
    > a dot like expected. What is the difference
    > of perl's interpretions of quoted (string)
    > and unqoted decimal values. And how the
    > hell do arithmetic operations on $val
    > influence print() 's behaviour ??



    When a number is "stringified", it follows the locale.

    There is no need to convert number->string for the 1st "print $val;"
    so the dot remains.

    The 2nd "print $val;" needs to do the conversion, so it uses comma.


    > Is it really true that print() will NOT
    > use locale definitions?



    It *is* using locale definitions.

    Whenever it needs to convert a number into a string, it is using a comma,
    just like it is supposed to.


    ><snippet>
    > use strict;
    > use POSIX qw(locale_h);
    > use locale; # also tried without this line.
    > setlocale(LC_NUMERIC,"de_DE@EURO");

    ^^^^^
    ^^^^^

    Isn't strict complaining about that undeclared variable?

    Is this your actual code?


    > my $val;
    >
    > $val = "6.02";
    > printf ("%.2f\n",$val);
    > print $val;



    It started life as a string, no need to convert a number to a string here.


    > $val = 6.02;
    > printf ("%.2f\n",$val);
    > print $val;



    It started life as a number, need to stringify it for output.


    > Output:
    > 6,02
    > 6.02
    > 13,02
    >
    > 6,02
    > 6,02
    > 13,02



    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Oct 12, 2004
    #2
    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.
Similar Threads
  1. Maurice Hulsman
    Replies:
    1
    Views:
    1,839
    Guus Bosman
    Jul 25, 2004
  2. Replies:
    4
    Views:
    1,000
  3. Gabriel Genellina
    Replies:
    0
    Views:
    692
    Gabriel Genellina
    Feb 18, 2009
  4. zade
    Replies:
    1
    Views:
    601
    James Kanze
    Mar 5, 2010
  5. Sibylle Koczian
    Replies:
    2
    Views:
    1,109
    Sibylle Koczian
    Nov 20, 2010
Loading...

Share This Page