Perl CGI.pm example - a password field form

Here's the source code for a Perl CGI password field example. The Perl code below shows how you can display an HTML form with a password field, using the Perl CGI.pm module. The first time this script is called it displays a password field in a form. After you submit the form, this script displays the text that you entered in the password field.

Here's the source code for this Perl CGI.pm password field example script:

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

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

if (!$query->param) {

	print $query->startform;
	print $query->password_field(-name=>'the_password',
		-size=>35,
		-maxlength=>50);

	# Note: password_field can also take a '-value' parameter,
	#       that can be used as a default value to display.
	#       Using that parameter also, the entry above might look like 
	#	this:
	#
	#print $query->password_field(-name=>'the_password',
	#	-value=>'default value',
	#	-size=>35,
	#	-maxlength=>50);
	
	print $query->br;
	print $query->submit(-value=>'Submit your password');
	print $query->endform;

} else {

	#----------------------------------------------------------#
	#  4b.  If the program is called with parameters, retrieve #
	#  the 'the_password' parameter, save it in a variable     #
	#  named $yourPassword, and then print it out.             #
	#----------------------------------------------------------#

	print $query->h3('The password is:');
	$yourPassword = $query->param('the_password');
	print "<BLOCKQUOTE>\n";
	print $yourPassword;
	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.