Perl Programming Question:

Download Job Interview Questions and Answers PDF

How to read from a pipeline with Perl

Perl Programming Interview Question
Perl Programming Interview Question

Answer:

Example 1:

To run the date command from a Perl program, and read the output
of the command, all you need are a few lines of code like this:

open(DATE, "date|");
$theDate = <DATE>;
close(DATE);

The open() function runs the external date command, then opens
a file handle DATE to the output of the date command.

Next, the output of the date command is read into
the variable $theDate through the file handle DATE.

Example 2:

The following code runs the "ps -f" command, and reads the output:

open(PS_F, "ps -f|");
while (<PS_F>) {
($uid,$pid,$ppid,$restOfLine) = split;
# do whatever I want with the variables here ...
}
close(PS_F);

Download Perl Programming Interview Questions And Answers PDF

Previous QuestionNext Question
How do I send e-mail from a Perl/CGI program on a Unix system?Why is it hard to call this function: sub y { "because" }