Eugene Yarmash
Eugene Yarmash

Reputation: 149814

Is locale setting global in perl?

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?

Upvotes: 3

Views: 425

Answers (2)

Eugene Yarmash
Eugene Yarmash

Reputation: 149814

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.

Upvotes: 1

mob
mob

Reputation: 118605

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.

Upvotes: 5

Related Questions