简体   繁体   中英

Undefined subroutine CGI::remote_port

Why am I getting this error? server_port() works as intended, afaik REMOTE_PORT is a valid environment variable too.

Are there any alternative ways of getting the client's port number?

Here's my full code:

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

my $time = localtime();
my $dir = cwd();
my $parameter = param('name');

my $q = new CGI;
my $addr = $q->remote_host();
my $request = $q->request_method();
my $port = $q->server_port();


print "Content-type:text/html\r\n\r\n";
print '<html>';
print '<head>';
print '<title>Auth2</title>';
print '</head>';
print '<body>';
print "<h1> The time is $time </h1>";
print "<p> Current directory is $dir</p>";
print "<p> Request parameter: $parameter</p>";
print "<p> Remote address: $addr</p>";
print "<p> Remote port: $port</p>";
print "<p> Request method: $request </p>";
print '</body>';
print '</html>';


1;

The available methods are documented in the man page and it looks like remote_port is not one of them.

afaik REMOTE_PORT is a valid environment variable too.

If it is an environment variable then you can simply access it as one:

my $port = $ENV{REMOTE_PORT};

Apart from that it is not clear why you need the remote port number at all, because it will probably simply be some arbitrary number from the ephemeral port range of the remote system. This was maybe also the reasoning to not provide a method to access it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM