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

Hi,
I'm trying to write a simple script that will take file or directory and copy it to a destination. I basicaly want to emulate the unix command "cp -rp blah1 blah2". File::Copy doesn't seem to be powerful enough since doesn't do directories and I'm it doesn't say how it handles permissions.

I'm willing to do a check to see I'm copying a file or directory. What I don't want to do is a bunch of looping to traverse down the directory tree and keep track of where I am. Unix does this on one line. Perl has to have some simple equivalent.

Thanks in advance,
zinthefe

Replies are listed 'Best First'.
Re: cp -rp src dst
by PrimeLord (Pilgrim) on Mar 05, 2002 at 21:59 UTC
    I believe File::NCopy on Cpan will accomplish what you are looking for.
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: cp -rp src dst
by data64 (Chaplain) on Mar 05, 2002 at 22:06 UTC

    Maybe cp from Perl Power Tools project will help you. From its manpage it has -p option but seems to lack the -r.


    <cite>Just a tongue-tied, twisted, earth-bound misfit. -- Pink Floyd</cite>

Re: cp -rp src dst
by mpeppler (Vicar) on Mar 05, 2002 at 21:58 UTC
    Errmm... Unix does it in one line but that line is a fairly complex C program.

    Why do you want to rewrite cp -rp in perl?
    Personally I'd just use a system() call to perform that operation.

    Maybe not what you want to hear, but, well... why not use the tools that already exist?

    Michael

      I'd love to just say `cp -rp ...` but this has to run on Windows as well as Unix.
Re: cp -rp src dst
by dug (Chaplain) on Mar 05, 2002 at 21:59 UTC
    why not system?
    my @arguments = ("cp", "-rp", "$source_dir", "$destination_dir"); system(@arguments);
    You could also use File::NCopy, which is mentioned in the directories Q and A.
Re: cp -rp src dst
by ajwans (Scribe) on Mar 05, 2002 at 22:02 UTC
    What Perl does have is multiple mechanisms for running *nix commands. Often this is the clearest way to do something. Would you rather write 10+ lines of code, checking whether a node is a file or a directory then making that directory or copying that file or would you prefer the following?
    `cp -rp blah1 blah2`;
    or
    system ("cp -rp blah1 blah2");
    or even
    qx/cp -rp blah1 blah2/;
    Just don't forget to check the process' return value "$?"