blib/lib/Templ/Tag.pm

filter: Statementstmt 86.4% 51 / 59 Branchbran 64.2% 9 / 14 Conditioncond 33.3% 3 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 84.6% 11 / 13 Podpod 0.0% 0 / 8 totaltotal 68.5% 74 / 108 SCAR 25.3
CC
11
Cov
77%
new
7.2
check
4.1
char
2.1
CRAP 12.5
« blib/lib/Templ/Template.pm ↑ Summary Build »
1 package Templ::Tag;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Carp qw(carp croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Templ::Util qw(unquote);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 my $valid_chars = '`~!&^*_|;.,#?@$';
11  
12 sub new {
13 6 ▶ my $class = shift;
Subroutine new: called CC=5 SCAR=19.7
Pod undocumented
14 6 ▶ if ( not defined $class || ref $class || $class !~ m/^(\w+\:\:)*\w+$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $class or ref $class or not $class =~ /^(\w+\:\:)*\w+$/) 0 6 6
Condition33%2/6
defined $class or ref $class or not $class =~ /^(\w+\:\:)*\w+$/
l!l&&r!l&&!r
600
defined $class or ref $class
l!l&&r!l&&!r
600
MC/DC0%0/3
defined $class or ref $class or not $class =~ /^(\w+\:\:)*\w+$/
defined $class ref $class not $class =~ /^(\w+\:\:)*\w+$/
Truth table25%1/4
A or B or not(C)
A= defined $classB= ref $classC= not $class =~ /^(\w+\:\:)*\w+$/
ABCresult
1XX1
01X1
0011
0000
15 0 croak "Can only be called as __PACKAGE__->new";
16 }
17 6 ▶ if ( $class eq 'Templ::Tag' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class eq "Templ::Tag") 0 6 6
18 0 croak "Can't instantiate a Templ::Tag object, please use a subclass";
19 }
20  
21 6 my $self = bless {@_}, $class;
22 6 $self->check;
23  
24 6 return $self;
25 }
26  
27 sub check {
28 6 ▶ my $self = shift;
Subroutine check: called CC=3 SCAR=14.2
Pod undocumented
29 6 ▶ unless ( index( $valid_chars, $self->char )
Branch: 1 branch
BranchTrueFalseTotal
unless (index $valid_chars, $self->char and length $self->char == 1) 0 6 6
Condition33%1/3
index $valid_chars, $self->char and length $self->char == 1
!ll&&!rl&&r
006
MC/DC0%0/2
index $valid_chars, $self->char and length $self->char == 1
index $valid_chars, $self->char length $self->char == 1
Truth table33%1/3
A and B
A= index $valid_chars, $self->charB= length $self->char == 1
ABresult
0X0
100
111
30 && length( $self->char ) == 1 )
31 {
32  
33 # The above $self->char statement will croak if the char didn't get
34 # set or was not defaulted. If we got into this block, then the char
35 # wasn't a valid settable character
36 0 croak "Invalid character in 'char' specification of tag object";
37 }
38 }
39  
40 sub char {
41 24 ▶ my $self = shift;
Subroutine char: called CC=2 SCAR=7.6
Pod undocumented
42 24 ▶ if ( not defined $self->{'char'} ) { croak "No 'char' specification"; }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"char"}) 0 24 24
43 24 return $self->{'char'};
44 }
45  
46 # Regex for matching the beginning of the tag, with optional
47 # whitespace removal greediness
48 #
49 # <?+ Don't trim preceding whitespace
50 # <?- Trim all preceding whitespace
51 # <?= Keep indentation
52 # <? Default, trim tabs and space only if there's a preceding newline
53 sub pre_rx {
54 21 ▶ my $self = shift;
Subroutine pre_rx: called CC=2 SCAR=6.9
Pod undocumented
55 21 ▶ if ( not defined $self->{'_pre_rx'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"_pre_rx"}) 6 15 21
56 6 my $char = $self->char;
57 6 $self->{'_pre_rx'}
58 = qr/(?:<\Q$char\E\+|\s*<\Q$char\E\-|[ \t]*<\Q$char\E\=|(?:(?:(?<=\r\n)|(?<=\n))[\t ]*)?<\Q$char\E)\s+/;
59 }
60 21 return $self->{'_pre_rx'};
61 }
62  
63 # Regex for matching the end of the tag, with optional
64 # whitespace removal greediness
65 #
66 # +?> Don't trim any trailing whitespace
67 # -?> Trim all trailing whitespace
68 # ?> Default, trim trailing whitespace including a newline, only if
69 # there's a newline
70 sub post_rx {
71 21 ▶ my $self = shift;
Subroutine post_rx: called CC=2 SCAR=6.9
Pod undocumented
72 21 ▶ if ( not defined $self->{'_post_rx'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"_post_rx"}) 6 15 21
73 6 my $char = $self->char;
74 6 $self->{'_post_rx'}
75 = qr/\s+(?:\+\Q$char\E>|\-\Q$char\E>\s*|\Q$char\E>(?:[\t ]*\r?\n)?)/;
76 }
77 21 return $self->{'_post_rx'};
78 }
79  
80 sub perl {
81 0 ▶ die "Cannot call ->perl() method of Templ::Tag (must be subclassed)";
Subroutine perl: not called CC=1 SCAR=6.9
Pod undocumented
82 }
83  
84 # For the given template input string, change any appearance of the tag
85 # into the appropriate substitution
86 sub process {
87 21 ▶ my $self = shift;
Subroutine process: called CC=2 SCAR=7.0
Pod undocumented
88 21 my $perl = shift; # Perl version of the template
89 21 my $parser = shift;
90  
91 21 my $pre_rx = $self->pre_rx;
92 21 my $post_rx = $self->post_rx;
93
94 21 my $append = $parser->append;
95  
96 21 $perl =~ s{ ($pre_rx) (.*?) ($post_rx) }
97 {
98 18 my $pre = $1;
99 18 my $content = unquote($2);
100 18 my $indent = '';
101 18 ▶ if ($pre =~ m/^([ \t]*).*\=/)
Branch: 1 branch
BranchTrueFalseTotal
if ($pre =~ /^([ \t]*).*\=/) 0 18 18
102 {
103 0 $indent = $1;
104 }
105 18 "';\n".$self->perl($content,$indent,$append)."\n$append'"
106 }egsx;
107  
108 21 return $perl;
109 }
110  
111 sub dump {
112 0 ▶ my $self = shift;
Subroutine dump: not called CC=1 SCAR=6.9
Pod undocumented
113 0 return Data::Dumper->Dump( [$self], ['tag'] );
114 }
115  
116 1 1;
« blib/lib/Templ/Template.pm ↑ Summary Build »