Perl 多字符串解析到循环

Perl 多字符串解析到循环,perl,Perl,我目前有一些返回站点标题内容的代码: #!/usr/bin/perl use strict; require IO::Socket; my @header; my $host = shift; my $socket = new IO::Socket::INET( PeerAddr => $host, PeerPort => 80, Proto => 'tcp') || die "Could not Connect $!\n"; pri

我目前有一些返回站点标题内容的代码:

#!/usr/bin/perl 

use strict;
require IO::Socket; 

my @header; 
my $host = shift;

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

print "Connected.\n"; 
print "Getting Header\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";
#/usr/bin/perl
严格使用;
需要IO::Socket;
我的@header;
我的$host=shift;
my$socket=新IO::socket::INET(
PeerAddr=>$host,
皮尔波特=>80,
Proto=>'tcp')| | die“无法连接$!\n”;
打印“已连接。\n”;
打印“获取标题\n”;
打印$socket“GET/HTTP/1.0\n\n”;
我的$i=0;
而{
@标题[$i]=$\u1;
$i++;
} 
$i=0;
打印“--------------------------------------\n”;
而($iReplace

my @header; 
my $host = shift;
...

while(){
chomp(我的$host=$);
我的@header;
...
}

您只需打开文件,将内容读入列表,然后在列表上迭代:

open my $fh, '<', $file or die "$!";
my @ips = <$fh>;
close $fh;
foreach my $ip ( @ips ) {
   chomp $ip;
   ...
}

打开我的$fh,'你真的应该添加
使用警告
,并整理你再次运行代码时收到的所有警告。执行类似
@header[$i]的操作=$$
可能不是您想要做的。只需指出,使用@Ilmari Karonen在此处描述的
|
创建$socket时存在一个微妙的错误
open my $fh, '<', $file or die "$!";
my @ips = <$fh>;
close $fh;
foreach my $ip ( @ips ) {
   chomp $ip;
   ...
}