Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
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
  [12] - ()
  [114] - ()
  [255] - ()
  [616] - ()
  [618] - ()
  [702] - ()
  [867] - ()
  [1312] - ()
  [1312] - ()
  [1416] - ()
  [1442] - ()
  [1467] - ()
  [1709] - ()
  [1841] - ()
  [1860] - ()
  [1861] - ()
  [2233] - ()
  [2405] - ()
  [2818] - ()
  [2821] - ()
  [2833] - ()
  [3326] - ()
  [3548] - ()
  [3830] - ()
  [3839] - ()

Perl - More about Files
  [1225] - ()
  [1709] - ()
  [1832] - ()
  [2405] - ()
  [2964] - ()
  [3320] - ()
  [3412] - ()
  [3839] - ()

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, Lua, 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.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2024: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/solutions/perl-wri ... files.html • PAGE BUILT: Wed Mar 28 07:47:11 2012 • BUILD SYSTEM: wizard