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

Hi all,

I'm using CGI/DBI to access an Oracle DB, which is something I've done in the past. Many query forms will use popup_menu's to help out the users. My question is about how to populate those boxes efficiently. For boxes that do not have separate labels and values I can always use:

$cgi->popup_menu( -name => 'unigene_build', -values => [@{$dbh->selectcol_arrayref($sql)}], )

Which looks reasonably clear and efficient to my eyes. I'd be very happy to see better ways of doing that, but more troublingly is the idiom I've been using for popup's that need separate labels and values, which is:

$sth_menu->execute(); while (my $ref = $sth_menu->fetchrow_arrayref()) { $menu{$$ref[0]} = $$ref[1]; } $cgi->popup_menu( -name => 'external_database_id', -values => [keys(%menu)], -labels => \%menu );

Incidentally I knowingly left out some stuff here (like the prints and variable declarations and so forth. I hope the idioms I'm using are clear.

The problem I have with the latter code fragment is that I have to create a hash and populate it, after which CGI goes ahead and loop through the hash *twice* -- once to get the values (via keys()) and once to associate each value with the appropriate label. Seems wasteful to me (although this couldn't possibly be an issue given the lack of load on my DB). Is there a better way fellow monks?

Replies are listed 'Best First'.
Re: Populating CGI popup_menu via DBI
by jsegal (Friar) on May 29, 2003 at 22:06 UTC
    There is always the option of gathering the labels in your upper loop:
    $sth_menu->execute(); my @vals; while (my $ref = $sth_menu->fetchrow_arrayref()) { $menu{$$ref[0]} = $$ref[1]; push @vals, $$ref[0]; } $cgi->popup_menu( -name => 'external_database_id', -values => \@vals, -labels => \%menu );
    Though I wouldn't worry too much about the time efficiency here -- if you really find yourself needing to worry about the extra time looping over the hash twice, then your hash is big enough that the client browsers would probably have trouble parsing/rendering the html....

    --JAS

      Thanks for your reply. You're right about the efficiency not being critical here, but seeing potentially inefficient code like that always puts me on alert. As I see it, the following steps are needed:

      1. Fetch data from DB
      2. Write data into hash
      3. Loop through hash to get labels
      4. Loop through hash to get values
      5. Print HTML

      With many other functions, like the pop-up menu where labels and values are the same that I showed above, you can skip steps 3 and 4 and go straight from the DB to the CGI printing. Not only that, it just feels like a lot of code! I had been hoping that there was a more concise idiom somewhere out there waiting to be discovered....

      -Tats
        I fully agree with jsegal on this, the performance of accessing this small hash twice can't be much. If you have more that a cople dozen entries in the popup it can become unmanageable for the user. I suppose it could be a concern for a heavily laden server though.

        It is probably possibe to avoid these steps, but I it would have to be one of two ways:

        1. Not using the CGI module, or
        2. Using techniques known only to the maintainers of the CGI module

        I'll also add that the you hinted at a worry about the load on your DB. That also won't be an issue as you are only sucking the data into the hash once.

        Good luck with your app. It looks like you are concerned with proper coding and that can't be bad.

        -Kurt