locale and print()

N

Nico Rittner

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
 
T

Tad McClellan

Nico Rittner said:
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.
 

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,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top