| |||||||||||
| |||||||||||
Writing to and reading from files
1. You open the file using the open function. This returns a "file handle" through which you access the data; a file handle comprise a buffer within your program to allow the data to be read from or written to the disc in blocks, and to control whereabouts in the file you are. You should always check the return value from die to ensure that the open worked 2. If you're reading from the file, you may use the read function if you want to read up to a specified number of bytes, the <> operator in a scalar context if you want to read up to a delimiter such as the next new line, or the <> operator in a list context if you want to read the rest of the file, saving the data line by line (or in other chunks) in a list. 3. If you're writing to the file, you should specify the file handle directly after the print or printf function name, then give a list of expressions to be printed (in the case of print), or the one item to be printed followed by any parameters necessary to fill it in (in the case of printf). 4. When you finish with a file, you should close it in order to ensure that any data left in memory (via the file handle) is flushed. If you fail to close a file, Perl does it for you anyway when you end your program or reuse the file handle. Examples a. Reading a file, line by line open (FILEH,"abc.txt") or die "Could not open abc.txt"; while ($line = <FILEH>){ # Process $line } close FILEH; b. Reading a file, all into a single list open (FILEH,"abc.txt") or die "Could not open abc.txt"; @lines = <FILEH>; close FILEH; # Process @lines; c. Reading a file, all into a single scalar open (FILEH,"abc.txt") or die "Could not open abc.txt"; read (FILEH,$info,-s "abc.txt"); close FILEH; # Process $info; d. Overwriting a file, all from a single list # Set up content into @lines; open (FILEH,">abc.txt") or die "Could not open abc.txt"; print FILEH @lines; close FILEH; e. Adding to a file from a scalar # Set up content into $line open (FILEH,">>abc.txt") or die "Could not open abc.txt"; print FILEH $line; close FILEH; NOTES There's an alternative 3 parameter form of the open function in which you specify the open mode as an additional parameter. This form is not available in older versions of Perl. Data read in will include new line characters that you may want to remove with chop or chomp from the scalar or lists that you've placed the data into. You may also need to add back on the new line characters at the end of each line before you write back out to file. If you want to know why your file open failed, the error message will be in the special variable $! See also File handling and file formatting in Perl Please note that articles in this section of our
web site were current and correct to the best of our ability when published,
but by the nature of our business may go out of date quite quickly. The
quoting of a price, contract term or any other information in this area of
our website is NOT an offer to supply now on those terms - please check
back via our main web site
Related Material
Perl - File Handling Perl - More about Files resource index - Perl Solutions centre home page You'll find shorter technical items at The Horse's Mouth and delegate's questions answered at the Opentalk forum. At Well House Consultants, we provide training courses on subjects such as Ruby, Perl, Python, Linux, C, C++, Tcl/Tk, Tomcat, PHP and MySQL. We're asked (and answer) many questions, and answers to those which are of general interest are published in this area of our site. | |||||||||||
PH: 0800 043 8225 or 01225 708225 • FAX: 0845 8382 405 or 01225 707126 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho | |||||||||||