Subject: | unknown_currency does not restore the locale |
The unknown_currency function in Math::Currency iterates over the
installed locales, and uses the data from the first locale that
matches the specified currency code to populate the $format hashref.
However, it does not restore the original locale after determining
which is relevant. I don't think there's any reason to not do this; if
there is, then it should at least restore the original locale when it
can't find one that is appropriate. Patch attached.
(The $VERSION-related code in the patch is for handling the case where
the effective locale renders $VERSION as e.g. 0,47, rather than 0.47.
Perl does not appear to take the locale into account when processing
the version for the new currency modules, and warns about 'useless use
of a constant in void context'.)
perl: v5.8.8 built for i386-linux-thread-multi
Cheers
-Tom
Subject: | Math-Currency-0.47-locale.patch |
diff -Naur Math-Currency-0.47/lib/Math/Currency.pm Math-Currency-0.47-patch/lib/Math/Currency.pm
--- Math-Currency-0.47/lib/Math/Currency.pm 2010-08-11 05:33:55.000000000 +1000
+++ Math-Currency-0.47-patch/lib/Math/Currency.pm 2012-01-04 08:18:17.000000000 +1000
@@ -370,6 +370,8 @@
{
my ($currency) = @_;
$DB::single=1;
+ my $original_locale = setlocale( LC_ALL );
+ my $version = "$Math::Currency::VERSION";
open LOCALES, "-|", "locale -a";
while (my $LOCALE = <LOCALES>) {
chomp($LOCALE);
@@ -389,14 +391,14 @@
package Math::Currency::${LOCALE};
use vars qw(\$VERSION \@ISA \$LANG);
-\$VERSION = $Math::Currency::VERSION;
+\$VERSION = $version;
\$LANG = '$LOCALE';
\@ISA = qw/Math::Currency/;
1;
package Math::Currency::${int_curr};
use vars qw(\$VERSION \@ISA \$LANG);
-\$VERSION = $Math::Currency::VERSION;
+\$VERSION = $version;
\$LANG = '$LOCALE';
\@ISA = qw/Math::Currency/;
1;
@@ -405,6 +407,7 @@
}
}
close LOCALES;
+ setlocale( LC_ALL, $original_locale );
}
# additional methods needed to get/set package globals
diff -Naur Math-Currency-0.47/t/007_restore_locale.t Math-Currency-0.47-patch/t/007_restore_locale.t
--- Math-Currency-0.47/t/007_restore_locale.t 1970-01-01 10:00:00.000000000 +1000
+++ Math-Currency-0.47-patch/t/007_restore_locale.t 2012-01-04 09:25:46.000000000 +1000
@@ -0,0 +1,31 @@
+use POSIX qw(locale_h);
+use Test::More tests => 20;
+
+BEGIN { use_ok( Math::Currency ); }
+
+my $locale = setlocale( LC_ALL );
+ok ( $locale, 'Got current locale' );
+
+for my $currency_code (qw(USD EUR GBP CAD AUD JPY ZAR)) {
+ Math::Currency->format($currency_code);
+
+ is ( setlocale( LC_ALL ), $locale,
+ "Setting currency format did not change the locale ".
+ "($currency_code)");
+
+ my $currency_obj = Math::Currency->new('1.23', $currency_code);
+
+ is ( setlocale( LC_ALL ), $locale,
+ "Setting currency for single object did not change the ".
+ "locale ($currency_code)");
+}
+
+for my $invalid_currency_code (qw(ABC DEF GHI JKL)) {
+ Math::Currency->format($invalid_currency_code);
+
+ is ( setlocale( LC_ALL ), $locale,
+ "Setting invalid currency format did not change the locale ".
+ "($invalid_currency_code)");
+}
+
+1;