blib/lib/Text/Template/Tiny.pm

filter: Statementstmt 82.2% 37 / 45 Branchbran 66.6% 8 / 12 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 83.3% 5 / 6 Podpod 0.0% 0 / 3 totaltotal 71.8% 51 / 71 CC 8 SCAR 13.8
CC
8
Cov
77%
expand
6.3
__ANON__
3.0
add
2.0
CRAP 4.0
↑ Summary
1 #! perl
2  
3 package Text::Template::Tiny;
4  
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 =head1 NAME
9  
10 Text::Template::Tiny - Variable substituting template processor
11  
12 =cut
13  
14 2 our $VERSION = '1.000.1';
15  
16 =head1 SYNOPSIS
17  
18 This is a very small and limited template processor. The only thing it
19 can do is substitute variables in a text.
20  
21 Often that is all you need :-).
22  
23 Example:
24  
25 use Text::Template::Tiny;
26  
27 # Create a template processor, with preset subtitutions.
28 my $xp = Text::Template::Tiny->new(
29 home => $ENV{HOME},
30 lib => {
31 dev => "/tmp/mylib",
32 std => "/etc/mylib",
33 },
34 version => 1.02,
35 );
36  
37 # Add some more substitutions.
38 $xp->add( app => "MyApp" );
39  
40 # Apply it.
41 print $xp->expand(<<EOD);
42 For [% app %] version [% version %], the home of all operations
43 will be [% home %], and the library is [% lib.std %].
44 EOD
45  
46 # Same, with additional substitutions for this call only.
47 print $xp->expand( <<EOD, { app => "ThisApp" } );
48 For [% app %] version [% version %], the home of all operations
49 will be [% home %], and the library is [% lib.std %].
50 EOD
51  
52 =cut
53  
54 1 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
55 1 my ($pkg, %ctrl) = @_;
56 1 bless { _ctrl => { %ctrl } }, $pkg;
57 }
58  
59 0 ▶sub add {
Subroutine add: not called CC=1 SCAR=6.9
Pod undocumented
60 0 my ($self, %ctrl) = @_;
61 0 @{$self->{_ctrl}}{keys %ctrl} = values %ctrl;
62 0 delete $self->{_pat};
63 0 delete $self->{_rep};
64 }
65  
66 1 ▶sub expand {
Subroutine expand: called CC=6 SCAR=18.4
Pod undocumented
67 1 my ($self, $text, %ctrl) = @_;
68  
69 1 my $save_ctrl;
70 1 ▶ if ( %ctrl ) {
Branch: 1 branch
BranchTrueFalseTotal
if (%ctrl) 0 1 1
71 0 $save_ctrl = { %{$self->{_ctrl} } };
72 0 $self->add(%ctrl);
73 }
74  
75 1 my $pat = $self->{_pat};
76 1 my $rep = $self->{_rep};
77 1 my $ctrl = $self->{_ctrl};
78  
79 1 ▶ unless ( $pat && $rep ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pat and $rep) 1 0 1
Condition33%1/3
$pat and $rep
!ll&&!rl&&r
100
MC/DC0%0/2
$pat and $rep
$pat $rep
Truth table33%1/3
A and B
A= $patB= $rep
ABresult
0X0
100
111
80 1 my $addpat;
81 3 ▶ $addpat = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
82 3 my ( $c, $pfx ) = @_;
83 3 ▶ while ( my ($k,$v) = each %$c ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$c) 6 3 9
84 6 ▶ if ( UNIVERSAL::isa( $v, 'HASH' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($v, 'HASH')) { } 2 4 6
85 2 $addpat->( $v, "$pfx$k." );
86 }
87 else {
88 4 $pat .= quotemeta($pfx.$k) . "|";
89 4 $rep->{$pfx.$k} = $v;
90 }
91 }
92 1 };
93 1 $pat = "(";
94 1 $addpat->( $self->{_ctrl}, "" );
95 1 chop($pat);
96 1 $pat .= ")";
97 1 $pat = qr/\[\%\s+$pat\s+\%\]/;
98 1 ▶ unless ( %ctrl ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (%ctrl) 1 0 1
99 1 $self->{_pat} = $pat;
100 1 $self->{_rep} = $rep;
101 }
102 }
103  
104 1 $text =~ s/$pat/$rep->{$1}/ge;
105  
106 1 ▶ $self->{_ctrl} = $save_ctrl if $save_ctrl;
Branch: 1 branch
BranchTrueFalseTotal
if $save_ctrl 0 1 1
107  
108 1 return $text;
109 }
110  
111 =head1 AUTHOR
112  
113 Johan Vromans, C<< <jv at cpan.org> >>
114  
115 =head1 SUPPORT AND DOCUMENTATION
116  
117 Development of this module takes place on GitHub:
118 https://github.com/sciurius/perl-Text-Template-Tiny.
119  
120 You can find documentation for this module with the perldoc command.
121  
122 perldoc Text::Template::Tiny
123  
124 Please report any bugs or feature requests using the issue tracker on
125 GitHub.
126  
127  
128 =head1 COPYRIGHT & LICENSE
129  
130 Copyright 2008,2015,2024 Johan Vromans, all rights reserved.
131  
132 This program is free software; you can redistribute it and/or modify it
133 under the same terms as Perl itself.
134  
135 =cut
136  
137 2 1; # End of Text::Template::Tiny
↑ Summary