develooper Front page | perl.perl5.porters | Postings from January 2019

[perl #133504] Issues with thousands separators in perllocale

From:
Tony Cook via RT
Date:
January 7, 2019 00:43
Subject:
[perl #133504] Issues with thousands separators in perllocale
Message ID:
rt-4.0.24-17567-1546821799-352.133504-15-0@perl.org
On Sat, 08 Sep 2018 08:22:24 -0700, jkeenan wrote:
> On Sat, 08 Sep 2018 11:00:00 GMT, rdiezmail-perl@yahoo.de wrote:
> > Adding thousands separators to a number is a rather common operation.
> > Most languages use a printf format string like "%'d", but Perl does
> > not support it. I suggest that you add this feature and be done with
> > it.
> >
> > Otherwise, searching the Internet will throw up a few alternatives
> > with different degrees of trouble. The following official Perl
> > documentation page has example code for that task:
> >
> > https://perldoc.perl.org/perllocale.html
> >
> > Search for "Format command line params for current locale" in that
> > page to find the example code I am talking about.
> >
> > My first impression when looking at that example was: A "1 while"
> > loop
> > with a cryptic regex inside... what an alarming Perl nerdiness
> > sign...
> >
> > A traditional loop without regular expressions is easier to
> > understand. Search for "This is for manually testing of
> > AddThousandsSeparators()" in the following Perl script I recently
> > wrote for an alternative implementation.
> >
> > https://github.com/rdiez/Tools/blob/master/AnnotateWithTimestamps/AnnotateWithTimestamps.pl
> >
> > I have done a quick benchmark, and my loop version is much faster, at
> > least with Perl v5.22.1.
> >
> > One bad practice that stands out in the perllocale example is the
> > usage of 2 variables with the same name but with different types,
> > namely $grouping and @grouping. Furthermore, I would not encourage
> > implicit usage of special variable $_ anymore. Using named variables
> > makes the code easier to grasp.
> >
> > But more seriously, the perllocale example code does not work
> > correctly with the Spanish locale, where the thousands separator is
> > the period ('.') character, probably because that character has a
> > special meaning in regular expressions. I guess some quoting is
> > necessary.
> 
> OP and list:  Please review patch attached.
> 
> Thank you very much.

@@ -748,6 +748,11 @@ Here's a simple-minded example program that rewrites its command-line
 parameters as integers correctly formatted in the current locale:
 
     use POSIX qw(locale_h);
+    use locale qw(:numeric);
+
+    my $old_locale;
+    $old_locale = setlocale(LC_NUMERIC);
+    setlocale(LC_NUMERIC, "es_ES.utf8");

You changed the locale, so it's no longer using what was the current locale.

+
+    my @grouping_list;
     if ($grouping) {
-        @grouping = unpack("C*", $grouping);
+        @grouping_list = unpack("C*", $grouping);
     } else {
-        @grouping = (3);
+        @grouping_list = (3);
     }
 
As others have said, @grouping_list isn't great, maybe rename $grouping instead,  eg. $packed_grouping.

     # Format command line params for current locale
-    for (@ARGV) {
-        $_ = int;    # Chop non-integer part
+    for my $input (@ARGV) {
+        my $i = int($input);

In this case I think $_ is just fine, since it's acting as a current "thing" to work on.

If it were to be renamed, I'd rather the "named" version ($input) be used inside the loop rather than the "not really named" $i.

Unrelated to the patch, the grouping code doesn't interpret the grouping list correctly, though I don't know if there's any locales anywhere that use the full power of the grouping list.  From POSIX:

The elements of grouping and mon_grouping are interpreted according to the following:

{CHAR_MAX}
    No further grouping is to be performed.
0
    The previous element is to be repeatedly used for the remainder of the digits.
other
    The integer value is the number of digits that comprise the current group. The next element is examined to determine the size of the next group of digits before the current group.

Tony

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



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