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

Hello.

I am a 100% perl newbie but am learning. My school has a few kiosk-type computers that only allow sites w/in the school domain to be viewed. This always bugged me so i thought it might be somewhat fun to write a perl cgi that would fetch a page and display it back on the school's server, thus bypassing the filter.

So... what i have is very simple but it works in a terminal but no when i try to view it in a browser... it says "internal server error" which really helps me out. Here we go...

#!/usr/bin/perl -w
use LWP::Simple;

my $PAGE="http://www.slashdot.org";

$_ = get $PAGE or die "can't fetch page!";
print $_;

Any help would be excellent, as this simple problem really bugs me...

Replies are listed 'Best First'.
Re: very simple question...
by Trimbach (Curate) on Nov 11, 2001 at 09:05 UTC
    Using use CGI::Carp qw(fatalsToBrowser carpout); is a great idea in general, but it won't help in this case. If the program works from the command line but you get an "Internal Server Error" from the browser your Top Two things to look for are 1) permissions... make sure the CGI is executable by "all", and 2) Make sure your script outputs some sort of valid header before it tries to output data. That's what the "print header;" does... if #1 and #2 are good to go, and the program compiles, then you can start hunting run-time errors, which CGI::Carp will gladly and happily let you find.

    Gary Blackburn
    Trained Killer

Re: very simple question...
by George_Sherston (Vicar) on Nov 11, 2001 at 06:07 UTC
    I've never used LWP::Simple, so there may be something else going on, but one thing I think is, you need to print a header, to tell the browser to expect html. The easiest way to do this is
    use CGI qw(:standard); print header;
    (It may seem like a sledgehammer to crack a nut, using a big module like CGI.pm just to print out a little old header, but... it does get the nut cracked. I used to get really bored typing print "content-type:text/html\n\n";.)

    One other thing - you say all you get in the way of an error message is internal server error? Then CGI:Carp is your friend. Add these lines:
    use CGI::Carp qw(fatalsToBrowser warningsToBrowser); print header; warningsToBrowser(1);
    ... then it'll send you some more informative stuff, delivered straight to your browser (or View Source to see the warnings).

    § George Sherston
Re: very simple question...
by Anarion (Hermit) on Nov 11, 2001 at 14:14 UTC
    You forgot to print the header before printing the document, try it at the console first and if it works without calling it with "perl file", add the next line, perhaps you didn't have the LWP, you can download it from CPAN, and install it, at least at your home and refer to it with use lib 'path':
    print "Content-type:text/html\n\n";

    before printing the page. Did you think of saving the pages in files ?

    $anarion=\$anarion;

    s==q^QBY_^=,$_^=$[x7,print

Re: very simple question...
by dockthepod (Acolyte) on Nov 12, 2001 at 04:26 UTC
    Hey guys,

    Thanks for the help... I found out that I had commited the ultimate perl sin: my shebang line was wrong!!! The path to perl on the web server is different then on the machine i have shell access on; that's why the script ran in the terminal and not on the server. I just dropped a local in the path and it worked... so now i get to go a step further and make it replace the links to pass through my script and everything will be great.

    This really isn't as michevious as it sounds. I'm a sysadmin here so I don't have anything to worry about...

    josh

Re: very simple question...
by Elliott (Pilgrim) on Nov 12, 2001 at 00:29 UTC
    I do this a lot and I agree with he last comment. You must print the Content-type first. The puzzle to me is why it worked on the kiosk.