Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact

Well House Consultants
You are on the site of Well House Consultants who provide Open Source Training Courses and business hotel accommodation. You are welcome to browse and use our resources subject to our copyright statement and to add in links from your pages to ours.
Other subject areas - resources
Java Resources
Well House Manor Resources
Perl Resources
Python Resources
PHP Resources
Object Orientation and General topics
MySQL Resources
Linux / LAMP / Tomcat Resources
Well House Consultants Resources
Extras Resources
C and C++ Resources
Ruby Resources
Tcl/Tk Resources
Web and Intranet Resources
Perl module P207
File Handling
Exercises, examples and other material relating to training module P207. This topic is presented on public courses Perl Programming, Perl bootcamp, Learning to program in Perl, Perl Programming

Opening data files, reading from them, and writing to them. Checking whether files exist. And if so, how big are they? Using the file handling elements of Perl to handle keyboard, screen, other processes, and connections to other computers.

Related technical and longer articles
Writing to and reading from files

Articles and tips on this subjectupdated
12How many people in a room?
How many people do you need to gather in a room for there to be an even chance that two of them have the same birthday? Would you believe that it's as few as 23? This is something I've heard said over the years but I wasn't sure if I believed it. Well - here you go: Day 18, chance is 35.5% Day ...
2012-11-04
 
