Skip to content

Commit

Permalink
initial implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
wchristian committed Jun 26, 2012
1 parent 1f47a18 commit 7ea56f8
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 15 deletions.
28 changes: 16 additions & 12 deletions META.json
@@ -1,10 +1,10 @@
{
"abstract" : "#",
"abstract" : "a Number::Format that ignores the system locale",
"author" : [
"Christian Walde <walde.christian@googlemail.com>"
],
"dynamic_config" : 0,
"generated_by" : "Dist::Zilla version 4.300006, CPAN::Meta::Converter version 2.113640",
"generated_by" : "Dist::Zilla version 4.300017, CPAN::Meta::Converter version 2.120921",
"license" : [
"unrestricted"
],
Expand Down Expand Up @@ -32,25 +32,29 @@
},
"runtime" : {
"requires" : {
"perl" : "5.006",
"strictures" : 0
"Carp" : "0",
"Number::Format" : "0",
"base" : "0",
"perl" : "5.008",
"strictures" : "0"
}
},
"test" : {
"requires" : {
"File::Find" : 0,
"File::Temp" : 0,
"Test::InDistDir" : 0,
"Test::More" : 0,
"strict" : 0,
"warnings" : 0
"File::Find" : "0",
"File::Temp" : "0",
"Test::InDistDir" : "0",
"Test::More" : "0",
"strict" : "0",
"utf8" : "0",
"warnings" : "0"
}
}
},
"provides" : {
"Number::Format::FixedLocale" : {
"file" : "lib/Number/Format/FixedLocale.pm",
"version" : "0.000001"
"version" : "1.121780"
}
},
"release_status" : "stable",
Expand All @@ -66,6 +70,6 @@
"web" : "https://github.com/wchristian/number-format-fixedlocale"
}
},
"version" : "0.000001"
"version" : "1.121780"
}

30 changes: 28 additions & 2 deletions README.pod
@@ -1,10 +1,34 @@
=head1 NAME

Number::Format::FixedLocale
Number::Format::FixedLocale - a Number::Format that ignores the system locale

=head1 VERSION

version 0.000001
version 1.121780

=head1 SYNOPSIS

use Number::Format::FixedLocale;
my $f = Number::Format::FixedLocale->new(
-mon_thousands_sep => '.',
-mon_decimal_point => ',',
-int_curr_symbol => 'EUR',
-n_cs_precedes => 0,
-p_cs_precedes => 0,
);
print $f->format_price( -45208.23 ); # "-45.208,23 EUR"

=head1 DESCRIPTION

L<Number::Format> is a very useful module, however in environments with many
systems it can be a liability due to the fact that it gathers its default
settings from the system locale, which can lead to surprising results when
formatting numbers in production.

Number::Format::FixedLocale is a sub-class of L<Number::Format> that contains
only a slightly modified constructor, which will only use a fixed set of en_US
default settings. Thus any results from this module will be predictable no
matter how the system it is being run on is configured.

=for :stopwords cpan testmatrix url annocpan anno bugtracker rt cpants kwalitee diff irc mailto metadata placeholders metacpan

Expand All @@ -31,10 +55,12 @@ Christian Walde <walde.christian@googlemail.com>

=head1 COPYRIGHT AND LICENSE


Christian Walde has dedicated the work to the Commons by waiving all of his
or her rights to the work worldwide under copyright law and all related or
neighboring legal rights he or she had in the work, to the extent allowable by
law.

Works under CC0 do not require attribution. When citing the work, you should
not imply endorsement by the author.

73 changes: 72 additions & 1 deletion lib/Number/Format/FixedLocale.pm
Expand Up @@ -4,8 +4,79 @@ package Number::Format::FixedLocale;

# VERSION

# ABSTRACT:
# ABSTRACT: a Number::Format that ignores the system locale

# COPYRIGHT

=head1 SYNOPSIS
use Number::Format::FixedLocale;
my $f = Number::Format::FixedLocale->new(
-mon_thousands_sep => '.',
-mon_decimal_point => ',',
-int_curr_symbol => 'EUR',
-n_cs_precedes => 0,
-p_cs_precedes => 0,
);
print $f->format_price( -45208.23 ); # "-45.208,23 EUR"
=head1 DESCRIPTION
L<Number::Format> is a very useful module, however in environments with many
systems it can be a liability due to the fact that it gathers its default
settings from the system locale, which can lead to surprising results when
formatting numbers in production.
Number::Format::FixedLocale is a sub-class of L<Number::Format> that contains
only a slightly modified constructor, which will only use a fixed set of en_US
default settings. Thus any results from this module will be predictable no
matter how the system it is being run on is configured.
=cut

use base 'Number::Format';

use Carp 'croak';

sub new
{
my $type = shift;
my %args = @_;

# Fetch defaults from current locale, or failing that, using globals
my $me = {};

my $arg;

while(my($arg, $default) = each %$Number::Format::DEFAULT_LOCALE)
{
$me->{$arg} = $default;

foreach ($arg, uc $arg, "-$arg", uc "-$arg")
{
next unless defined $args{$_};
$me->{$arg} = $args{$_};
delete $args{$_};
last;
}
}

#
# Some broken locales define the decimal_point but not the
# thousands_sep. If decimal_point is set to "," the default
# thousands_sep will be a conflict. In that case, set
# thousands_sep to empty string. Suggested by Moritz Onken.
#
foreach my $prefix ("", "mon_")
{
$me->{"${prefix}thousands_sep"} = ""
if ($me->{"${prefix}decimal_point"} eq
$me->{"${prefix}thousands_sep"});
}

croak "Invalid argument(s)" if %args;
bless $me, $type;
$me;
}

1;
19 changes: 19 additions & 0 deletions t/basic.t
Expand Up @@ -6,12 +6,31 @@ use Test::InDistDir;
use Test::More;

use Number::Format::FixedLocale;
use utf8;

run();
done_testing;
exit;

sub run {

ok my $f = Number::Format::FixedLocale->new();

is $f->format_price( -45208.23 ), "-USD 45,208.23",
"basic price is formatted according to Number::Format default locale";

my %settings = (
-mon_thousands_sep => '.',
-mon_decimal_point => ',',
-int_curr_symbol => '',
-n_cs_precedes => 0,
-p_cs_precedes => 0,
);
my $neg_loc_price = Number::Format::FixedLocale->new( %settings )->format_price( -45208.23 );
my $pos_loc_price = Number::Format::FixedLocale->new( %settings )->format_price( 45208.23 );

is $neg_loc_price, "-45.208,23 €", "customized price is formatted according to settings";
is $pos_loc_price, "45.208,23 €", "customized price is formatted according to settings";

return;
}

0 comments on commit 7ea56f8

Please sign in to comment.