我正在阅读infile并想将其打印$digit
到outfile。但是 Perl 脚本给出了一个错误,提示“全局符号“$digit”需要显式包名”。但是,如果我确实将其声明为 global ,那么 this 值将打印到outfile而不是从infile中提取/读取的值。关于应该做什么的任何建议?$digit=''
这就是我的做法:
my $digit='';
open (DATA, "</usr/infile") || die "cant open infile\n"; #file from digit has to read
while (<DATA>){
($digit)= $_=~ /\s9\s(\d+)/; #regex to capture digit '234' from ' 9 234'
if ($digit ne ""){
print "digit is $digit\n"; # this prints fine
}
}
open (FILE, ">/usr/outfile") || die "cant open outfile\n"; #file to which digit has to be finally written
print FILE "9 $digit"; #$digit takes in the value declared globally i.e. ''
close(DATA);
close (FILE);