in reply to Re: Hitting a moving target (transient files)
in thread Hitting a moving target (transient files)

You still seem to be under the mistaken impression that "less code" means "faster". Several of your code reductions will make the code slower. The only change that I see potentially making your code much faster is using slurping (which means the files must be fairly small). Given that, I'd do:

#!/usr/bin/perl -w use strict; my $Directory= "/var/spool/mail"; my $BadDir= "bad/"; my $BadString= ("\0"x30)."\t\0\0(\0"; my $BadCount= 10; my $ExceptionsFile= "exclude"; my $ExceptionsUpdated; my %Exception; my $SleepLength= 1; chdir( $Directory ) or die "Can't chdir to $Directory: $!\n"; opendir DIR, "." or die "Can't open directory, $Directory: $!\n"; undef $/; while( 1 ) { if( $ExceptionsUpdated != -M $ExceptionsFile ) { open EXCEPT, "< $ExceptionsFile" or die "Can't read $ExceptionsFile: $!\n"; my @except= <EXCEPT>; close EXCEPT; chomp( @except ); @Exception{@except}= (1) x @except; } rewinddir( DIR ) or die "Can't rewind directory, $Directory: $!\n"; my $file; while( $file= readdir(DIR) ) { if( $file =~ /^di/ && ! $Exception{$file} ) { if( ! open FILE, "< $file" ) { warn "Can't read $file: $!\n"; next; } my $data= <FILE>; close FILE; my $count= ()= $data =~ /\Q$BadString/g; next if $BadCount <= $count; if( ! rename( $file, $BadDir.$file ) ) { warn "Can't rename $file to $BadDir$file: $!\n"; } else { warn "Moved $file to $BadDir$file.\n"; } } } sleep $SleepLength; }

        - tye