Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

cp in perl

by sanku (Beadle)
on Nov 06, 2006 at 14:15 UTC ( #582433=perlquestion: print w/replies, xml ) Need Help??
sanku has asked for the wisdom of the Perl Monks concerning the following question:

hi all, can anyone plz tell me the command for copying file from one destination to another inside the perl script? i tried,
cp /home/bla/bla.ent /home/sla/sla
doesnt seem to work plz help.

Replies are listed 'Best First'.
Re: cp in perl
by davorg (Chancellor) on Nov 06, 2006 at 14:16 UTC

    As I told you in the chatterbox, you need File::Copy - which is a standard part of the Perl distribution.

    use File::Copy; copy('/home/bla/bla.ent', '/home/sla/sla') or die "Copy failed: $!";

    It's also worth pointing out that this is a FAQ (see How can I copy a file?) When you're looking for help with Perl programming, it's always worth looking in the FAQ.


    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

Re: cp in perl
by xdg (Monsignor) on Nov 06, 2006 at 14:27 UTC

    Also, while it's not part of the core distribution, I've been pleased with File::Copy::Recursive.

    Update: Another post reminded me about ExtUtils::Command and, more appropriate for this question, Shell::Command. I rarely think of them, but if you really want a "cp" clone...


    Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

Re: cp in perl
by explorer (Chaplain) on Nov 06, 2006 at 14:17 UTC
Re: cp in perl
by Anonymous Monk on Nov 06, 2006 at 16:01 UTC
    I prefer system "cp", "/home/bla/bla.ent", "/home/sla/sla"; as it never surprises me, and just works they way I expect it to work.

    Unlike File::Copy which loses permission bits, and will do unexpected things if you happen to give it more than two arguments.

      Actually, unless you mean, "It acts differently depending on the OS", I think you could be in for a surprise. For example, look at the difference between cp on Linux and on FreeBSD.
        cp file1 file2 dir doesn't copy file1 and file2 to dir? cp executable non-existing drops execution bits on FreeBSD?

      Wow, something documented as taking only two parameters will not behave correctly if given more than two parameters. I am astounded.

      (I'm however sympathetic on the charge about permissions problems; the system's cp will try and preserve permissions rather than subjecting you to the whims of your umask.)

        Except of course that File::Copy::copy will take three arguments, and that's documented as well.

        But since File::Copy::copy is usually promoted as "Perl's replacement for cp", people will expect it to behave as cp. And not study the documentation carefully.

        You for instance either didn't study the documentation carefully, or if you did, forgot it. Because you wrongly assumed File::Copy::copy only takes two arguments. Now your mistake isn't as costly as forgetting the meaning of the third parameter. But if you can forget File::Copy::copy having a third parameter, you might as well forget that File::Copy::copy acts the same as cp when given two arguments, but very, very different when given three.

        use File::Copy; my @files1 = qw [foo]; my @files2 = qw [bar baz]; my $dir = "/tmp"; copy @files1, $dir; # No problem. copy @files2, $dir; # File::Copy will warn, but erase 'baz' anyw +ay.
        Now had you used your systems 'cp', it would have copied all three files into /tmp.

        And had you used an all numeric $dir in the above code fragment, File::Copy::copy would have overwritten baz without so much as a warning.

Re: cp in perl
by Anonymous Monk on Nov 07, 2006 at 16:40 UTC
    Use 'system cp' if you want to be POSIX compliant. Use File::Copy::copy if you don't care about standards.
      use File::Copy::syscopy if you want to preserve OS specific file attributes.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://582433]
Approved by Joost
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (10)
As of 2017-09-25 20:08 GMT
Find Nodes?
    Voting Booth?
    During the recent solar eclipse, I:

    Results (289 votes). Check out past polls.