1 package HTML::Form::Simple;
18 my $attr = ref $_[-1] eq 'HASH' ? pop : {};
19 push @_, undef for @_+1 .. $expect;
26 return XML::Quote::xml_quote_min($_[0]);
30 my ($self, $tag, $attr) = @_;
32 # strip empty if it shouldn't be
33 defined $attr->{$_} and $attr->{$_} eq '' and delete $attr->{$_}
34 for qw(id type class style);
36 my $return = '<' . $tag;
39 delete $attr->{$_} and $return .= ' '.$_
40 for qw(selected checked disabled readonly);
42 $return .= sprintf ' %s="%s"', $_, $self->quote($attr->{$_})
43 for sort grep { defined $attr->{$_} } keys %$attr;
50 my ($self, $attr) = @_;
52 return $self->tag(form => $attr);
62 my ($value, $attr) = $self->_attr(1, @_);
64 $attr->{value} = $value if defined $value;
65 $attr->{type} = 'submit' unless defined $attr->{type};
67 return $self->tag(input => $attr);
72 my ($name, $value, $attr) = $self->_attr(2, @_);
74 $attr = {type => 'hidden', name => $name, value => $value};
77 return $self->tag(input => $attr);
82 my ($name, $value, $attr) = $self->_attr(2, @_);
84 $attr->{name } = $name if defined $name;
85 $attr->{value} = $value if defined $value;
86 $attr->{id} = $attr->{name} unless defined $attr->{id};
87 $attr->{type} = 'text' unless defined $attr->{type} or defined $attr->{rows};
88 $value = delete $attr->{value} if defined $attr->{rows};
90 return defined $attr->{rows} ? sprintf(
92 $self->tag(textarea => $attr),
93 $self->quote(defined $value ? $value : '')
94 ) : $self->tag(input => $attr);
99 my ($name, $rows, $default, $attr) = $self->_attr(3, @_);
101 $default = $attr->{value} unless defined $default;
102 delete $attr->{value}; # never a parent attribute
104 $attr->{name} = $name;
105 $attr->{id} = $attr->{name} unless defined $attr->{id};
106 $attr->{type} = 'select' unless defined $attr->{type};
108 my @options = map { ref $_ ? $_ : {value => $_} } @$rows;
110 if ($attr->{type} eq 'select') {
111 delete $attr->{type};
112 if (defined $default) {
114 $_->{selected} = 1 if defined $_->{value} and $_->{value} eq $default;
118 $self->tag(select => $attr),
119 (map { $self->tag(option => $_) } @options),
122 return wantarray ? @return : join('', @return);
125 if (defined $attr->{id}) {
126 defined $_->{id} or defined $_->{value} and $_->{id} = $attr->{id}.'_'.$_->{value}
129 if (defined $attr->{label}) {
130 defined $_->{value} and not defined $_->{label}
131 and $_->{label} = $attr->{label}->{$_->{value}}
133 delete $attr->{label};
135 if (defined $default) {
137 $_->{checked} = 1 if defined $_->{value} and $_->{value} eq $default;
140 $_ = {%$attr, %$_} for @options;
142 my $label = delete $_->{label};
143 defined $label && $label ne ''
144 ? '<label>'.$self->tag(input => $_)." $label</label>"
145 : $self->tag(input => $_)
147 return wantarray ? @return : join('', @return);
153 my ($name, $value, $attr) = $self->_attr(2, @_);
155 $self->select($name, [$value], {%$attr, type => 'radio'});
160 my ($name, $label, $checked, $attr) = $self->_attr(3, @_);
162 $attr->{label } = $label if defined $label;
163 $attr->{checked} = $checked if defined $checked;
164 $attr->{value } = '1' unless exists $attr->{value};
166 $self->select($name, [$attr], {type => 'checkbox'});
177 my $input = HTML::Form::Simple->new;
178 say $input->start; # <form>
179 printf "<label>%s: %s</label>\n", @$_ for (
180 [ 'Your Name' => $input->text(
181 user => 'Mr. Default'
183 [ Message => $input->text(
184 msg => 'Textarea default', {rows => 4, style => 'background:red'}
186 [ Gender => join ' or ', $input->radio(
189 [ Colour => scalar $input->select(
190 favcolour => [qw(Blue Green Red)], 'Green'
192 [ Options => $input->check(
193 spam => 'Receive weekly newsletter'
196 say $input->stop; # </form>