http://www.perlmonks.org?node_id=29896


in reply to bag of questions...

Perl will close the old file or handle upon opening a new one. To localize FOO you can either local(*FOO) or else store your handle in a variable. With 5.6 you can use a regular variable as a handle. With 5.005 and earlier you can either
use Symbol; # Time passes my $foo = gensym(); open ($foo, "<$file") or die "Cannot read $file: $!";
If you don't want to use a module you can always do it yourself:
my $foo = do {local *FOO};

Now for more useful comments. Do as it says in perlstyle and on every open of a file, directory, etc check for errors and die with an informative message. The basic rule is avoid making errors if you can, arrange that any errors you make will be caught ASAP, and arrange that they are caught with enough information to fix the error. With your code should the code be run by a user who does not have sufficient permissions to create the directory you won't get told what the real problem is and it may take a while to even notice the problem.

And finally for this task I strongly suggest getting and installing rsync. (Search at Google if you do not have it already.) Efficiently recursively backing up directories from one spot to another, one machine to another if need be, using a variety of protocols is a solved problem. :-)