in reply to dynamic HTML pull down

To answer your question directly, I would split off the file-reading bit from the HTML-generation bit (to avoid reading the file multiple times), then pass some arguments to your subroutine so it can be a little more flexible. Why make 4 nearly identical subroutines whose outputs differ in only 1 character? Pass in an argument!
my @countries = do { my $fh; open($fh => "countries.txt") ? map { /(.*):(.*)/ ? $1 : () } <$fh> : (); }; my $dropdown = make_dropdown("dropdown", @countries); my $another = make_dropdown("another", @countries); sub make_dropdown { my ($form_name, @countries) = @_; ## generate and return a dropdown with <select name="$form_name"> }
But a much better solution for your problem in general would be to just use the CGI modules's existing capabilities, including the (horribly-named) popup_menu routine which generates a dropdown. You are using CGI, right?
use CGI 'popup_menu'; my $dropdown = popup_menu("dropdown", \@countries); my $another = popup_menu("another", \@countries, "USA"); ## set a de +fault
The benefit of this is that you can easily set the selected value of the dropdown.. And it's already been written and tested, etc.

blokhead

Replies are listed 'Best First'.
Re: Re: dynamic HTML pull down
by csuhockey3 (Curate) on Nov 05, 2003 at 03:29 UTC
    Yes, I am using CGI. I like the idea of of popup_menu, I had not known about that. Thanks for the tips -- I think I will try that and also some things suggested by injunjoel.