He had bought a large map representing the sea,\n Without the l
east vestige of land:\nAnd the crew were much pleased when they
found it to be\n A map they could all understand.\n\n"What's th
e good of Mercator's North Poles and Equators,\n Tropics, Zones
, and Meridian Lines?"\nSo the Bellman would cry: and the crew w
ould reply\n "They are merely conventional signs!\n\n"Other map
s are such shapes, with their islands and capes!\n But we've go
t our brave Captain to thank:"\n(So the crew would protest) "tha
t he's bought us the best-\n A perfect and absolute blank!"\n\n
If you had this file open in your text editor, it would be easy to
change a word, add a comma, or fix a misspelling. If your editor is
powerful enough, in fact, you could change the indentation of each
line with a single command. But the text file is a stream of bytes;
if you wanted to add even a single comma, the remainder of the text
file (possibly thousands or millions of bytes) would have to move
over to make room. Nearly every tiny change would mean lots of slow
copying operations on the file. So how can we edit the file
efficiently?
The most common way of programmatically updating a text file is by
writing an entirely new file that looks similar to the old one, but
making whatever changes we need as we go along. As you'll see,
this technique gives nearly the same result as updating the file
itself, but it has some beneficial side effects as well.
In this example, we've got hundreds of files with a similar
format. One of them is fred03.dat, and
it's full of lines like these:
Program name: granite
Author: Gilbert Bates
Company: RockSoft
Department: R&D
Phone: +1 503 555-0095
Date: Tues March 9, 1999
Version: 2.1
Size: 21k
Status: Final beta
We need to fix this file so that it has some different information.
Here's roughly what this one should look like when we're
done:
Program name: granite
Author: Randal L. Schwartz
Company: RockSoft
Department: R&D
Date: June 12, 2002 6:38 pm
Version: 2.1
Size: 21k
Status: Final beta
In short, we need to make three changes. The name of the
Author should be changed; the
Date should be updated to today's date, and
the Phone should be removed completely. And we
have to make these changes in hundreds of similar files as well.
my $date = localtime;
To get the list of files for the diamond operator, we read them from
a glob. The next line sets $^I, but keep ignoring
that for the moment.
The main loop reads, updates, and prints one line at a time. (With
what you know so far, that means that all of the files' newly
modified contents will be dumped to your terminal, scrolling
furiously past your eyes, without the files being changed at all. But
stick with us.) Note that the second substitution can replace the
entire line containing the phone number with an empty
string -- leaving not even a newline -- so when that's
printed, nothing comes out, and it's as if the
Phone never existed. Most input lines won't
match any of the three patterns, and those will be unchanged in the
output.