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

kapper has asked for the wisdom of the Perl Monks concerning the following question:

the following piece of code, is working fine, but I have my doubts about some of the stuff i do in it...

use POSIX; use File::Copy; use strict; my $serverdir="/kode/perlstuff/test/"; # function for copying all files and directories recursivly with new r +ead only permissions sub copyrecursivly{ my $p_source=shift; my $p_destination=shift; if (-d $p_source){ mkdir $p_destination,"0755"; opendir(datadir,$p_source); foreach my $tempvalue (grep {!/^\./} readdir(datadir)){ copyrecursivly($p_source . "/" . $tempvalue,$p_destination . " +/" . $tempvalue); } closedir(datadir); }else{ copy($p_source, $p_destination); } } # Check that the BACKUP/ directory is present, otherwise create it if (!opendir(backupdir,$serverdir . "BACKUP/")){ mkdir $serverdir . "BACKUP/" , "0755"; opendir(backupdir,$serverdir . "BACKUP/") or die "could not create + backup directory!"; } closedir(backupdir); # Open the server directory, and create the backup destination directo +ry opendir(datadir,$serverdir) or die "could not open server directory!"; my $destinationdir=$serverdir . "BACKUP/dailyimage_" . POSIX::strftime + "%Y-%d-%m", localtime; mkdir $destinationdir; # Copy all files from source to destination foreach my $tempvalue (grep {!/^\.|[BACKUP]/ } readdir (datadir)){ copyrecursivly($serverdir . $tempvalue,$destinationdir . "/" . $te +mpvalue); } closedir(datadir);

ok first of all... for my normal variables i use my within my recursive function, which should make them safe... but what about my file handles? The code works fine, but since I read all data from the filehandles before i recurse further, it should work fine even if my filehandle names are within the same scope... but then my closedir statements are not closing all opened directories... which they should! So my question is, are they within in the same scope? and if so, how do i make them local to the function?

Next question... am I giving the right parameters to mkdir inorder to achieve 755 rights on the directory?

Final question... is there anything in this piece of code which i ought to do differently?

thanks Kasper

Replies are listed 'Best First'.
Re (tilly) 1: bag of questions...
by tilly (Archbishop) on Aug 27, 2000 at 18:22 UTC
    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. :-)

      Thanks for the comments =)
      I do fully realize that I am reinventing the wheel with this script... but after about 4 years of mostly Java and c++ coding, I thought I needed to excercise my perl skills with a couple of small problems... this being one of them =)
      I have had only minor experience with rsync, and as far as i know it's unix only... the script i am writing, is meant to run on a w2k file server...
        Win2K? I feel for you, believe me I do...

        Anyways, rsync has been ported. So play around with this wheel for practice (I certainly have done that plenty! :-) but I still recommend that you try rsync for the real job.

RE: bag of questions...
by merlyn (Sage) on Aug 27, 2000 at 18:48 UTC