[ Pobierz całość w formacie PDF ]
.Instead, the header for the new reply is added tothe list file containing the header information for the message the reply references.The lines for retrieving, incrementing, and saving the new message count file name as well as the lines for creating the new message file are exactly the same as those in the Add_New_Message subroutine.However, the addition of the header information tothe list file is different.The following lines of Perl code open the existing list file and append the new header information to it:open(LIST,">>$list_dir/$data{\"list\"}") || die "Content-type:text/html\n\nCannot open list!";print LIST "$data{\"name\"}::$data{\"subject\"}::${date}::$num_messages\n";close(LIST);Notice that the file name in the open statement is preceded by the >> operator.This operator indicates that the file will be opened for output and the output will be appended to the file rather than overwriting it.Listing 9.6 contains the complete Add_Reply subroutine.As with the Add_New_Message subroutine, the Name, Subject, and Comments fields are checked for values, and the subroutine exists if they do not contain any.Also, the Display_Message_Lists subroutineis called after the user's reply has been posted to the bulletin board.Listing 9.6: The Add_Reply Subroutinesub Add_Reply {local (%data) = @_;local ($num_lists, $num_messages);# Verify the user entered the required fieldsdie "Content-type: text/html\n\nYou must enter data for every fieldexcept the E-mail address."unless ($data{'name'} && $data{'subject'} &&$data{'comments'});# Get the last message numberopen(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 fileopen(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);# Add message header to the list# Windows users need to change the string">>$list_dir/$data{\"list\"}"# to ">>$list_dir\\$data{\"list\"}"open(LIST,">>$list_dir/$data{\"list\"}") || die "Content-type:text/html\n\nCannot open list!";print LIST"$data{\"name\"}::$data{\"subject\"}::${date}::$num_messages\n";close(LIST);&Display_Message_Lists;}Expiring MessagesYou could set up a working bulletin board with the four subroutines you have created.However, eventually, your bulletin board will contain messages that are very old.Of course, you could always delete the message files yourself, but then you would alsoneed to modify the list files, removing the header information for any message files you deleted.Handling the expiration of messages is a job better left to your bulletin board script.It can easily check messages for expiration, removing all reference tothem in the list files when deleting them.It can also regularly check for expiration, keeping your bulletin board fresh for everyone who uses it.The Expires subroutine you develop in this section checks the date when each message file was last modified.Because the message files are only modified when they are created, this is the date the file was created
[ Pobierz całość w formacie PDF ]