in reply to General Question: File triggered job launcher

use Fcntl qw/:flock/; #list of directories to look in my @directoryList=('/dir/dir1','/dir2'); #for each dir foreach my $dir (@directoryList){ #open dir opendir(DIRHANDLE, "$dir ") or die "could't open dir $!"; #create array of files present in dir my @files= grep(!/^\./,readdir(DIRHANDLE)); #for each file test open and test lock foreach my $file (@files){ open FILE , "/$dir/$file" || die "Could not open"; #if lock obtained do your job if (flock (FILE,LOCK_EX)){ #do your job on $file } } }
Instead of a foreach loop you could use map to map your jobs onto each file.

Replies are listed 'Best First'.
Re: Re: General Question: File triggered job launcher
by rob_au (Abbot) on Nov 28, 2001 at 18:43 UTC
    I'd have to say that while your code logic is sound, I probably would opt for using File::Find instead of this sort of hand-coded solution. The reason for this is that your code is fairly sensitive to changes in the file structure and expected layout - It doesn't allow for the likelihood of other sub-directories within your directories (and is trying to open them as files), ignores files which may start with a period (grep(!/^\./, readdir(DIRHANDLE))) and while, probably more beyond the scope of the problem, doesn't check file permissions before trying to access the file.

    These are all small(-ish) things I know, but if you take this code and extend it into a recursive search of directories, you could end up with some very nasty results with directory cross-links (especially to higher level directories). This is where widely tested modules offer the greatest advantage whereby you can to a certain extent rely on the testing and ground-work provided by others.

    Just a few random thoughts that this code provoked

     

    perl -e 's&&rob@cowsnet.com.au&&&split/[@.]/&&s&.com.&_&&&print'