| 1 | package Template::Plugin::Number::Format; | ||||||||||
| 2 | |||||||||||
| 3 | # ---------------------------------------------------------------------- | ||||||||||
| 4 | # Template::Plugin::Number::Format - Plugin/filter interface to Number::Format | ||||||||||
| 5 | # Copyright (C) 2002-2015 Darren Chamberlain <darren@cpan.org> | ||||||||||
| 6 | # | ||||||||||
| 7 | # This program is free software: you can redistribute it and/or modify | ||||||||||
| 8 | # it under the terms of the GNU General Public License as published by | ||||||||||
| 9 | # the Free Software Foundation, either version 3 of the License, or | ||||||||||
| 10 | # (at your option) any later version. | ||||||||||
| 11 | # | ||||||||||
| 12 | # This program is distributed in the hope that it will be useful, | ||||||||||
| 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||||||||
| 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||||||||
| 15 | # GNU General Public License for more details. | ||||||||||
| 16 | # | ||||||||||
| 17 | # You should have received a copy of the GNU General Public License | ||||||||||
| 18 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | ||||||||||
| 19 | # ------------------------------------------------------------------- | ||||||||||
| 20 | |||||||||||
| 21 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 22 | 1 | ▶ | use vars qw($VERSION $DYNAMIC $AUTOLOAD); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 23 | |||||||||||
| 24 | 1 | $VERSION = '1.03'; | |||||||||
| 25 | 1 | $DYNAMIC = 1; | |||||||||
| 26 | |||||||||||
| 27 | 1 | ▶ | use Number::Format; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 28 | 1 | ▶ | use base qw(Template::Plugin::Filter); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 29 | |||||||||||
| 30 | # ---------------------------------------------------------------------- | ||||||||||
| 31 | # filter($text) | ||||||||||
| 32 | # | ||||||||||
| 33 | # The default filter is format_number, i.e., commify. | ||||||||||
| 34 | # ---------------------------------------------------------------------- | ||||||||||
| 35 | sub filter { | ||||||||||
| 36 | 1 | ▶ | my ($self, $text, $args) = @_; | ||||||||
|
Subroutine
filter: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 37 | 1 | $self->{ _NFO }->format_number($text, @$args); | |||||||||
| 38 | } | ||||||||||
| 39 | |||||||||||
| 40 | |||||||||||
| 41 | # ---------------------------------------------------------------------- | ||||||||||
| 42 | # init($config) | ||||||||||
| 43 | # | ||||||||||
| 44 | # Initialize the instance. Creates a Number::Format object, which is | ||||||||||
| 45 | # used to create closures that implement the filters. | ||||||||||
| 46 | # ---------------------------------------------------------------------- | ||||||||||
| 47 | sub init { | ||||||||||
| 48 | 14 | ▶ | my ($self, $config) = @_; | ||||||||
|
Subroutine
init: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||
| 49 | 14 | my ($sub, $filter, $nfo); | |||||||||
| 50 | 14 | $nfo = Number::Format->new(%$config); | |||||||||
| 51 | |||||||||||
| 52 | 14 | $self->{ _DYNAMIC } = 1; | |||||||||
| 53 | 14 | $self->{ _NFO } = $nfo; | |||||||||
| 54 | |||||||||||
| 55 | # ------------------------------------------------------------------ | ||||||||||
| 56 | # This makes is dependant upon Number::Format not changing the | ||||||||||
| 57 | # Exporter interface it advertises, which is unlikely. | ||||||||||
| 58 | # | ||||||||||
| 59 | # It is likely that each of these subroutines should accept all | ||||||||||
| 60 | # the configuration options of the constructor, and instantiate a | ||||||||||
| 61 | # new Number::Format instance. This is easier, for now. | ||||||||||
| 62 | # ------------------------------------------------------------------ | ||||||||||
| 63 | 14 | for my $sub (@{$Number::Format::EXPORT_TAGS{"subs"}}) { | |||||||||
| 64 | my $filter = sub { | ||||||||||
| 65 | 13 | ▶ | my ($context, @args) = @_; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 66 | return sub { | ||||||||||
| 67 | 13 | ▶ | my $text = shift; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 68 | 13 | return $nfo->$sub($text, @args); | |||||||||
| 69 | 13 | }; | |||||||||
| 70 | 98 | }; | |||||||||
| 71 | 98 | $self->{ _CONTEXT }->define_filter($sub, $filter, 1); | |||||||||
| 72 | } | ||||||||||
| 73 | |||||||||||
| 74 | 14 | return $self; | |||||||||
| 75 | } | ||||||||||
| 76 | |||||||||||
| 77 | # ---------------------------------------------------------------------- | ||||||||||
| 78 | # AUTOLOAD | ||||||||||
| 79 | # | ||||||||||
| 80 | # Catches method calls; so that the plugin can be used like you'd | ||||||||||
| 81 | # expect a plugin to work: | ||||||||||
| 82 | # | ||||||||||
| 83 | # [% USE nf = Number.Format; nf.format_number(num) %] | ||||||||||
| 84 | # ---------------------------------------------------------------------- | ||||||||||
| 85 | sub AUTOLOAD { | ||||||||||
| 86 | 1 | ▶ | my $self = shift; | ||||||||
|
Subroutine
AUTOLOAD: called CC=2 SCAR=7.0
| |||||||||||
| 87 | 1 | (my $autoload = $AUTOLOAD) =~ s/.*:://; | |||||||||
| 88 | |||||||||||
| 89 | 1 | ▶ | return if $autoload eq 'DESTROY'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 90 | |||||||||||
| 91 | 1 | $self->{ _NFO }->$autoload(@_); | |||||||||
| 92 | } | ||||||||||
| 93 | |||||||||||
| 94 | 1 | 1; | |||||||||
| 95 | |||||||||||
| 96 | __END__ | ||||||||||