Welcome to Perl and the Monastery, Apt_Addiction!
AFAIK, you should be able to rename files even if they're open on Mac OS X, although if you're going to do a mass rename in a directory tree, you probably don't want to be doing any other operations on that tree in the meantime, and therefore exit any programs that might be using it. What problems are you worried about here?
As for the code, a couple of notes: You don't check the return values of rename or open for errors, as in rename($_, $txt) or die "rename('$_','$txt'): $!"; and open(my $fh, '>>', $filename) or die "$filename: $!";. And if you're worried that other processes might be writing to the directory while you're doing your rename, note your code has a race condition: It's possible the file with the name $txt might be created in between the -e $txt and rename($_, $txt) (hence my above recommendation to try and ensure that your script is the only one working on the directory).
As for the dot files, I assume that's what you're trying to do with -f != m/^\./? Note that -f only checks if $_ is a regular file (or is a symbolic link that points to a regular file) and returns a true/false value, so doing an != on its return value doesn't really make sense. I guess you might have meant $_ !~ m/^\./, which evaluates to true if $_ doesn't match the regex - that's written more briefly as !/^\./.
Another improvement might be to not open your log file on every rename, but only open it once at the start of the program, that would be more efficient.
In general, you should Use strict and warnings, and you might be interested in Corion's Text::CleanFragment, which does a thorough cleanup of strings, although it might do too much for your purposes.
In reply to Re: Easy way to check if a file is open needed.
by haukex
in thread Easy way to check if a file is open needed.
by Apt_Addiction
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |