Perl CGI form example - a CGI.pm scrolling list

Here's the source code for a Perl CGI scrolling list example. The CGI.pm module calls this a scrolling list, but it renders an HTML SELECT/OPTION form field with the "multiple" attribute.

The Perl code below shows how you can display an HTML form with a scrolling list field, using the Perl CGI.pm module. The first time this script is called it displays a scrolling list field in a form. After you submit the form, this script displays the item you selected in the scrolling list.

Here's the source code for this Perl CGI.pm scrolling list example script:

#!/usr/bin/perl -Tw
#
#  PROGRAM:	scrolling_list.cgi
#
#  PURPOSE:	Demonstrate (1) how to create a scrolling_list form and
#		(2) how to determine the value(s) 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 scrolling_list.cgi program');


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

if (!$query->param) {

	print $query->startform;
	print $query->h3('Select your favorite programming language(s):');
	print $query->scrolling_list(-name=>'languages',
				 -values=>[
					   'Basic',
					   'C',
					   'C++',
					   'Cobol',
					   'DHTML',
					   'Fortran',
					   'HTML',
					   'Korn Shell (Unix)',
					   'Perl',
					   'Java',
					   'JavaScript',
					   'Python',
					   'Ruby',
					   'Tcl/Tk'],
				 -size=>8,
				 -multiple=>'true',
				 -default=>'Perl');

	# Notes:
	# ------
	#	"-multiple=>'true'" lets the user make multiple selections
	#		from the scrolling_list
	#	"-default" is optional
	#	"-size" lets you specify the number of visible rows in the list
	#	can also use an optional "-labels" parameter to let the user
	#		see labels you want them to see, while you use
	#		different names for each parameter in your program
	
	print $query->br;
	print $query->submit(-value=>'Submit your favorite language(s)');
	print $query->endform;

} else {

	#----------------------------------------------------------#
	#  4b.  If the program is called with parameters, retrieve #
	#  the 'languages' parameter, assign it to an array        #
	#  named $languages, then print the array with each        #
	#  name separated by a <BR> tag.                           #
	#----------------------------------------------------------#

	print $query->h3('Your favorite languages are:');
	@languages = $query->param('languages');
	print "<BLOCKQUOTE>\n";
	foreach $language (@languages) {
		print "$language<BR>";
	}
	print "</BLOCKQUOTE>\n";

}

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

print $query->end_html;

See this Perl CGI CGI.pm scrolling list form in action

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