Home Index

Start with Perl CGI now!,  Part 4 - File Operations Examples
 
Date: July  15 2001
Subject: Online Programmer Ezine, Volume 2, No.6
Article: Start with Perl CGI now!,  Part 4 - File Operations Examples

As we saw in previous issue of this article, writing to file
is very similar to other programming languages.

We even wrote a simple example to write a small string 
'Hello World!' in a 'out.txt' file.

We have rewritten previous example to have a correct html 
format in its output.


#!/usr/bin/perl
print "Content-type: text/html\n\n";
print "<html>\n<head><title>File Write Opearations</title>
</head>";
print "\n<body>File Written.</body>\n</html>";

open (OUTFILE,">out.txt");
print (OUTFILE "Hello World!");
close OUTFILE


After you have copied above script in your web directory 
you must set correct access permissions on it. For this 
step you can set full permission on the file. In final 
steps of a web project you must decide on final permissions 
of each file in your project.

In unix servers you can do this with below command.

chmod 777 file1.pl

Above command will set read/write/execute rights on 
'file1.pl' file. In windows environment you must set 
permissions in IIS management console.

Above script wants to create a file and write its output 
into it. Therefore we will need write permission on the 
output file diretory too.

Changing rights on directories is very similar to files. You 
will set rights on a directory by using its name instead of 
a filename. For example you can use chmod exactly the same 
way, you used it on files.

Next example opens a file in read mode and sends all its 
contents to browser.

Pay attention that, file will be opened in read mode if you 
do not mention openning mode before filename.



open (infile,"out.txt");

print "Content-type: text/html\n\n";
print "<html>\n<head><title>File Read Opearations</title>
</head>";
print "\n<body>File Opened :<br><br>\n";


while (<infile>)
{
print;
}

print "\n<br><br></body>\n</html>";

close infile;


Look at while condition. This line serves two purposes. First 
is reading a line from input file each time and second one is 
testing file read process to see when we reach end of file. 
In this case logical value of this condition becomes false 
and loop terminates.

Next example is the same as our previous example but we have
improved error trapping in it. When script is unable to open 
the file it issues an error message on browser screen.



print "Content-type: text/html\n\n";
print "<html>\n<head><title>File Read Opearations</title>
</head>";
print "\n<body>File Opened :<br><br>\n";


if(!open (infile,"./out.txt"))

print "Can not open file";
print "</body></html>";
die;
}


while (<infile>)
{
print;
}

print "\n<br><br></body>\n</html>";

close infile;


In next example we see how we can use pipes in our perl 
programs. In next example we run shell command 'date /t' and 
redirect its output to our program. As we have openned it 
like a file, our program will use it like other files.

In fact results of shell command are written into a temp file
and then file is openned in browser.



print "Content-type: text/html\n\n";
print "<html>\n<head><title>File Pipe In Opearations</title>
</head>";
print "\n<body>File Opened :<br><br>\n";

open (infile,"date /t|");

while (<infile>)
{
print;
print "<br>";
}

print "\n<br><br></body>\n</html>";

close infile;


Well, it's enough for now. In next issue we will start by 
studeing subroutines and sending variables to them.


If you have any question or comment about this article you 
can visit our message board at :

http://op.htmsoft.com/forums


Home Index
© 2001 OnlineProgrammer (HtmSoft.com) . All Rights Reserved