line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Template::Trivial; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
20716
|
use 5.00503; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
45
|
|
4
|
1
|
|
|
1
|
|
5
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
35
|
|
5
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
7
|
|
|
1
|
|
|
|
|
44
|
|
6
|
|
|
|
|
|
|
|
7
|
1
|
|
|
1
|
|
6
|
use vars qw($VERSION); |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
82
|
|
8
|
|
|
|
|
|
|
$VERSION = '0.08'; |
9
|
|
|
|
|
|
|
|
10
|
1
|
|
|
1
|
|
5
|
use vars qw($STRICT); ## template stricture |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
34
|
|
11
|
1
|
|
|
1
|
|
4
|
use vars qw($TMPL_DIR); ## template directory |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
33
|
|
12
|
1
|
|
|
1
|
|
4
|
use vars qw(%VARIABLES); ## template variables (e.g., FOO, BAR) |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
27
|
|
13
|
1
|
|
|
1
|
|
4
|
use vars qw(%TEMPLATE_NAME); ## template aliases (e.g., foo, bar) |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
51
|
|
14
|
1
|
|
|
1
|
|
4
|
use vars qw(%TEMPLATE_CACHE); ## template contents |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
1499
|
|
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
my $VAR_RE = qr(^[A-Z][A-Z0-9_-]*?$)o; |
17
|
|
|
|
|
|
|
my $TMPL_RE = qr(^[a-z][a-z0-9_-]*?$)o; |
18
|
|
|
|
|
|
|
my $TVAR_RE = qr!\{([A-Z][A-Z0-9_-]*?)\}!o; |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
sub new { |
21
|
6
|
|
|
6
|
1
|
21
|
my $class = shift; |
22
|
6
|
|
33
|
|
|
24
|
my $proto = ref($class) || $class; |
23
|
6
|
|
|
|
|
9
|
my $self = { }; |
24
|
6
|
|
|
|
|
14
|
my %parms = @_; |
25
|
|
|
|
|
|
|
|
26
|
6
|
|
|
|
|
11
|
bless $self, $proto; |
27
|
|
|
|
|
|
|
|
28
|
6
|
100
|
|
|
|
15
|
$STRICT = ( defined $parms{strict} |
29
|
|
|
|
|
|
|
? $parms{strict} |
30
|
|
|
|
|
|
|
: 1 ); |
31
|
6
|
100
|
|
|
|
13
|
$TMPL_DIR = ( defined $parms{templates} |
32
|
|
|
|
|
|
|
? $parms{templates} |
33
|
|
|
|
|
|
|
: '' ); |
34
|
6
|
|
|
|
|
10
|
%VARIABLES = (); |
35
|
6
|
|
|
|
|
9
|
%TEMPLATE_NAME = (); |
36
|
6
|
|
|
|
|
8
|
%TEMPLATE_CACHE = (); |
37
|
|
|
|
|
|
|
|
38
|
6
|
|
|
|
|
18
|
return $self; |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
sub strict { |
42
|
6
|
|
|
6
|
1
|
13
|
my $self = shift; |
43
|
6
|
100
|
|
|
|
27
|
$STRICT = ( @_ ? shift : $STRICT ); |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
sub templates { |
47
|
2
|
|
|
2
|
1
|
7
|
my $self = shift; |
48
|
|
|
|
|
|
|
|
49
|
2
|
50
|
|
|
|
6
|
if( @_ ) { ## new assignment |
50
|
0
|
|
|
|
|
0
|
$TMPL_DIR = shift; |
51
|
0
|
0
|
0
|
|
|
0
|
if( $TMPL_DIR && $TMPL_DIR !~ m!/$! ) { |
52
|
0
|
|
|
|
|
0
|
$TMPL_DIR .= '/'; |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
2
|
|
|
|
|
6
|
return $TMPL_DIR; |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
sub define { |
60
|
5
|
|
|
5
|
1
|
10
|
my $self = shift; |
61
|
5
|
|
|
|
|
12
|
my %defines = @_; |
62
|
|
|
|
|
|
|
|
63
|
5
|
|
|
|
|
12
|
for my $tmpl ( keys %defines ) { |
64
|
6
|
50
|
|
|
|
19
|
my $path = ( $defines{$tmpl} =~ m!^/!o |
65
|
|
|
|
|
|
|
? $defines{$tmpl} |
66
|
|
|
|
|
|
|
: $TMPL_DIR . $defines{$tmpl} ); |
67
|
|
|
|
|
|
|
|
68
|
6
|
100
|
|
|
|
13
|
if( $STRICT ) { |
69
|
5
|
50
|
|
|
|
30
|
warn "Illegal template alias name '$tmpl'\n" |
70
|
|
|
|
|
|
|
unless $tmpl =~ $TMPL_RE; |
71
|
|
|
|
|
|
|
|
72
|
5
|
100
|
66
|
|
|
65
|
unless( -f $path || -c _ ) { |
73
|
1
|
|
|
|
|
49
|
warn "File '$path' ($tmpl) is not a file\n"; |
74
|
1
|
|
|
|
|
6
|
next; |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
|
78
|
5
|
|
|
|
|
10
|
$TEMPLATE_NAME{$tmpl} = $path; |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
## redefining a template clears it from the cache |
81
|
5
|
100
|
|
|
|
22
|
delete $TEMPLATE_CACHE{$tmpl} |
82
|
|
|
|
|
|
|
if exists $TEMPLATE_CACHE{$tmpl}; |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
## FIXME: add an alias or two for define_from_string method |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
sub define_from_string { |
89
|
3
|
|
|
3
|
1
|
6
|
my $self = shift; |
90
|
3
|
|
|
|
|
9
|
my %defines = @_; |
91
|
|
|
|
|
|
|
|
92
|
3
|
|
|
|
|
6
|
for my $tmpl ( keys %defines ) { |
93
|
4
|
|
|
|
|
14
|
$TEMPLATE_CACHE{$tmpl} = $defines{$tmpl}; |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
sub assign { |
98
|
13
|
|
|
13
|
1
|
33
|
my $self = shift; |
99
|
13
|
|
|
|
|
29
|
my %assign = @_; |
100
|
|
|
|
|
|
|
|
101
|
13
|
|
|
|
|
26
|
for my $var ( keys %assign ) { |
102
|
14
|
|
|
|
|
32
|
my $append = $var =~ s/^\.//o; |
103
|
|
|
|
|
|
|
|
104
|
14
|
50
|
|
|
|
30
|
if( $STRICT ) { |
105
|
14
|
50
|
|
|
|
78
|
warn "Illegal variable name '$var'\n" |
106
|
|
|
|
|
|
|
unless $var =~ $VAR_RE; |
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
|
109
|
14
|
100
|
|
|
|
25
|
if( $append ) { |
110
|
5
|
|
|
|
|
27
|
$VARIABLES{$var} .= $assign{".$var"}; |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
else { |
113
|
9
|
|
|
|
|
39
|
$VARIABLES{$var} = $assign{$var}; |
114
|
|
|
|
|
|
|
} |
115
|
|
|
|
|
|
|
} |
116
|
|
|
|
|
|
|
} |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
sub assign_from_file { |
119
|
2
|
|
|
2
|
1
|
188
|
my $self = shift; |
120
|
2
|
|
|
|
|
7
|
my %assign = @_; |
121
|
|
|
|
|
|
|
|
122
|
2
|
|
|
|
|
5
|
for my $var ( keys %assign ) { |
123
|
2
|
|
|
|
|
4
|
my $append = $var =~ s/^\.//o; |
124
|
|
|
|
|
|
|
|
125
|
2
|
100
|
|
|
|
8
|
my $path = ( $assign{$var} =~ m!^/! |
126
|
|
|
|
|
|
|
? $assign{$var} |
127
|
|
|
|
|
|
|
: $TMPL_DIR . $assign{$var} ); |
128
|
|
|
|
|
|
|
|
129
|
2
|
50
|
|
|
|
6
|
if( $STRICT ) { |
130
|
2
|
50
|
|
|
|
13
|
warn "Illegal variable name '$var'\n" |
131
|
|
|
|
|
|
|
unless $var =~ $VAR_RE; |
132
|
|
|
|
|
|
|
|
133
|
2
|
50
|
66
|
|
|
27
|
unless( -f $path || -c _ ) { |
134
|
0
|
|
|
|
|
0
|
warn "File '$path' is not a file\n"; |
135
|
0
|
|
|
|
|
0
|
next; |
136
|
|
|
|
|
|
|
} |
137
|
|
|
|
|
|
|
} |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
open FILE, "$path" |
140
|
2
|
50
|
|
|
|
51
|
or do { |
141
|
0
|
|
|
|
|
0
|
warn "Could not open '$path': $!\n"; |
142
|
0
|
|
|
|
|
0
|
next; |
143
|
|
|
|
|
|
|
}; |
144
|
2
|
|
|
|
|
8
|
local($/) = undef; |
145
|
2
|
50
|
|
|
|
4
|
if( $append ) { |
146
|
0
|
|
|
|
|
0
|
$VARIABLES{$var} .= ; |
147
|
|
|
|
|
|
|
} |
148
|
|
|
|
|
|
|
else { |
149
|
2
|
|
|
|
|
39
|
$VARIABLES{$var} = ; |
150
|
|
|
|
|
|
|
} |
151
|
2
|
|
|
|
|
41
|
close FILE; |
152
|
|
|
|
|
|
|
} |
153
|
|
|
|
|
|
|
} |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
sub append { |
156
|
1
|
|
|
1
|
0
|
2
|
my $self = shift; |
157
|
1
|
|
|
|
|
4
|
my %assign = @_; |
158
|
|
|
|
|
|
|
|
159
|
1
|
|
|
|
|
3
|
for my $var ( keys %assign ) { |
160
|
1
|
50
|
|
|
|
3
|
if( $STRICT ) { |
161
|
1
|
50
|
|
|
|
7
|
warn "Illegal key name '$var'\n" |
162
|
|
|
|
|
|
|
unless $var =~ $VAR_RE; |
163
|
|
|
|
|
|
|
} |
164
|
|
|
|
|
|
|
|
165
|
1
|
|
|
|
|
5
|
$VARIABLES{$var} .= $assign{$var}; |
166
|
|
|
|
|
|
|
} |
167
|
|
|
|
|
|
|
} |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
## FIXME: append_from_file? |
170
|
|
|
|
|
|
|
## FIXME: any defaults for to_string('MAIN'), etc.? How about |
171
|
|
|
|
|
|
|
## FIXME: parse('MAIN') implies parse('MAIN => 'main')? |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
sub parse { |
174
|
10
|
|
|
10
|
1
|
19
|
my $self = shift; |
175
|
10
|
|
|
|
|
21
|
my %parse = @_; |
176
|
|
|
|
|
|
|
|
177
|
10
|
|
|
|
|
30
|
while( my($var, $tmpl) = each %parse ) { |
178
|
10
|
|
|
|
|
12
|
my $append = $var =~ s/^\.//o; |
179
|
10
|
|
|
|
|
13
|
my $file; |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
## find the template in our cache |
182
|
10
|
100
|
|
|
|
21
|
unless( defined $TEMPLATE_CACHE{$tmpl} ) { |
183
|
|
|
|
|
|
|
open TMPL, "$TEMPLATE_NAME{$tmpl}" |
184
|
4
|
50
|
|
|
|
114
|
or do { |
185
|
0
|
|
|
|
|
0
|
warn "Could not open template ($tmpl) '$TEMPLATE_NAME{$tmpl}': $!\n"; |
186
|
0
|
|
|
|
|
0
|
next; |
187
|
|
|
|
|
|
|
}; |
188
|
4
|
|
|
|
|
15
|
local($/) = undef; |
189
|
4
|
|
|
|
|
84
|
$TEMPLATE_CACHE{$tmpl} = ; |
190
|
4
|
|
|
|
|
43
|
close TMPL; |
191
|
|
|
|
|
|
|
} |
192
|
|
|
|
|
|
|
|
193
|
10
|
|
|
|
|
15
|
$file = $TEMPLATE_CACHE{$tmpl}; |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
## parse the template |
196
|
10
|
|
|
|
|
58
|
$file =~ s{$TVAR_RE}{ |
197
|
10
|
100
|
|
|
|
27
|
if( exists $VARIABLES{$+} ) { |
198
|
9
|
|
|
|
|
33
|
$VARIABLES{$+}; |
199
|
|
|
|
|
|
|
} |
200
|
|
|
|
|
|
|
else { |
201
|
1
|
50
|
|
|
|
4
|
if( $STRICT ) { |
202
|
1
|
|
|
|
|
67
|
warn "Unknown variable '$+' found.\n"; |
203
|
|
|
|
|
|
|
} |
204
|
1
|
|
|
|
|
5
|
"{$+}"; ## put it back how it was |
205
|
|
|
|
|
|
|
} |
206
|
|
|
|
|
|
|
}xge; |
207
|
|
|
|
|
|
|
|
208
|
10
|
100
|
|
|
|
18
|
if( $append ) { |
209
|
1
|
|
|
|
|
9
|
$VARIABLES{$var} .= $file; |
210
|
|
|
|
|
|
|
} |
211
|
|
|
|
|
|
|
else { |
212
|
9
|
|
|
|
|
52
|
$VARIABLES{$var} = $file; |
213
|
|
|
|
|
|
|
} |
214
|
|
|
|
|
|
|
} |
215
|
|
|
|
|
|
|
} |
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
sub to_string { |
218
|
17
|
|
|
17
|
1
|
32
|
my $self = shift; |
219
|
|
|
|
|
|
|
|
220
|
17
|
100
|
|
|
|
77
|
if( $STRICT ) { |
221
|
16
|
100
|
66
|
|
|
171
|
unless( $_[0] && exists $VARIABLES{$_[0]} && $_[0] =~ $VAR_RE ) { |
|
|
|
66
|
|
|
|
|
222
|
1
|
|
|
|
|
36
|
warn "Undefined or unknown variable '$_[0]' found.\n"; |
223
|
1
|
|
|
|
|
4
|
return undef; |
224
|
|
|
|
|
|
|
} |
225
|
|
|
|
|
|
|
} |
226
|
|
|
|
|
|
|
|
227
|
16
|
100
|
|
|
|
630
|
return ( defined $VARIABLES{$_[0]} ? $VARIABLES{$_[0]}: '' ); |
228
|
|
|
|
|
|
|
} |
229
|
|
|
|
|
|
|
|
230
|
|
|
|
|
|
|
## FIXME: clear methods? |
231
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
1; |
233
|
|
|
|
|
|
|
__END__ |