line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package System::InitD::Template; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
=head1 NAME |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
System::InitD::Template |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
=head1 DESCRIPTION |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
Simple template system just for internal usage in System::InitD |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
=head1 METHODS |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
=cut |
14
|
|
|
|
|
|
|
|
15
|
1
|
|
|
1
|
|
10
|
use strict; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
56
|
|
16
|
1
|
|
|
1
|
|
8
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
34
|
|
17
|
|
|
|
|
|
|
|
18
|
1
|
|
|
1
|
|
7
|
use Carp; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
1124
|
|
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
our $ANCHOR = ['\[%', '%\]']; |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
=over |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
=item B |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
Returns rendered template. Accepts as parameters file or handle |
27
|
|
|
|
|
|
|
and variables hashref(key=>value). |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
=back |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
=cut |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
sub render { |
34
|
1
|
|
|
1
|
1
|
4
|
my (%params) = @_; |
35
|
|
|
|
|
|
|
|
36
|
1
|
0
|
33
|
|
|
5
|
if (!$params{file} && !$params{handle} && !$params{text}) { |
|
|
|
0
|
|
|
|
|
37
|
0
|
|
|
|
|
0
|
croak "Can't render nothing"; |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
|
40
|
1
|
50
|
33
|
|
|
9
|
if ($params{render_params} && ref $params{render_params} ne 'HASH') { |
41
|
0
|
|
|
|
|
0
|
croak "render_params must be a hashref"; |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
|
44
|
1
|
|
|
|
|
2
|
my $render_params = $params{render_params}; |
45
|
|
|
|
|
|
|
|
46
|
1
|
|
|
|
|
3
|
$render_params->{PERL} = $^X; |
47
|
1
|
|
|
|
|
1
|
my @template; |
48
|
|
|
|
|
|
|
|
49
|
1
|
50
|
|
|
|
3
|
if ($params{file}) { |
|
|
0
|
|
|
|
|
|
50
|
1
|
|
|
|
|
6
|
@template = _tff($params{file}); |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
elsif($params{handle}) { |
53
|
0
|
|
|
|
|
0
|
@template = _tfd($params{handle}); |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
else { |
56
|
0
|
|
|
|
|
0
|
@template = split "\n", $params{text}; |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
local *{System::InitD::Template::parse} = sub { |
59
|
52
|
|
|
52
|
|
53
|
my $string = shift; |
60
|
52
|
|
|
|
|
102
|
for my $key (keys %$render_params) { |
61
|
364
|
50
|
|
|
|
623
|
next unless $render_params->{$key}; |
62
|
364
|
|
|
|
|
3328
|
$string =~ s/$ANCHOR->[0]\s*?$key\s*?$ANCHOR->[1]/$render_params->{$key}/gs; |
63
|
|
|
|
|
|
|
} |
64
|
52
|
|
|
|
|
116
|
my $template = "$ANCHOR->[0].*?$ANCHOR->[1]"; |
65
|
|
|
|
|
|
|
# print $template; |
66
|
52
|
|
|
|
|
95
|
$string =~ s/$template//gs; |
67
|
|
|
|
|
|
|
|
68
|
52
|
|
|
|
|
134
|
return $string; |
69
|
1
|
|
|
|
|
8
|
}; |
70
|
|
|
|
|
|
|
|
71
|
52
|
|
|
|
|
71
|
@template = map { |
72
|
1
|
|
|
|
|
3
|
parse($_); |
73
|
|
|
|
|
|
|
} @template; |
74
|
1
|
|
|
|
|
23
|
return join '', @template; |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
# template from file |
79
|
|
|
|
|
|
|
sub _tff { |
80
|
1
|
|
|
1
|
|
1
|
my $file = shift; |
81
|
|
|
|
|
|
|
|
82
|
1
|
50
|
|
|
|
23
|
unless (-e $file) { |
83
|
0
|
|
|
|
|
0
|
croak "File $file does not exists."; |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
1
|
|
|
|
|
2
|
my $fh; |
87
|
|
|
|
|
|
|
|
88
|
1
|
50
|
|
|
|
46
|
open $fh, $file or croak "Can't open file $file: $!"; |
89
|
1
|
|
|
|
|
4
|
return _tfd($fh); |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
#template from descriptor |
94
|
|
|
|
|
|
|
sub _tfd { |
95
|
1
|
|
|
1
|
|
1
|
my $glob = shift; |
96
|
|
|
|
|
|
|
|
97
|
1
|
|
|
|
|
49
|
my @content = <$glob>; |
98
|
1
|
|
|
|
|
10
|
close $glob; |
99
|
1
|
|
|
|
|
24
|
return @content; |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
1; |