1 package HTML::Form::Simple;
17 return XML::Quote::xml_quote_min($_[0]);
21 my ($self, $tag, $attr) = @_;
23 # strip empty if it shouldn't be
24 defined $attr->{$_} and $attr->{$_} eq '' and delete $attr->{$_}
27 my $return = '<' . $tag;
30 delete $attr->{$_} and $return .= ' '.$_
31 for qw(disabled readonly);
33 $return .= sprintf ' %s="%s"', $_, $self->quote($attr->{$_})
34 for sort grep { defined $attr->{$_} } keys %$attr;
41 my ($self, $attr) = @_;
43 return $self->tag(form => $attr);
52 my ($self, $value, $attr) = @_;
54 if (ref $value eq 'HASH') {
59 $attr->{value} = $value;
62 $attr->{type} = 'submit' unless defined $attr->{type};
64 return $self->tag(input => $attr);
68 my ($self, $name, $value) = @_;
72 return $self->tag(input => {type => 'hidden', name => $name, value => $value});
76 my ($self, $name, $value, $attr) = @_;
78 if (ref $name eq 'HASH') {
79 # only attributes provided (first argument)
82 elsif (ref $value eq 'HASH') {
83 # name shorthand (attributes in value parameter)
85 $attr->{name} = $name;
88 # name and value shorthands (all vars keep their assigned values)
90 $attr->{name} = $name;
91 $attr->{value} = $value;
94 $attr->{id} = $attr->{name} unless defined $attr->{id};
95 $attr->{type} = 'text' unless defined $attr->{type} or defined $attr->{rows};
96 $value = delete $attr->{value} if defined $attr->{rows};
98 return defined $attr->{rows} ? sprintf(
100 $self->tag(textarea => $attr),
101 $self->quote(defined $value ? $value : '')
102 ) : $self->tag(input => $attr);
106 my ($self, $name, $rows, $value, $attr) = @_;
108 if (ref $value eq 'HASH') {
114 $attr->{name} = $name;
116 return $self->tag(select => $attr) . join('',
117 map { $self->tag(option => (ref $_ ? $_ : {value => $_})) } @$rows
129 my $input = HTML::Form::Simple->new;
130 say $input->start; # <form>
131 printf "<label>%s: %s</label>\n", @$_ for (
132 [ 'Your Name' => $input->text(username => 'Mr. Default') ],
133 [ Colour => $input->select(
134 favcolour => [qw(Blue Green Red)], 'Green'
137 say $input->stop; # </form>