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

Please be kind if I am missing something simple, I have almost no experience in Perl.

I was frustrated by the lack of an address bar in my cell phone's mini-browser, so I decided to make a custom homepage on my site and to use a perl script to forward me to the input URL. Here's my form code:
<form action="http://site/cgi-bin/go.cgi" method="GET"> <input type="text" name="browse_url"> <input type="submit"> </form>

And the backend code:

#!/usr/bin/perl use strict; use CGI; my $redirect_url = param('browse_url'); #my $redirect_url = "http://google.com"; #(used for testing) print "<html><head>"; print "<meta http-equiv=\"refresh\" content=\"10;url=$redirect_url\">" +; print "</head><body><a href=\"$redirect_url\">click</a></body></html>\ +n";
My primary problem is that it runs fine on the command line eg. perl go.cgi but not through apache/mod_perl, it gives an internal server error. Any advice would be wonderful.

Replies are listed 'Best First'.
Re: CGI error with simple script
by ptum (Priest) on Jan 10, 2006 at 18:32 UTC

    It is possibly a permissions problem in that the webserver user can't execute your script. Also, consider adding the following code, which may help you identify problems:

    use CGI::Carp qw(fatalsToBrowser);

    Update: Of course, the main thing wrong is that you don't print a header, which the browser needs. Do this before you print anything else:

    print header();

    Another update: Sigh. As [id://tirwhan] pointed out below, if you're using CGI.pm in the function-interface manner, you need to explicitly import the functions. This usually does it (you'll want to check CGI to make sure, though):

    use CGI qw(:standard);

      As ptum has already said, you need to print the HTTP header first. CGI.pm doesn't export the header subroutine into your namespace by default though, so you'll need to do one of these three things:

      use CGI; print CGI::header();
      use CGI qw(:standard); print header;
      use CGI; my $q = new CGI; print $q->header;

      Take a look at perldoc CGI as well, it contains a lot of functions which will make writing your dynamic pages easier.


      There are ten types of people: those that understand binary and those that don't.
        Thanks for the advice from both of you. Now it functions perfectly. I'll take a look into the perldoc, too.
      Thanks for the reply. I added that bit and rechecked the permissions. Unfortunately it still gave the same error and the permissions are 755. It's on my personal server, and there is a test script in the cgi-bin from the install that runs, so it has to be configured correctly.
      Oh, thank you very much.
Re: CGI error with simple script
by wazzuteke (Hermit) on Jan 10, 2006 at 20:01 UTC
    Not to completely gripe, but this topic has probably been covered in here a number of times before.

    As a recommendation, sometimes it is easier/faster/more convinient to run a Super Search on things such as:
    • "cgi", "error", "500"
    • "cgi error 500"
    • etc
    No biggie, just some usefull (relatively) advice I've found to be helpful.

    ---hA||ta----
    print map{$_.' '}grep{/\w+/}@{[reverse(qw{Perl Code})]} or die while ( 'trying' );