PERL - Use of uninitialized value in pattern match (m//)

I have written a PERL script to read files from directory that the filename contains OT. It then takes each line of each file and prints the first 5 characters before the first occurence of a /.

Currently I am getting the error:

Use of uninitialized value in string at rowGrab.pl line 43.
Use of uninitialized value in pattern match (m//) at rowGrab.pl line 41.

#!/usr/bin/perl
use strict;
my $log_dir = '/home/user1';
my $trans_dir = '/data/directoy1';
my $file;
my $participant;
my @files;
my $line;
 
if (!open(LOG, " >>$log_dir/trans.log")) {
   syslog("Warning:","Cannot open log file called: $log_dir/trans.log");
   die "Cannot open the file $log_dir/trans.log, $!";
}
undef (@files);
opendir(DIR,$trans_dir);
my @files = grep {
            /(OT)/             # Filename contains OT
            && -f "$trans_dir/$_"   # and is a file
        } readdir(DIR);
foreach $file (@files) {
   print STDERR "File: $file\n";
   open(FILE,"<$trans_dir/$file") or die "Cannot open $file";
      print "$file\n";
   for $line (<FILE>) {
      ($participant) =~/(.....)\//;
      print "$participant";
      close (FILE);
   }
}
closedir(DIR);
 

Can anyone provide some guidance?

Also prior to putting in string matching, I just printed the filename. This however got the error "Out of memory".

The directory contains 5487 files and each file averages 119 MB.

I understand this is a genuine memory issue, I was just wondering if anyone knew a way round it?

Thanks

Chris

This cannot be your actual script. There are no lines 41 and 43.

Hi,

You are partly correct, I had some debug lines in which I removed to make it neater for posting.

The script actually errors on lines 25 and 26 now.

Thanks

Chris

I think this is what you mean:

   for $line (<FILE>) {
      if ($line =~ /(.....)\//) {
        $participant = $1;
      }
      print "$participant";
   }
   close(FILE)

And don't close(FILE) inside the for-loop. If you do so, perl will not be able to read from the file in next iteration.