#!/usr/local/bin/perl
# Use the CGI.pm library
use CGI;
# Make a new CGI object.
$query = new CGI;
# Write out the content-type header followed by a blank
line.
print $query->header;
# Write the HEAD section, including the title.
print $query->start_html("Your Data");
# Get the data from the form.
$bearcatID = $query->param('bearcatID');
# Open accountdata.txt, the file that holds
the data.
# Read the contents into the array @data,
# Then close the file.
open(PAGE, "<../accountdata.txt");
@data = <PAGE>;
close(PAGE);
# Loop through the @data array. The loop will assign each
element to the
# scalar $words.
foreach $words(@data) {
# This is a little tricky.
# The first three scalars, in
the parenthesis, will hold the values that we parse
# out of the $words element.
# The $words element holds one
line of data from the text file.
# The =~ and / / says to split
up $words, based on the parameters we have given it.
# These parameters are: multiple
non-whitespace characters (\S*) , the tilde \~
# then more characters, another
tilde, and more characters.
# Note that we had to escape
out the tilde with a \ because the tilde is a special
# character.
# For more information, see chapter 11 in the book.
($ID, $firstName, $lastName) = $words =~ /(\S*)\~(\S*)\~(\S*)/;
# Uncomment the following statements for debugging,
# if you want to see what you are getting.
# print $ID;
# print $firstName;
# print $lastName;
# Assign the input data to a hash, for use
later.
# Note that we are using the scalars that
we retrieved from the file.
$first{$ID} = $firstName;
$last{$ID} = $lastName;
}
# Use the has and the bearcat ID to print
a welcome message,
# using the hashes we defined above.
print "Welcome $first{$bearcatID} $last{$bearcatID} !";
# End the form.
print $query->end_html;