Programming Perl 101

Let's take this one line at a time. The first line tells the script to open a file - emails.txt. If this file does not exist, Perl will create it on its own. If you plan to let Perl create the file, make sure your directory has the necessary permissions set. If you don't know what I mean by directory permission, then just upload a blank text file named emails.txt to the same directory as email.html and email.cgi. The first line in the script also assigns the file a name: "EMAILS."

The second like makes use of something called "file locking." This is done to make sure that the file being opened does not change while the script is still processing this code. Imagine what could happen if someone made use of this script by trying to enter their information into emails.txt just as someone else is also doing the same thing? Which would go first? This leaves potential for corruption and a bunch of mixed up text.

File locking is obviously shortened here to "flock." In this case, it is being used to make sure that no other script calling upon the emails.txt file will be allowed to open it for use until this script is finished, or until the file-locking command has ended.

Line three is simple: it prints whatever the user has entered in the "email" field into emails.txt, followed by a line break - represented here by a newline character: "n".

Line four unlocks the emails.txt file for use elsewhere, and line five officially closes it. In case it isn't obvious yet, so far, this script takes the email address entered by the user, enters it into the emails.txt file, starts a new line, and does the whole thing over again the next time an email is entered. Because of this, you can allow people to fill out a form, have their email stored in a text file, and come view it whenever you please. The addresses will be broken up so that one address is on each line of the file.

Enter this next:

print "Content-type: text/html

";
print "The email address '$email' has been successfully entered. ";
print "Thank you. You will receive a confirmation email shortly.";