Search code examples
perlposixlocale

Get current locale name in perl


I want to use thousands_sep in Perl. I can get it using the following code

use strict;
use POSIX qw(setlocale locale_h LC_ALL);
setlocale(LC_ALL, "fr_FR");
my $lc=localeconv();
print $lc->{thousands_sep};

After that I would like to change locale back to previous value, but I don't know how to get locale before I used setlocale. Should I parse it from $ENV{LANG} which is set to en_US.UTF-8 ? Or is there any other method to get locale name?


Solution

  • setlocale() returns the current locale when called without a second argument, eg.

    my $oldlocale = setlocale(LC_ALL);
    

    If you want to set locale according to the current environment, call it with an empty string (ie. "").