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

I've been using Perl for about 2 weeks. I've been in a steep learning curve, but am stumped right now. I'm on Windows...

I'm trying to successfully use File::Copy::Recursive from File-Copy-Recursive.-0.34.tar

I installed the PM correctly, as I don't get an error that the subroutine can't be found.

The function dircopy takes 2 variables, $orig and $dest for directories.

When I run it, I get an error:

"Uncaught exception from user code: Copy failed at copyfiles.pl at line 17"

Line 17 is simply the call to the function:

dircopy($orig, $dest) or die "Copy failed: $!";

The 2 variables are declared just before:

my $orig = "myOriginal";<p> my $dest = "myNewDirectory";<p>
I have found absolutely no help anywhere on this.

Basically, I want to simply copy a directory recursively and create a copy of it somewhere else. Is there a better module to do this? What am I missing here?

Here is the entire bit:

#!perl use warnings; use diagnostics; use File::Copy; use File::Path; use File::Copy::Recursive qw(fcopy rcopy dircopy fmove rmove dirmove); print "start\n"; my $orig = "nose"; my $dest = "nosey"; dircopy($orig, $dest) or die "Copy failed: $!"; print "done\n";
Thanks,

Doug

Replies are listed 'Best First'.
Re: File::Copy::Recursive chokes on input
by andyford (Curate) on Aug 23, 2007 at 16:13 UTC

    I've been trying to replicate your problem on Unix by running your program in places where I don't have the permissions because that's really the only reason that I can imagine this failing.

    I always get an error like

    Uncaught exception from user code: Copy failed: Not a directory at /home/forda/filecopy line 15.

    or

    Uncaught exception from user code: Copy failed: Permission denied at /home/forda/filecopy line 15.

    depending.

    Random thought: When you installed the module, did you run "make test"? Did you get all OK?

    non-Perl: Andy Ford

      I'm on windows, and for the life of me can't find any instructions that are useful on how to install a module on XP (or NT?). I'm using Activestate.

      I did however find some vague instructions to take the module (or any module) "Recursive.pm" and place it in the File > Copy folder in my Lib in my Perl directory. I also created a "File > Copy" directory where I'm executing the program. Either way, it appears to be working because I'm not getting the error about @INC (or something like that).

      I posted this thread a couple hours ago. I just did another google search. My question here on perlmonks is now in the top 20 search results on google. Do a google on "dircopy perl" and you'll see this thread! Talk about recursive.

      I'm completely baffled, and my search for help is recursive. That's not good! Any other ideas? Can you get this to work at all on your system? Can you post an example that works?

      Thanks!

Re: File::Copy::Recursive chokes on input
by dogz007 (Scribe) on Aug 23, 2007 at 17:32 UTC
    It's definitely easier (and more reliable) to install new modules in Windows using the Perl Package Manager that came with ActivePerl. Find it in the Start Menu and run the following command after it loads up. (Be sure to remove your previously installed version, of course!)

    ppm> install File-Copy-Recursive

    I went ahead and tried the install on my machine and it took about 3 seconds to download and install. Let us know if it works.

    Update: I got to fooling around looking at all the different modules that came with ActiveState and found one that may help as a substitute. Look at ActiveState::Handy for the cp_tree command. If it does what it says then you shouldn't need File::Copy. However, I don't know which one is better efficiency-wise. Depending on how large your file tree is, you should choose wisely between the two.

      I tried ActiveState::Handy and it worked the first time! Thanks!

      I tried PPM for Recursive, but there must be a firewall issue as ppm.activestate.com:80 times out. I wonder if it would have worked if the install was right.

      On that note, how DO you install a module like Recursive manually? I'd like to see it work only because it bothers me that I couldn't figure it out.

        I've never done it successfully on Windows. Linux of course comes with the tools to compile modules written in C or whatever, but Windows does not. If it's a pure Perl module (as in, written solely in Perl) then you can just download and copy it into the C:\Perl\lib directory. Otherwise, I've always relied on ppm. Use the help command and familiarize yourself with the controls. It does have a search function to help you lookup the package names before you download them.
Re: File::Copy::Recursive chokes on input
by xdg (Monsignor) on Aug 23, 2007 at 17:25 UTC

    In scalar context, dircopy() returns the number of files copied. Are there actually files in the source directory?

    The documentation isn't very clear but dircopy() returns undef if there is an error. So you actually should check to see if the return value is defined, not just true.

    defined( dircopy($orig, $dest) ) or die "Copy failed: $!";

    Or, actually check the return value.

    my $files_copied = dircopy($orig, $dest); if (defined $files_copied) { print "Files copied: $files_copied\n" } else { die "Copy failed: $!"; }

    Try that and see if that helps you figure out what's going on.

    -xdg

    Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.