http://qs321.pair.com?node_id=430544

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

Hi, I will try to write a small program which
1) get a webpage from a url
2) check some patterns or do sth.
3) display the result to user via browser.

However, I had problems dealing with the display of feedback. So I tested this small code:
#-------------------------------- #! /usr/bin/perl -w use CGI; use LWP::Simple; print "Content-type: text/html\n\n"; $url = "http://www.yahoo.com"; my $webpage = get($url); print $webpage; print<<_HTMLEND; <HTML> <BODY> <H1> hello </H1> </BODY> </HTML> _HTMLEND #-----------------------------------------
It does work if I run it in the console using "perl test.pl", but it is not working as a cgi program, basically I cannot display anything in the browser, even it is just a "hello" here.
Anything wrong? or my idea is wrong
Can anyone help me? Thank you<

Edit by castaway - Added code tags

Replies are listed 'Best First'.
Re: about use LWP::Simple
by chromatic (Archbishop) on Feb 13, 2005 at 07:59 UTC

    What does your error log say? What happens when you run the program on the server from the command line?

    If neither of those questions make sense to you, I recommend reading the unpleasantly named but very informative The Idiot's Guide to Solving Perl CGI Problems.

    If that doesn't lead you to a solution, please do reply to your post here with answers to some of the questions, especially those in the error log.

Re: about use LWP::Simple
by holli (Abbot) on Feb 13, 2005 at 08:55 UTC
    add
    use CGI::Carp qw(fatalsToBrowser);
    at the top of your script. So you can see any errors that arise in your browser window.

    holli, /regexed monk/
Re: about use LWP::Simple
by dws (Chancellor) on Feb 13, 2005 at 18:37 UTC

    It does work if I run it in the console using "perl test.pl", but ...

    But does it work if you do "./test.pl"? The executable bit must be set on the file for it to work as at CGI, on Unix at least. Try

    % chmod 755 test.pl % ./test.pl

Re: about use LWP::Simple
by johnnywang (Priest) on Feb 13, 2005 at 08:18 UTC
    The code itself looks ok, you didn't say whether your CGI setup actually works at all, does the following work for you?
    #!/usr/bin/perl -w print "Content-type: text/html\n\n"; print<<_HTMLEND; <HTML> <BODY> <H1> hello </H1> </BODY> </HTML> _HTMLEND
      This works ---> it will run as a cgi program and display 'hello' in the browser; now, if I add one line in the beginning use LWP::Simple; it will work in the command line, but if I try to display 'hello' in the browser, it will automatically redirect to some 'foo.example.com' and tell me 'This page cannot display'. Thanks to others for all the replies. I reply this one as it is easier for me to describe the problem.
Re: about use LWP::Simple
by geektron (Curate) on Feb 13, 2005 at 17:05 UTC
    aside from checking the error log for the causes, there is one additional 'problem' and one way to make things easier:

    1) if you're already sending a completed page to the browser, attempting to send another one ( the  <html> ... </html> page in your HERE doc ) may not display ... the fetched page already has the same tags.

    2) you can make things easier with  getprint( $url );.

Re: about use LWP::Simple
by Paulster2 (Priest) on Feb 14, 2005 at 01:03 UTC

    Something that I didn't see mentioned above was if your web server is set up right. I was trying to get a script to work a while back that wouldn't display in a browser. Come to find out that I had to tell apache that it was alright to allow scripts to run and then display to the screen. I could run it from the command line and get the output that I was looking for, but when I tried to launch it into the browser, all I got back was the script itself. I finally figured this out after asking several people about it.

    If you think that this might be the case, check the Apache (or what ever web server you might be using) documentation to see how to set it right. I am not remembering off the top of my head what is needed, but remember it was a pretty simple fix when it got right down to it.

    Paulster2

    You're so sly, but so am I. - Quote from the movie Manhunter.