Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Perl Monks. I have a perl script test3.pl. The get function of LWP won't work in the browser until httpd is restarted although it always works in a terminal.

#!/usr/bin/perl # What's Wrong with LWP::Simple::get ? use strict; use LWP::UserAgent; use LWP::Simple; use constant URL => 'http://www.google.com'; main(); exit(0); sub main { print "Content-type: text/html\n\n"; print "Fetching '" . URL . "' with LWP::UserAgent\n<br>\n"; my $ua = LWP::UserAgent->new(); my $response = $ua->get(URL); if ( $response->is_success ) { my $len = length($response->content); print "Retrieved $len bytes\n<br>\n"; } else { my $code = $response->code; print "Failed to retrieve URL: $code\n<br>\n"; } print "Fetching '" . URL . "' with LWP::Simple\n<br>\n"; my $content = LWP::Simple::get( URL ); if ( $content ) { my $len = length($content); print "Retrieved $len bytes\n<br>\n"; } else { print "Failed to retrieve URL\n<br>\n"; } }

Before I restart httpd

In terminal, I run "perl test3.pl", which gives the correct result, i.e. the get function retrieving the URL.

Content-type: text/html Fetching 'http://www.google.com' with LWP::UserAgent <br> Retrieved 9972 bytes <br> Fetching 'http://www.google.com' with LWP::Simple <br> Retrieved 10044 bytes <br>

However, in a browser (Firefox), I access this script and I get the following:

Fetching 'http://www.google.com' with LWP::UserAgent <br> Failed to retrieve URL: 500 <br> Fetching 'http://www.google.com' with LWP::Simple <br> Failed to retrieve URL <br>

The problem persists if I don't restart httpd.

If I restart httpd, the script works fine in the browser, returning the following:

Fetching 'http://www.google.com' with LWP::UserAgent <br> Retrieved 10026 bytes <br> Fetching 'http://www.google.com' with LWP::Simple <br> Retrieved 10008 bytes <br>

Please note httpd was running even before I restart it.

Please advise me of how to figure out the problem, Apache (httpd), mod_perl, or ... . Thank you Perl Monks.

Replies are listed 'Best First'.
Re: LWP get function does not work in browser until httpd is restarted
by Anonymous Monk on Jun 16, 2011 at 06:24 UTC
    Things to try
    • increase logging level for apache and check the logs
    • check for any firewalls on the machine
    • incerase LWP logging, see LWP::Debug
    • use http://WireShark.org and examine the live traffic