lib/Template/Namespace/Constants.pm

filter: Statementstmt 93.1% 54 / 58 Branchbran 53.8% 14 / 26 Conditioncond 40.0% 2 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 100% 8 / 8 Podpod 100% 1 / 1 totaltotal 78.2% 79 / 101 CC 16 SCAR 25.9
CC
16
Cov
79%
ident
19.9
_init
2.0
BEGIN
1.0
CRAP 13.4
« lib/Template/Provider.pm ↑ Summary blib/lib/Template/Plugin/Image.pm »
1 #================================================================= -*-Perl-*-
2 #
3 # Template::Namespace::Constants
4 #
5 # DESCRIPTION
6 # Plugin compiler module for performing constant folding at compile time
7 # on variables in a particular namespace.
8 #
9 # AUTHOR
10 # Andy Wardley <abw@wardley.org>
11 #
12 # COPYRIGHT
13 # Copyright (C) 1996-2022 Andy Wardley. All Rights Reserved.
14 #
15 # This module is free software; you can redistribute it and/or
16 # modify it under the same terms as Perl itself.
17 #
18 #============================================================================
19  
20 package Template::Namespace::Constants;
21  
22 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 6 ▶use base 'Template::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
25 6 ▶use Template::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 6 ▶use Template::Directive;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 6 ▶use Template::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 6 our $VERSION = '3.106';
30 6 ▶our $DEBUG = 0 unless defined $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $Template::Namespace::Constants::DEBUG 6 0 6
31  
32  
33 11 ▶sub _init {
Subroutine _init: called CC=2 SCAR=7.1
34 11 my ($self, $config) = @_;
35 11 ▶ $self->{ STASH } = Template::Config->stash($config)
Condition50%1/2
'Template::Config'->stash($config) || (return $self->error('Template::Config'->error))
l!l
110
MC/DC0%0/1
'Template::Config'->stash($config) || (return $self->error('Template::Config'->error))
'Template::Config'->stash($config)
Truth table50%1/2
A
A= 'Template::Config'->stash($config)
Aresult
11
00
36 || return $self->error(Template::Config->error());
37 11 return $self;
38 }
39  
40  
41  
42 #------------------------------------------------------------------------
43 # ident(\@ident) foo.bar(baz)
44 #------------------------------------------------------------------------
45  
46 44 ▶sub ident {
Subroutine ident: called CC=15 SCAR=29.9
Pod documented
47 44 my ($self, $ident) = @_;
48 44 my @save = @$ident;
49  
50 # discard first node indicating constants namespace
51 44 splice(@$ident, 0, 2);
52  
53 44 my $nelems = @$ident / 2;
54 44 my ($e, $result);
55 44 local $" = ', ';
56  
57 44 ▶ print STDERR "constant ident [ @$ident ] " if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Template::Namespace::Constants::DEBUG 0 44 44
58  
59 44 foreach $e (0..$nelems-1) {
60 # node name must be a constant
61 74 ▶ unless ($ident->[$e * 2] =~ s/^'(.+)'$/$1/s) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($$ident[$e * 2] =~ s/^'(.+)'$/$1/s) 0 74 74
62 0 ▶ $self->DEBUG(" * deferred (non-constant item: ", $ident->[$e * 2], ")\n")
Branch: 1 branch
BranchTrueFalseTotal
if $Template::Namespace::Constants::DEBUG 0 0 0
63 if $DEBUG;
64 0 return Template::Directive->ident(\@save);
65 }
66  
67 # if args is non-zero then it must be eval'ed
68 74 ▶ if ($ident->[$e * 2 + 1]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$ident[$e * 2 + 1]) 4 70 74
69 4 my $args = $ident->[$e * 2 + 1];
70 4 my $comp = eval "$args";
71 4 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 4 4
72 0 ▶ $self->DEBUG(" * deferred (non-constant args: $args)\n") if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Template::Namespace::Constants::DEBUG 0 0 0
73 0 return Template::Directive->ident(\@save);
74 }
75 4 ▶ $self->DEBUG("($args) ") if $comp && $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $comp and $Template::Namespace::Constants::DEBUG 0 4 4
Condition33%1/3
$comp and $Template::Namespace::Constants::DEBUG
!ll&&!rl&&r
040
MC/DC0%0/2
$comp and $Template::Namespace::Constants::DEBUG
$comp $Template::Namespace::Constants::DEBUG
Truth table33%1/3
A and B
A= $compB= $Template::Namespace::Constants::DEBUG
ABresult
0X0
100
111
76 4 $ident->[$e * 2 + 1] = $comp;
77 }
78 }
79  
80  
81 44 $result = $self->{ STASH }->get($ident);
82  
83 44 ▶ if (! length $result) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $result) 1 43 44
84 1 ▶ $self->DEBUG(" * deferred (no result)\n") if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Template::Namespace::Constants::DEBUG 0 1 1
85 1 return Template::Directive->ident(\@save);
86 }
87  
88 43 ▶ if (ref $result) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $result) 4 39 43
89 4 require Data::Dumper;
90 4 local $Data::Dumper::Terse = 1;
91 4 local $Data::Dumper::Indent = 0;
92 4 local $Data::Dumper::Sortkeys = 1;
93 4 my $code = Data::Dumper::Dumper($result);
94  
95 4 ▶ $self->DEBUG(" * resolved ref => $code\n") if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Template::Namespace::Constants::DEBUG 0 4 4
96  
97 4 return $code;
98 }
99  
100 39 $result =~ s/'/\\'/g;
101  
102 39 ▶ $self->DEBUG(" * resolved => '$result'\n") if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Template::Namespace::Constants::DEBUG 0 39 39
103  
104 39 return "'$result'";
105 }
106  
107 6 1;
108  
109 __END__
« lib/Template/Provider.pm ↑ Summary blib/lib/Template/Plugin/Image.pm »