|There's more than one way to do things|
Re: renameby Aristotle (Chancellor)
|on Sep 02, 2002 at 04:37 UTC||Need Help??|
dir_list() is pretty useless; it takes a list and pushes it item by item onto an array. A simple array assignment will do that job just as well.
Not that there has to be one, because the @Dir_List array only gets used in one place. You can just as well use @ARGV itself there.
The [0000-9999] bit in your filename checking regex does not do what you think it does. Its result is a character class that will match a single digit from 0-9.
Also note that the filename may contain characters that have special meaning to the regex engine. In that case the regex will malfunction. You need to put \Q and \E around the $file_name.
Using an array, @files_good in your case, to check for existence, is very awkward and slow. Instead, put the values as keys into a hash, and checking for existance becomes a single operation rather than a loop.
A minor point is that you may want to include $! (or whichever other variable is appropriate) in your die message when dealing with filesystem operations. As a rule of thumb, each die message should contain at least one variable; that usually makes tracking down the source of the error a lot easier.
grep is essentially a loop; you don't need it if you're going to use a for loop to separate the resulting list into two arrays anyway.
Your code that checks for whether a filename already exists is defective; the new filename it checks for has no extension until after the test has succeeded.
Rather than starting a shell to call its ren command for every file, a very slow procedure, you can just use Perl's own rename function.
All that applied, the code looks much more manageably like this:
Makeshifts last the longest.