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. :-)


In reply to Re (tilly) 1: bag of questions... by tilly
in thread bag of questions... by kapper

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.