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

Hello, My goal is to wait-for and recognize when files have changed their file extension. I will be placing 5 files in a directory. Another program will modify those files and change their file extension from .prc to .prd. When all files have their extensions changed to .prd I know that I then need to do something. I'm looking for suggestions as to the best way to go about this.

Here's what I've got so far (untested):
sub wait_for_files { my $files = shift; my %not_changed_yet = %$files; while () { last if (! %not_changed_yet); for my $file (keys(%$files)) { if (-s $files->{$file}) { # file has .prd delete $not_changed_yet{$file}; } } } }
I'm sure that could be improved by an order of magnitude :-) How would you do it?

Replies are listed 'Best First'.
Re: Waiting For File Extension Changes
by Thelonius (Priest) on May 21, 2003 at 17:21 UTC
    I would prefer:
    sub wait_for_files { my $files = shift; my @not_changed_yet = keys %$files; while (@not_changed_yet) { @not_changed_yet = grep { ! -e } @not_changed_yet; sleep(1) if @not_changed_yet; } }
    You should at least sleep(1) and maybe longer.

    Also, I am testing only the files that are still left.

      Hmm, this looks interesting. It would appear (on first glance) that this would be more efficient than my original idea.
Re: Waiting For File Extension Changes
by BrowserUk (Patriarch) on May 21, 2003 at 16:57 UTC

    If, purchance, you are on Win32 and this only needs to run under Win32, then there is a native api that you can get at through Win32::ChangeNotify design for exactly this kind of thing. Very non-portable though, but worth a mention.


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller
      I appreciate your suggestion but I'm on Solaris. Thanks anyway.

        Sorry. I guess 3-char extensions are pretty pervasive:)

        It might be a tad more efficient to use a hash instead of greping an array for the lookup.

        sub wait_for_files{ my %not_changed = %{ +shift }; while( sleep 1 and keys %not changed ) { -e and delete $not_changed{ $_ } for keys %not_changed; } }

        Examine what is said, not who speaks.
        "Efficiency is intelligent laziness." -David Dunham
        "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller
Re: Waiting For File Extension Changes
by halley (Prior) on May 21, 2003 at 17:16 UTC
    Would these do what you want? You're not mentioning the possibility of extraneous plc or pld files, which would throw off these methods.
    1 while (@_ = <*.plc>);
    1 while ((@_ = <*.pld>) < $count);

    (Just calling glob() or the <glob> operator in scalar context will pick a filename for you, not return the count. Hence the assignments.)

    --
    [ e d @ h a l l e y . c c ]

      Good idea. I'm 'King Of The Paranoid' though and really wanted to keep a list of the filenames that I know I'm waiting for - just in case there are any old files still lurking in that directory. I appreciate your suggestion though.
Re: Waiting For File Extension Changes
by ScooterQ (Pilgrim) on May 22, 2003 at 14:35 UTC
    Is the wait absolutely necessary? I'm asking because my first thought was that you might be better served by setting up a cron job that runs a script to inspect the directory, taking action when the file extension has changed.
    Just my own two cents in the TMTOWTDI argument.