next up previous
Next: Subroutines or functions Up: Some useful Perl Previous: Reading Directories

Reading and Writing Files

We have just introduced the concept of a Directory Handle for referring to a Directory on disk.

We now introduce a similar concept of File Handle for referring to a File on disk from which we can read data and to which we can write data.

Similar ideas of opening and closing the files exist.

You use the open() operator to open a file (for reading):

     open(FILEHANDLE,"file_on_device");

To open a file for writing you must use the ``>'' symbol in the open() operator:

     open(FILEHANDLE,">outfile");

Write always starts writing to file at the start of the file. If the file already exists and contains data. The file will be opened and the data overwritten.

To open a file for appending you must use the ``>>'' symbol in the open() operator:

     open(FILEHANDLE,">>appendfile");

The close() operator closes a file handle:

     close(FILEHANDLE);

Three special file handles are always open STDIN, STDOUT and STDERR.

STDIN reads from standard input which is usually the keyboard in normal Perl script or input from a Browser in a CGI script. Cgi-lib.pl reads from this automatically.

STDOUT (Standard Output) and STDERR (Standard Error)by default write to a console or a browser in CGI.

To read from a file you simply use the <FILEHANDLE> command which reads one line at a time from a FILEHANDLE and stores it in a special Perl variable $_.

For example:

open(FILE,"myfile") 
   || die "cannot open file";
while(<FILE>)
{ print $_; # echo line read
}
close(FILE);

To write to a file you use the Print command and simply refer to the FILEHANDLE before you format the output string via:

    print FILEHANDLE "Output String\n";

Therefore to read from one file infile and copy line by line to another outfile we could do:

open(IN,"infile") 
   || die "cannot open input file";
open(OUT,"outfile") 
   || die "cannot open output file";
while(<IN>)
{ print OUT $_; # echo line read
}
close(IN);
close(OUT);

NOTE In order to be able to write to a file (on UNIX in particular) you must make sure that the cgi-bin diectory is Group Writable.

To do this:

You need to set up your public/project cgi bin for the correct access privileges

For more detals refer to

http://www.cm.cf.ac.uk/User/howto.html (public) and http://www.cm.cf.ac.uk/User/howto_project.html

which essentially say:

Normally cgi scripts run under a user-id not used for any other purpose. They cannot output to a file in your filespace unless you make it group-writeable. To Do this:

                       
           chmodwww public or project_cgi-bin/outputfile
           chmod og+w public or project_cgi-bin/outputfile

On the Cardiff Computer Science Infromation Serever there is a maximum CPU time of 30 seconds allocated to scripts executing from your project_cgi-bin directory. At the end of 30 seconds, the program is terminated. Any HTML which it has produced is flushed to the client browser but no error message or indication of termination is given.


next up previous
Next: Subroutines or functions Up: Some useful Perl Previous: Reading Directories
Dave Marshall
9/28/2001