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.


In reply to LWP get function does not work in browser until httpd is restarted by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.