Remember you can always check the syntax of a perl script using the -c
option!

perl -c myscript.pl

One thing that frequently gets over looked is that you can run a script
through perl to check the syntax of a script.  This quick check will point
out all sorts of details that are hard to see when running the script
through CGI or when using perl in an embeded situation.

Bruce W. Lowther
Micron Technology, Inc.
Boise, Idaho

-----Original Message-----
From: Dan Brown [mailto:[EMAIL PROTECTED]]
Sent: Thursday, April 19, 2001 4:23 PM
To: Pam Derks
Cc: [EMAIL PROTECTED]
Subject: Re: CGI problem, no luck so far


Is this really the exact script?  The reason I ask is that you shouldn't
even get as far as to get the error you present ("Premature end of
script headers").  There is an extra curley brace at the end that should
have caused the Perl compiler to croak with something like

        Unmatched right curly bracket at pam_derks.pl line 59, at end of
line
        syntax error at pam_derks.pl line 59, near "}"
        Execution of pam_derks.pl aborted due to compilation errors.

Try running it from command line and see what you get.

There are a couple of pointers I can give you.

1. I strongly suggest using the pragma

       use strict;

   It will force you to write less error-prone code of which you will be
glad of later.

2. Please read the documentation on the CGI module.  Based on the code
provided, you're reinventing the wheel.

If the code has

       print $cgi->header;  # print this before printing *anything*
else!

there's no need to do this

       print "Content-type: text/html\n\n";

since the 'header' routine does it for you.  By the way, for a CGI
program printing the "Content-type" alone it will do fine also.

If you're not going to use the CGI module for anything other than
printing the header, there's no need to include it.  But, I *strongly*
recommend using it since it'll save you a great deal of effort.  

For example, using the CGI module the FormInput routine can be
completely removed.  The CGI module gives you access to all input,
regardless of how it came to the script (request_method), via the param
routine.  Using CGI to its full, the size of the script is cut in half.

      1      #!/usr/bin/perl -w
      2
      3      use strict;
      4      use CGI qw( :standard );
      5
      6      #------- Start User Configuration
----------------------------
      7      my $main = 'xxx.berkeley.edu';
      8      #---End User Configuration
-----------------------------------
      9
     10      # Declare/Initialize the variable
     11      #
     12      my $goto = '';
     13
     14      # Assign the variable a new value if that new value was
passed in
     15      # If it was not passed in, $goto will have the value of ''
     16      #
     17      $goto = param( 'goto' ) if( defined param( 'goto' )  );
     18
     19      if( $goto eq '' )
     20      {
     21          # CGI::redirect will do everything you need to redirect
     22          #
     23          print redirect( "http://xxx.berkeley.edu/cat/$main" );
     24      }
     25      else
     26      {
     27          print redirect( "http://xxx.berkeley.edu/cat/$goto" );
     28      }
     29
     30      exit;

I hope this helps.
Dan


Pam Derks wrote:
> 
> Thanks all for your suggestions regarding earlier post (below):
> 
> Am testing out a new web server running SunOS5.8 " and I can't get any of
my cgi scripts to run.  I  get this error message:
>  "Premature end of script headers"
>   the cgi scripts worked perfectly on the old server which was running
Digital  UNIX.
> 
> ------------------
> 
> Here's a more info, as I've tried several things and still am having no
luck.
> 
> New web server:
>  perl version is 5.005_03 for sun4-solaris
> Running Apache
>  (not exactly sure which version but I THINK  this is a newer version than
other machine))
> 
> Old web server:
> perl version is 5.004_04 for alpha-dec-osf
> Running Apache
> 
> file permissions are set to 755 on the cgi script
> path to perl is correct
> error message has remained the same:
>  "Premature end of script headers"
> 
> The script is very simple and I'm sure can be written better, but it
worked on the other web server. it's a simple script that activates the "Go"
button from a pull down menu on a web page.
> 
> Here's the CGI script that won't run, on new web server.
> 
>  #!usr/bin/local/perl -w
>      2
>      3
>      4
>      5  #------- Start User Configuration ----------------------------
>      6  $main = 'xxx.berkeley.edu';
>      7  #---End User Configuration -----------------------------------
>      8
>      9
>     10  use CGI;
>     11
>     12    $cgi = new CGI;
>     13
>     14    print $cgi->header;  # print this before printing *anything*
else!
>     15  print "Content-type: text/html\n\n";
>     16
>     17  &FormInput(*input);
>     18
>     19  if($input{'goto'} eq "")
>     20     {
>     21      print ("Location: http://xxx.berkeley.edu/cat/$main\n\n");
>     22     }
>     23  else
>     24      {
>     25      print ("Location:
http://xxx.berkeley.edu/cat/$input{'goto'}\n\n");
>     26      }
>    27
>     28  exit;
>     29  #-------------------------------------------------------------
>     30  # FormInput: Function
>     31  #-------------------------------------------------------------
>     32  sub FormInput
>     33  {
>     34  local (*qs) = @_ if @_;
>     35
>     36  if ($ENV{'REQUEST_METHOD'} eq "GET")
>     37          {
>     38          $qs = $ENV{'QUERY_STRING'};
>     39          }
>     49          $qs[$i] =~ s/\+/ /g;
>     50          $qs[$i] =~ s/%(..)/pack("c",hex($1))/ge;
>     51
>     52          ($name,$value) = split(/=/,$qs[$i],2);
>     53
>     54          if($qs{$name} ne "")
>     55                  {
>     56                  $qs{$name} = "$qs{$name}:$value";
>     57                  }
>     58          else
>     59                  {
>     60                  $qs{$name} = $value;
>     61                  }
>     62          }
>     63
>     64  return 1;
>     65  }
> 
> thanks, Pam :(

Reply via email to