
piping command output to open()
:
:Hi,
:
:I need to be able to pipe the output of a command to open(). I understand
:how to do this, but is it possible if the command itself contains multiple
:pipes?
:
:Example:
:
:open (MAXNUM, "cat testfile | awk -F: '{print $3}' |");
:
:I want take each line in testfile, pull the third token (delimited by :),
:and have Perl process it. When I do this though, Perl stops
:execution on the command after the first pipe. So all it does is
:'cat testfile', but it doesn't parse out the third token with awk.
:I know I can parse it out inside Perl, but out of curiousity, I'd
:like to know if there's a way to do it like this.
:
:Is there a way around this? I don't want to have to redirect this
:output to any intermediate file.
:
:Environment: Perl 5.000 on Solaris 2.3
:
:thanks for any pointers,
(I bet references would be even better.)
The problem is $3 is interpolating, leaving awk with merely {print}.
You mean:
open (MAXNUM, "awk -F: '{print \$3}' testfile |")
|| die "cannot fork awk: $!";
or
open (MAXNUM, q{awk -F: '{print $3}' testfile |})
|| die "cannot fork awk: $!";
There's (nearly) never any need to cat just one file at something, btw.
For the record, doing it all in perl would be:
open (MAXNUM, "testfile") || die "cannot open tesfile: $!";
while (<MAXNUM>) {
print +(split(/:/))[3], "\n";
}
That silly plus is to inhibit perl from thinking I'd said:
(print split(/:/)) [3], "\n";
which wuldn't have worked well at all.
--tom
--
There is, however, a strange, musty smell in the air that reminds me of
something...hmm...yes...I've got it...there's a VMS nearby, or I'm a Blit.
--Larry Wall in Configure from the perl distribution