develooper Front page | perl.perl5.porters | Postings from September 2017

[perl #131155] string-to-number coercion caching broken by locale

Thread Next
From:
James E Keenan via RT
Date:
September 30, 2017 23:03
Subject:
[perl #131155] string-to-number coercion caching broken by locale
Message ID:
rt-4.0.24-10719-1506812585-967.131155-15-0@perl.org
On Sat, 15 Apr 2017 00:44:17 GMT, zefram@fysh.org wrote:
> 
> This is a bug report for perl from zefram@fysh.org,
> generated with the help of perlbug 1.40 running under perl 5.25.11.
> 
> 
> -----------------------------------------------------------------
> [Please describe your issue here]
> 
> Riffing off the discussion in [perl #130801] of the locale dependence
> of
> number-to-string coercion, and hence why we no longer cache that
> coercion
> in the scalar, I had a look at the converse string-to-number coercion,
> and found a bug:
> 
> $ LANG=de_DE perl -lwe '$a = "1,50"; { use locale; print 0+$a; } print
> 0+$a'
> 1,5
> 1.5
> $ LANG=de_DE perl -lwe '$a = "1,50"; print 0+$a'
> Argument "1,50" isn't numeric in addition (+) at -e line 1.
> 1
> 
> Observe that the string-to-number coercion is affected by locale,
> accepting comma as a decimal separator iff the locale uses comma in
> that way.  (Not shown: dot is accepted as a decimal separator
> regardless
> of locale.)  The result of the coercion is cached in the scalar, and
> subsequent numeric use of the scalar returns the cached value without
> recomputing the coercion.  Given the locale dependence, this caching
> is in principle wrong, because it means that coercions performed under
> different locale settings aren't getting their locale-specific
> results.
> That can be seen above, with the non-locale coercion producing a
> different
> result depending on whether a locale-using coercion was earlier
> performed.
> 
> But actually I think the locale dependence here is a mistake.  Unlike
> the
> locale dependence of number-to-string coercion, the locale dependence
> of
> this operation doesn't exist in any form in old perls.  It appeared
> from
> nowhere in perl 5.19.8, presumably in the same edit that
> (intentionally)
> changed the form of the locale control for number-to-string coercion.
> We also have some semantic reliance on the caching for reasons other
> than
> this effect on the value yielded: we only warn once about a non-
> numeric
> value, and we're somewhat open about the influence of an argument
> having
> been used in numeric context on the bitwise operators.
> 


To analyze this problem I added the 'de_DE' locale per instructions at https://askubuntu.com/questions/76013/how-do-i-add-locale-to-ubuntu-server#76106.  I then opened two terminals with (via perlbrew) two different versions of perl.

#####
[p5p] 510 $ perl -v | head -2 | tail -1
This is perl 5, version 18, subversion 4 (v5.18.4) built for x86_64-linux
[p5p] 511 $ LANG=de_DE perl -lwe '$a = "1,50"; { use locale; print 0+$a; } print 0+$a'
Argument "1,50" isn't numeric in addition (+) at -e line 1.
1
1
[p5p] 512 $ LANG=de_DE perl -lwe '$a = "1,50"; print 0+$a'
Argument "1,50" isn't numeric in addition (+) at -e line 1.
1
#####
[tmp] 521 $ perl -v | head -2 | tail -1
This is perl 5, version 26, subversion 0 (v5.26.0) built for x86_64-linux
[tmp] 522 $ LANG=de_DE perl -lwe '$a = "1,50"; { use locale; print 0+$a; } print 0+$a'
1,5
1.5
[tmp] 525 $ LANG=de_DE perl -lwe '$a = "1,50"; print 0+$a'
Argument "1,50" isn't numeric in addition (+) at -e line 1.
1
#####

Are these the results I should have expected for these two versions of perl?

Thank you very much.
-- 
James E Keenan (jkeenan@cpan.org)

---
via perlbug:  queue: perl5 status: new
https://rt.perl.org/Ticket/Display.html?id=131155

Thread Next


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