Lecture 3: Files Processing
Reading to or Writing to a File:
In order to read or write to a file, we need to open this file first using open function. This function has the following format:
open (filevar, filename);
Where: filevar: is the name used in Perl program to refer to the file.
Filename: is the full pathname of the file.
Files in perl can be opened in three modes: read, write, and append modes.
To specify write mode, we put “ > ” character in front of the filename. If we want to open the file in append mode, we put “ >> ” in front of the file name. If not specified, then the file is opened in read mode (we can use “<” in front of the file name).
For example if we want to open myfile in write mode we write:
open (MYFILE, ">myfile");
To read from a file with file variable name MYFILE , we write:
$line = <MYFILE>;
This statement reads a line of input from MYFILE and stores the line of input in the scalar variable $line.
Perl enables us to read an entire file into a single array variable as shown in the following example:
@arr = <MYFILE>;
Here each line of the file becomes an element of the array @arr.
To save data to a file we use print function. This function has the following format:
1
print filevar datalist;
For example:
Print MYFILE “This is one line of data\n”;
Besides the standard input file and the standard output file, Perl also defines a third built-in file variable, STDERR, which represents the standard error file.
By default, text sent to this file is written to the screen. This enables the program to send messages to the screen even when the standard output file has been redirected to write to a file. As with STDIN and STDOUT, you do not need to open STDERR because it automatically is opened for you.
After finishing a file processing we can use close function to close the file. The format of this function is: close (filevar);
File Status:
Perl provides many operators that enable us to test the status of a file. The following table list some of the file-test operators.
-w
-x
-B
-M
-T
Operator
-b
-c
-d
-e
-f
-l
-o
-r
-s
Description
Is the file a block device?
Is the file a character device?
Is the file a directory?
Does the file name exist?
Is the file a normal file?
Is the file a symbolic link?
Does the user own the file?
Is the file readable?
Is the file empty?
Is the file writable?
Is the file a text file.
Is the file executable?
Is the file a binary file?
How long since name modified?
Examples:
2
Ex1: filestatus.cgi
The following example test the status of a file selected by the user. Following is an HTML form called fileStatus.html that enable the user to select a file and submit it to the server.
<html>
<head>
<title> File status </title>
</head>
<body>
<form name="myForm" action="http://localhost/cgi-bin/filestatus.cgi">
Select a file:<input type="file" name="filename"/>
<p>
<input type="submit">
<input type="reset">
</p>
</form>
</body>
</html>
The Perl program filestatus.cgi will get the file and test its status.
#!c:/perl/bin/perl print "Content-Type: text/html\n\n"; use CGI qw(:standard);
$filename=param('filename'); print "<html>\n <title> \n file status \n </title>\n"; print "<body>\n"; if (!(-e $filename))
{ print "$filename does not exist<br>";
} else
{ print "$filename does exist<br>";
} if(! (-r $filename))
{ print "you are not allowed to read $filename <br>";
} else
{ print "you are allowed to read $filename<br>";
} if(! (-w $filename))
{ print "you are not allowed to write to $filename <br>";
}
3
else
{ print "you are allowed to write to $filename<br>";
} print "</body>"; print "</html>";
Following is a screenshot of the output:
4
Ex2: In this example we will write the data sent by the user to a flat file (text file). A form called userdata.html will be used to submit the data to the server:
<html>
<body>
<center>
User Data Form</center>
<hr>
<p> please enter your information, then click on submit.
<form method="post" action="http://localhost/cgi-bin/userdata.cgi">
<table border="0" width="90%">
<tr>
<td width="18%">First Name:</td>
<td width="82%"><input type="text" name="firstname" size="28"></td>
</tr>
<tr>
<td width="18%">Last Name:</td>
<td width="82%"><input type="text" name="lastname" size="28"></td>
</tr>
<tr>
<td width="18%">Phone:</td>
<td width="82%"><input type="text" name="phone" size="20"></td>
</tr>
<tr>
<td width="18%">Email:</td>
<td width="82%"><input type="text" name="email" size="44"></td>
</tr>
<tr>
<td width="18%">Address</td>
<td width="82%"><input type="text" name="address" size="44"></td>
</tr>
<tr>
<td width="18%">Contact:</td>
<td width="82%">mail:
<input type="checkbox" name="contact" value="mail"> email:<input type="checkbox" name="contact" value="email"> phone:<input type="checkbox" name="contact" value="phone"></td>
</tr>
<tr>
<td width="100%" colspan="2">
<input type="submit" value="Submit" >
<input type="reset" ></td>
</tr>
</table>
</form>
5
</body>
</html>
Following is the Perl program userdata.cgi that will save the data to a file called userdata.txt
:
#!c:/perl/bin/perl use CGI qw(:standard);
$firstname=param('firstname');
$lastname=param('lastname');
$phone=param('phone');
$address=param('address');
$email=param('email');
@contact=param('contact'); print "Content-type: text/html\n\n"; print "<html>\n<body>\n\n\n"; open (USERFILE, ">>userdata.txt") or die;
($contmail,$contemail,$contphone)=param('contact');
$userline=$firstname.':'.$lastname.':'.$phone.':'.$email.':'.
$address.':'.$contmail.':'.$contemail.':'.$contphone."\n"; print USERFILE "$userline"; print "Thank you $firstname, your data has been saved\n"; print "</body>\n</html>\n"; close(USERFILE);
Following is a screenshot of the output:
6
Ex3:
In this example we will generate a report from the file userdata.txt created in
Ex2. the Perl program is called userreport.cgi
:
7
#! c:/perl/bin/perl use CGI qw(:standard); print "Content-type: text/html\n\n"; print "<html>\n<body>\n\n\n"; open(INFILE,"userdata.txt") or die; print "<table border=\"1\">\n"; print "<tr><th rowspan=\"2\"> First Name</th><th rowspan=\"2\">"; print "Last Name</th>\n"; print"<th rowspan=\"2\"> Phone No.</th><th rowspan=\"2\">
Email</th>\n"; print "<th rowspan=\"2\"> Address</th><th colspan=\"3\">"; print "Contact Methods</th></tr>\n"; print "<tr><th>Method 1 </th><th>Method 2 </th><th>Method 3
</th></tr>\n"; while ($line=<INFILE>)
{ print "<tr>\n";
@arr=split(':', $line); foreach $var (@arr)
{ if ($var eq "")
{ print "<td> NA</td>";
}
}
} else
{ print "<td>$var</td>"; print "</tr>\n";
} print "</body></body>\n</html>\n"; close (INFILE);
Following is a screenshot of the output:
8
9