line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Templ::Util; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
require Exporter; |
4
|
|
|
|
|
|
|
our @ISA = qw(Exporter); |
5
|
|
|
|
|
|
|
our @EXPORT_OK |
6
|
|
|
|
|
|
|
= qw(attempt quote unquote default indent undent number_lines headerize expand_isa); |
7
|
|
|
|
|
|
|
|
8
|
1
|
|
|
1
|
|
5
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
23
|
|
9
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
24
|
|
10
|
|
|
|
|
|
|
|
11
|
1
|
|
|
1
|
|
5
|
use Carp qw(croak); |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
298
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
# Run a block of code, return 0 if the code died, 1 if it didn't |
14
|
|
|
|
|
|
|
sub attempt (&) { |
15
|
2
|
|
|
2
|
0
|
5
|
my $sub = shift; |
16
|
2
|
|
|
|
|
4
|
eval { $sub->(); }; |
|
2
|
|
|
|
|
5
|
|
17
|
2
|
100
|
|
|
|
15
|
return $@ ? 0 : 1; |
18
|
|
|
|
|
|
|
} |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
# Escapes a single-quoted string |
21
|
|
|
|
|
|
|
sub quote ($) { |
22
|
4
|
|
|
4
|
0
|
8
|
my $str = shift; |
23
|
4
|
|
|
|
|
9
|
$str =~ s|\\|\\\\|gs; |
24
|
4
|
|
|
|
|
12
|
$str =~ s|'|\\'|gs; |
25
|
4
|
|
|
|
|
15
|
return $str; |
26
|
|
|
|
|
|
|
} |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
# Escapes a single-quoted string |
29
|
|
|
|
|
|
|
sub unquote ($) { |
30
|
18
|
|
|
18
|
0
|
35
|
my $str = shift; |
31
|
18
|
|
|
|
|
28
|
$str =~ s|\\\\|\\|gs; |
32
|
18
|
|
|
|
|
25
|
$str =~ s|\\'|'|gs; |
33
|
18
|
|
|
|
|
45
|
return $str; |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
# Returns the first defined value in a list, or a blank string if there are |
37
|
|
|
|
|
|
|
# no defined values |
38
|
|
|
|
|
|
|
sub default (@) { |
39
|
51
|
100
|
|
51
|
0
|
94
|
foreach (@_) { defined($_) && return $_; } |
|
123
|
|
|
|
|
455
|
|
40
|
0
|
|
|
|
|
0
|
return ''; |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
sub undent ($) { |
44
|
1
|
|
|
1
|
|
5
|
no warnings 'uninitialized'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
181
|
|
45
|
1
|
50
|
|
1
|
0
|
556
|
return $_[0] if ( $_[0] !~ m/^(?:\r?\n)*([ \t]+)/ ); |
46
|
1
|
|
|
|
|
3
|
my $i = $1; |
47
|
1
|
|
|
|
|
11
|
return join '', map { s/^\Q$i\E//; $_ } grep { $_ ne '' } |
|
5
|
|
|
|
|
30
|
|
|
5
|
|
|
|
|
15
|
|
|
10
|
|
|
|
|
18
|
|
48
|
|
|
|
|
|
|
split /(.*?\n)/, $_[0]; |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
sub indent ($$) { |
52
|
1
|
|
|
1
|
|
6
|
no warnings 'uninitialized'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
110
|
|
53
|
0
|
|
|
0
|
0
|
0
|
return join '', map {"$_[0]$_"} grep { $_ ne '' } split /(.*?\n)/, $_[1]; |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
sub headerize ($$) { |
57
|
1
|
|
|
1
|
|
5
|
no warnings 'uninitialized'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
98
|
|
58
|
0
|
|
|
0
|
0
|
0
|
return join '', map {"$_[0]$_\n"} split /\n/, $_[1]; |
|
0
|
|
|
|
|
0
|
|
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
sub number_lines ($) { |
62
|
1
|
|
|
1
|
|
4
|
no warnings 'uninitialized'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
134
|
|
63
|
0
|
|
|
0
|
0
|
0
|
my @lines = split /\n/, $_[0]; |
64
|
0
|
|
|
|
|
0
|
my $format = '%' . length( scalar(@lines) . '' ) . "s: %s\n"; |
65
|
0
|
|
|
|
|
0
|
return join '', map { sprintf( $format, ( $_ + 1 ), $lines[$_] ) } |
|
0
|
|
|
|
|
0
|
|
66
|
|
|
|
|
|
|
0 .. $#lines; |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
# Inspired by Class::ISA::self_and_super_path |
70
|
|
|
|
|
|
|
sub expand_isa { |
71
|
1
|
|
|
1
|
|
5
|
no strict 'refs'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
151
|
|
72
|
38
|
100
|
|
38
|
0
|
90
|
my $seen = ref($_[0]) eq 'HASH' ? shift @_ : {}; |
73
|
|
|
|
|
|
|
return |
74
|
72
|
|
|
|
|
85
|
map { s/^::/main::/; $_ } |
|
72
|
|
|
|
|
155
|
|
75
|
72
|
50
|
|
|
|
279
|
grep { defined($_) && $_ ne '' } |
76
|
30
|
|
|
|
|
34
|
map { $_, expand_isa( $seen, @{ $_ . '::ISA' } ) } |
|
30
|
|
|
|
|
110
|
|
77
|
38
|
|
|
|
|
72
|
grep { !$seen->{$_}++ } |
|
30
|
|
|
|
|
102
|
|
78
|
|
|
|
|
|
|
@_; |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
1; |