lib/Template/Constants.pm

filter: Statementstmt 100% 145 / 145 Branchbran 72.2% 13 / 18 Conditioncond 77.7% 7 / 9 MC/DCmcdc 60.0% 3 / 5 Subroutinesub 100% 38 / 38 Podpod 0.0% 0 / 1 totaltotal 95.3% 206 / 216 CC 15 SCAR 16.5
CC
15
Cov
96%
debug_flags
15.7
BEGIN
1.0
BEGIN
1.0
CRAP 5.2
« lib/Template/Directive.pm ↑ Summary lib/Template/Plugin/Directory.pm »
1 #============================================================= -*-Perl-*-
2 #
3 # Template::Constants.pm
4 #
5 # DESCRIPTION
6 # Definition of constants for the Template Toolkit.
7 #
8 # AUTHOR
9 # Andy Wardley <abw@wardley.org>
10 #
11 # COPYRIGHT
12 # Copyright (C) 1996-2022 Andy Wardley. All Rights Reserved.
13 #
14 # This module is free software; you can redistribute it and/or
15 # modify it under the same terms as Perl itself.
16 #
17 #============================================================================
18  
19 package Template::Constants;
20  
21 117 require Exporter;
22 117 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 117 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 117 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 117 ▶use base qw( Exporter );
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 117 our ( @EXPORT_OK, %EXPORT_TAGS );
29 117 our ( $DEBUG_OPTIONS, @STATUS, @ERROR, @CHOMP, @DEBUG, @ISA );
30  
31 117 our $VERSION = '3.106';
32  
33 #========================================================================
34 # ----- EXPORTER -----
35 #========================================================================
36  
37 # STATUS constants returned by directives
38 117 ▶use constant STATUS_OK => 0; # ok
Subroutine BEGIN: called CC=1 SCAR=0.0
39 117 ▶use constant STATUS_RETURN => 1; # ok, block ended by RETURN
Subroutine BEGIN: called CC=1 SCAR=0.0
40 117 ▶use constant STATUS_STOP => 2; # ok, stopped by STOP
Subroutine BEGIN: called CC=1 SCAR=0.0
41 117 ▶use constant STATUS_DONE => 3; # ok, iterator done
Subroutine BEGIN: called CC=1 SCAR=0.0
42 117 ▶use constant STATUS_DECLINED => 4; # ok, declined to service request
Subroutine BEGIN: called CC=1 SCAR=0.0
43 117 ▶use constant STATUS_ERROR => 255; # error condition
Subroutine BEGIN: called CC=1 SCAR=0.0
44  
45 # ERROR constants for indicating exception types
46 117 ▶use constant ERROR_RETURN => 'return'; # return a status code
Subroutine BEGIN: called CC=1 SCAR=0.0
47 117 ▶use constant ERROR_FILE => 'file'; # file error: I/O, parse, recursion
Subroutine BEGIN: called CC=1 SCAR=0.0
48 117 ▶use constant ERROR_VIEW => 'view'; # view error
Subroutine BEGIN: called CC=1 SCAR=0.0
49 117 ▶use constant ERROR_UNDEF => 'undef'; # undefined variable value used
Subroutine BEGIN: called CC=1 SCAR=0.0
50 117 ▶use constant ERROR_PERL => 'perl'; # error in [% PERL %] block
Subroutine BEGIN: called CC=1 SCAR=0.0
51 117 ▶use constant ERROR_FILTER => 'filter'; # filter error
Subroutine BEGIN: called CC=1 SCAR=0.0
52 117 ▶use constant ERROR_PLUGIN => 'plugin'; # plugin error
Subroutine BEGIN: called CC=1 SCAR=0.0
53  
54 # CHOMP constants for PRE_CHOMP and POST_CHOMP
55 117 ▶use constant CHOMP_NONE => 0; # do not remove whitespace
Subroutine BEGIN: called CC=1 SCAR=0.0
56 117 ▶use constant CHOMP_ALL => 1; # remove whitespace up to newline
Subroutine BEGIN: called CC=1 SCAR=0.0
57 117 ▶use constant CHOMP_ONE => 1; # new name for CHOMP_ALL
Subroutine BEGIN: called CC=1 SCAR=0.0
58 117 ▶use constant CHOMP_COLLAPSE => 2; # collapse whitespace to a single space
Subroutine BEGIN: called CC=1 SCAR=0.0
59 117 ▶use constant CHOMP_GREEDY => 3; # remove all whitespace including newlines
Subroutine BEGIN: called CC=1 SCAR=0.0
60  
61 # DEBUG constants to enable various debugging options
62 117 ▶use constant DEBUG_OFF => 0; # do nothing
Subroutine BEGIN: called CC=1 SCAR=0.0
63 117 ▶use constant DEBUG_ON => 1; # basic debugging flag
Subroutine BEGIN: called CC=1 SCAR=0.0
64 117 ▶use constant DEBUG_UNDEF => 2; # throw undef on undefined variables
Subroutine BEGIN: called CC=1 SCAR=0.0
65 117 ▶use constant DEBUG_VARS => 4; # general variable debugging
Subroutine BEGIN: called CC=1 SCAR=0.0
66 117 ▶use constant DEBUG_DIRS => 8; # directive debugging
Subroutine BEGIN: called CC=1 SCAR=0.0
67 117 ▶use constant DEBUG_STASH => 16; # general stash debugging
Subroutine BEGIN: called CC=1 SCAR=0.0
68 117 ▶use constant DEBUG_CONTEXT => 32; # context debugging
Subroutine BEGIN: called CC=1 SCAR=0.0
69 117 ▶use constant DEBUG_PARSER => 64; # parser debugging
Subroutine BEGIN: called CC=1 SCAR=0.0
70 117 ▶use constant DEBUG_PROVIDER => 128; # provider debugging
Subroutine BEGIN: called CC=1 SCAR=0.0
71 117 ▶use constant DEBUG_PLUGINS => 256; # plugins debugging
Subroutine BEGIN: called CC=1 SCAR=0.0
72 117 ▶use constant DEBUG_FILTERS => 512; # filters debugging
Subroutine BEGIN: called CC=1 SCAR=0.0
73 117 ▶use constant DEBUG_SERVICE => 1024; # context debugging
Subroutine BEGIN: called CC=1 SCAR=0.0
74 117 ▶use constant DEBUG_ALL => 2047; # everything
Subroutine BEGIN: called CC=1 SCAR=0.0
75  
76 # extra debugging flags
77 117 ▶use constant DEBUG_CALLER => 4096; # add caller file/line
Subroutine BEGIN: called CC=1 SCAR=0.0
78 117 ▶use constant DEBUG_FLAGS => 4096; # bitmask to extract flags
Subroutine BEGIN: called CC=1 SCAR=0.0
79  
80 117 $DEBUG_OPTIONS = {
81 &DEBUG_OFF => off => off => &DEBUG_OFF,
82 &DEBUG_ON => on => on => &DEBUG_ON,
83 &DEBUG_UNDEF => undef => undef => &DEBUG_UNDEF,
84 &DEBUG_VARS => vars => vars => &DEBUG_VARS,
85 &DEBUG_DIRS => dirs => dirs => &DEBUG_DIRS,
86 &DEBUG_STASH => stash => stash => &DEBUG_STASH,
87 &DEBUG_CONTEXT => context => context => &DEBUG_CONTEXT,
88 &DEBUG_PARSER => parser => parser => &DEBUG_PARSER,
89 &DEBUG_PROVIDER => provider => provider => &DEBUG_PROVIDER,
90 &DEBUG_PLUGINS => plugins => plugins => &DEBUG_PLUGINS,
91 &DEBUG_FILTERS => filters => filters => &DEBUG_FILTERS,
92 &DEBUG_SERVICE => service => service => &DEBUG_SERVICE,
93 &DEBUG_ALL => all => all => &DEBUG_ALL,
94 &DEBUG_CALLER => caller => caller => &DEBUG_CALLER,
95 };
96  
97 117 @STATUS = qw( STATUS_OK STATUS_RETURN STATUS_STOP STATUS_DONE
98 STATUS_DECLINED STATUS_ERROR );
99 117 @ERROR = qw( ERROR_FILE ERROR_VIEW ERROR_UNDEF ERROR_PERL
100 ERROR_RETURN ERROR_FILTER ERROR_PLUGIN );
101 117 @CHOMP = qw( CHOMP_NONE CHOMP_ALL CHOMP_ONE CHOMP_COLLAPSE CHOMP_GREEDY );
102 117 @DEBUG = qw( DEBUG_OFF DEBUG_ON DEBUG_UNDEF DEBUG_VARS
103 DEBUG_DIRS DEBUG_STASH DEBUG_CONTEXT DEBUG_PARSER
104 DEBUG_PROVIDER DEBUG_PLUGINS DEBUG_FILTERS DEBUG_SERVICE
105 DEBUG_ALL DEBUG_CALLER DEBUG_FLAGS );
106  
107 117 @EXPORT_OK = ( @STATUS, @ERROR, @CHOMP, @DEBUG );
108 117 %EXPORT_TAGS = (
109 'all' => [ @EXPORT_OK ],
110 'status' => [ @STATUS ],
111 'error' => [ @ERROR ],
112 'chomp' => [ @CHOMP ],
113 'debug' => [ @DEBUG ],
114 );
115  
116  
117 4 ▶sub debug_flags {
Subroutine debug_flags: called CC=15 SCAR=27.5
Pod undocumented
118 4 my ($self, $debug) = @_;
119 4 my (@flags, $flag, $value);
120 4 ▶ $debug = $self unless defined($debug) || ref($self);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $debug or ref $self 0 4 4
Condition33%1/3
defined $debug or ref $self
l!l&&r!l&&!r
400
MC/DC0%0/2
defined $debug or ref $self
defined $debug ref $self
Truth table33%1/3
A or B
A= defined $debugB= ref $self
ABresult
1X1
011
000
121  
122 4 ▶ if ( $debug !~ tr{0-9}{}c) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $debug =~ tr/0-9//c) { } 1 3 4
123 1 foreach $flag (@DEBUG) {
124 15 ▶ next if $flag eq 'DEBUG_OFF' || $flag eq 'DEBUG_ALL' || $flag eq 'DEBUG_FLAGS';
Branch: 1 branch
BranchTrueFalseTotal
if $flag eq "DEBUG_OFF" or $flag eq "DEBUG_ALL" or $flag eq "DEBUG_FLAGS" 3 12 15
Condition100%6/6
$flag eq "DEBUG_OFF" or $flag eq "DEBUG_ALL" or $flag eq "DEBUG_FLAGS"
l!l&&r!l&&!r
2112
$flag eq "DEBUG_OFF" or $flag eq "DEBUG_ALL"
l!l&&r!l&&!r
1113
MC/DC100%3/3
$flag eq "DEBUG_OFF" or $flag eq "DEBUG_ALL" or $flag eq "DEBUG_FLAGS"
$flag eq "DEBUG_OFF" $flag eq "DEBUG_ALL" $flag eq "DEBUG_FLAGS"
Truth table100%4/4
A or B or C
A= $flag eq "DEBUG_OFF"B= $flag eq "DEBUG_ALL"C= $flag eq "DEBUG_FLAGS"
ABCresult
1XX1
01X1
0011
0000
125  
126 # don't trash the original
127 12 ▶ substr($flag,0,6,'') if index($flag,'DEBUG_') == 0;
Branch: 1 branch
BranchTrueFalseTotal
if index($flag, "DEBUG_") == 0 12 0 12
128 12 $flag = lc $flag;
129 return $self->error("no value for flag: $flag")
130 12 ▶ unless defined($value = $DEBUG_OPTIONS->{ $flag });
Branch: 1 branch
BranchTrueFalseTotal
unless defined($value = $Template::Constants::DEBUG_OPTIONS->{$flag}) 0 12 12
131 12 $flag = $value;
132  
133 12 ▶ if ($debug & $flag) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug & $flag) 2 10 12
134 2 $value = $DEBUG_OPTIONS->{ $flag };
135 2 ▶ return $self->error("no value for flag: $flag") unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 2 2
136 2 push(@flags, $value);
137 }
138 }
139 1 ▶ return wantarray ? @flags : join(', ', @flags);
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 1 1
140 }
141 else {
142 3 @flags = split(/\W+/, $debug);
143 3 $debug = 0;
144 3 foreach $flag (@flags) {
145 5 $value = $DEBUG_OPTIONS->{ $flag };
146 5 ▶ return $self->error("unknown debug flag: $flag") unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 1 4 5
147 4 $debug |= $value;
148 }
149 2 return $debug;
150 }
151 }
152  
153  
154 117 1;
155  
156 __END__
« lib/Template/Directive.pm ↑ Summary lib/Template/Plugin/Directory.pm »