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

Dear Fellow Monks,
I've been racking my trying to figure out why the dircopy function of File::Copy::Recursive behaves in the following way with the below code:

#!/usr/bin/perl use strict; use File::Copy; use File::Copy::Recursive qw ( dircopy ); my $machine = Win32::NodeName; my $shop = substr($machine,0,4); my $snum = substr($machine,5); my $BOX1 = $shop . "1" . $snum; my $BOX2 = $shop . "2" . $snum; print " ###################\n"; print " # COPYING TIREPIX #\n"; print " ###################\n"; my $box1_tirepix_dir = "\\\\$BOX1\\e\\dbmonro\\tirepics"; my $box2_tirepix_dir = "\\\\$BOX2\\e\\dbmonro\\tirepics"; print "Copying from $box1_tirepix_dir to $box2_tirepix_dir\n"; print "Removing $box2_tirepix_dir\n"; system("rmdir /S /Q $box2_tirepix_dir") or print localtime()." in syst +em TIREPIX rmdir: $!\n"; sleep(10); print "creating directory $box2_tirepix_dir\n"; mkdir($box2_tirepix_dir) or print " in TIREPIX mkdir: $!\n"; dircopy($box1_tirepix_dir,$box2_tirepix_dir) || print " TIREPIX dircop +y: $!\n";

RESULTS:

  1. The tirepics directory is properly copied as expected with all directories and files.
  2. On computer 1, an added directory c:\SHOP20003 with only a directory structure and no files.
  3. Computer 1 is where the script it called from.

NOTES:

  1. There are 2 computers networked together. One named SHOP10003 and the other is SHOP20003.
  2. Computer 1 is where the script it called from.
  3. It appears that dircopy is producing a side-effect of copying the directory structure of the source directory but I don't know why. Any insights from my brethren would be appreciated.
Dave

Replies are listed 'Best First'.
Re: File::Copy::Recursive dircopy side effect (File::Spec)
by tye (Sage) on Feb 12, 2014 at 15:09 UTC

    It is a simple bug in how File::Copy::Recursive uses File::Spec. Look at "sub pathmk" in Recursive.pm.

    It does @parts = File::Spec->splitdir($path) and joins those parts back together one-at-at-time using File::Spec->catdir(). That process turns \\server\d\dir into \server\d\dir because splitdir returns ( '', '', 'server', 'd', 'dir' ) but File::Spec->catdir('','') returns '\\' (the string \) not '\\\\' (the string \\).

    - tye        

      Thanks. I see and understand the issue. Other than removing the side-effect created directories, is there any known fix for the File::Spec module? Thanks for your help.

        Fixing the bug is a rather simple matter of programming. When you get the fix working, then you can even submit it as part of your bug report so that the module eventually gets updated with the fix on CPAN.

        - tye