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

When I am passing source and destination file names to copy function, in the destination file is not getting created. Why is that ? whats wrong with following code ?
use warnings; use strict; use File::Copy; my $copy_from = "C:/Users/Admin/Desktop/Staples files/readfile.txt"; my $copy_to = "C:/Users/Admin/Desktop/staples/staples_readfile.txt"; copy($copy_from, $copy_to);

Replies are listed 'Best First'.
Re: file is not getting copied
by zwon (Abbot) on Jul 17, 2011 at 15:07 UTC
    copy($copy_from, $copy_to) or die "Can't copy because: $!";
      C:\Users\Admin>perl C:\Users\Admin\Desktop\perl_programs\copyfn.pl Can't copy because: No such file or directory at C:\Users\Admin\Desktop\perl_programs\copyfn.pl line 7.

      Why am I getting this message ? My source file is located in "C:\Users\Admin\Desktop\staples" and thats where I am trying to copy it. So why am I getting this message ?

        That's not what your source code says:

        my $copy_from = "C:/Users/Admin/Desktop/Staples files/readfile.txt";

        This means that your source file is in C:/Users/Admin/Desktop/Staples files. I would assume that Perl is correct when it says that it cannot find the file.

Re: file is not getting copied
by Jim (Curate) on Jul 17, 2011 at 17:23 UTC

    The operating system is telling your Perl script what the problem is; your Perl script is just not reporting it. Don't ignore the operating system when doing operating system operations such as copying files.

    use strict; use warnings; use English qw( -no_match_vars ); use File::Copy; my $copy_from = 'C:/Users/Admin/Desktop/Staples Files/ReadFile.txt'; my $copy_to = 'C:/Users/Admin/Desktop/Staples/Staples_ReadFile.txt'; copy($copy_from, $copy_to) or die "Can't copy file from $copy_from to $copy_to:\n$OS_ERROR\n" +; exit 0;