blib/lib/Template/Plugin/Filter.pm untested

filter: Statementstmt 0.0% 0 / 56 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 9 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 100 CC 8 SCAR 21.1
CC
8
Cov
0%
factory
20.0
new
12.0
merge_config
6.0
CRAP 8.2
1 #============================================================= -*-Perl-*-
2 #
3 # Template::Plugin::Filter
4 #
5 # DESCRIPTION
6 # Template Toolkit module implementing a base class plugin
7 # object which acts like a filter and can be used with the
8 # FILTER directive.
9 #
10 # AUTHOR
11 # Andy Wardley <abw@wardley.org>
12 #
13 # COPYRIGHT
14 # Copyright (C) 2001-2022 Andy Wardley. All Rights Reserved.
15 #
16 # This module is free software; you can redistribute it and/or
17 # modify it under the same terms as Perl itself.
18 #
19 #============================================================================
20  
21 package Template::Plugin::Filter;
22  
23 use strict;
24 use warnings;
25 use base 'Template::Plugin';
26 use Scalar::Util 'weaken', 'isweak';
27  
28  
29 our $VERSION = '3.100';
30 our $DYNAMIC = 0 unless defined $DYNAMIC;
31  
32  
33 sub new {
34 my ($class, $context, @args) = @_;
35 my $config = @args && ref $args[-1] eq 'HASH' ? pop(@args) : { };
36  
37 # look for $DYNAMIC
38 my $dynamic;
39 {
40 no strict 'refs';
41 $dynamic = ${"$class\::DYNAMIC"};
42 }
43 $dynamic = $DYNAMIC unless defined $dynamic;
44  
45 my $self = bless {
46 _CONTEXT => $context,
47 _DYNAMIC => $dynamic,
48 _ARGS => \@args,
49 _CONFIG => $config,
50 }, $class;
51  
52 return $self->init($config)
53 || $class->error($self->error());
54 }
55  
56  
57 sub init {
58 my ($self, $config) = @_;
59 return $self;
60 }
61  
62  
63 sub factory {
64 my $self = shift;
65 my $this = $self;
66  
67 # avoid a memory leak
68 weaken( $this->{_CONTEXT} ) if ref $this->{_CONTEXT}
69 && !isweak $this->{_CONTEXT};
70  
71 if ($self->{ _DYNAMIC }) {
72 return [ sub {
73 my ($context, @args) = @_;
74 my $config = ref $args[-1] eq 'HASH' ? pop(@args) : { };
75  
76 return sub {
77 $this->filter(shift, \@args, $config);
78 };
79 }, 1 ];
80 }
81 else {
82 return sub {
83 $this->filter(shift);
84 };
85 }
86 }
87  
88 sub filter {
89 my ($self, $text, $args, $config) = @_;
90 return $text;
91 }
92  
93  
94 sub merge_config {
95 my ($self, $newcfg) = @_;
96 my $owncfg = $self->{ _CONFIG };
97 return $owncfg unless $newcfg;
98 return { %$owncfg, %$newcfg };
99 }
100  
101  
102 sub merge_args {
103 my ($self, $newargs) = @_;
104 my $ownargs = $self->{ _ARGS };
105 return $ownargs unless $newargs;
106 return [ @$ownargs, @$newargs ];
107 }
108  
109  
110 sub install_filter {
111 my ($self, $name) = @_;
112 $self->{ _CONTEXT }->define_filter( $name => $self->factory );
113 return $self;
114 }
115  
116  
117  
118 1;
119  
120 __END__