develooper Front page | perl.perl5.porters | Postings from June 2013

[perl #82418] sprintf() not respecting locale in 5.10+

Thread Previous
From:
James E Keenan via RT
Date:
June 20, 2013 23:44
Subject:
[perl #82418] sprintf() not respecting locale in 5.10+
Message ID:
rt-3.6.HEAD-2552-1371771845-292.82418-15-0@perl.org
On Thu Jun 20 09:38:41 2013, adi wrote:
> On Tue Jun 18 15:54:26 2013, khw wrote:
> > On Mon Jul 02 23:14:23 2012, sprout wrote:
> > > On Mon Jul 02 22:41:20 2012, sprout wrote:
> > > > On Mon Jul 02 20:17:15 2012, doy wrote:
> > > > > If changes to the runtime environment can cause sprintf to need to
> > > > > output different things, shouldn't we really just not be constant
> > > > > folding sprintf?
> > > > 
> > > > I could have sworn this was already fixed (see commit b3fd6149),
> but it
> > > > was only fixed for ‘use locale’, not POSIX::setlocale.
> > > 
> > > Actually, the perllocale page seems to say that setlocale is for
> > > switching locales at run time, but ‘use locale’ is necessary for
> > > enabling it.  It’s a bit vague, though.  This is what perl actually
> does:
> > > 
> > > $  ./perl -Ilib -MPOSIX -e 'setlocale(LC_ALL, "nl_NL"); print
> > > sprintf("%f", $_=2.5)."\n";'
> > > 2,500000
> > > $  ./perl -Ilib -MPOSIX -e 'setlocale(LC_ALL, "nl_NL"); print
> > > sprintf("%f", 2.5)."\n";'
> > > 2.500000
> > > 
> > 
> > You shouldn't expect to get locale effects outside of a 'use locale',
> > and I have just changed perllocale to be clear about that (I hope it's
> > clear anyway).
> > 
> > So this ticket is the complement of what it should be (in blead anyway).
> >  The sprintf is working fine: it should print a dot since there is no
> > 'use locale'.  If you add one, it prints the comma:
> > 
> > $ blead -MPOSIX -e 'setlocale(LC_NUMERIC, "fr_FR.utf8"); print
> > sprintf("%f",2.5)."\n";'         
> > 2.500000
> > $ blead -Mlocale -MPOSIX -e 'setlocale(LC_NUMERIC, "fr_FR.utf8"); print
> > sprintf("%f",2.5)."\n";'
> > 2,500000
> > 
> > The printf should not be printing a comma unless "use locale" is
> > specified, and hence, there is a bug there.
> 
> FYI- the above does not work in perl 5.10.1 or 5.12.4:
> 
> $ /opt/local/bin/perl -v
> This is perl 5, version 12, subversion 4 (v5.12.4) built for
> darwin-thread-multi-2level
> [...]
> $ /opt/local/bin/perl -MPOSIX -e 'setlocale(LC_NUMERIC, "fr_FR.utf8");
> print sprintf("%f",2.5)."\n";'
> 2.500000
> $ /opt/local/bin/perl -Mlocale -MPOSIX -e 'setlocale(LC_NUMERIC,
> "fr_FR.utf8"); print sprintf("%f",2.5)."\n";'
> 2.500000
> 
> $ perl -v
> This is perl, v5.10.1 (*) built for x86_64-linux-thread-multi
> [...]
> $ perl -MPOSIX -e 'setlocale(LC_NUMERIC, "fr_FR.utf8"); print
> sprintf("%f",2.5)."\n";'
> 2.500000
> $ perl -Mlocale -MPOSIX -e 'setlocale(LC_NUMERIC, "fr_FR.utf8"); print
> sprintf("%f",2.5)."\n";'
> 2.500000

What I got with blead:

#####
$ ./perl -v
This is perl 5, version 19, subversion 1 (v5.19.1
(v5.19.0-609-g28371e3)) built for x86_64-linux

$ ./perl -Ilib -MPOSIX -e 'setlocale(LC_NUMERIC, "fr_FR.utf8");print
sprintf("%f",2.5)."\n";'
2.500000

$ ./perl -Ilib -Mlocale -MPOSIX -e 'setlocale(LC_NUMERIC,
"fr_FR.utf8");print sprintf("%f",2.5)."\n";'
2,500000
#####

---
via perlbug:  queue: perl5 status: open
https://rt.perl.org:443/rt3/Ticket/Display.html?id=82418

Thread Previous


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About