Congradulations! You just re-invented CGI.pm's
popup_menu().
samtregar (the author of
HTML::Template)
recommends using CGI.pm
for this very thing. Here is a quick example (with
validation!):
use strict;
use warnings;
use HTML::Template;
use CGI qw(:standard);
my @color = qw(red green blue black purple orange);
my %valid = map {$_ => 1} @color;
my $tmpl = HTML::Template->new(filehandle => *DATA);
my $color = param('color');
my $select = popup_menu(
-name => 'color',
-values => [@color],
-default => $color,
);
$tmpl->param(error => 1) unless $valid{$color};
$tmpl->param(
colors => $select,
color => $color,
);
print header, $tmpl->output;
__DATA__
<form>
<p>
Pick a color:
<tmpl_var colors>
</p>
<input type="submit">
</form>
<tmpl_if color>
<p><hr/></p>
<tmpl_unless error>
You picked
<span style="color: <tmpl_var color>;"><tmpl_var color></span>
<tmpl_else>
Invalid choice, "<tmpl_var color>"
</tmpl_unless>
</tmpl_if>
Now then, you say that you use '
the same type of code to
build 6 other select lists' ... why not use
one
function to handle them all? ;) (and don't say because
they have different 'business rules' ... if that's the
case, do the extra specific stuff first, then call
popup_menu()). Happy coding to ya. :)
jeffa
L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
(the triplet paradiddle with high-hat)
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.