line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
|
2
|
|
|
|
|
|
|
package Data::Template; |
3
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
=encoding utf8 |
5
|
|
|
|
|
|
|
=cut |
6
|
|
|
|
|
|
|
|
7
|
3
|
|
|
3
|
|
62192
|
use strict; |
|
3
|
|
|
|
|
7
|
|
|
3
|
|
|
|
|
73
|
|
8
|
3
|
|
|
3
|
|
16
|
use warnings; |
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
121
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
our $VERSION = '0.02'; |
11
|
|
|
|
|
|
|
|
12
|
3
|
|
|
3
|
|
15
|
use base qw(Class::Accessor); |
|
3
|
|
|
|
|
9
|
|
|
3
|
|
|
|
|
2591
|
|
13
|
|
|
|
|
|
|
Data::Template->mk_accessors(qw(engine prefix)); |
14
|
|
|
|
|
|
|
|
15
|
3
|
|
|
3
|
|
8574
|
use Template; |
|
3
|
|
|
|
|
74841
|
|
|
3
|
|
|
|
|
1460
|
|
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
sub new { |
18
|
2
|
|
|
2
|
1
|
21
|
my $self = shift; |
19
|
2
|
|
|
|
|
34
|
my $TT = Template->new(INTERPOLATE => 1); |
20
|
|
|
|
|
|
|
# return $self->SUPER::new({engine => $TT, prefix => '=', @_}); |
21
|
2
|
|
|
|
|
50689
|
return $self->SUPER::new({engine => $TT, @_}); |
22
|
|
|
|
|
|
|
} |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
sub process { |
25
|
28
|
|
|
28
|
1
|
2128
|
my $self = shift; |
26
|
28
|
|
|
|
|
43
|
my $tt = shift; |
27
|
28
|
|
|
|
|
36
|
my $vars = shift; |
28
|
|
|
|
|
|
|
|
29
|
28
|
100
|
|
|
|
76
|
if (!ref $tt) { |
|
|
100
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
30
|
22
|
|
|
|
|
52
|
return $self->process_s($tt, $vars); |
31
|
|
|
|
|
|
|
} elsif (ref $tt eq 'ARRAY') { |
32
|
4
|
|
|
|
|
14
|
return $self->process_a($tt, $vars); |
33
|
|
|
|
|
|
|
} elsif (ref $tt eq 'HASH') { |
34
|
2
|
|
|
|
|
10
|
return $self->process_h($tt, $vars); |
35
|
|
|
|
|
|
|
} else { |
36
|
0
|
|
|
|
|
0
|
die 'burp' |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
sub process_h { |
41
|
2
|
|
|
2
|
1
|
3
|
my $self = shift; |
42
|
2
|
|
|
|
|
4
|
my $h = shift; |
43
|
2
|
|
|
|
|
4
|
my $vars = shift; |
44
|
|
|
|
|
|
|
|
45
|
2
|
|
|
|
|
8
|
my %ph = (); |
46
|
2
|
|
|
|
|
13
|
while (my ($k, $v) = each %$h) { |
47
|
8
|
|
|
|
|
26
|
$k = $self->process_s($k, $vars); |
48
|
8
|
|
|
|
|
26
|
$v = $self->process($v, $vars); |
49
|
8
|
|
|
|
|
40
|
$ph{$k} = $v; |
50
|
|
|
|
|
|
|
} |
51
|
2
|
|
|
|
|
8
|
return \%ph; |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
sub process_a { |
55
|
4
|
|
|
4
|
1
|
6
|
my $self = shift; |
56
|
4
|
|
|
|
|
7
|
my $a = shift; |
57
|
4
|
|
|
|
|
7
|
my $vars = shift; |
58
|
|
|
|
|
|
|
|
59
|
4
|
|
|
|
|
25
|
my @pa; |
60
|
4
|
|
|
|
|
10
|
foreach (@$a) { |
61
|
12
|
|
|
|
|
30
|
push @pa, $self->process($_, $vars); |
62
|
|
|
|
|
|
|
} |
63
|
4
|
|
|
|
|
42
|
return \@pa; |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
=begin private |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
($p, $t) = $self->_split_scalar($s) |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
Determines if C<$s> is a plain scalar or |
71
|
|
|
|
|
|
|
a text template. If it is a plain scalar, |
72
|
|
|
|
|
|
|
C<$p> gets its content and C<$t> gets C. |
73
|
|
|
|
|
|
|
Otherwise, C<$p> is C and C<$t> |
74
|
|
|
|
|
|
|
gets the contents of the template. |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
There is difference between C<$s> and |
77
|
|
|
|
|
|
|
the content of C<$s> only if there is a |
78
|
|
|
|
|
|
|
C. In this case, a plain scalar |
79
|
|
|
|
|
|
|
is (1) one which does not begin with the prefix |
80
|
|
|
|
|
|
|
(the content is C<$s> itself) or (2) |
81
|
|
|
|
|
|
|
one which begins with C<"\\"> followed |
82
|
|
|
|
|
|
|
by the prefix (the content is C<$s> without |
83
|
|
|
|
|
|
|
the leading escape). When there is |
84
|
|
|
|
|
|
|
a prefix, C<$s> is a template if it begins |
85
|
|
|
|
|
|
|
with the prefix and its content is C<$s> |
86
|
|
|
|
|
|
|
without the prefix. |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
# say prefix is '=' |
89
|
|
|
|
|
|
|
($p, $t) = $tt->_split_scalar('foo') |
90
|
|
|
|
|
|
|
# ($p, $t) = ('foo', undef) |
91
|
|
|
|
|
|
|
($p, $t) = $tt->_split_scalar('=foo') |
92
|
|
|
|
|
|
|
# ($p, $t) = (undef, 'foo') |
93
|
|
|
|
|
|
|
($p, $t) = $tt->_split_scalar('\\=foo') |
94
|
|
|
|
|
|
|
# ($p, $t) = ('=foo', undef) |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
=end private |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
=cut |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
sub _split_scalar { |
101
|
34
|
|
|
34
|
|
45
|
my $self = shift; |
102
|
34
|
|
|
|
|
40
|
my $s = shift; |
103
|
34
|
|
|
|
|
97
|
my $prefix = $self->prefix; |
104
|
34
|
100
|
|
|
|
363
|
if ($prefix) { |
105
|
17
|
100
|
|
|
|
76
|
if ($s =~ s/^\Q$prefix\E//) { # it is a template |
106
|
4
|
|
|
|
|
12
|
return (undef, $s); |
107
|
|
|
|
|
|
|
} else { |
108
|
13
|
|
|
|
|
46
|
$s =~ s/^\\(\Q$prefix\E)/$1/; # chomp the leading escape |
109
|
13
|
|
|
|
|
38
|
return ($s, undef); |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
# by now everything else looks like a template |
113
|
17
|
|
|
|
|
37
|
return (undef, $s); |
114
|
|
|
|
|
|
|
} |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
sub process_s { |
117
|
34
|
|
|
34
|
1
|
51
|
my $self = shift; |
118
|
34
|
|
|
|
|
42
|
my $s = shift; |
119
|
34
|
|
|
|
|
53
|
my $vars = shift; |
120
|
|
|
|
|
|
|
|
121
|
34
|
|
|
|
|
70
|
my ($p, $t) = $self->_split_scalar($s); |
122
|
34
|
100
|
|
|
|
103
|
return $p if defined $p; |
123
|
|
|
|
|
|
|
|
124
|
21
|
|
|
|
|
26
|
my $ps; |
125
|
21
|
50
|
|
|
|
58
|
$self->engine->process(\$t, $vars, \$ps) |
126
|
|
|
|
|
|
|
or die $self->engine->error(); |
127
|
21
|
|
|
|
|
80805
|
return $ps; |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
} |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
1; |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
__END__ |