I am reading $digit
from infile and want to print it to outfile. But the Perl script is giving an error saying 'Global symbol "$digit" requires explicit package name'. But, if I do declare it globally , $digit=''
, then the this value is printed to the outfile instead of the value extracted/read from infile. Any suggestions as to what should be done?
This how I am doing it:
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);