http://qs321.pair.com?node_id=11108051


in reply to make infinite loop for live directory

First of all, i took the liberty of beautifying your code, removing the use of $_ in favour of named variables and added the use strict; use warnings; stuff that makes your code easier to debug. I also took the liberty of introducing the magic of use Carp; and use English;. Hope you are not angry about that ;-)

One way to mark files done is to add a second empty ".done" file and checking for that. If some other programs removes the .txt files, you could just as easy check for ".done" files without corresponding ".txt" files and remove them.

Here is a simple version of that (untested since i only had a few minutes and couldn't be bothered to recreate your directory structure):

#!/usr/bin/env perl use strict; use warnings; use Carp; use English; # Do some work foreach my $file ( glob('/root/fd/fg/*.txt') ) { next if(-f $file . '.done'); # Skip if '*.done' file exists open(my $ifh, '<', $file) or croak($ERRNO); my $ad; my @rt; while((my $line = <$ifh>)) { if($line =~ m/ad/) { ($line =~ tr/a-z,=//d ) ; $ad = $line ; } if ($line =~ m/rt /){ ($line =~ tr/a-z,=//d) ; push (@rt , $line); } } close $ifh; # Need to close fille after working on it. open(my $ofh, '>', $file . '.done') or croak($ERRNO); # Create '*. +done' file close($ofh); } # Cleanup foreach my $file ( glob('/root/fd/fg/*.txt.done') ) { my $realfname = $file; $realfname =~ s/\.done$//; if(!-f $realfname) { unlink $file; } }

If no other program works with the files after your program, you might as well just rename them.

#!/usr/bin/env perl use strict; use warnings; use Carp; use English; use File::Copy; # Do some work foreach my $file ( glob('/root/fd/fg/*.txt') ) { open(my $ifh, '<', $file) or croak($ERRNO); my $ad; my @rt; while((my $line = <$ifh>)) { if($line =~ m/ad/) { ($line =~ tr/a-z,=//d ) ; $ad = $line ; } if ($line =~ m/rt /){ ($line =~ tr/a-z,=//d) ; push (@rt , $line); } } close $ifh; # Need to close fille after working on it. # Rename/move file move($file, $file . '.done'); }

There is potentially another problem. If the external program that generates these files writes to a file while you are still reading it, you might get an incomplete read - which is damn hard to debug and can give you a lot of grey hairs. One workaround hack to that is to get the filelist, then wait a few seconds and then process the file list. Something like this should work:

... # Do some work my @files = glob('/root/fd/fg/*.txt'); sleep(4); foreach my $file(@files) { ...

perl -e 'use MIME::Base64; print decode_base64("4pmsIE5ldmVyIGdvbm5hIGdpdmUgeW91IHVwCiAgTmV2ZXIgZ29ubmEgbGV0IHlvdSBkb3duLi4uIOKZqwo=");'