Perl:foreach 行,拆分,修改字符串,设置为数组。 Opendir,next 如果 files=modified string。取消链接文件

Perl: foreach line, split, modify the string, set to array. Opendir, next if files=modified string. Unlink files

我在使用以下代码块时遇到问题:$output 是 netstat -lnt | grep“:::60”。特别是评论下的部分 #Create filename format

my @lines = split /^/, $output;
foreach my $line (@lines) {
 my ($garb, $long_ports)  = (split /\s*:::\s*/, $line);

#Get the last 2 digits of the 60XX port number
 my ($garb2, $ports) = (split /60/, $long_ports);

#Split values to numbers 0-9 for correct filename format
if ($ports < 10) {
  my ($garb3, $ports2) = (split /0/, $ports);

#Add 0 since 0 port is split to empty string
if (length($ports2) == 0){
  $ports2 = "0$ports2";
}

#Create file name format
my @locked_ports = ".X$ports2-lock";
 }
}
 my %h = map {$_ => 1 } @locked_ports;
 #open /tmp and find the .X*-lock files that DO NOT match locked_ports
 opendir (DIR, $tmp ) or die "Error in opening dir $tmp\n";
 while (my $files = readdir(DIR)) {
   if (exists $h{$files}){
   next}
   unlink $files;
 }
   closedir(DIR);

我也试过:

#Create file name format
my @locked_ports = ".X$ports2-lock";
 }
}
 #open /tmp and find the .X*-lock files that DO NOT match locked_ports
 opendir (DIR, $tmp ) or die "Error in opening dir $tmp\n";
 while (my $files = readdir(DIR)) {
   next if $files =~ @locked_ports;
   unlink $files;
 }
   closedir(DIR);

并且:

#Create file name format
my $locked_ports = ".X$ports2-lock";
 }
}
 #open /tmp and find the .X*-lock files that DO NOT match locked_ports
 opendir (DIR, $tmp ) or die "Error in opening dir $tmp\n";
 while (my $files = readdir(DIR)) {
   next if $files =~ $locked_ports;
   unlink $files;
 }
   closedir(DIR);

每次我收到类似于以下内容的错误:Global symbol "@locked_ports" requires explicit package nameGlobal symbol "$locked_ports" requires explicit package name

如何让 "next" 的文件名等于 locked_ports 行?

非常感谢任何帮助。

谢谢。

my 将变量范围限定在它所在的最内层块(卷曲)。

{
   my $foo;
   ...
}

# $foo not accessible here.

该块在您创建变量的那一行之后结束。移动变量的声明,使其具有足够大的范围。