Perl:foreach line,split,修改字符串,设置为array. Opendir,next if files=modified string.取消链接文件

iovurdzv  于 2023-06-06  发布在  Perl
关注(0)|答案(1)|浏览(157)

我遇到了以下代码块的问题:其中$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
我怎样才能让while“next”覆盖等于locked_ports行的文件名?
任何帮助非常感谢。
谢谢

iszxjhcz

iszxjhcz1#

my将变量的范围限定到它所在的最内部的块(curlies)。

{
   my $foo;
   ...
}

# $foo not accessible here.

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

相关问题