Scalar value errors (with IO :: Socket)

This is my current code:

#!/usr/bin/perl -w use strict; require IO::Socket; while (<>) { chomp(my $host = $_); my @header; print "Connecting to: $host\n"; my $socket = new IO::Socket::INET( PeerAddr => $host, PeerPort => 80, Proto => 'tcp') || print "Could not connect: $!\n"; print "Connected.\n"; print $socket "GET / HTTP/1.0\n\n"; my $i = 0; while (<$socket>) { @header[$i] = $_; $i++; } $i = 0; print "--------------------------------------\n"; while ($i <= 8) { print "@header[$i++]"; } print "-------------------------------------\n"; print "Finished $host\n\n"; } 

If, when I go through the list of IP addresses and the host is denied, instead of continuing with the next IP address, it will give me the error "Cannot use string (" 1 ") as a ref character, whereas" strict refs "in use".

If I then changed @header [$ i] = $; to $ header [$ i] = $; I am also getting the same error. How can I make this script better.

+4
source share
2 answers

Wouldn't it be a simple decision to use a version with a lower priority of "or" that has a lower priority than "="?

 my $socket = new IO::Socket::INET( PeerAddr => $host, PeerPort => 80, Proto => 'tcp') or print "Could not connect: $!\n"; 

In fact, the 'or' and 'xor' operators have the lowest operator priority (see perlop).

0
source

Source: https://habr.com/ru/post/1395702/


All Articles