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


In reply to Re: dynamic HTML pull down by blokhead
in thread dynamic HTML pull down by csuhockey3

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.