Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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=");'

In reply to Re: make infinite loop for live directory by cavac
in thread make infinite loop for live directory by GHMON

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2024-03-29 06:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found