line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package HTML::FormHelpers; |
2
|
|
|
|
|
|
|
{ |
3
|
|
|
|
|
|
|
$HTML::FormHelpers::VERSION = '0.004'; |
4
|
|
|
|
|
|
|
} |
5
|
|
|
|
|
|
|
# ABSTRACT: Useful routines for generating HTML form elements |
6
|
|
|
|
|
|
|
|
7
|
4
|
|
|
4
|
|
145534
|
use strict; |
|
4
|
|
|
|
|
9
|
|
|
4
|
|
|
|
|
157
|
|
8
|
4
|
|
|
4
|
|
24
|
use warnings; |
|
4
|
|
|
|
|
7
|
|
|
4
|
|
|
|
|
115
|
|
9
|
4
|
|
|
4
|
|
7911
|
use Try::Tiny; |
|
4
|
|
|
|
|
14016
|
|
|
4
|
|
|
|
|
381
|
|
10
|
4
|
|
|
4
|
|
33
|
use Scalar::Util qw/ blessed /; |
|
4
|
|
|
|
|
8
|
|
|
4
|
|
|
|
|
538
|
|
11
|
4
|
|
|
4
|
|
5956
|
use parent 'Exporter'; |
|
4
|
|
|
|
|
1675
|
|
|
4
|
|
|
|
|
25
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
our @EXPORT_OK = qw( process_attributes radio text select button hidden checkbox ); |
14
|
|
|
|
|
|
|
our %EXPORT_TAGS = ( all => [ qw(radio text select button hidden checkbox )] ); |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
# NOTE: The first hashref we come across is assumed to be the |
18
|
|
|
|
|
|
|
# attributes |
19
|
|
|
|
|
|
|
sub process_attributes { |
20
|
0
|
|
|
0
|
0
|
|
for my $i (0..$#_) { |
21
|
0
|
0
|
|
|
|
|
if (ref $_[$i] eq 'HASH') { |
22
|
0
|
|
|
|
|
|
my $attrs = splice @_, $i, 1; |
23
|
0
|
|
|
|
|
|
return join " ", map { $_ . '="' . $attrs->{$_} . '"' } keys %{$attrs}; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
} |
25
|
|
|
|
|
|
|
} |
26
|
0
|
|
|
|
|
|
return ""; |
27
|
|
|
|
|
|
|
} |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
sub process_args { |
30
|
0
|
|
|
0
|
0
|
|
my $obj; |
31
|
0
|
0
|
|
|
|
|
$obj = shift if blessed $_[0]; |
32
|
0
|
|
|
|
|
|
my $attributes = &process_attributes; |
33
|
0
|
0
|
0
|
0
|
|
|
my $idx = try { $obj->can('id') && "[" . ($obj->id // "") . "]" } catch { "" }; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
34
|
0
|
|
|
|
|
|
my $name = $_[0] . $idx; |
35
|
0
|
|
|
|
|
|
return ($obj,$name,$attributes); |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
sub radio { |
39
|
0
|
|
|
0
|
1
|
|
my ($obj, $fname, $attributes) = &process_args; |
40
|
0
|
|
|
|
|
|
my ($name, $values, $sep) = @_; |
41
|
0
|
|
0
|
|
|
|
$sep ||= ''; |
42
|
0
|
|
|
|
|
|
my ($i, @ret) = 0; |
43
|
0
|
|
0
|
0
|
|
|
my $on = do { try { $obj->$name } } // @{$values}[0]; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
44
|
0
|
|
|
|
|
|
while ($i < @$values) { |
45
|
0
|
|
|
|
|
|
my ($val,$disp) = @{$values}[$i, $i+1]; |
|
0
|
|
|
|
|
|
|
46
|
0
|
0
|
|
|
|
|
my $checked = $on eq $val ? 'checked="checked"' : ""; |
47
|
0
|
|
|
|
|
|
push @ret, qq(); |
48
|
0
|
|
|
|
|
|
} continue { $i+=2 } |
49
|
0
|
0
|
|
|
|
|
return ref $sep eq 'ARRAY' ? @ret : join $sep,@ret; |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
sub text { |
54
|
0
|
|
|
0
|
1
|
|
my ($obj, $fname, $attributes) = &process_args; |
55
|
0
|
|
|
|
|
|
my ($name, $value) = @_; |
56
|
0
|
|
0
|
0
|
|
|
my $val = do { try { $obj->$name } } // $value // ""; |
|
0
|
|
0
|
|
|
|
|
|
0
|
|
|
|
|
|
|
57
|
0
|
|
|
|
|
|
return qq(); |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
sub select { |
62
|
0
|
|
|
0
|
1
|
|
my ($obj, $fname, $attributes) = &process_args; |
63
|
0
|
|
|
|
|
|
my ($name, $options, $key, $value) = @_; |
64
|
0
|
0
|
|
|
|
|
my $str = $name ? qq( |
65
|
0
|
0
|
0
|
|
|
|
my $on = $obj && $name ? ($obj->$name // "") : ""; |
|
|
|
0
|
|
|
|
|
66
|
0
|
|
|
|
|
|
for my $o (@$options) { |
67
|
0
|
|
|
|
|
|
my ($k, $v); |
68
|
0
|
0
|
0
|
|
|
|
if (ref $o eq 'HASH') { |
|
|
0
|
|
|
|
|
|
69
|
0
|
|
|
|
|
|
($k,$v) = each %$o; |
70
|
|
|
|
|
|
|
} elsif ($key && $value) { |
71
|
0
|
|
0
|
0
|
|
|
$k = do { try { $o->$key } catch { $o->{$key} } } // ""; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
72
|
0
|
|
0
|
0
|
|
|
$v = do { try { $o->$value } catch { $o->{$value} } } // ""; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
} else { |
74
|
0
|
|
|
|
|
|
$k = $v = $o; |
75
|
|
|
|
|
|
|
} |
76
|
0
|
0
|
|
|
|
|
$str .= qq(); |
77
|
|
|
|
|
|
|
} |
78
|
0
|
|
|
|
|
|
$str .= ""; |
79
|
0
|
|
|
|
|
|
return $str; |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
sub button { |
84
|
0
|
|
|
0
|
1
|
|
my ($obj, $fname, $attributes) = &process_args; |
85
|
0
|
|
|
|
|
|
my ($name, $value) = @_; |
86
|
0
|
|
0
|
|
|
|
$value //= $name; |
87
|
0
|
|
|
|
|
|
return qq(); |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
sub hidden { |
91
|
0
|
|
|
0
|
1
|
|
my ($obj, $fname, $attributes) = &process_args; |
92
|
0
|
|
|
|
|
|
my ($name, $value) = @_; |
93
|
0
|
|
|
|
|
|
return qq(); |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
sub checkbox { |
98
|
0
|
|
|
0
|
1
|
|
my ($obj, $fname, $attributes) = &process_args; |
99
|
0
|
|
|
|
|
|
my ($name, $checked) = @_; |
100
|
0
|
|
0
|
0
|
|
|
$checked = try { $obj->$name } catch { $checked // 1 }; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
101
|
0
|
0
|
|
|
|
|
$attributes .= " checked" if $checked; |
102
|
0
|
|
|
|
|
|
return qq(); |
103
|
|
|
|
|
|
|
} |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
1; |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
__END__ |