7.2.1 Printing Typed Lines

7.2.1  Printing Typed Lines

The following program shows another simple use of pipes.

 Program 7.5

#!/usr/bin/perl
#more.pl

#prints the two lines on the laser printer
open (LPR, "|more | lpr ");
print LPR "This is the first line.\n";
print LPR "This is the second line.\n";

close (LPR);

The program opens a pipe handle called LPR. Two lines of text are printed to the pipe handle. These two lines are piped to the more command and then piped again to a printer using the lpr command. The more command in Unix shows a long text document page by page on the terminal. It is useless here, but used to show that there can be several pipes in the pipe handle’s string argument. The two lines of text are sent to the printer once the
LPR pipe handle is closed. One can easily see that this program can be modified so that it can read input from a keyboard, a file or a database and send the output directly to a printer, or any other device or program.

A pipe has a source process and a destination process. The following program uses a pipe whose source process is the Unix system’s

 

ls -R

 

command, and whose destination process is the Perl script.

 Program 7.6

#!/usr/bin/perl
#lsRFind1.pl

use strict;
use Cwd;

my ($filename, @lines, $lines, $word);
$word = $ARGV[0];
open LSR, "ls -R|";
while ($filename = ){
    chomp $filename;
    next if (!(-T $filename));
    open TEXTFILE, $filename;
    @lines = ;
    $lines = join "", @lines;
    if ($lines =~ /$word/){
        print "Found word $word in file ", cwd(). "/$filename\n";
    }
}

This program takes a command line argument that is a word. The program searches for the presence of this word in any file recursively under the current directory. The pipe handle is called LSR.

 

open LSR, "ls -R|";

 

ls -R gives a recursive listing of files under the current directory. The LSR pipe handle returns one file name at a time when read using the read operator <...>. Thus, $filename used in the conditional of the while loop gets one file at a time. If this file is a text file, the program opens it for reading using the filehandle TEXTFILE. The file is read in
list context, and hence @lines is a list containing all the lines in the file in sequence. The lines are joined together into a single string $lines. If the word given as command line argument occurs in the file, the file’s fully qualified name is printed. The Cwd module’s cwd() command is used to print the full name of the current working directory. The output of the program for one particular run is given below.

%lsRFind1.pl open
Found word open in file /home/kalita/cikm.pdf
Found word open in file /home/kalita/cikm1.pdf
Found word open in file /home/kalita/counter.pl
Found word open in file /home/kalita/lsRFind1.pl
ls: ./Math-Cephes-0.25/blib: Permission denied
Found word open in file /home/kalita/lsRFind1.pl
Found word open in file /home/kalita/lsRFind1.pl

Perl also provides a mechanism where the two ends of a pipe can be assigned names—one a producer process and the other a consumer process. It is called a named pipe. We not do discuss named pipes in this book.