Exercises, examples and other material relating to training module P215. This topic is presented on public course
Perl for Larger Projects
You can do much more with files in Perl than just read or write text from and to them. This module covers checking on file size and status, checking all the files in a directory, handling binary data and more. It also covers file system management functions that let you create and delete files and directories, and change permissions on them.
Related technical and longer articles
Solution Centre - all article listingSolution Centre - all article listingWriting to and reading from files
Articles and tips on this subject | updated |
3839 | Spraying 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 |
3412 | Handling binary data in Perl is easy! Perl can handle binary data just as easily as ASCII text - but YOU - if you're the programmer - must understand the format of the data that you'll be working with. With binary data it's every bit as important to get the right bytes in the right places as it is to get the appropriate separators between ... | 2011-08-30 |
3320 | Reading the nth line from a file (Perl and Tcl examples) "How do I find the 100th line in a file" - a common question for newcomers to coding. The short answer is to open the file, and loop through to read lines until the one that you want. Although most languages have a seek command or function, that works by bytes and with a typical text / ascii file, ... | 2011-06-09 |
2964 | An introduction to file handling in programs - buffering, standard in and out, and file handles Stdout and Stderr
Programmers typically don't write code to output to the screen / current window, as to to so would be to provide an inflexible system - instead, they output to what's known as "standard out", also known as stdout. Usually stdout defaults to the screen / current window so there's no ... | 2010-09-21 (longer) |
1832 | Processing all files in a directory - Perl From this week's Perl course:
opendir(DH, ".");
while ($igot = readdir(DH)) {
next if ($igot =~ /^\.{1,2}$/);
print "igot $igot\n";
}
You'll notice that I have used a regular expression to check for files called dot and dot-dot, which are the current and parent directory, as ... | 2010-06-23 |
2405 | But 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 |
1709 | There 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 |
1225 | Perl - functions for directory handling Perl has many built in functions for file and directory handling and you should use them in preference to shelling out in your scripts because:
• They work across operating systems (at least as far as is practical)
• They are much more efficient as there's no extra processes being started ... | 2007-06-12 |
Examples from our training material
ab_file | Using some of the file status operators |
af | using the stat function to report on a file |
allin | Read and report contents of a directory |
chdirddemo | Using directory functions cwd, chdir, opendir, readdir |
dend | Data on end of program file |
dotty | globbing on a file handle |
dxyz | Finding all files with names matching a pattern |
f2 | Formatting using "format" and "write" |
fal | split an incoming file lots of ways |
file | _ and $_ in file operators |
ft | testing file existence, size and other stats |
hunt | Random access to a file of fixed length records |
inter | See is STDIN is interactive, and prompt if it is |
lowther.pl | Reading data from the end of the program file |
segment | Spray one input file across 16 different outputs |
whatsmypath | Lists out all duplicate executables |
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
[here].
Topics covered in this module
Other file status requests.
Operators.
globbing.
stat.
File locking, random access files, etc.
File locking.
Random access file.
Accessing the file system.
Including data within your programs.
Here documents.
Reading from the end of your program.
Formatted write.
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
Ruby,
Lua,
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.