nwboy74 has asked for the wisdom of the Perl Monks concerning the following question:
Why isn't this working? When I run it, it hangs when it gets to "print WRITER $content". This is a simplified version of a bigger program I'm troubleshooting. Disregard the reason I'm forking or whatnot.
use strict;
use warnings;
use LWP::UserAgent;
use HTTP::Request;
use HTTP::Response;
use HTTP::Message;
my $ua = LWP::UserAgent->new();
my $iteration = 1;
open(URLS, "SomeFileOfURLs");
while (<URLS>) {
my $url = $_;
chomp $url;
&get_url($url, $iteration);
print "($iteration) $url\n";
sleep 15;
$iteration++;
}
close URLS;
sub get_url {
my ($url, $iteration) = @_;
pipe(READER, WRITER);
if (my $pid = fork) {
close WRITER;
waitpid($pid, 0);
my @data = (<READER>);
open(FH, ">SomeDirectory\\$iteration");
print FH @data;
close READER;
close FH;
} elsif (defined $pid) {
close READER;
my $request = HTTP::Request->new("GET", $url);
my $response = $ua->request($request);
my $content = $response->content();
print WRITER $content;
undef $request;
undef $response;
close WRITER;
exit;
}
}
The program is supposed to take a url, download the page from the Internet, then return the results.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Forking Issue
by ikegami (Patriarch) on Oct 20, 2010 at 21:30 UTC | |
by nwboy74 (Novice) on Oct 20, 2010 at 22:12 UTC | |
|
Re: Forking Issue
by SuicideJunkie (Vicar) on Oct 20, 2010 at 19:20 UTC | |
by nwboy74 (Novice) on Oct 20, 2010 at 20:12 UTC |