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