A File Test Example

Here's an example program that uses the file tests described above. It accepts two strings as arguments, followed by a file or list of files. It replaces any occurrences of the first string in the file with the second string. Before it modifies any file, though, it performs a number of tests on it to make sure that performing the search and replace is okay. Let's skip straight to the source code, which is in Listing 15.2

Listing 15.2. The sr.pl Search and Replace Program
1:  #!/usr/bin/perl
2:
3:  use strict;
4:
5:  if (@ARGV < 3)
6:  {
7:      print "Usage: sr.pl old_string new_string file(s)
";
8:      exit;
9:  }
10:
11: my $old = shift @ARGV;
12: my $new = shift @ARGV;
13:
14: my $filename;
15:
16: foreach $filename (@ARGV)
17: {
18:     print "Processing $filename ...
";
19:
20:     # If the file does not exist, move along.
21:     unless (-e $filename)
22:     {
23:         print "Skipping $filename, it does not exist.
";
24:         next;
25:     }
26:
27:     # If the file is a directory, move along.
28:     if (-d $filename)
29:     {
30:         print "Skipping $filename, it's a directory.
";
31:         next;
32:     }
33:
34:
35:     # If the file is not readable, move along.
36:     unless (-r $filename)
37:     {
38:         print "Skipping $filename, it's not readable.
";
39:         next;
40:     }
41:
42:     # If the file is not writable, move along.
43:     unless (-w $filename)
44:     {
45:         print "Skipping $filename, it's not writable.
";
46:         next;
47:     }
48:
49:     # If the file is not a text file, move along.
50:     if (-B $filename)
51:     {
52:         print "Skipping $filename, it's a binary file.
";
53:         next;
54:     }
55:
56:     my $backup_filename = $filename . "~";
57:
58:     # If the backup file already exists, raise an error
59:     if (-e $backup_filename)
60:     {
61:         print "Skipping $filename, backup file $backup_filename exists.
";
62:         next;
63:     }
64:
65:     rename $filename, $backup_filename;
66:
67:     open (IN, "< $backup_filename")
68:         or die "Can't open $backup_filename";
69:
70:     open (OUT,"> $filename")
71:         or die "Can't write to $filename";
72:
73:     while (<IN>)
74:     {
75:         s/$old/$new/g;
76:         print OUT;
77:     }
78:
79:     close IN;
80:     close OUT;
81: }
					

First things first, on lines 5 through 9, check to see whether the user called the script properly. If he didn't include at least three arguments, print a message indicating the proper usage of the script. On lines 11 and 12, extract the string to search for and the string to replace it with from @ARGV. Any further arguments are assumed to be the names of files that will be searched.

Loop over the remaining arguments to the array, treating them as filenames. Five tests are performed on each argument before even trying to deal with it as a file. If the file does not exist, is a directory, is not readable or writable, or is a binary file, it is skipped. These tests assure that a file is not processed and fails, or that a binary file or directory is not corrupted by processing it without checking out what it is first.

If any of these tests fail, a message is printed indicating that something went wrong, and use next to stop processing the current filename and move to the next one. If the tests all succeed, append to the file's name to create the name of the backup file that the script will produce. If the backup file exists, go ahead and skip the file so that the user's work isn't inadvertently destroyed.

If the backup file doesn't exist everything is okay for the script to proceed. First, rename the file to the backup file name that was generated. Then, open the backup file for input and the original filename for output. On line 73, start looping over the contents of the backup file, replacing the old string with the new one, and printing out those lines to the output file, which has the name of the file that we're currently processing.

In the end, the original file is saved as the file named in the variable $backup_file, and $filename contains the results of the processed file.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset