http://qs321.pair.com?node_id=1100781

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

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; } } }
ERRORS:
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 lin +e 7 Global symbol "$proto" requires explicit package name at test.pl line +7 Global symbol "$port" requires explicit package name at test.pl line 1 +3 Global symbol "$port" requires explicit package name at test.pl line 1 +5 Global symbol "$proto" requires explicit package name at test.pl line +7 test.pl has too many errors.

Replies are listed 'Best First'.
Re: whats wrong? it gives me many error
by LanX (Saint) on Sep 16, 2014 at 16:15 UTC
    Additionally to the answers you already got on SO you should use my for all variables including $i .

    have a look at use diagnostics to improve error messages (and read them ;-)

    use strict; use warnings; use diagnostics; for $i (0..10) { print $i; }

    read the part about lexically scoped variables:

    Global symbol "$i" requires explicit package name at /tmp/tst2.pl line + 6. Global symbol "$i" requires explicit package name at /tmp/tst2.pl line + 7. Execution of /tmp/tst2.pl aborted due to compilation errors (#1) (F) You've said "use strict" or "use strict vars", which indicates + that all variables must either be lexically scoped (using "my" or +"state"), declared beforehand using "our", or explicitly qualified to say which package the global variable is in (using "::"). Uncaught exception from user code: Global symbol "$i" requires explicit package name at /tmp/tst2.pl +line 6. Global symbol "$i" requires explicit package name at /tmp/tst2.pl line + 7. Execution of /tmp/tst2.pl aborted due to compilation errors. at /tmp/tst2.pl line 10

    if you don't know what lexically scoped is, have a look at Coping with Scoping

    Cheers Rolf

    (addicted to the Perl Programming Language and ☆☆☆☆ :)

    PS: crossposting provokes downvotes! :)

Re: whats wrong? it gives me many error
by toolic (Bishop) on Sep 16, 2014 at 14:44 UTC
Re: whats wrong? it gives me many error
by GotToBTru (Prior) on Sep 16, 2014 at 14:49 UTC

    I would start by reading the error messages.

    1 Peter 4:10
Re: whats wrong? it gives me many error
by blue_cowdawg (Monsignor) on Sep 16, 2014 at 18:10 UTC

    Greetings Monk,
    Welcome to Perl Monks. Since this is your first day in the Monastery you may want to consider reading this tutorial as well as this one just as an overview of how to use the Monastery.

    Getting down to more brass tacks let me also suggest this tutorial.

    Even more to the point it would seem that you are struggling with the concept of context within Perl. This tutorial will help with that.

    To give you fish for the day let me say that this:

    my $host, $port, $request, $proto = 'tcp';
    is not doing what you think it is and this
    my ($host, $port, $request, $proto) = ('tcp');
    might work better for you.

    Let me finish by saying that posting a question on one forum, getting answers and then posting on another forum is not a great way to make friends and influence people. A lot of the monks here visit multiple forums and it doesn't set well to see the same questions asked multiple places after they've been answered.


    Peter L. Berghold -- Unix Professional
    Peter -at- Berghold -dot- Net; Blog: http://blog.berghold.net Warning: No political correctness allowed.
      my ($host, $port, $request, $proto) = ('tcp');

      But that assigns 'tcp' to $host. Shouldn't it be assigned to $proto?

      So, maybe one of these:

      my ($host, $port, $request, $proto) = ( undef, undef, undef, 'tcp' ); # or my ($host, $port, $request, $proto) = ( '', '', '', 'tcp' ); # or my ($host, $port, $request, $proto); $proto = 'tcp';
        > my ($host, $port, $request, $proto) = ('tcp');

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

        But ikegami already showed this on SO... :)

        Cheers Rolf

        (addicted to the Perl Programming Language and ☆☆☆☆ :)

        Personally I'd go with the latter.


        Peter L. Berghold -- Unix Professional
        Peter -at- Berghold -dot- Net; Blog: http://blog.berghold.net Warning: No political correctness allowed.