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

Hi Monks,
I have found a problem on Mac OS X with File::Copy Module. If there is a "Blank" in the folder or file name of a source or target file File::Copy fails. # Any sugesstions what I can do?
Here is a test script:
#!/usr/bin/perl -w
use File::Copy;
$source = $ARGV[0];
$target = $ARGV1;
copy($source, $target) or die "Can't copy files ($!) ($?)\n";
exit 0;

Thanks Alex

Replies are listed 'Best First'.
Re: File::Copy Problem on Mac OS X
by Corion (Patriarch) on Jun 02, 2008 at 18:39 UTC

    This is not a bug in File::Copy but more likely an error in your understanding of how your shell launches programs and parses command line parameters. You can confirm this by printing out the parameters your program gets:

    #!/usr/bin/perl -w use strict; print "Using '$ARGV[0]' as source\n"; print "Using '$ARGV[1]' as target\n"; print "Leftover arguments: '$ARGV[2]'\n";

    Depending on what shell you're using, reading the manual on "quoting" helps, but alternatively, most shells use quoting rules not entirely unlikely to Perl's own rules. In short, if you want to include a blank in a command line argument, you need to quote it in the shell.

Re: File::Copy Problem on Mac OS X
by marto (Cardinal) on Jun 02, 2008 at 18:49 UTC
    Is that a direct copy and paste of your script? I doubt that this is a problem with File::Copy or using it on OS X. Are you remembering to put the arguments (given that they can contain spaces) in quotes? Take a look at this slight reworking of your example:
    #!/usr/bin/perl use strict; use warnings; use File::Copy; my $source = $ARGV[0]; my $target = $ARGV[1]; copy($source, $target) or die "Can't copy files ($!) ($?)\n";
    I created a file named 'testing spaces.txt' containing some text, and ran the script via
    ./copy.pl "testing spaces.txt" output.txt

    If you have any further questions let us know

    Hope this helps

    Martin
      hi, thanks for your reply to all of you.

      The sample I gave was more for testing. In my real project I get the source url from a database.
      I pass them also quoated to copy(), but I always get this error ($!): (No such file or directory)

      If I print source and target URL to check if they are quoted I get:
      source: '/Users/ademmler/testing mit leerzeichen/myfile.pdf'
      target: '/Users/wfdata/myfile.pdf'

      I have tried to use singel and double quotes . . . no change.
      Mac OS X escapes the blank with \ and if I pass the path to my above testscript like this it works also. But if I try to add a \ via regex into the URL it does not work . . .

      Regards Alex
        ademmler,

        Firstly, these are not URLs the are paths to files.

        Secondly, are you sure you have permissions to read from/write to the source file and target directory? Please show us the code you are actually using, and how you are calling it. Have you tried quoting the parameters you are passing to the copy command?
        #!/usr/bin/perl use strict; use warnings; use File::Copy; my $source = $ARGV[0]; my $target = $ARGV[1]; copy("$source", "$target") or die "Can't copy files ($!)\n"
        Update: Fixed typo, reworded last sentence. Hope this helps

        Martin