use strict;
use IO::Socket;
#initial variables to work with my server
my $host, $port, $request, $proto = 'tcp';
my $connectresponses = 2; #my ftp server responds with 2 lines when you connect.
print "What hostname do you want to connect to? ";
chomp( $host = <STDIN> );
print "What port do you want to use? ";
chomp( $port = <STDIN> );
my $sock = IO::Socket::INET->new(
PeerAddr => $host,
PeerPort => $port,
Proto => $proto
) || die "Failure: $!";
print "Connection to $host on port $port successful!\n";
unless ( $port == 80 ) {
for ( $i = 0; $i < $connectresponses; $i++ ) {
$_ = <$sock>;
print;
}
}
print "Type commands (solely press enter to exit)\n";
&Terminal;
close($sock);
print "\nConnection to $host on port $port was closed successfully!\n";
exit;
#sub to emulate a terminal
sub Terminal {
while (1) {
$request = "";
chomp( $request = <STDIN> );
print $sock "$request\n";
if ( $port == 80 ) {
while (<$sock>) {
print;
}
last;
} else {
unless ($request) {
last;
}
$_ = <$sock>;
print;
}
}
}
来源http://www.perlmonks.org/?abspart=1;displaytype=displaycode;node_id=202955;part=4
错误:
parenthesis missing arounf "my" list at test.pl line 7
print (..) interpreted as function at test.pl line 37
Global symbol "$port" requires explicit package name at test.pl line 7
Global symbol "$request" requires explicit package name at test.pl line 7
Global symbol "$proto" requires explicit package name at test.pl line 7
Global symbol "$port" requires explicit package name at test.pl line 13
Global symbol "$port" requires explicit package name at test.pl line 15
Global symbol "$proto" requires explicit package name at test.pl line 7
test.pl has too many errors.
答案 0 :(得分:2)
当你忽略my
个参数的parens时,它就像一元运算符一样。这意味着
my $host, $port, $request, $proto = 'tcp';
与
相同(my $host), $port, $request, $proto = 'tcp';
您可以使用
my ($host, $port, $request, $proto);
$proto = 'tcp';
或
my ($host, $port, $request);
my $proto = 'tcp';
您也可以使用
my $host, my $port, my $request, my $proto = 'tcp';
但这只是一种奇怪的写作方式
my $host; my $port; my $request; my $proto = 'tcp';
答案 1 :(得分:0)
将您的第7行替换为:
my ($host, $port, $request, $proto);
$proto = 'tcp';
这也有效(如@mpapec所说):
my ($proto, $host, $port, $request) = 'tcp';