![]() |
![]() |
|
There are many things that you can do with cgi-scripts:
Usually, you will need to read and write to text files in order to store information. Here is how to do that: First you have to open the file. You do this with the open() command: open(FRUIT,"fruit.txt"); As you can guess, this opens the file called fruit.txt. In that space, you can also put path names if the file isn't in the same directory as the script. The other part of the command is called the Filehandle. That is the FRUIT. All this does is it gives you an easy way to refer to this file when reading and writing. Sometimes, you don't want to always open the same file. Maybe you want to open a file that a user names on a form. After you have the variable with the file name stored in it, then all you have to do is put the variable in the place of the file name. You don't need quotes, but you can use them if you want to. You do need them if you need to add anything to the file name, like an extension.
$file = "hello.txt";
or
$file = "hello";
You can open more than one file at a time, but make sure you give each one a different filehandle. If you open a file that doesn't exist, then Perl creates it for you. It will be a blank file. Filehandles can be any text, as long as it doesn't contain any special characters. They can also be any case, upper, lower, or mixed, as long as you keep it that way through out your script. It is case-sensitive (upper case and lower case makes a difference!). All of the filehandles are capitalized here just for clarification. To write to a file, you would simply add in the filehandle that you wantto write to in the print statement:
print FRUIT "Apple";
This command wrote 'Apple' and 'Bananas' to fruit.txt. But if you viewed the file right now, it would still be blank. This is because you haven't 'saved' it yet. You would do this by closing the file: close FRUIT; That was easy enough. Now if you viewed fruit.txt, it would look like this:
Apple
There are a few quirks about writing to files: If you open a new file, then write to it, each print statement will write a new line in the file, just like if you were printing to the screen. If you open a file that already has information in it, and start printing, it will start at the first line and rewrite the information, effectively erasing the contents of the file. You don't always want this, though. Sometimes you want to append, or write to the end of the file. You do this by adding a '>>' to the open() command, as in the following example: open (FRUIT, ">>fruit.txt"); This will make it so that you can ONLY append to fruit.txt, not write over the current contents.
print FRUIT "Seedless Grapes";
Close FRUIT; Now fruit.txt will look like this:
Apple
There are a few other things that you can do with the open() command besides the '>>':
That is a lot to remember, but the only two you will probably need are: '' (nothing), '>', '<', and '>>'. There is an easy way to remember which way the < and > should point. Examine the open() statement: open (FRUIT, ">fruit.txt"); That is using to the write only symbol. If you look at it, you can see that the symbol is 'pointing' to the file name, which means that the data will only go to the file from your script. Try the read symbol: open (FRUIT, "<fruit.txt"); Likewise, the symbol is pointing to the filehandle, which represents your script, and that the data will only go from the file to your script, or be read. '>>' is the same way. Reading from files is a bit more complicated, but not much. Since Perl reads files one line at a time, that means that you will have to process them one line at a time. This would require a loop, called the while() loop:
open (ANIMALS, "<animals.txt");
What this does is open animals.txt and prints the contents of it to the screen. You will notice an odd looking symbol, '$_'. This is a special string variable, which contains the current line from the file. What the loop does is it repeats the code contained in the { and }. The first time it cycles through, the $_ variable contains the first line of the file. The second time, it contains the second line, and so on. When it runs out of lines, the while() loop stops cycling, and the code continues executing whatever is after the }. You might not always want to cycle through the entire script, so there is a way to stop the loop when it encounter a line with a certain word or symbol.
open (ANIMALS, "animals.txt");
This will open animals.txt, then read and print each line until it encounters a line that says 'done'. Be careful
though, because if the file doesn't contain 'done', then this becomes an infinite loop and you'll have a problem.
You should only use this method when you are sure you need it. One thing you could do is combine the two methods
by using while( Within the parenthesis in the while() loop, there can be any statement in there. The way the while() loop works is it continues repeating the code inside the { and } as long as the value inside the () is positive. You can put variables in the () also, as long as you make sure to change it inside the {}. After each line in a text file, there is usually a carriage return, which in Perl is denoted by a '\n' (newline). When you are printing text, you have to make sure that you have one when ever you want a new line. In a text file, a new line is determine by where the '\n' is located. After you read lines and possibly store them to variables, you might not want newline statements floating around. You can get rid of the newlines by using the chomp() command.
open (MOVIES, "movies.txt");
Notice that you don't have to include the newline symbol now. Remember though, that since you took off the newline from the $_ variable, you have to add it back when you print it, so that everything isn't on one line when it prints. There is another thing that you can do to this script, and that is leave out all of the $_'s. Perl automatically puts that statement in if you leave it out.
while (1) {
Cool huh? |