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

Hi, all. I use FC5 linux to develop CGI program. The config file of Apache are defaule and I put my script on /var/www/cgi-bin/ folder. I do not modify any config file in FC5 and shutdown the firewall. But I find I can not use the IO::socket module in the apache web environment. The script is below:
#!/usr/bin/perl use strict; use warnings; use IO::Socket; my $pop3 = IO::Socket::INET->new( PeerAddr => "pop3.163.com", PeerPort => 110, Proto => 'tcp', Timeout => 20 ); print "Content-type: text/html\n\n"; print "Result: "; print $pop3;
The permission of the script file is 755, and the path of the file is /var/www/cgi-bin/test_socket.pl

When I run the script in command line. It print the result:

Content-type: text/html Result: IO::Socket::INET=GLOB(0x9314838)
But I run it in apache by the URL: http://192.168.0.200/cgi-bin/test_socket.pl . It only print:

Content-type: text/html Result:
It seem that the value of the $pop3 is undef. So the use of IO::Socket is failed on apache.

Why have this problem. How can I get the rignt result?

DuanXuepeng.

Replies are listed 'Best First'.
Re: IO::Socket error in CGI on apache
by gellyfish (Monsignor) on May 25, 2006 at 08:11 UTC

    There could be a number of reasons for this, most of which are outside the control of the Perl program. In the first instance you should print out the actual error message from the system in the case where the returned socket is undef. You could do something like:

    if ( $pop3 ) { # do something with the socket } else { print "error in socket: $!"; }

    If you are trying to create a POP3 client you might be better off using a pre-existing module such as Net::POP3 or Mail::POP3Client which will do all the work for you.

    /J\

      I only take pop3 for example. Actually any protocol which are right in command line use can not work correctly in my apache cgi environment . Like this 80 port:

      #!/usr/bin/perl use strict; use warnings; use IO::Socket; my $http = IO::Socket::INET->new( PeerAddr => "mail.163.com", PeerPort => 80, Proto => 'tcp', Timeout => 20 ); print "Content-type: text/html\n\n"; print "Result: "; print $http;