Perl Programming: Clear and Complete Guide by K. Martin Carl

Perl Programming: Clear and Complete Guide by K. Martin Carl

Author:K. Martin, Carl
Language: eng
Format: azw3, epub, pdf
Publisher: UNKNOWN
Published: 2020-09-07T16:00:00+00:00


Figure 8.2

Output from the

highlight_trailing_ws script

15 You might think it sufficient to highlight the offending whitespace characters themselves, rather than an inserted word, but reverse video mode doesn’t affect the display of tabs on most terminals.

This is a good example of using tput to draw the user’s attention to important information on the screen, and I’m sure you’ll find other places to use it in your own programming.

Command interpolation is used to solve many other pesky problems in the IT workplace. In the next section, you’ll see how it can be used to write a grep -like script that handles directory arguments sensibly, by searching for matches in the files within them.

8.5.2 Grepping recursively: The rgrep script

As mentioned in chapter 6, a recursive grep , which automatically descends into subdirectories to search the files within them, can be a useful tool. Although the GNU grep provides this capability through an invocation option, a Perl-based grepper has several intrinsic advantages, as discussed in section 3.2. What’s more, writing a script that provides recursive grepping will allow us to demonstrate some additional features of Perl that are worth knowing.

For starters, let’s observe a sample run of the rgrep script, whose code we’ll examine shortly. In the situation depicted, the Linux superuser was having trouble with a floppy disk, and knew that some file(s) in the /var/log directory would contain error reports—but he wasn’t sure which ones:

$ rgrep '\bfloppy\b' /var/log # output edited for fit /var/log/warn:

kernel: floppy0: data CRC error: track 1, head 1, sector 14 /var/log/messages:

kernel: I/O error, dev 02:00 (floppy)

These reports, which were extracted from the indicated files under the user-specified directory, indicate that the diskette was not capable of correctly storing data in certain sectors.16 The script can be examined in listing 8.3.

Listing 8.3 The rgrep script

1 #! /usr/bin/perl -wnl

2

3 BEGIN {

4 $Usage="Usage: $0 'pattern' dir1 [dir2 ...]";

5 @ARGV >= 2 or warn "$Usage\n" and exit 255;

6

7 $pattern=shift; # preserve pattern argument

8

9 # `@ARGV` treated like "@ARGV"; elements space-separated 10 @files=grep { chomp; -r and -T } # <- find feeds files 11 `find @ARGV -follow -type f -print`; 12 @files or warn "$0: No files to search\n" and exit 1; 13 @ARGV=@files; # search for $pattern within these files 14 }

15 # Because it's very likely that we'll search more than one file, 16 # prepend filename to each matching line with printf 17

18 /$pattern/ and printf "$ARGV: " and print;

16 Which is one reason this venerable but unreliable storage technology has become nearly obsolete.

Because this script requires a pattern argument and at least one directory argument, the argument count is checked in Line 5 to determine if a warning and early termination are in order. Then, Line 7 shifts the pattern argument out of the array, leaving only directory names within it.

The find command on Line 11 appears within the back quotes of command interpolation, but these quotes are treated like double quotes as far as variable interpolations are concerned. The result is that @ARGV is turned into a



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.