Perl CGI.pm - a popup menu example

Here's the source code for a Perl CGI popup menu example. (It's called a "popup menu" in the CGI.pm module, but it renders an HTML SELECT/OPTION form in a browser.)

The Perl code below shows how you can display an HTML form with a popup menu, using the Perl CGI.pm module. The first time this script is called it displays the popup menu (combo box) in a form. After you submit the form, this script displays the item you selected from the combo box.

Here's the source code for this Perl CGI.pm combo box example script:

#!/usr/bin/perl -Tw
#
#  PROGRAM:	popup_menu.cgi
#
#  PURPOSE:	Demonstrate (1) how to create a popup_menu form and
#		(2) how to determine the value selected by the user.
#
#  Created by alvin alexander, devdaily.com.
#

#-----------------------------------#
#  1. Create a new Perl CGI object  #
#-----------------------------------#

use CGI;
$query = new CGI;

#----------------------------------#
#  2. Print the doctype statement  #
#----------------------------------#

print $query->header;

#----------------------------------------------------#
#  3. Start the HTML doc, and give the page a title  #
#----------------------------------------------------#

print $query->start_html('My popup_menu.cgi program');

#------------------------------------------------------------#
#  4a.  If the program is called without any params, print   #
#       the popup_menu form.                                 #
#------------------------------------------------------------#

if (!$query->param) {

	print $query->startform;
	print $query->h3('Select a dinner entree:');
	print $query->popup_menu(-name=>'entrees',
				 -values=>['Steak','Seafood','Veggies'],
				 -default=>'Veggies');

	#Note:  You can specify an optional '-labels' parameter to
	#       let the user see one value, while you use a different
	#       parameter in your program.  Using the '-labels' parameter,
	#       your popup_menu might be defined like this:
	#
	#print $query->popup_menu(-name=>'entrees',
	#			 -values=>['Steak',
	#				   'Seafood',
	#				   'Veggies'],
	#			 -default=>'Veggies',
	#			 -labels=>{'Steak'=>'Filet Mignon',
	#				   'Seafood'=>'Maui Maui',
	#				   'Veggies'=>'Hand-picked vegetables'});
	
	print $query->br;
	print $query->submit(-value=>'Select your meal');
	print $query->endform;

} else {

	#----------------------------------------------------------#
	#  4b.  If the program is called with parameters, retrieve #
	#  the 'entrees' parameter, assign it to a variable named  #
	#  $yourMeal, and then print it out.                       #
	#----------------------------------------------------------#

	print $query->h3('Looks like you ordered:');
	$yourMeal = $query->param('entrees');
	print "<BLOCKQUOTE>\n";
	print $yourMeal;
	print "</BLOCKQUOTE>\n";

}

#--------------------------------------------------#
#  5. After either case above, end the HTML page.  #
#--------------------------------------------------#

print $query->end_html;

See this Perl CGI CGI.pm example in action

If it helps, you can see this Perl CGI.pm example in action at this url.