shadowline
Files - 1 << Files - 2 >> Files - 3
Files generally have 3 "modes" when they are referrenced. Read, write, and append. Read is the default mode. It lets you read file contents without changing them. Write will delete all the current information in a file and replace it with new information. Append adds new content to the end of a file without destroying the current information.



Read does not use any symbols. It allows you to gather information from a file. It will not allow you to change or add to the file.

Here is an example which will open a file, gather the first line information, and print out the information.

#!/usr/bin/perl

print "Content-type: text/html\n\n";

print "This program will open an example file. <br>";
open (example, "example.txt") || die ("Could not open file <br> $!");
$text = <example>;
print "The first line of the file reads : $text ";
close (example);

The $text variable will find and hold all data from the first line of the example file. When the first "RETURN" is encounted in the example file, that states the end of that line.

That first example is OK for reading the first line, but in many cases, you may want to know what is on each line of a file. This can be done by using a WHILE loop.

#!/usr/bin/perl

print "Content-type: text/html\n\n";

print "This program will open an example file. <br>";
open (example, "example.txt") || die ("Could not open file. <br> $!");
$text = <example>;
while ($text){
    print "$text <br>";
    $text = <example>;
    }
close (example);

- The first $text reads in the first line as previous example.
- The while loop tests the $text value. If the value is true then do this loop.
    Print the $text value.
    $text is then assigned to the next line of the file if applicable.
    The loop is tested again. True value continues the loop otherwise break out and continue on.

So now you have it reading each line of a file, but the information is lost during each loop. How about storing the information from a file into an array? Good idea!

#!/usr/bin/perl

print "Content-type: text/html\n\n";

print "This program will open an example file. <br>";
open (example, "example.txt") || die ("Could not open file. <br> $!");
@text = <example>;
print (@text);
close (example);

Notice the use of the array symbol @ in place of the scalar symbol $. This is a much "cleaner" method of using file data. It may also come in handy for future uses. (hint hint). The information from the file is now contained in the array and can be used as necessary for other operations or functions.



Using the WRITE mode, you can take the above examples one step further. Arrays are nice if you are using the data right now, but what about making a backup file of the information?

#!/usr/bin/perl

open (example, "example.txt") || die ("Could not open file. $!");
open (backup, ">backup.txt") || die ("Could not open file. $!");
@copy_this = <example>;
print backup (@copy_this);
close (example);
close (backup);

- example.txt is opened in read mode.
- backup.txt is opened in write mode.
- @copy_this is an array gathering the contents of the example file.
- the information in @copy_this is printed to the backup file.
- both files are closed.

The WRITE mode is symbolized by the single "greater than" bracket. Take a step back and realize the abilities of WRITE mode. As said before, when a file is opened in write mode, any current contents in the file are ERASED, DELETED, GONE. So any information you are now printing to that file are becoming all of the new current contents for the file.

This example also brought up a new command. Actually it is an old one, but in this case, it is new. PRINT. Have a look at that line :

print backup (@copy_this);

Up to now you probably haven't seen any words between the PRINT and the brackets. Now in this example the word "backup" is there. So what does this mean? In the example, the word "backup" is in reference to the file we opened. The command line is saying to print the specified information to this file. If you left out the word to make it look like a normal print command, the default would take over and the information would be printed to your screen.



Append means to "add onto". In this case, it will add new information to the end of a file. APPEND will not delete the current file information like the write mode. Opening a file in append mode is sybmolized by two "greater than" angle brackets.

#!/usr/bin/perl

$new_info = "My example text here";

open (example, ">>example.txt") || die ("Could not open file. $!");
print example ($new_info);
close (example);

- Some text is set for the example.
- The example file is opened in append mode.
- The new text is printed to the file.
- The file is closed.
Files - 1 << Files - 2 >> Files - 3
shadow

Advertise in the banner along the top, Click here!

Valid XHTML 1.0 Transitional Valid CSS! Text Link Ads
shadow
shadow