17.5 Variable-Length ( Text) DatabasesSome system databases (and quite a few user-created databases) are a series of human-readable text lines, with one record per line. For example, the TCP/IP hosts file contains one line per hostname. Most often, these databases are updated with simple text editors. Updating such a database consists of reading it all into a temporary area (either memory or another disk file), making the necessary changes, and then either writing the result back to the original file or creating a new file with the same name (after deleting or renaming the old version). You can think of this process as a copy pass : the data is copied from the original database to a new version of the database, and changes are made during the copy.
Perl supports a copy-pass-style edit on line-oriented databases using
inplace editing
. Inplace editing is a modification of the way the
diamond operator (
To trigger the inplace editing mode, set a value into the
When the $ARGV = shift @ARGV; open(ARGV,"<$ARGV"); rename($ARGV,"$ARGV$^I"); ## INPLACE ## unlink($ARGV); ## INPLACE ## open(ARGVOUT,">$ARGV"); ## INPLACE ## select(ARGVOUT); ## INPLACE ##
The effect is that reads from the diamond operator come from the old file, and writes to the default filehandle go to a new copy of the file. The old file remains in a backup file, which is the filename with a suffix equal to the value of the
Typical values for Here's a way to change everyone's login name to lowercase in some file that contains a list of user logins, one per line: @ARGV = ("userlist.txt"); # prime the diamond operator $^I = ".bak"; # write userlist.bak for safety while (<>) { # main loop, once for each line tr/A-Z/a-z/; # change everything to lower case print; # send output to ARGVOUT: the new userlist.txt } As you can see, this program is pretty simple. In fact, the same program can be generated entirely with a few command-line arguments: perl -p -i.bak -e 'tr/A-Z/a-z/' userlist.txt
The
Command-line arguments are discussed in greater detail in Programming Perl or the perlrun documentation. |
|