How to read from a pipeline with Perl
Submitted by: AdministratorExample 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);
Submitted by: Administrator
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);
Submitted by: Administrator
Read Online Perl Programming Job Interview Questions And Answers
Top Perl Programming Questions
☺ | How do you give functions private variables that retain their values between calls? |
☺ | How many ways can we express string in Perl? |
☺ | How do you match one letter in the current locale? |
☺ | How do I set environment variables in Perl programs? |
☺ | How to open and read data files with Perl |
Top Coding/Programming Categories
☺ | Python Interview Questions. |
☺ | OOP Interview Questions. |
☺ | Software engineering Interview Questions. |
☺ | PHP Interview Questions. |
☺ | VBA (Visual Basic for Applications) Interview Questions. |