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

I'm populating a HTML drop down menu using CGI's popup_menu method. The popup_menu method says you can generate the drop down menu with a Hash Reference. I believe I have a Hash Reference generated from a call to a DB but I'm getting "HASH(0xa278214)" instead of my desired output. I searched the Perl Monks archive and the net but couldn't figure it out.

What am I doing wrong with this code?
#!/usr/bin/perl -wT use DBI; use strict; use CGI; use CGI::Carp qw(fatalsToBrowser); use Data::Dumper; my $cgi = CGI->new(); # Create new CGI object. my $database = "db"; my $db_server = "localhost"; my $user = "user"; my $password = "pass"; my $dbh = DBI->connect("DBI:mysql:$database;host=$db_server", $user, $password) or die $DBI::errstr; my $authorSQL = "SELECT idAuthor, Author FROM Author ORDER BY Author"; my $hash_ref = $dbh->selectall_hashref($authorSQL, "idAuthor"); print $cgi->header; print $cgi->start_form; print Dumper($hash_ref); print $cgi->popup_menu(-name=>'menu_name', -values=>[$hash_ref]); print $cgi->end_form;
Here's my HTML output from the code above
<form method="post" action="/cgi-bin/addlocation.pl" enctype="multipar +t/form-data"> $VAR1 = { '1' => { 'Author' => 'Mike', 'idAuthor' => '1' }, '2' => { 'Author' => 'Cecil', 'idAuthor' => '2' } }; <select name="menu_name" tabindex="1"> <option value="HASH(0xa278214)">HASH(0xa278214)</option> </select><div></div></form>
As you can see from Data::Dumper, I need the drop down menu to show "Mike" and "Cecil" with their values 1 and 2 respectively.

Replies are listed 'Best First'.
Re: CGI's "popup_menu" Using Hash Reference
by pg (Canon) on Aug 31, 2005 at 06:16 UTC

    You need to form the hash in this way:

    my $hash_ref = { "1" => "Peter", "2" => "David" };

    And the code in this way:

    print $cgi->popup_menu(-name=>'menu_name', -values=>$hash_ref);
Re: CGI's "popup_menu" Using Hash Reference
by fizbin (Chaplain) on Aug 31, 2005 at 09:22 UTC
    The posters so far don't seem to have looked up the CGI perldoc before posting. In any case, in order to create a popup menu with labels different from the values, you need to be using both the -values parameter and the -labels parameter. This first of those takes a list reference, and the second a hash reference. So let's take a look at what they require.

    As it stands now, you're passing into -values a structure like this:

    [ { '1' => { 'Author' => 'Mike', 'idAuthor' => '1' }, '2' => { 'Author' => 'Cecil', 'idAuthor' => '2' } } ]
    when what you need to do is pass a structure like this as the -values parameter: [ '1', '2' ], and this as the -labels parameter:
    { '1' => 'Mike', '2' => 'Cecil' }
    So the question is, how do you go from one to the other? As with many structure reformulations, the answer is with a clever use of map:
    my $values_list = [ sort(keys(%$hash_ref)) ]; my $labels_href = { map {$_->{idAuthor} => $_->{Author}} values(%$hash_ref) }; print $cgi->popup_menu(-name=>'menu_name', -values=>$values_list, -labels=>$labels_href);
    If you don't like the order authors are shown in once you have ten or more, change the sort call above so that it sorts numerically.
    --
    @/=map{[/./g]}qw/.h_nJ Xapou cets krht ele_ r_ra/; map{y/X_/\n /;print}map{pop@$_}@/for@/
Re: CGI's "popup_menu" Using Hash Reference
by duff (Parson) on Aug 31, 2005 at 06:13 UTC

    But you aren't passing a hash reference to -values; you're passing an array reference to a single element array (that single element is a hash ref). Try this instead:

    print $cgi->popup_menu(-name=>'menu_name', -values=>$hash_ref);

      The hash itself also need to be reformated.