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

Hello Monks,

In order to always understand better Perl (for info I am using Perl 5.8.0 on linux). I have a little question.

I want to copy a file that is executable but when I use the File::Copy module it copies the file but not the permissions.

For example:

#!/usr/bin/perl -w use strict; use File::Copy qw/cp/; my $file = "/tmp/blubb"; my $target = "/tmp/bollux"; cp $file, $target or die "Oops: $!\n";

will copy the file blubb to the file bollux but if blubb is executable, bollux will not be!

Is this behaviour normal, is there anyway of changing it?

thanks in advance for help

--
jey

Replies are listed 'Best First'.
Re: File::Copy but...
by cored (Scribe) on Nov 18, 2003 at 00:01 UTC
    I think you can use the 'Shell' module
    #!/usr/bin/perl -w use Shell; cp ("/path/file1","/path/file2");
    or even you can use exec or system
Re: File::Copy but...
by pg (Canon) on Nov 18, 2003 at 00:31 UTC

    cored is correct. Just want to add a little bit: cored's way perserves the execution permission, at least according to my testing, and that's what you required. However, it does not perserve all permissions, when I test, it missed the write permissions, and if you really want as many permissions as possible being kept the same, you can use -p switch of the unix cp command.

    use Shell; cp("-p", "BumpMAD.ksh","c.ksh");
Re: File::Copy but...
by simonm (Vicar) on Nov 18, 2003 at 00:16 UTC
    You might try something like the following (untested):
    my $mode = (stat $oldfile)[2]; chmod( $mode, $newfile );