| 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
|
|
6
|
use strict; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
24
|
|
|
16
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
|
1
|
|
|
|
|
1
|
|
|
|
1
|
|
|
|
|
18
|
|
|
17
|
|
|
|
|
|
|
|
|
18
|
1
|
|
|
1
|
|
5
|
use Carp; |
|
|
1
|
|
|
|
|
1
|
|
|
|
1
|
|
|
|
|
547
|
|
|
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
|
5
|
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
|
|
|
14
|
if ($params{render_params} && ref $params{render_params} ne 'HASH') { |
|
41
|
0
|
|
|
|
|
0
|
croak "render_params must be a hashref"; |
|
42
|
|
|
|
|
|
|
} |
|
43
|
|
|
|
|
|
|
|
|
44
|
1
|
|
|
|
|
3
|
my $render_params = $params{render_params}; |
|
45
|
|
|
|
|
|
|
|
|
46
|
1
|
|
|
|
|
2
|
$render_params->{PERL} = $^X; |
|
47
|
1
|
|
|
|
|
2
|
my @template; |
|
48
|
|
|
|
|
|
|
|
|
49
|
1
|
50
|
|
|
|
6
|
if ($params{file}) { |
|
|
|
0
|
|
|
|
|
|
|
50
|
1
|
|
|
|
|
5
|
@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
|
|
55
|
my $string = shift; |
|
60
|
52
|
|
|
|
|
115
|
for my $key (keys %$render_params) { |
|
61
|
364
|
50
|
|
|
|
685
|
next unless $render_params->{$key}; |
|
62
|
364
|
|
|
|
|
3502
|
$string =~ s/$ANCHOR->[0]\s*?$key\s*?$ANCHOR->[1]/$render_params->{$key}/gs; |
|
63
|
|
|
|
|
|
|
} |
|
64
|
52
|
|
|
|
|
121
|
my $template = "$ANCHOR->[0].*?$ANCHOR->[1]"; |
|
65
|
|
|
|
|
|
|
# print $template; |
|
66
|
52
|
|
|
|
|
100
|
$string =~ s/$template//gs; |
|
67
|
|
|
|
|
|
|
|
|
68
|
52
|
|
|
|
|
121
|
return $string; |
|
69
|
1
|
|
|
|
|
11
|
}; |
|
70
|
|
|
|
|
|
|
|
|
71
|
52
|
|
|
|
|
79
|
@template = map { |
|
72
|
1
|
|
|
|
|
3
|
parse($_); |
|
73
|
|
|
|
|
|
|
} @template; |
|
74
|
1
|
|
|
|
|
31
|
return join '', @template; |
|
75
|
|
|
|
|
|
|
} |
|
76
|
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
# template from file |
|
79
|
|
|
|
|
|
|
sub _tff { |
|
80
|
1
|
|
|
1
|
|
2
|
my $file = shift; |
|
81
|
|
|
|
|
|
|
|
|
82
|
1
|
50
|
|
|
|
37
|
unless (-e $file) { |
|
83
|
0
|
|
|
|
|
0
|
croak "File $file does not exists."; |
|
84
|
|
|
|
|
|
|
} |
|
85
|
|
|
|
|
|
|
|
|
86
|
1
|
|
|
|
|
2
|
my $fh; |
|
87
|
|
|
|
|
|
|
|
|
88
|
1
|
50
|
|
|
|
62
|
open $fh, $file or croak "Can't open file $file: $!"; |
|
89
|
1
|
|
|
|
|
5
|
return _tfd($fh); |
|
90
|
|
|
|
|
|
|
} |
|
91
|
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
#template from descriptor |
|
94
|
|
|
|
|
|
|
sub _tfd { |
|
95
|
1
|
|
|
1
|
|
3
|
my $glob = shift; |
|
96
|
|
|
|
|
|
|
|
|
97
|
1
|
|
|
|
|
58
|
my @content = <$glob>; |
|
98
|
1
|
|
|
|
|
12
|
close $glob; |
|
99
|
1
|
|
|
|
|
22
|
return @content; |
|
100
|
|
|
|
|
|
|
} |
|
101
|
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
1; |