lib/Template/Plugin/Filter.pm

filter: Statementstmt 73.5% 39 / 53 Branchbran 37.5% 6 / 16 Conditioncond 33.3% 3 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 66.6% 10 / 15 Podpod 14.2% 1 / 7 totaltotal 55.6% 59 / 106 CC 11 SCAR 12.5
CC
11
Cov
62%
merge_config
6.0
merge_args
6.0
new
5.5
CRAP 3.5
« lib/Template/VMethods.pm ↑ Summary lib/Template/Iterator.pm »
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 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 3 ▶use base 'Template::Plugin';
Subroutine BEGIN: called CC=1 SCAR=0.0
26 3 ▶use Scalar::Util 'weaken', 'isweak';
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28  
29 3 our $VERSION = '3.100';
30 3 ▶our $DYNAMIC = 0 unless defined $DYNAMIC;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $Template::Plugin::Filter::DYNAMIC 3 0 3
31  
32  
33 sub new {
34 7 ▶ my ($class, $context, @args) = @_;
Subroutine new: called CC=5 SCAR=17.1
Pod documented
35 7 ▶ my $config = @args && ref $args[-1] eq 'HASH' ? pop(@args) : { };
Branch: 1 branch
BranchTrueFalseTotal
@args && ref $args[-1] eq 'HASH' ? : 0 7 7
Condition33%1/3
@args and ref $args[-1] eq "HASH"
!ll&&!rl&&r
700
MC/DC0%0/2
@args and ref $args[-1] eq "HASH"
@args ref $args[-1] eq "HASH"
Truth table33%1/3
A and B
A= @argsB= ref $args[-1] eq "HASH"
ABresult
0X0
100
111
36  
37 # look for $DYNAMIC
38 7 my $dynamic;
39 {
40 3 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
41 7 $dynamic = ${"$class\::DYNAMIC"};
42 }
43 7 ▶ $dynamic = $DYNAMIC unless defined $dynamic;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dynamic 7 0 7
44  
45 7 my $self = bless {
46 _CONTEXT => $context,
47 _DYNAMIC => $dynamic,
48 _ARGS => \@args,
49 _CONFIG => $config,
50 }, $class;
51  
52 7 ▶ return $self->init($config)
Condition33%1/3
$self->init($config) || $class->error($self->error)
l!l&&r!l&&!r
700
MC/DC0%0/2
$self->init($config) || $class->error($self->error)
$self->init($config) $class->error($self->error)
Truth table33%1/3
A || B
A= $self->init($config)B= $class->error($self->error)
ABresult
1X1
011
000
53 || $class->error($self->error());
54 }
55  
56  
57 sub init {
58 0 ▶ my ($self, $config) = @_;
Subroutine init: not called CC=1 SCAR=6.9
Pod undocumented
59 0 return $self;
60 }
61  
62  
63 sub factory {
64 7 ▶ my $self = shift;
Subroutine factory: called CC=4 SCAR=15.4
Pod undocumented
65 7 my $this = $self;
66  
67 # avoid a memory leak
68 weaken( $this->{_CONTEXT} ) if ref $this->{_CONTEXT}
69 7 ▶ && !isweak $this->{_CONTEXT};
Branch: 1 branch
BranchTrueFalseTotal
if ref $this->{"_CONTEXT"} and not builtin::is_weak($this->{"_CONTEXT"}) 7 0 7
Condition33%1/3
ref $this->{"_CONTEXT"} and not builtin::is_weak($this->{"_CONTEXT"})
!ll&&!rl&&r
007
MC/DC0%0/2
ref $this->{"_CONTEXT"} and not builtin::is_weak($this->{"_CONTEXT"})
ref $this->{"_CONTEXT"} not builtin::is_weak($this->{"_CONTEXT"})
Truth table33%1/3
A and not(B)
A= ref $this->{"_CONTEXT"}B= not builtin::is_weak($this->{"_CONTEXT"})
ABresult
0X0
100
111
70  
71 7 ▶ if ($self->{ _DYNAMIC }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_DYNAMIC'}) { } 7 0 7
72 return [ sub {
73 6 ▶ my ($context, @args) = @_;
Subroutine __ANON__: called CC=2 SCAR=7.0
74 6 ▶ my $config = ref $args[-1] eq 'HASH' ? pop(@args) : { };
Branch: 1 branch
BranchTrueFalseTotal
ref $args[-1] eq 'HASH' ? : 0 6 6
75  
76 return sub {
77 6 ▶ $this->filter(shift, \@args, $config);
Subroutine __ANON__: called CC=1 SCAR=0.0
78 6 };
79 7 }, 1 ];
80 }
81 else {
82 return sub {
83 0 ▶ $this->filter(shift);
Subroutine __ANON__: not called CC=1 SCAR=6.9
84 0 };
85 }
86 }
87  
88 sub filter {
89 0 ▶ my ($self, $text, $args, $config) = @_;
Subroutine filter: not called CC=1 SCAR=6.9
Pod undocumented
90 0 return $text;
91 }
92  
93  
94 sub merge_config {
95 0 ▶ my ($self, $newcfg) = @_;
Subroutine merge_config: not called CC=2 SCAR=17.9
Pod undocumented
96 0 my $owncfg = $self->{ _CONFIG };
97 0 ▶ return $owncfg unless $newcfg;
Branch: 1 branch
BranchTrueFalseTotal
unless $newcfg 0 0 0
98 0 return { %$owncfg, %$newcfg };
99 }
100  
101  
102 sub merge_args {
103 0 ▶ my ($self, $newargs) = @_;
Subroutine merge_args: not called CC=2 SCAR=17.9
Pod undocumented
104 0 my $ownargs = $self->{ _ARGS };
105 0 ▶ return $ownargs unless $newargs;
Branch: 1 branch
BranchTrueFalseTotal
unless $newargs 0 0 0
106 0 return [ @$ownargs, @$newargs ];
107 }
108  
109  
110 sub install_filter {
111 7 ▶ my ($self, $name) = @_;
Subroutine install_filter: called CC=1 SCAR=0.0
Pod undocumented
112 7 $self->{ _CONTEXT }->define_filter( $name => $self->factory );
113 7 return $self;
114 }
115  
116  
117  
118 3 1;
119  
120 __END__
« lib/Template/VMethods.pm ↑ Summary lib/Template/Iterator.pm »