Is locale setting global in perl?

半城伤御伤魂 提交于 2019-12-23 21:11:10

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!