File Coverage

blib/lib/HTML/FormFu/Filter/FormatNumber.pm
Criterion Covered Total %
statement 19 20 95.0
branch 1 4 25.0
condition n/a
subroutine 5 5 100.0
pod 0 1 0.0
total 25 30 83.3


line stmt bran cond sub pod time code
1             package HTML::FormFu::Filter::FormatNumber;
2              
3 3     3   650 use strict;
  3         6  
  3         130  
4             our $VERSION = '2.05'; # VERSION
5              
6 3     3   13 use Moose;
  3         3  
  3         17  
7             extends 'HTML::FormFu::Filter';
8              
9 3     3   12368 use Number::Format;
  3         6  
  3         136  
10 3     3   11 use POSIX qw( setlocale LC_NUMERIC );
  3         4  
  3         21  
11              
12             __PACKAGE__->mk_inherited_accessors(qw( locale ));
13              
14             sub filter {
15 1     1 0 2 my ( $self, $value ) = @_;
16              
17 1         3 my $backup_locale = setlocale(LC_NUMERIC);
18              
19 1 50       7 if ( my $locale = $self->locale ) {
20              
21             # if unable to set locale, this isn't a validation error that the user
22             # should see, so return the original value, rather than throwing an error
23              
24 0 0       0 setlocale( LC_NUMERIC, $locale )
25             or return $value;
26             }
27              
28 1         3 my $format = Number::Format->new;
29              
30 1         194 $value = $format->unformat_number($value);
31              
32             # restore orginal locale
33 1         163 setlocale( LC_NUMERIC, $backup_locale );
34              
35 1         5 return $value;
36             }
37              
38             __PACKAGE__->meta->make_immutable;
39              
40             1;
41              
42             __END__
43              
44             =head1 NAME
45              
46             HTML::FormFu::Filter::FormatNumber - Convert a formatted number from a known locale
47              
48             =head1 VERSION
49              
50             version 2.05
51              
52             =head1 SYNOPSIS
53              
54             This filter simply does the opposite of
55             L<HTML::FormFu::Deflator::FormatNumber>. It removes all thousands separators
56             and decimal points and returns the raw number which can be handled by perl.
57             See L<Number::Format/unformat_number> for more details.
58              
59             locale: de_DE
60             elements:
61             - type: Text
62             name: number
63             filters:
64             - type: FormatNumber
65              
66             # An input value like "13.233.444,22" will be transformed to "13233444.22"
67             # Same for "13233444,22"
68              
69             =head1 METHODS
70              
71             =head2 locale
72              
73             If no locale is found, the server's locale will be used.
74              
75             This method is a special 'inherited accessor', which means it can be set on
76             the form, a enclosing block element, the field, or this filter.
77             When the value is read, if no value is defined it automatically traverses
78             the element's hierarchy of parents, through any block elements and up to the
79             form, searching for a defined value.
80              
81             =head1 AUTHOR
82              
83             Moritz Onken, C<onken at houseofdesign.de>
84              
85             =head1 SEE ALSO
86              
87             Is a sub-class of, and inherits methods from L<HTML::FormFu::Filter>
88              
89             L<HTML::FormFu::Deflator::FormatNumber>
90              
91             L<HTML::FormFu>
92              
93             =head1 COPYRIGHT & LICENSE
94              
95             Copyright 2008 Moritz Onken, all rights reserved.
96              
97             This program is free software; you can redistribute it and/or modify it
98             under the same terms as Perl itself.