| 1 | 3 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 2 | |||||||||||
| 3 | package HTML::FormFu::Deflator::FormatNumber; | ||||||||||
| 4 | 3 | $HTML::FormFu::Deflator::FormatNumber::VERSION = '2.06'; | |||||||||
| 5 | # ABSTRACT: Format a number for a locale | ||||||||||
| 6 | |||||||||||
| 7 | 3 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 3 | ▶ | use MooseX::Attribute::Chained; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 3 | extends 'HTML::FormFu::Deflator'; | |||||||||
| 10 | |||||||||||
| 11 | 3 | ▶ | use Number::Format; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 3 | ▶ | use POSIX qw( setlocale LC_NUMERIC ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | |||||||||||
| 14 | 3 | has precision => ( | |||||||||
| 15 | is => 'rw', | ||||||||||
| 16 | default => 2, | ||||||||||
| 17 | lazy => 1, | ||||||||||
| 18 | isa => 'Int', | ||||||||||
| 19 | traits => ['Chained'], | ||||||||||
| 20 | ); | ||||||||||
| 21 | |||||||||||
| 22 | 3 | has trailing_zeroes => ( | |||||||||
| 23 | is => 'rw', | ||||||||||
| 24 | default => 0, | ||||||||||
| 25 | lazy => 1, | ||||||||||
| 26 | isa => 'Int', | ||||||||||
| 27 | traits => ['Chained'], | ||||||||||
| 28 | ); | ||||||||||
| 29 | |||||||||||
| 30 | sub deflator { | ||||||||||
| 31 | 2 | ▶ | my ( $self, $value ) = @_; | ||||||||
|
Subroutine
deflator: called CC=3 SCAR=12.0
Pod
undocumented
| |||||||||||
| 32 | |||||||||||
| 33 | 2 | my $backup_locale = setlocale(LC_NUMERIC); | |||||||||
| 34 | |||||||||||
| 35 | 2 | ▶ | if ( my $locale = $self->locale ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 36 | |||||||||||
| 37 | # throwing errors from deflator() isn't supported | ||||||||||
| 38 | # if unable to set locale, return the original value | ||||||||||
| 39 | |||||||||||
| 40 | 0 | ▶ | setlocale( LC_NUMERIC, $locale ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 41 | or return $value; | ||||||||||
| 42 | } | ||||||||||
| 43 | |||||||||||
| 44 | 2 | my $format = Number::Format->new; | |||||||||
| 45 | |||||||||||
| 46 | 2 | $value = $format->format_number( $value, $self->precision, | |||||||||
| 47 | $self->trailing_zeroes ); | ||||||||||
| 48 | |||||||||||
| 49 | # restore locale | ||||||||||
| 50 | 2 | setlocale( LC_NUMERIC, $backup_locale ); | |||||||||
| 51 | |||||||||||
| 52 | 2 | return $value; | |||||||||
| 53 | } | ||||||||||
| 54 | |||||||||||
| 55 | 3 | __PACKAGE__->meta->make_immutable; | |||||||||
| 56 | |||||||||||
| 57 | 3 | 1; | |||||||||
| 58 | |||||||||||
| 59 | __END__ | ||||||||||
| 60 | |||||||||||
| 61 | =pod | ||||||||||
| 62 | |||||||||||
| 63 | =encoding UTF-8 | ||||||||||
| 64 | |||||||||||
| 65 | =head1 NAME | ||||||||||
| 66 | |||||||||||
| 67 | HTML::FormFu::Deflator::FormatNumber - Format a number for a locale | ||||||||||
| 68 | |||||||||||
| 69 | =head1 VERSION | ||||||||||
| 70 | |||||||||||
| 71 | version 2.06 | ||||||||||
| 72 | |||||||||||
| 73 | =head1 SYNOPSIS | ||||||||||
| 74 | |||||||||||
| 75 | locale: de_DE | ||||||||||
| 76 | elements: | ||||||||||
| 77 | - type: Text | ||||||||||
| 78 | name: number | ||||||||||
| 79 | deflators: | ||||||||||
| 80 | - type: FormatNumber | ||||||||||
| 81 | precision: 4 | ||||||||||
| 82 | |||||||||||
| 83 | - type: Text | ||||||||||
| 84 | name: price | ||||||||||
| 85 | deflators: | ||||||||||
| 86 | - type: FormatNumber | ||||||||||
| 87 | precision: 2 | ||||||||||
| 88 | trailing_zeroes: 1 | ||||||||||
| 89 | |||||||||||
| 90 | # "123456.22" will be rendered to "123.456,22" (german locale) | ||||||||||
| 91 | |||||||||||
| 92 | =head2 locale | ||||||||||
| 93 | |||||||||||
| 94 | If no locale is found, the server's locale will be used. | ||||||||||
| 95 | |||||||||||
| 96 | This method is a special 'inherited accessor', which means it can be set on | ||||||||||
| 97 | the form, a enclosing block element, the field, or this filter. | ||||||||||
| 98 | When the value is read, if no value is defined it automatically traverses | ||||||||||
| 99 | the element's hierarchy of parents, through any block elements and up to the | ||||||||||
| 100 | form, searching for a defined value. | ||||||||||
| 101 | |||||||||||
| 102 | =head1 AUTHOR | ||||||||||
| 103 | |||||||||||
| 104 | Moritz Onken, C<onken at houseofdesign.de> | ||||||||||
| 105 | |||||||||||
| 106 | =head1 SEE ALSO | ||||||||||
| 107 | |||||||||||
| 108 | Is a sub-class of, and inherits methods from L<HTML::FormFu::Deflator> | ||||||||||
| 109 | |||||||||||
| 110 | L<HTML::FormFu::Filter::FormatNumber> | ||||||||||
| 111 | |||||||||||
| 112 | L<HTML::FormFu> | ||||||||||
| 113 | |||||||||||
| 114 | =head1 COPYRIGHT & LICENSE | ||||||||||
| 115 | |||||||||||
| 116 | Copyright 2008 Moritz Onken, all rights reserved. | ||||||||||
| 117 | |||||||||||
| 118 | This program is free software; you can redistribute it and/or modify it | ||||||||||
| 119 | under the same terms as Perl itself. | ||||||||||
| 120 | |||||||||||
| 121 | =head1 AUTHOR | ||||||||||
| 122 | |||||||||||
| 123 | Carl Franks <cpan@fireartist.com> | ||||||||||
| 124 | |||||||||||
| 125 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 126 | |||||||||||
| 127 | This software is copyright (c) 2018 by Carl Franks. | ||||||||||
| 128 | |||||||||||
| 129 | This is free software; you can redistribute it and/or modify it under | ||||||||||
| 130 | the same terms as the Perl 5 programming language system itself. | ||||||||||
| 131 | |||||||||||
| 132 | =cut | ||||||||||