Perl failure with "main::$fn" used only once:" in error logs

Hi all,

Can anyone guess why this is happening? I am new to Perl, so please help me to fix this error:

  • I have a static html file which calls the cgi-perl script.
    HTML Code:
<html>
<head>
        <title> Hey Dude! </title>
</head>

<body>
        <form method="POST" action="/cgis/dude.pl">
        <p><input type="text" name="name" size="49"></p><br>
        <p><input type="submit" name="submit" value="GO!"></p><br>
        </form>
</body>

</html>

and my CGI-PERL code is:

#!/usr/bin/perl -w
 # Read the standard input (sent by the form):
read(STDIN, $form, $ENV{'CONTENT_LENGTH'});
# Get the name and value for each form input:
@pairs = split(/&/, $form);
# Then for each name/value pair....
foreach $pair (@pairs) {
    # Separate the name and value:
    ($name, $value) = split(/=/, $pair);
    # Convert + signs to spaces:
    $value =~ tr/+/ /;
    # Convert hex pairs (%HH) to ASCII characters:
    $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
    # Store values in a hash called %FORM:
    $form{$name} = $value;
}
#This will generate XHTML-compliant page headers
sub start_html()
{
        print qq~ 
        <?xml version="1.0" encoding="iso-8859-1"?>
        <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
        <html xmlns="http://www.w3.org/1999/xhtml">

        <head>
                <title> Cookies </title>
    
        </head>
        <body>
        ~;  
}

if ($form{'submit'} eq "submit")
    {
    #Start hearders
    print "Set-Cookie: name=$form{'name'}\n";
    print "Content-Type: text/html\n\n";
    #end of headers
    &start_html();
    print "Nice to meet you $form{'name'}", ". I hope to see you again soon";
    }
else
{
    $old_cookie = $ENV{"HTTP_COOKIE"};
    
    #if cookie was found
    if ($old_cookie)
    {
    ($cookie_name, $name) = split(/=/, $old_cookie);
    #start headers
    print "Content-Type: text/html\n\n";
    #end of headers
    &start_html();
    print "Hey! I know you dude! You are $name";
    print "</body></html>\n";
    }
#cookie not fond
else
    {
    # redirect user to the form with a redirect header
    print "Location:http://mydomain.com/dude.html\n\n";
    }
}

Error in log_error:

[client 10.10.192.168] Name "main::cookie_name" used only once: possible typo at /dir/cgi-bin/dude.pl line 49., referer: http://mydomain.com/dude.html

Beginners Intro to Perl - Part 6 - Perl.com says:

  1. Name ``main::filename'' used only once: possible typo at ./a6-warn.pl line 3. and Name ``main::fn'' used only once: possible typo at ./a6-warn.pl line 4. Perl notices that $filename and $fn both only get used once, and guesses that you've misspelled or misnamed one or the other. This is because this almost always happens because of typos or bugs in your code, like using $filenmae instead of $filename, or using $filename throughout your program except for one place where you use $fn (like in this program).

You can run CGI on the command line, just set the variables it reads.

Quick fix:
replace the unreferenced variable by undef

    ($cookie_name, $name) = split(/=/, $old_cookie);

by

    # ($cookie_name, $name) = split(/=/, $old_cookie);
   (undef, $name) = split(/=/, $old_cookie);

I forget the exact rules, but CGI and cron can be fussy about output on stdout, stderr and exit status. I see lots of error printouts on web returns, presumably from programmers who might have a hard time finding the httpd log.