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

Hi monks,
when I tried run the script below, I got the following error:
Software error:
Can't use an undefined value as a symbol reference at
/home/www/myspace.superihost.com/cgi-bin/script.pl line 16.

What should be corrected?
#!/usr/bin/perl use CGI::Carp qw(fatalsToBrowser); use strict; use URI; use IO::Socket; my $showOpenedSockets=1; my $server = IO::Socket::INET->new ( LocalPort => 8080, Type => SOCK_STREAM, Reuse => 1, Listen => 10); binmode $server; while (my $browser = $server->accept()) { print "\n\n--------------------------------------------\n"; binmode $browser; my $method =""; my $content_length = 0; my $content = 0; my $accu_content_length = 0; my $host; my $hostAddr; my $httpVer; while (my $browser_line = <$browser>) { unless ($method) { ($method, $hostAddr, $httpVer) = $browser_line =~ /^(\w+) +(\S+) + +(\S+)/; my $uri = URI->new($hostAddr); $host = IO::Socket::INET->new ( PeerAddr=> $uri->host, PeerPort=> $uri->port ); die "couldn't open $hostAddr" unless $host; if ($showOpenedSockets) { print "Opened ".$uri->host." , port ".$uri->port."\n"; } binmode $host; print $host "$method ".$uri->path_query." $httpVer\n"; print "$method ".$uri->path_query." $httpVer\n"; next; } $content_length = $1 if $browser_line=~/Content-length: +(\d+ +)/i; $accu_content_length+=length $browser_line; print $browser_line; print $host $browser_line; last if $browser_line =~ /^\s*$/ and $method ne 'POST'; if ($browser_line =~ /^\s*$/ and $method eq "POST") { $content = 1; last unless $content_length; next; } if ($content) { $accu_content_length+=length $browser_line; last if $accu_content_length >= $content_length; } } print "\n\n....................................\n"; $content_length = 0; $content = 0; $accu_content_length = 0; while (my $host_line = <$host>) { print $host_line; print $browser $host_line; $content_length = $1 if $host_line=~/Content-length: +(\d+)/i; if ($host_line =~ m/^\s*$/ and not $content) { $content = 1; #last unless $content_length; next; } if ($content) { if ($content_length) { $accu_content_length+=length $host_line; #print "\nContent Length: $content_length, accu: $accu_content +_length\n"; last if $accu_content_length >= $content_length; } } } $browser-> close; $host -> close; }

Replies are listed 'Best First'.
Re: Error "undefined value as a symbol reference "
by polettix (Vicar) on Apr 22, 2005 at 10:36 UTC
    I don't have the time to try your code, but if line 16 is the one calling binmode $server; I bet it has to do with the fact that you don't check $server validity after the call to IO::Socket::INET->new.

    As a general rule, always check that things dinamically set up are ok before using them.

    Flavio (perl -e 'print(scalar(reverse("\nti.xittelop\@oivalf")))')

    Don't fool yourself.
Re: Error "undefined value as a symbol reference "
by Anonymous Monk on Apr 22, 2005 at 10:36 UTC
    You aren't checking (and taking appropriate action) whether IO::Socket::INET->new returns a true value. Creation of sockets might fail.
      What should be corrected? (it's http-proxy script, I'd used ready code, I am not a very familiar in perl)

        When you open your socket, you can do something else if it didn't open. For instance, if you wanted the program to end immediately:

        my $server = IO::Socket::INET->new ( LocalPort => 8080, Type => SOCK_STREAM, Reuse => 1, Listen => 10) or die ("Can't open socket : $!");

        It's a good idea to check anything that has a potential to fail. (opening files, closing files that you've written to, opening database connections, etc.)

Re: Error "undefined value as a symbol reference "
by cog (Parson) on Apr 22, 2005 at 10:40 UTC
    On a side note, ++ for putting the error in blue. Much easier on the eye.

      On a side note, ++ for putting the error in blue. Much easier on the eye.

      That is really dependent on which theme the user has chosen.
        It's also called sarcasm