Exercises, examples and other material relating to training module P602. This topic is presented on public courses
Perl for Larger Projects,
Extra Day
File locking, database file handling, directory manipulation and more. This module also covers Perl's format and write commands used for traditional text formatting applications, but now of specialist interest.
Related technical and longer articles
File Locking
Articles and tips on this subject | updated |
3429 | Searching through all the files in or below a directory - Ruby, Tcl, Perl Many of our customers want to learn how to traverse all the files in a directory, or perhaps even all the files in or below a directory. I quite often write a demonstration program during our courses which looks though part of a file system tree for files over a certain size, or for the largest (so ... | 2011-09-09 |
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 |
2876 | Different perl examples - some corners I rarely explore The private Perl course that I ran on Wednesday through Friday of last week was a little out of the ordinary as we were concentrating far more that usual on a wide variety of practices that may be found - either in legacy code or advanced recent code. Great fun for me, and plenty of new examples.
Here ... | 2010-12-04 (longest) |
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 |
1861 | Reactive (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 |
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 |
975 | Answering ALL the delegate's Perl questions During courses, questions arise. "I'll get back to that" could make people feel that I'm brushing something off ... except that I explain, early on, that some questions require a great deal of background knowledge to be answered sensibly. And I keep a list of topics that I'll be getting back to ... | 2006-12-09 |
839 | Reporting on the 10 largest files or 10 top scores What are the biggest 10 files in or below this directory?
What are the 20 'worst' spams I have received in the last month?
What are the five top scores recorded for a popular game on my web site?
It's a very common requirement indeed to provide a program to answer questions like these, and if you've ... | 2006-08-19 |
Examples from our training material
bchop | Random access to a file - using a binary chop |
biggest | Look for biggest files in a tree |
flockdemo | Co-operative file locing with flock |
ftype | Establishing the type of a file system object |
huge | finding lost disc space - hunting for huge files |
huge2 | A progress line - hunting for huge files |
loglook | reading a specific number of bytes |
ndd | Accessing NDBM files |
nf | Use of $! to find out why an action failed |
nopen | $! in a string and a numeric context |
pfc | Copying a file block by block |
pls | Four ways of finding the contents of a directory |
recur | recursively scanning directories |
webfiles | Using typeglobs to handle an effective array of files |
wr_loop | Using file locking constants |
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
Basic file handling.
Opening files.
Reading from file.
Writing to file.
Enquiring about a file.
Other uses of the file interface.
Arrays of file handles.
Directory Handling.
Going recursive.
Random access files.
An example using a sorted, fixed-length record file.
File Locking.
Alternative schemes.
Database files.
Built-in file system handlers.
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.