问题
I'm debugging a perl script which looks like this (simplified):
#!/usr/bin/perl
use strict;
use warnings;
use Evil::Module;
printf "%.3f\n", 0.1;
This script outputs 0,100
(note ,
instead of .
). If I comment out the use Evil::Module
statement, the output will be 0.100
.
I believe that this is related to locale setting in the module. But locale is a lexical pragma (according to the manpage), and it's not used within the script. What's happening here?
回答1:
The use locale
pragma is lexical, but if the Evil::Module module uses POSIX::setlocale
, then the locale change is global.
See perldoc perllocale for more information.
回答2:
Here's an excerpt from perldoc perllocale which makes the issue clear:
write() and LC_NUMERIC
Formats are the only part of Perl that unconditionally use information from a program's locale; if a program's environment specifies an LC_NUMERIC locale, it is always used to specify the decimal point character in formatted output. Formatted output cannot be controlled by
use locale
because the pragma is tied to the block structure of the program, and, for historical reasons, formats exist outside that block structure.
It seems that print()
and printf()
have the same behaviour.
来源:https://stackoverflow.com/questions/3214418/is-locale-setting-global-in-perl