in reply to this script normaly deplace files in a directory !
That looks like a perfectly respectable script. You could make it a bit shorter, but for the most part there's really no reason to.
One thing I would consider is using the File::Copy module to copy the file; it will be shorter, and perhaps faster on some platforms. Even if you don't use that, reading the entire file into memory then writing the entire thing out wastes some memory. A normal idiom for that is something like:
open(INPUT,$file) or die ("Error opening '$file' for input: $!\n"); open(OUTPUT,">".$file) or die ("Error opening '$file' for output: $!\n +"); while(read(INPUT,$buf,8192)) { print OUTPUT $buf; } close(INPUT) or die "Couldn't close '$file' after input: $!\n"; close(OUTPUT) or die "Couldn't close '$file' after output: $!\n";
One other thing to think about is error handling. It looks like when you encounter an error, you print out a message, and put the entire rest of the program inside an else block. If you just print the error and immediately exit, such as with die, the flow of your program will be more clear. Also, you might as well use more useful error messages than "Error 3", and print the reason for the error with the $! variable.
When you call readdir the first time, you may be throwing away the first item from the directory; be careful of that. Also, if you want to see if there's something in the directory, using $allThings[0] eq "" is a sloppy way to do it; $allThings[0] is undefined, and it so happens that when compared to a string it will be converted to an empty string. It's probably best to just see how many elements are in the array, with @allThings == 0 or !@allthings.
|
|---|