jimbobfurley has asked for the wisdom of the Perl Monks concerning the following question:

Oh most wise and holy ones, I've written a CGI application entirely in Perl, no templating done, and I would like to know the best way to populate a pull-down menu in a text field on my app's front page. What would happen is that this field would be populated from several records extracted from an external database each time the front page was hit. Would HTML::Mason be the best choice for this ??? Thanks, Travis Weir

Replies are listed 'Best First'.
Re: Perl CGI question
by xorl (Deacon) on Oct 25, 2004 at 17:57 UTC
    I usually just query the DB. Then loop through the results and do a print "<option value=\"" . $fields[0] . "\">" . $fields[1] . "</option>" Before and after the loop don't forget to put the <select> tags. If you can figure out HTML::Mason it might work. I've never used it, so I really can't comment on it. Edit: Can someone tell me how to display square brackets without PM thinking they're a weird link or something. Thanks.

      Use code tags (<code></code>) to display code and your print could more easily be coded as print '<option value="' . $fields[0] . '">' . $fields[1] . '</option>'; #changed " for ' so you don't have to use \"

      "Cogito cogito ergo cogito sum - I think that I think, therefore I think that I am." Ambrose Bierce

        In times where <code> isn't appropriate, use &#91; for [ and &#93; for ].
Re: Perl CGI question
by bradcathey (Prior) on Oct 25, 2004 at 18:22 UTC

    I use HTML::Template exclusively for my templating and works well when populating a pull-down, using its looping function. Be advised, that the loop requires a referenced AoH to work properly. Also, the <tmpl_var xxx> you use in the HTML must match the key names in your hash.

    Perl: use HTML::Template; ...DBI connect, etc. ... my $stmt = "SELECT id, name FROM sometable"; $sth = $dbh->prepare($stmt); $sth->execute(); my $options = $sth->fetchall_arrayref({}); $template = HTML::Template->new(filename => 'form.tmpl') $template -> param(pulldown => $options); print $template->output(); HTML: <select name="names"> <option value="">Select...</option> <tmpl_loop pulldown> <option value="<tmpl)_var id>"><tmpl_var name></option> </tmpl_loop> </select>
    Enjoy.

    UPDATE: fixed a couple of typos.


    —Brad
    "Don't ever take a fence down until you know the reason it was put up." G. K. Chesterton
Re: Perl CGI question
by csuhockey3 (Curate) on Oct 25, 2004 at 21:26 UTC
    I had a flat file that looked like "country:region" This is how I made a pull down with all of those countries -- it's old and a little ugly, but it works and I didn't use anything special to build it. (I was limited to the mods I could use when I wrote this way back when).
    sub generateAllCountries{ my (@all_countries, $HTML_String); open(FILE, "<countries.data") or die "I cannot read the file"; while(<FILE>){ if(/(.*):(.*)/i) { push @all_countries, $1; } } my $HTML_String = "\t" . '<select name = "country"> <option name="country" value="">-- Select a Country --</option +>'; for(my $i = 0; $i < $#all_countries; $i++){ $HTML_String .= "\t\t" . '<option value = "' . $all_countries +[$i] . '">' . $all_countries[$i] . "</option>\n"; } $HTML_String .= "\t</select>"; return $HTML_String; } # sub generateAllCountries