3839Spraying data from one incoming to series of outgoing files in Perl
Scenario. I have a lot of data that contains large numbers of records which I want to separate into groups. For example, an incomeing web server log file which I want to split out and process visitor by visitor. Using Perl, I can loop through my data line by line and store it into a hash - for example:     while ($lyne ...
2012-08-18
 
3830Traversing a directory in Perl
As part of the revision of our Perl Programming course, we're adding an example on directory handling in with our basic file handling module. That's because, over recent years, Perl's use in parsing directories and searching around for data - systems admin tasks - has increased disproportionately to ...
2012-08-18
 
3548Dark mornings, dog update, and Python and Lua courses before Christmas
It's significantly darker each morning as we take Gypsy and Billy out for a morning walk. We're out well before sunrise now, which was at 07:29 on his first morning with us, it is at 08:04 today - just a fortnight later - and will be at 08:16 by 31st December. Only from 1st January does it start getting ...
2011-12-17
 
3326Finding your big files in Perl - design considerations beyond the course environment
It took me about 20 lines of code - just over 300 bytes - to come up with a Perl program which looks in the current directory and everywhere below for all files over a certain size. Have a look at the code [here] (opens in a new window). Even on such a short piece of code, written in front of delegates ...
2011-06-15
 
2833Fresh Perl Teaching Examples - part 2 of 3
Three part article ... this is part 2. Jump to part [1] or [3] Perl is the most tremendously powerful and fully featured langauge - you've probably seen that from the first set of sample programs from last week's course, which I wrote up over the following weekend to share with a wider audience. Here ...
2010-06-27
(longer)
2405But I am reading from a file - no need to prompt (Perl)
If you're writing a script that calls for user input, you had better prompt the user ... otherwise, the terminal / window will appear to hang and the user won't know what's going on. However - if you're reading your answers from a pre-prepared file or piping them in from another process, all of these ...
2010-06-23
 
2821Chancellor George Osborne inspires Perl Program
From today's budget speech ... $oldvat = 1.175; $newvat = 1.2;   printf ("%8s %8s %8s\n","net","old","new"); for ($amount=60; $amount<90; $amount++) {   next if ($amount%2 and $amount%5);   $oldgross = $amount * $oldvat;   $newgross = $amount * $newvat;   printf ...
2010-06-23
 
2818File open and read in Perl - modernisation
Some updates on file opening and reading in Perl ... 1. The original open function was good, but technically questionable in that the second parameter includes (as a single string) both the name of the file and an indicator as to whether it's being opened for read or write. The newer (three parameter ...
2010-06-19
 
2233Transforming data in Perl using lists of lists and hashes of hashes
In Perl, you don't have conventional two dimensional arrays - you have lists which are single dimensional only. However, you can have a list that contains a reference to another list, in effect a list of lists. Very powerful stuff as it leads to a very flexible way of handling data structures - we ...
2009-06-14
 
1860Seven new intermediate Perl examples
From the "learning to program in Perl" course I'm running this week, I am pleased to present seven new examples ... written during the course, in front of the delegates, to show them NOT ONLY how the code works, BUT ALSO how a programmer will develop such code. Lists and Context * If you use an @abc, ...
2008-11-02
(longer)
1861Reactive (dynamic) formatting in Perl
If you want to format your data neatly in columns, you can use sprintf or printf to do so if you're using a fixed width font. A format of "%20s", for example, calls for a string that's 20 characters long and will be trailing space padded ... except ...that figure "20" is a minimum width, and if your ...
2008-10-31
 
1841Formatting with a leading + / Lua and Perl
In formatted printing, you can often use a leading "+" in the format string to force a positive sign to be added in front of positive numbers - for example "%+4d" means an integer, to base 10, 4 character positions as a minimum, right justified. Here's an example of that, in code, in Lua for john = ...
2008-10-15
 
1709There is more that one way - Perl
"There are six ways of doing anything in Perl." So say I on Perl courses and just occasionally I come up with an example that proves it. This one doesn't quite - I show you just five ways of finding the names of all the files in the current directory: $stuff = `ls`;   @fings = glob("*");   opendir(DH,"."); @allfings ...
2008-07-23
 
1467stdout v stderr (Tcl, Perl, Shell)
When you're programming, you shouldn't write code to read directly from the keyboard and write to the screen .... what if you want to have your program read from or write to a file sometimes? Instead, you should write code to read from stdin (Tcl) or STDIN (Perl) and write to stdout (Tcl) or STDOUT ...
2007-12-12
 
1442Reading a file multiple times - file pointers
When you open a file for read, you create a "file pointer" which knows where in the file you are, and each time you read from the file this advances so that you get the next piece of data each time you do a read. Usually you don't see the file pointer at all - it's internal - and you think nothing ...
2007-11-27
 
1416Good, steady, simple example - Perl file handling
I write a lot of demonstration pieces, and there's always a need for a balance. Spike solutions / proof of principle and concept work well in front of a class. But then they are subject to major criticism due to a lack of robustness, and for security reasons, if they are published without comment ...
2007-10-31
 
1312Some one line Perl tips and techniques
I'm running a Perl Course this week ... a small group, so we can look at some very interesting constructs that I wouldn't normally cover / consider on a public course. Comment out a block of code with an impossible condition if (1 == 0) { .... This is a great way to temporarily comment out a whole ...
2007-08-21
 
867Being sure to be positive in Perl
Perl's printf and sprintf routines (and functions with similar names in C and PHP, and the Python % operator) all provide us with a reasonably flexible way of formatting floating point numbers, but they can't meet every eventuallity. At times, you'll need to use sprintf to do most of the work then adjust ...
2006-09-15
 
618Perl - its up to YOU to check your file opened
When you open a file in Perl, you should ALWAYS check the return status to see if your open function worked. If it can't open the file you've requested it won't print out an error - the failure is silent unless you check for it. Thus: open (FH,"abc.txt") or die ("Cannot open abc.txt\n"); Perl will ...
2006-06-05
 
702Iterators - expressions tha change each time you call them
If you're programming and you write the same expression into your code twice without changing any of the variables, you're simply writing the same expression twice, right? For example, here's a piece of Perl code that exits if $userval is 0, but adds it in to a total and keep going if it's not zero. if ...
2006-04-27
 
Examples from our training material
addplus   Formatter - adding a leading plus
biggy   Finding huge files
bridges   sample answer - town with Bridges in name
christmas   Formatted printing - splittin money between the grandchildren
ck   Splitting money many ways - formatted printing
costa   Output file handles, open, and defaults
ddd   Splitting and reformatting data
fan   One input and five output files in a single program
forvat   Formatting strings and numbers
fprint   Formatted printing - some specifier details
fsv   File open, read, write test example
leedspark   alternative answer to practical at end of module
legacy   Divide money between children and print neatly
newread   <> v readline
park   Sample answer to "the park" exercise
paut   Piping a ping into your program
pe2rl   Single parse of data, multiple reports generated
perlwget   Running an OS command from within a Perl script
piper   Use of file handles to pipe from and to processes
procmon   Process monitoring - demonstration of principle
reporter   file status operators -x and -s
sp   Formatted printing
sswd   Directory parsing, formatting data read
tin   Filter and reformat a data file
wacsal   Re-active formatting
Pictures
"Now where was that data file??"
"Now where did I save that data file?"
Background information
Some modules are available for download as a sample of our material or under an Open Training Notes License for free download from http://www.training-notes.co.uk.
Topics covered in this module
File input and output.
File Handles.
The open function.
Reading from a file handle.
Writing to a file handle.
Closing a file.
Other things you can handle through the file interface.
File testing.
Formatted printing.
Floating point formats.
Formats for whole numbers (integers).
Formats for variable text strings.
Constant text.
sprintf.
Summary.
Complete learning
If you are looking for a complete course and not just a information on a single subject, visit our Listing and schedule page.

Well House Consultants specialise in training courses in Python, Perl, PHP, and MySQL. We run Private Courses throughout the UK (and beyond for longer courses), and Public Courses at our training centre in Melksham, Wiltshire, England. It's surprisingly cost effective to come on our public courses - even if you live in a different country or continent to us.

We have a technical library of over 700 books on the subjects on which we teach. These books are available for reference at our training centre. Also available is the Opentalk Forum for discussion of technical questions.


You can Add a comment or ranking to this page

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

PAGE: http://www.wellho.net/resources/P207.html • PAGE BUILT: Thu Sep 18 11:03:17 2014 • BUILD SYSTEM: WomanWithCat