问题描述:

I am forking a process in Perl, actually it is a wget command. Wget writes some data to a file. I want to capture the output (not the file) which wget has generated and bring it to the main program. Here is my code snippet I am working on:

my $pid;

my @wgetDump;

my $videoFileName = "abc";

my $fileURL = "http://www.youtube.com/watch?v=Y8NI2qUZ1co&feature=relmfu";

if ($pid = fork) {

####Parent Process

print "Child Process ID: $pid";

} else {

####child process

@wgetDump = `wget -O $videoFileName -c \"$fileURL\" 2>&1`;

}

foreach (@wgetDump) {

### Here it want to get the @wgetDump Data which is actually the output of child process.

### But I am not getting anything here.

}

Can any one please suggest.

网友答案:

First, why are you using wget? Why not use LWP::UserAgent to get the web page? Then you don't need to read the file afterwards, you already have the data.

In your code, only the child process will get the wget data, so do your processing in the child process. If you really want to pass it back to the parent process, you'll need IPC. Consider using IPC::Open2. But I would use LWP and process the data in the child.

相关阅读:
Top