0
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;
        }
    }
}

source http://www.perlmonks.org/?abspart=1;displaytype=displaycode;node_id=202955;part=4

Error:

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.
Miller
  • 34,962
  • 4
  • 39
  • 60
yenne
  • 1
  • 1

2 Answers2

2

When you omit parens around my's arguments, it acts as unary operator. That means

my $host, $port, $request, $proto = 'tcp';

is the same as

(my $host), $port, $request, $proto = 'tcp';

You could use

my ($host, $port, $request, $proto);
$proto = 'tcp';

or

my ($host, $port, $request);
my $proto = 'tcp';

You could also use

my $host, my $port, my $request, my $proto = 'tcp';

but that's just a weird way of writing

my $host; my $port; my $request; my $proto = 'tcp';
ikegami
  • 367,544
  • 15
  • 269
  • 518
  • 3
    `my ($proto, $host, $port, $request) = 'tcp';` – mpapec Sep 16 '14 at 14:41
  • 1
    Had to look for a minute, to figure out why that works. Which if I'd spotted that you'd moved `$proto` to the front would have been a lot quicker ;). – Sobrique Sep 16 '14 at 15:08
0

Replace your 7th line by:

my ($host, $port, $request, $proto);
$proto = 'tcp';

Also this works (like @mpapec says):

my ($proto, $host, $port, $request) = 'tcp';
Miguel Prz
  • 13,718
  • 29
  • 42