Previous
Functions or Subroutines 
Next
Regular Expression 
Beginner's Tutorial for CGI using Perl Language
File Operations

File Operations

File Operations involve Writing and Reading(and appending) to a file. No, you don't need a knife for this kind of operation. File Operations in perl is very easy.

open (FILE, "File.txt") || die "Can't open File.txt: $!\n";
@contents=<FILE>;
close(FILE);

First line opens the file called "File.txt" and names that stream 'FILE'. The stream name can be anything - you will need this later while referring to that stream. Use upper case characters for the stream name - not a must, but with one look one can understand that it is a File handle.

Now the rest of the line - || die "Can't open File.txt:$!\n"; Don't understand what that means? This got me when I was learning perl for the first time - I mean, the last part(:$!\n";) looks like the program is mouthing obscenities(Comic book style - "You *!$#").

This part is activated when there is some error. Another way of putting this statement is

if (!open (FILE, "File.txt"))
 {
 die "Can't open File.txt: $!\n";
 }

The 'die' command tells the interpreter that the program should end with error. Then our script will commit suicide.

So when ever the file can't be opened for reading, this error will be shown.
Can't open File.txt: No such file or directory
The '$!' will be replaced by the error message. If you want to show the error message but the script should proceed, give this code
open (FILE, "File.txt") || print "Can't open File.txt: $!\n";

This code is for reading from the file. But how does one know weather it is for reading, writing or for appending? To see how, note the differences between the three lines below...
open (FILE, "File.txt"); #For reading
open (FILE, ">File.txt"); #For Writing
open (FILE, ">>File.txt"); #For Appending

If there is no '>' symbol, the stream reads. If there is one '>', it writes. If there is two, appends.

The second line(@contents=<FILE>;) is much simpler when compared to the first line. This will read the full file and put it in an array called '@contents' with each line of the file as an item in the array.

These above method read the full file - very useful sometimes - but they are a memory hog. There is an alternate method to read the file one line at a time.

while (<FILE>) {     #Assigns each line in turn to $_ 
    print "This Line: $_";
}

This will read one line at a time from <FILE> and store it in $_. Don't know what '$_' is? This is another predefined variable in perl like $! and @_ that we discussed earlier.

Now let us move on to the next line - close(FILE);
This will close the stream - be sure to give it. If you don't, perl will close it for you - so no major problems.

Wow! Three lines of code, two pages of explanation. Sometime I amaze even myself.

Now that we know how to read and write, let write a program to do that for us.

#!/usr/local/bin/perl

#Open the file for writing
open (OUT, ">Jokes.txt") || die "Can't open Jokes.txt: $!\n";
#Write a few lines to it
print OUT "Error: Keyboard not attached. Press F1 to continue.\n";
print OUT "All wiyht. Rho sritched mg kegtops awound?
Excuse me for butting in, but I'm interrupt-driven.\n";
close(OUT); #Close the file

#Append more lines to it
open (APP, ">>Jokes.txt") || die "Can't open Jokes.txt: $!\n";
print APP "Hit any user to continue.\n";
print APP "Access denied--nah nah na nah nah!\n";
print APP "Enter any 11-digit prime number to continue...\n";
close(APP); #Close the file

#Now read the whole thing...
open (READ, "Jokes.txt") || die "Can't open Jokes.txt: $!\n";
while (<READ>) {
	print "$_"; #Print it line by line
}
close READ;
Previous
Functions or Subroutines 
Next
Regular Expression 
Subscribe to Feed