title: dircopy and dirmove not working as expected
body: <p>I am writing a Perl script that allows us to easily move or copy a HUGE directory (possibly +100,000 subdirectories) to another location. I am using <code>File::Copy::Recursive</code> for this purpose like follows (incomplete! Some variables not defined, but it gives a minimal idea of what's going on):</p>
  
  <pre><code>use strict;
  use warnings;
  
  use File::Copy::Recursive qw(dircopy dirmove pathrmdir);
  $File::Copy::Recursive::CPRFComp = 1;
  
  
  my ($action, $source_location, $target_location) = @ARGV;
  opendir(my $source_handle, $source_location) or die "Can't opendir $source_location: $!\n";
  my $directories_found = 0;
  
  while (my $sub_dir = readdir $source_handle) {
      unless (-d "$source_location/$sub_dir") {
          print STDERR "$source_location/$sub_dir not a dir\n";
          next;
      }
      # Makes sure we only move directories given a pattern defined elsewhere
      if ($sub_dir =~ $file_match_regex) {
          # $action is an input argument
          if ($action eq 'copy') {
              dircopy("$source_location/$sub_dir/", "$target_location/")
                  or die "Cannot copy $source_location/$sub_dir/: $!\n";
          } elsif ($action eq 'move') {
              dirmove("$source_location/$sub_dir/", "$target_location/")
                  or die "Cannot move $source_location/$sub_dir/: $!\n";
          }
          $directories_found = 1;
      } else {
          print STDERR "$source_location/$sub_dir did not match regex\n";
      }
  }
  
  if ($action eq 'move') {
      # Remove topmost directory
      pathrmdir($source_location)
          or die "Cannot remove $source_location: $!\n";
  }
  
  if (!$directories_found) {
      print STDERR "No items found to $action\n";
  }
  </code></pre>
  
  <p>The first run this <em>seems to work as expected</em>. Take this command</p>
  
  <pre><code>perl myscript.pl move source/ /home/otherdir/target/
  </code></pre>
  
  <p>The terminal output is</p>
  
  <pre><code>source/. did not match regex
  source/.. did not match regex
  </code></pre>
  
  <p>and that's it.</p>
  
  <p>However, when I run the same script on the moved folder, things go wrong.</p>
  
  <pre><code>perl myscript.pl move /home/otherdir/target/ /home/failing/target/
  
  /home/otherdir/target/. did not match regex
  /home/otherdir/target/.. did not match regex
  /home/otherdir/target/somefile1.txt not a dir
  /home/otherdir/target/somefile2.txt not a dir
  /home/otherdir/target/somefile3.txt a dir
  /home/otherdir/target/somefile4.txt a dir
  </code></pre>
  
  <p>Obviously I shouldn't get different responses when running the same copy/move script on the data. It is peculiar, though, that the files come from one directory (I can't figure out which one they are all identical concerning content) and that the other directories are preserved. So in each run, for one $sub_dir the script copied the contents of the directory to the destination rather than the directory itself. This means that I am losing a directory on each run of the script... I don't understand why, though.</p>
  
  <p>I think that I am misusing <code>dircopy</code> and <code>dirmove</code> , and I'm not sure if <code>$File::Copy::Recursive::CPRFComp = 1;</code> is correct either (I didn't found the documentation to be very clear for my novice eyes). Any thoughts?</p>
  
  <hr>
  
  <p>After some more digging I think that this is what happens.The little documentation there is on CPRFComp reads (assuming 'foo/file'):</p>
  
  <pre><code>dircopy('foo', 'bar') or die $!;
  # if bar does not exist the result is bar/file
  # if bar does exist the result is bar/file
  
  $File::Copy::Recursive::CPRFComp = 1;
  dircopy('foo', 'bar') or die $!;
  # if bar does not exist the result is bar/file
  # if bar does exist the result is bar/foo/file
  </code></pre>
  
  <p>My guess is, thus, that The first copy/move action of a subdir is triggered before the target location ('bar' in the example) exists, which leads to bar/file rather than bar/foo/file. The question then changes to: how can I make sure that my copy/move operations wait until the target directory is build?</p>