Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
Retiring, March 2020 - sorry, you have missed our final public course.
The Coronavirus situation has lead us to suspend public training - which was on the cards anyway, with no plans to resume

Please ask about private 'maintenance' training for Python, Tcl, Perl, PHP, Lua, etc
Happily continuing private consultancy / programming work
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.


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;


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
  [3839] Spraying data from one incoming to series of outgoing files in Perl - (2012-08-15)
  [3830] Traversing a directory in Perl - (2012-08-08)
  [3548] Dark mornings, dog update, and Python and Lua courses before Christmas - (2011-12-10)
  [3326] Finding your big files in Perl - design considerations beyond the course environment - (2011-06-14)
  [2833] Fresh Perl Teaching Examples - part 2 of 3 - (2010-06-27)
  [2821] Chancellor George Osborne inspires Perl Program - (2010-06-22)
  [2818] File open and read in Perl - modernisation - (2010-06-19)
  [2405] But I am reading from a file - no need to prompt (Perl) - (2009-09-14)
  [2233] Transforming data in Perl using lists of lists and hashes of hashes - (2009-06-12)
  [1861] Reactive (dynamic) formatting in Perl - (2008-10-31)
  [1860] Seven new intermediate Perl examples - (2008-10-30)
  [1841] Formatting with a leading + / Lua and Perl - (2008-10-15)
  [1709] There is more that one way - Perl - (2008-07-14)
  [1467] stdout v stderr (Tcl, Perl, Shell) - (2007-12-10)
  [1442] Reading a file multiple times - file pointers - (2007-11-23)
  [1416] Good, steady, simple example - Perl file handling - (2007-10-30)
  [1312] Some one line Perl tips and techniques - (2007-08-21)
  [1312] Some one line Perl tips and techniques - (2007-08-21)
  [867] Being sure to be positive in Perl - (2006-09-15)
  [702] Iterators - expressions tha change each time you call them - (2006-04-27)
  [618] Perl - its up to YOU to check your file opened - (2006-02-23)
  [616] printf - a flawed but useful function - (2006-02-22)
  [255] STDIN, STDOUT, STDERR and DATA - Perl file handles - (2005-03-23)
  [114] Relative or absolute milkman - (2004-11-10)
  [12] How many people in a room? - (2004-08-12)

Perl - More about Files
  [3839] Spraying data from one incoming to series of outgoing files in Perl - (2012-08-15)
  [3412] Handling binary data in Perl is easy! - (2011-08-30)
  [3320] Reading the nth line from a file (Perl and Tcl examples) - (2011-06-09)
  [2964] An introduction to file handling in programs - buffering, standard in and out, and file handles - (2010-09-21)
  [2405] But I am reading from a file - no need to prompt (Perl) - (2009-09-14)
  [1832] Processing all files in a directory - Perl - (2008-10-11)
  [1709] There is more that one way - Perl - (2008-07-14)
  [1225] Perl - functions for directory handling - (2007-06-09)

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., 2020: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01225 708225 • FAX: 01225 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