in reply to please explain print/say behavior with setlocale

Localization (or more precisely, LC_NUMERIC here) applies to numeric values being output.  Stringification, OTOH, as it happens in something like print 1/2 . "\n" before outputting the value, isn't always subject to localization* ...  And when you write print "1/2\n" etc., it's not represented as a number in the first place.

___

* update: interpolation and concatenation of numeric values stored in a variable apparently does localize:

setlocale LC_NUMERIC, "german"; $n = 1/2; print "$n\n"; # 0,5 print $n . "\n"; # 0,5 print 1/2 ."\n"; # 0.5

My guess would be this has to do with compile-time vs. run-time effects, i.e. the 1/2 in 1/2 . "\n" is being processed at compile-time without the locale setting having been modified yet, unless you put the setlocale in a BEGIN {} block...

Replies are listed 'Best First'.
Re^2: please explain print/say behavior with setlocale
by wwe (Friar) on Mar 30, 2010 at 11:40 UTC
    sorry, I don't understand why the output isn't represented like a number in case of print 1/2 . "\n" the output "0.5<newline>" looks like a number exactly like a division result of 1 divided by 2. And if it is a number it should be displayed using comma as german floating separator. In opposite it is very clear to me why print "1/2\n" produces "1/2<newline>" output - I feel completely fine with it.
Re^2: please explain print/say behavior with setlocale
by wwe (Friar) on Mar 30, 2010 at 11:54 UTC
    This is where I got stuck. Why
    $n = 1 / 2; print $n . "\n"; # 0,5 is different to print 1/2 ."\n"; # 0.5

      See my update as to compile-time vs. run-time.  IOW, when you write

      BEGIN { setlocale LC_NUMERIC, "german"; } $n = 1 / 2; print $n . "\n"; # 0,5 print 1/2 ."\n"; # 0,5

      you get localized output in both cases, because localization is already in effect at compile-time.

        Using BEGIN block is very cool solution for this problem. Thank you very much. Would you say missing German localization without a BEGIN block is a bug or intended behavior for advanced usage?