Win has asked for the wisdom of the Perl Monks concerning the following question:

What is the best way of getting the following bit of code to deal with the fact that there are now other directories in the $input_directory?
opendir(DIR_A,"$input_directory") or die $!; INNER_A: while(my $job =readdir(DIR_A)){
I have tried the following:
INNER_A: while(my $job =readdir(DIR_A)){ # chomp; next unless -f $job; # ignore specials
And a few other similar pieces. But without much luck.

Replies are listed 'Best First'.
Re: Problem with reading files when we have new directories
by davorg (Chancellor) on Feb 07, 2007 at 15:05 UTC
    to deal with the fact that there are now other directories in the $input_directory

    Depends what you mean by "deal with". If you want to ignore directories, then code similar to what you have will work. If you want to recursively process the directories then you should probably use File::Find.

    But without much luck

    That's really not a very good description of the problems that you are having.

    Piecing together your code:

    opendir(DIR_A,"$input_directory") or die $!; INNER_A: while(my $job =readdir(DIR_A)){ chomp; next unless -f $job; # other stuff I assume }

    Firstly, you're unnecessarily quoting the $input_directory variable in the "opendir" call.

    Secondly, you're making the most basic error in your use of opendir/readdir. It's so basic that it's described in the documentation.

    Oh, and what's that "chomp" there for? More cargo-cult programmming?

    Please try to do a little bit of your own research before posting questions here. The Perl documentation set is really good. It _will_ answer a large proportion of your questions if you give it a chance.

Re: Problem with reading files when we have new directories
by philcrow (Priest) on Feb 07, 2007 at 15:02 UTC
    When you do the file test you probably need to prepend the directory name, unless there is a chdir hiding in there somewhere:
    next unless -f "$input_directory/$job";
    where / must be your OS seperator.

    Phil

Re: Problem with reading files when we have new directories
by GrandFather (Saint) on Feb 07, 2007 at 19:57 UTC

    At what point do you expect new directories to arrive? At what point would you like to be prepared to handle them? What is the larger problem, because that will give us all a clearer idea of what you are trying to achieve and what the constraints are that affect how you might get the job done.

    At this point (and in light of previous posts) it seems that you are writing some sort of job dispatch system based on control files dropped into directories that serve to prioritise work. It seems most likely that what you are trying to do here is poll for new jobs. Maybe if there is anything trickier that solving that trivial problem you will let us know?

    Information like "how long a job dispatch/execution takes", "what the maximum allowable latency is", "how often new directories may be added", "the range of OS's supported" and so on will help determine a best solution.


    DWIM is Perl's answer to Gödel
Re: Problem with reading files when we have new directories
by fmerges (Chaplain) on Feb 07, 2007 at 15:05 UTC

    Hi,

    With Linux you can use Inotify, see on CPAN

    Other way is doing a polling of the directory and fire up an event if there is a new directory. You only have to "rewind" the dirhandle and look again if any new file is there that you haven't processed if you need to make a check like this.

    See also POE::Component::DirWatch::Object.

    Regards,

    fmerges at irc.freenode.net