Creating a simple CGI message board
The HTML Templates
The Bulletin Board Script
Displaying the Message List
Displaying Messages
Adding New Messages
Adding Replies
Expiring Messages
The Complete Bulletin Board Script
Adding New Messages
You now have subroutines for displaying a list of the headers and the actual contents of all the messages and replies that have been posted to your bulletin board. The next step is to create subroutines that will handle postings to the bulletin board. You have already created both the forms for posting messages and replies. In this section, you develop the Add_New_ Message subroutine, which is called when the user posts a message with the form on the bottom of the message list Web page. This form was shown in Figure 2.
With the Add_New_Message subroutine, the user creates a new message. You need to create a new list file for the header information for this new message and any replies that may be posted at a later time. To create a new list file, you first need to read in the value stored in the count.dat file. Remember, this value is the file name of the last list file your bulletin board script created. So, once you read in the value, you need to increment it by one and store the new value back in the count.dat file. You do this with the following lines:
open(LISTS,"$list_count") || die "Content-type: text/html\n\nCannot open list count!"; $num_lists = <LISTS>; close(LISTS); $num_lists++; open(LISTS,">$list_count") || die "Content-type: text/html\n\nCannot open list count!"; print LISTS $num_lists; close(LISTS);
The first open statement works just like the open statement you used in the Display_Message_Lists subroutine to open the count.dat file. The line immediately following these three lines--that is, the line containing $num_lists++;--increments the value in the $num_lists variable. The ++ operator is a Perl operator that takes the current value in the variable ($num_lists in this case), adds one to it, and stores it back in the variable. Following the incrementation of the $num_lists variable, the count.dat file is once more opened and the incremented value is printed to the file. Notice that the operator > precedes the $list_count variable in the second open statement. This operator specifies to open the file for output, overwriting the file if it already exists.
You now have the value to use for the file name of the new list file you are going to create. You also need the file name for the new message file you will create. As with the list file, the file names for the message files are stored in the count.dat file. The only difference is the directory in which the count.dat file is located. For the message file, you need to use the count.dat file in the messages subdirectory you created earlier. The following lines of Perl are similar to the earlier ones for retrieving, incrementing, and saving the list file name value. The only difference is that the following lines retrieve, increment, and save the message file name value.
open(MESSAGES,"$message_count") || die "Content-type: text/html\n\nCannot open message count!"; $num_messages = <MESSAGES>; close(MESSAGES); $num_messages++; open(MESSAGES,">$message_count") || die "Content-type: text/html\n\nCannot open message count!"; print MESSAGES $num_messages; close(MESSAGES);
Now that you have the file names for both the new list file and the new message file, you are ready to create these files. First, you can create the new message file. The following lines open the new file for output and print the contents of the data received from the user:
open(NEWMESSAGE,">$message_dir/$num_messages") || die "Content-type:
text/html\n\nCannot create new message!";
print NEWMESSAGE "<B>Subject:</B> $data{\"subject\"}<BR>\n";
print NEWMESSAGE "<B>From:</B> $data{\"name\"}<BR>\n";
print NEWMESSAGE "<B>E-mail:</B> $data{\"email\"}<BR>\n" if $data{'email'};
print NEWMESSAGE "<B>Date:</B> $date<P>\n";
print NEWMESSAGE "$data{\"comments\"}<P>\n";
close(NEWLIST);
Remember that using the > operator before the file name in the open statement causes the file to be created if it doesn't already exist. You can create the new list file in a similar manner using the following lines of Perl code:
open(NEWLIST,">$list_dir/$num_lists") || die "Content-type: text/html\n\nCannot
create new list!";
print NEWLIST "$data{\"name\"}::$data{\"subject\"}::${date}::$num_messages\n";
close(NEWLIST);
Notice that the header information is formatted in the file. Each field is separated by two colons, which is the format you worked with in the Display_ Message_Lists and Display_Message subroutines.
Listing 5 contains all of the code for the Add_New_Message subroutine. This Listing contains a couple of lines that weren't discussed in this section, in addition to the subroutine and local variable declarations. The first new section is immediately after the local variable declarations. It consists of a die statement and an unless conditional. This code makes the subroutine exit if the user did not supply values for the Name, Subject, and Comments fields. Your bulletin board script does not have to have this code, but it helps to keep empty postings from appearing on your bulletin board. The other line that is was not discussed previously is the final line, which calls the Display_Message_Lists subroutine. After the user's message is added to the bulletin boards, it makes sense for your script to redisplay the message list, which will now contain the user's new message.
sub Add_New_Message {
local (%data) = @_;
local ($num_lists, $num_messages);
# Verify the user entered the required fields
die "Content-type: text/html\n\nYou must enter data for every field
except the E-mail address."
unless ($data{'name'} && $data{'subject'} &&
$data{'comments'});
# Get the last list number
open(LISTS,"$list_count") || die "Content-type: text/html\n\nCannot
open list count!";
$num_lists = <LISTS>;
close(LISTS);
# Increment the number
$num_lists++;
# Save the current list number to the file
open(LISTS,">$list_count") || die "Content-type: text/html\n\nCannot
open list count!";
print LISTS $num_lists;
close(LISTS);
# Get the last message number
open(MESSAGES,"$message_count") || die "Content-type:
text/html\n\nCannot open message count!";
$num_messages = <MESSAGES>;
close(MESSAGES);
# Increment the number
$num_messages++;
# Save the current message number to the file
open(MESSAGES,">$message_count") || die "Content-type:
text/html\n\nCannot open message count!";
print MESSAGES $num_messages;
close(MESSAGES);
# Create the new message
# Windows users need to change the string
">$message_dir/$num_messages"
# to ">$message_dir\\$num_messages"
open(NEWMESSAGE,">$message_dir/$num_messages") || die "Content-type:
text/html\n\nCannot create new message!";
print NEWMESSAGE "<B>Subject:</B> $data{\"subject\"}<BR>\n";
print NEWMESSAGE "<B>From:</B> $data{\"name\"}<BR>\n";
print NEWMESSAGE "<B>E-mail:</B> $data{\"email\"}<BR>\n" if
$data{'email'};
print NEWMESSAGE "<B>Date:</B> $date<P>\n";
print NEWMESSAGE "$data{\"comments\"}<P>\n";
close(NEWLIST);
# Create the new list
# Windows users need to change the string ">$list_dir/$num_lists" to
# ">$list_dir\\$num_lists"
open(NEWLIST,">$list_dir/$num_lists") || die "Content-type:
text/html\n\nCannot create new list!";
print NEWLIST "$data{\"name\"}::$data{\"subject\"}::${date}::$num_messages\n";
close(NEWLIST);
&Display_Message_Lists;
}
|