blib/lib/Templ/Tag.pm

filter: Statementstmt 86.7% 72 / 83 Branchbran 62.5% 15 / 24 Conditioncond 33.3% 4 / 12 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 86.6% 13 / 15 Podpod 0.0% 0 / 9 totaltotal 69.8% 104 / 149 CC 17 SCAR 21.0
CC
17
Cov
76%
add
19.8
params
3.3
process
2.0
CRAP 8.2
« blib/lib/Templ/Parser.pm ↑ Summary blib/lib/Templ/Spec.pm »
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  
9 # Keyed by start char, value of end char
10 1 my %char_pairs = (
11 '(' => ')',
12 '{' => '}',
13 '[' => ']',
14 '<' => '>',
15 );
16 1 my %invalid_chars = map { $_ => 1 } ( '+', '-', '=', '/' );
17  
18 sub add {
19 8 ▶ my $class = shift;
Subroutine add: called CC=11 SCAR=29.8
Pod undocumented
20 8 ▶ 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 8 8
Condition33%2/6
defined $class or ref $class or not $class =~ /^(\w+\:\:)*\w+$/
l!l&&r!l&&!r
800
defined $class or ref $class
l!l&&r!l&&!r
800
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
21 0 croak "Can only be called as ".__PACKAGE__."->new";
22 }
23 8 ▶ if ( $class eq 'Templ::Tag' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class eq "Templ::Tag") 0 8 8
24 0 croak "Can't instantiate a Templ::Tag object, please use a subclass";
25 }
26 8 my $char = shift;
27 8 ▶ if ( not defined $char ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $char) 0 8 8
28 0 croak "No character specification";
29 }
30 8 ▶ unless (length($char) == 1) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $char == 1) 0 8 8
31 0 croak "Tag object character specification can only be 1 character long";
32 }
33 8 ▶ if ($char !~ m/[[:print:]]/ || $char =~ m/[[:alnum:]]/i || $char !~ m/[[:ascii:]]/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $char =~ /[[:print:]]/ or $char =~ /[[:alnum:]]/i or not $char =~ /[[:ascii:]]/) 0 8 8
Condition33%2/6
not $char =~ /[[:print:]]/ or $char =~ /[[:alnum:]]/i or not $char =~ /[[:ascii:]]/
l!l&&r!l&&!r
008
not $char =~ /[[:print:]]/ or $char =~ /[[:alnum:]]/i
l!l&&r!l&&!r
008
MC/DC0%0/3
not $char =~ /[[:print:]]/ or $char =~ /[[:alnum:]]/i or not $char =~ /[[:ascii:]]/
not $char =~ /[[:print:]]/ $char =~ /[[:alnum:]]/i not $char =~ /[[:ascii:]]/
Truth table25%1/4
not(A) or B or not(C)
A= not $char =~ /[[:print:]]/B= $char =~ /[[:alnum:]]/iC= not $char =~ /[[:ascii:]]/
ABCresult
1XX1
01X1
0011
0000
34 0 croak "Tag object specification must be a printable non-alphanumeric ASCII character";
35 }
36 8 ▶ if ($invalid_chars{$char}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($invalid_chars{$char}) 0 8 8
37 0 croak "Tag object character specification cannot be $char";
38 }
39 8 my $self = bless { 'char' => $char, 'params' => [ @_ ] }, $class;
40 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
41 1 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
42 8 push @{ caller().'::TEMPL_TAGS' }, $self;
43 8 return $self;
44 }
45  
46 1 *new = *add;
47  
48 sub char {
49 8 ▶ my $self = shift;
Subroutine char: called CC=1 SCAR=0.0
Pod undocumented
50 8 return $self->{'char'};
51 }
52  
53 sub end_char {
54 8 ▶ my $self = shift;
Subroutine end_char: called CC=2 SCAR=6.9
Pod undocumented
55 8 ▶ if (exists $char_pairs{$self->{'char'}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $char_pairs{$self->{"char"}}) 1 7 8
56 1 return $char_pairs{$self->{'char'}}
57 }
58 7 return $self->{'char'};
59 }
60  
61 sub params {
62 6 ▶ my $self = shift;
Subroutine params: called CC=3 SCAR=12.0
Pod undocumented
63 6 ▶ if (not defined $self->{'params'}) { $self->{'params'} = [] }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"params"}) 0 6 6
64 6 ▶ return wantarray ? @{$self->{'params'}} : $self->{'params'};
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 6 0 6
65 }
66  
67 # Regex for matching the beginning of the tag, with optional
68 # whitespace removal greediness
69 #
70 # <?+ Don't trim preceding whitespace
71 # <?- Trim all preceding whitespace
72 # <?= Keep indentation
73 # <? Default, trim tabs and space only if there's a preceding newline
74 sub pre_rx {
75 28 ▶ my $self = shift;
Subroutine pre_rx: called CC=2 SCAR=6.9
Pod undocumented
76 28 ▶ if ( not defined $self->{'_pre_rx'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"_pre_rx"}) 8 20 28
77 8 my $char = qr/\Q${\$self->char}\E/;
78 8 $self->{'_pre_rx'} = qr/
79 (?: < $char \+ |
80 \s* < $char \- |
81 [ \t]* < $char \= |
82 (?:(?:(?<=\r\n)|(?<=\n))[\t ]*)? < $char
83 ) \s+
84 /x;
85 }
86 28 return $self->{'_pre_rx'};
87 }
88  
89 # Regex for matching the end of the tag, with optional
90 # whitespace removal greediness
91 #
92 # +?> Don't trim any trailing whitespace
93 # -?> Trim all trailing whitespace
94 # ?> Default, trim trailing whitespace including a newline, only if
95 # there's a newline
96 sub post_rx {
97 28 ▶ my $self = shift;
Subroutine post_rx: called CC=2 SCAR=6.9
Pod undocumented
98 28 ▶ if ( not defined $self->{'_post_rx'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"_post_rx"}) 8 20 28
99 8 my $char = qr/\Q${\$self->end_char}\E/;
100 8 $self->{'_post_rx'} = qr/
101 \s+
102 (?:
103 \+ $char > |
104 \- $char > \s* |
105 $char > (?:[\t ]*\r?\n)?
106 )
107 /x;
108 }
109 28 return $self->{'_post_rx'};
110 }
111  
112 sub perl {
113 0 ▶ die "Cannot call ->perl() method of Templ::Tag (must be subclassed)";
Subroutine perl: not called CC=1 SCAR=6.9
Pod undocumented
114 }
115  
116 # For the given template input string, change any appearance of the tag
117 # into the appropriate substitution
118 sub process {
119 28 ▶ my $self = shift;
Subroutine process: called CC=2 SCAR=7.0
Pod undocumented
120 28 my $perl = shift; # Perl version of the template
121 28 my $parser = shift;
122  
123 28 my $pre_rx = $self->pre_rx;
124 28 my $post_rx = $self->post_rx;
125
126 28 my $append = $parser->append;
127  
128 28 $perl =~ s{ ($pre_rx) (.*?) $post_rx }
129 {
130 23 my $pre = $1;
131 23 my $content = $2;
132 23 $content =~ s|\\\\|\\|gs;
133 23 $content =~ s|\\'|'|gs;
134 23 my $indent = '';
135 23 ▶ if ($pre =~ m/^([ \t]*).*\=/) { $indent = $1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($pre =~ /^([ \t]*).*\=/) 0 23 23
136 23 "';\n".$self->perl($content,$indent,$append)."\n$append'"
137 }egsx;
138  
139 28 return $perl;
140 }
141  
142 sub dump {
143 0 ▶ my $self = shift;
Subroutine dump: not called CC=1 SCAR=6.9
Pod undocumented
144 0 return Data::Dumper->Dump( [$self], ['tag'] );
145 }
146  
147 1 1;
« blib/lib/Templ/Parser.pm ↑ Summary blib/lib/Templ/Spec.pm »