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
Perl Programming: Clear and Complete Guide by K. Martin Carl.epub
Perl Programming: Clear and Complete Guide by K. Martin Carl.pdf
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.
Ada | Ajax |
Assembly Language Programming | Borland Delphi |
C & C++ | C# |
CSS | Compiler Design |
Compilers | DHTML |
Debugging | Delphi |
Fortran | Java |
Lisp | Perl |
Prolog | Python |
RPG | Ruby |
Swift | Visual Basic |
XHTML | XML |
XSL |
Hello! Python by Anthony Briggs(9433)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(9402)
The Mikado Method by Ola Ellnestam Daniel Brolund(9361)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(7905)
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(7451)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7427)
Grails in Action by Glen Smith Peter Ledbrook(7341)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7167)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6021)
Kotlin in Action by Dmitry Jemerov(4695)
Hands-On Full-Stack Web Development with GraphQL and React by Sebastian Grebe(4006)
Functional Programming in JavaScript by Mantyla Dan(3751)
Windows APT Warfare by Sheng-Hao Ma(3255)
Layered Design for Ruby on Rails Applications by Vladimir Dementyev(2945)
Learning Java by Patrick Niemeyer & Daniel Leuck(2896)
Mastering Python for Finance by Weiming James Ma;(2893)
Learning React: Functional Web Development with React and Redux by Banks Alex & Porcello Eve(2875)
Blueprints Visual Scripting for Unreal Engine 5 - Third Edition by Marcos Romero & Brenden Sewell(2745)
WordPress Plugin Development Cookbook by Yannick Lefebvre(2700)