line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Template::Plugin::Handy; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
21848
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
30
|
|
4
|
1
|
|
|
1
|
|
6
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
41
|
|
5
|
1
|
|
|
1
|
|
5
|
use base qw( Template::Plugin::VMethods ); |
|
1
|
|
|
|
|
6
|
|
|
1
|
|
|
|
|
913
|
|
6
|
1
|
|
|
1
|
|
33448
|
use Carp; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
67
|
|
7
|
1
|
|
|
1
|
|
838
|
use Data::Dump; |
|
1
|
|
|
|
|
6119
|
|
|
1
|
|
|
|
|
87
|
|
8
|
1
|
|
|
1
|
|
1103
|
use JSON::XS; |
|
1
|
|
|
|
|
10556
|
|
|
1
|
|
|
|
|
1506
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
our $VERSION = '0.003'; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
our @SCALAR_OPS = our @LIST_OPS = our @HASH_OPS |
13
|
|
|
|
|
|
|
= qw( as_json dump_stderr dump_data ); |
14
|
|
|
|
|
|
|
push( @SCALAR_OPS, qw( increment decrement ) ); |
15
|
|
|
|
|
|
|
push( @LIST_OPS, qw( sort_by ) ); |
16
|
|
|
|
|
|
|
push( @HASH_OPS, qw( sort_by ) ); |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
=head1 NAME |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
Template::Plugin::Handy - handy vmethods for Template Toolkit |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
=head1 SYNOPSIS |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
[% USE Handy; |
25
|
|
|
|
|
|
|
mything.dump_data; |
26
|
|
|
|
|
|
|
mything.dump_stderr; |
27
|
|
|
|
|
|
|
mything.as_json; |
28
|
|
|
|
|
|
|
%] |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
=head1 DESCRIPTION |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
Handy virtual methods I always use in my Template Toolkit files, |
34
|
|
|
|
|
|
|
especially for debugging. |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
=head1 METHODS |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
Only new or overridden method are documented here. |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
=cut |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
# package object |
43
|
|
|
|
|
|
|
my $JSON = JSON::XS->new; |
44
|
|
|
|
|
|
|
$JSON->convert_blessed(1); |
45
|
|
|
|
|
|
|
$JSON->allow_blessed(1); |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
# mysql serial fields are rendered with Math::BigInt objects in RDBO. |
48
|
|
|
|
|
|
|
# monkeypatch per JSON::XS docs |
49
|
|
|
|
|
|
|
sub Math::BigInt::TO_JSON { |
50
|
0
|
|
|
0
|
0
|
|
my ($self) = @_; |
51
|
0
|
|
|
|
|
|
return $self . ''; |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
# same with URI objets |
55
|
|
|
|
|
|
|
sub URI::TO_JSON { |
56
|
0
|
|
|
0
|
0
|
|
my ($uri) = @_; |
57
|
0
|
|
|
|
|
|
return $uri . ''; |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
=head2 dump_data |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
Replacement for the Dumper plugin. You can call this method on any variable |
63
|
|
|
|
|
|
|
to see its Data::Dump representation in HTML-safe manner. |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
[% myvar.dump_data %] |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
=cut |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
# virt method replacements for Dumper plugin |
70
|
|
|
|
|
|
|
sub dump_data { |
71
|
0
|
|
|
0
|
1
|
|
my $s = shift; |
72
|
0
|
|
|
|
|
|
my $d = Data::Dump::dump($s); |
73
|
0
|
|
|
|
|
|
$d =~ s/&/&/g; |
74
|
0
|
|
|
|
|
|
$d =~ s/</g; |
75
|
0
|
|
|
|
|
|
$d =~ s/>/>/g; |
76
|
0
|
|
|
|
|
|
$d =~ s,\n, \n,g; |
77
|
0
|
|
|
|
|
|
return "$d "; |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
=head2 dump_stderr |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
Like dump_data but prints to STDERR instead of returning HTML-escaped string. |
83
|
|
|
|
|
|
|
Returns undef. |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
=cut |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
sub dump_stderr { |
88
|
0
|
|
|
0
|
1
|
|
my $s = shift; |
89
|
0
|
|
|
|
|
|
print STDERR Data::Dump::dump($s); |
90
|
0
|
|
|
|
|
|
return; |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
=head2 as_json |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
Encode the variable as a JSON string. Wrapper around the JSON->encode method. |
96
|
|
|
|
|
|
|
The string will be encoded as UTF-8, and the special JSON flags for converted_blessed |
97
|
|
|
|
|
|
|
and allow_blessed are C by default. |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
=cut |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
sub as_json { |
102
|
0
|
|
|
0
|
1
|
|
my $v = shift; |
103
|
0
|
0
|
|
|
|
|
if (@_) { |
104
|
0
|
|
|
|
|
|
$JSON->pretty(1); |
105
|
|
|
|
|
|
|
} |
106
|
0
|
|
|
|
|
|
my $j = $JSON->encode($v); |
107
|
0
|
0
|
|
|
|
|
if (@_) { |
108
|
0
|
|
|
|
|
|
$JSON->pretty(0); |
109
|
|
|
|
|
|
|
} |
110
|
0
|
|
|
|
|
|
return $j; |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
=head2 increment( I ) |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
Increment a scalar number by one. |
116
|
|
|
|
|
|
|
Aliased as a scalar vmethod as 'inc'. |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
=cut |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
sub increment { |
121
|
0
|
|
|
0
|
1
|
|
$_[0]++; |
122
|
0
|
|
|
|
|
|
return; |
123
|
|
|
|
|
|
|
} |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
=head2 decrement( I ) |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
Decrement a scalar number by one. |
128
|
|
|
|
|
|
|
Aliased as a scalar vmethod as 'dec'. |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
=cut |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
sub decrement { |
133
|
0
|
|
|
0
|
1
|
|
$_[0]--; |
134
|
0
|
|
|
|
|
|
return; |
135
|
|
|
|
|
|
|
} |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
=head2 sort_by( I ) |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
Sort an array or hash ref of objects according to I. The |
140
|
|
|
|
|
|
|
sort assumes a C comparison and the return value of I |
141
|
|
|
|
|
|
|
is run through lc() first. |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
Returns a new sorted arrayref. |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
=cut |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
sub sort_by { |
148
|
0
|
|
|
0
|
1
|
|
my $stuff = shift; |
149
|
0
|
|
|
|
|
|
my $method = shift; |
150
|
0
|
0
|
|
|
|
|
if ( ref $stuff eq 'HASH' ) { |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
151
|
|
|
|
|
|
|
return [ |
152
|
0
|
|
|
|
|
|
sort { |
153
|
0
|
|
|
|
|
|
lc( $stuff->{$a}->$method ) cmp lc( $stuff->{$b}->$method ) |
154
|
|
|
|
|
|
|
} keys %$stuff |
155
|
|
|
|
|
|
|
]; |
156
|
|
|
|
|
|
|
} |
157
|
|
|
|
|
|
|
elsif ( ref $stuff eq 'ARRAY' ) { |
158
|
0
|
|
|
|
|
|
return [ sort { lc( $a->$method ) cmp lc( $b->$method ) } @$stuff ]; |
|
0
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
} |
160
|
|
|
|
|
|
|
elsif ( ref $stuff ) { |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
# might be a single blessed object |
163
|
0
|
|
|
|
|
|
return $stuff; |
164
|
|
|
|
|
|
|
} |
165
|
|
|
|
|
|
|
else { |
166
|
0
|
|
|
|
|
|
croak "sort_by only works with ARRAY or HASH references"; |
167
|
|
|
|
|
|
|
} |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
} |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
1; |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
__END__ |