LisaH
LisaH

Reputation: 173

Old Perl Script now with Error-: Name Used only once

I inherited an old perl script that takes search terms from various forms, and processes them.

I see now in our logs that the script is throwing the following error: mastersearch.cgi: Name "main::error_fields" used only once: possible typo at /var/www/cgi-bin/mastersearch.cgi line 203.

I've indicated the error line below, and here it is out of context:

 ($error,@error_fields) = @_;

I've read up on only using a variable once,so it would seem that this line, the array error fields is used only once. But why isnt that ok?

Can I use strict to correct this?Ive read other stack questions, but dont see how they relate, esp:

"Used only once" warnings can be generated when autodie or Fatal is used with package filehandles (eg, FILE ). Scalar filehandles are strongly recommended instead.

My questions are then:

and of lesser importance:

Thank you in advance

#!/usr/bin/perl -w
use CGI::Carp qw(fatalsToBrowser);

# @referers allows forms to be located only on servers which are defined 
# in this field.  This prevents other servers from using your SearchWWW script.

@referers = ('http://ourname.edu');

# Check Referring URL
&check_url;

# Parse Form Contents
  &parse_form;

# Submit to search engine
  &search;

sub check_url {
  if ($ENV{'HTTP_REFERER'}) {
    foreach $referer (@referers) {
      if ($ENV{'HTTP_REFERER'} =~ /$referer/i) {
      $check_referer = '1';
      last;
      }
   }
}
else {
  $check_referer = '1';
}

if ($check_referer != 1) {
   &error('bad_referer');
  }
}

sub parse_form {
    if ($ENV{'REQUEST_METHOD'} eq 'GET') {
     # Split the name-value pairs
     @pairs = split(/&/, $ENV{'QUERY_STRING'});
   }
   elsif ($ENV{'REQUEST_METHOD'} eq 'POST') {
     # Get the input
       read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
     # Split the name-value pairs
       @pairs = split(/&/, $buffer);
   }
   else {
      &error('request_method');
   }

foreach $pair (@pairs) {
    ($name, $value) = split(/=/, $pair);
    $value =~ tr/+/ /;
    $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
    $value =~ s/<!--(.|\n)*-->//g;
    $contents{$name} = $value;
   }
}

sub search {
    # Assign the value from search to search_for
   $search_for = $contents{'search'};

    # Places a plus between two words
    $search_for =~ tr/ /+/;

    # If no engine entered goto Google by default
    if ($contents{'location'} eq "15"){
     print "Location: http://ourname.edu/search?q=$search_for";
    }

    if ($contents{'location'} eq "11"){
        print "Location:http://ourname.edu/search~S9/?searchtype=X& searcharg=$search_for&searchscope=39\n\n"
     }
 # ....snipped as redundant for question- more locations here
 #resume

 exit;
}

  sub error {
  # -->the error line, 203,  is below
    ($error,@error_fields) = @_;
    print "Content-type: text/html\n\n";
    if ($error eq 'bad_referer') {
        print qq~ 
        <html>\n <head>\n  <title>Bad Referrer - Access Denied</title></head>\n
    <body>\n  <center>\n   <h1>Bad Referrer - Access Denied</h1></center>\n
    The form that is trying to use this SearchWWW Program</a>\n
    resides at: $ENV{'HTTP_REFERER'}, which is not allowed to access this cgi script.<p>\n
    Sorry!\n
    </body></html>\n ~;
}
 else {
    print qq~
    <html>\n <head>\n  <title>Error: Request Method</title>\n </head>
    </head>\n <body>
    <center>\n\n
    <h1>Error: Request Method</h1>\n  </center>\n\n
    The Request Method of the Form you submitted did not match\n
    either GET or POST.  Please check the form, and make sure the\n
    method= statement is in upper case and matches GET or POST.\n
    </body></html>\n ~;
    }
    exit;
}

Upvotes: 0

Views: 385

Answers (1)

ThisSuitIsBlackNot
ThisSuitIsBlackNot

Reputation: 24063

First, that's a warning, not an error. It doesn't halt execution of the script, it simply tries to alert you that something may be wrong and that you should take a look at it.

To get more detailed information about warnings, add use diagnostics; to the top of your script. In this case, you'll see:

Typographical errors often show up as unique variable names. If you had a good reason for having a unique name, then just mention it again somehow to suppress the message...

Since you never use @error_fields after the initial assignment, Perl thinks you might have made a typo and warns you about it. There's no reason to assign to @error_fields if you never do anything with it, so you can simply remove it from that line:

($error) = @_;

But there are a lot of other issues with that script (e.g. no use strict;). I would dump it.

Upvotes: 3

Related Questions