Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re^3: cp in perl

by Anonymous Monk
on Nov 07, 2006 at 10:43 UTC ( [id://582607]=note: print w/replies, xml ) Need Help??


in reply to Re^2: cp in perl
in thread cp in perl

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.

Replies are listed 'Best First'.
Re^4: cp in perl
by Fletch (Bishop) on Nov 07, 2006 at 14:50 UTC

    Funny, my copy of File::Copy's documentation plainly states:

    The "copy" function takes two parameters: a file to copy from and a file to copy to.

    It then quite clearly goes on to note the optional third parameter which is in no manner, shape, or form a path name of any kind. The post I was responding to seemed to imply that they expected copy to work like cp with multiple arguments and a trailing directory name, which it doesn't (as the documentation explains; "I copy from the first argument to the second argument").

    I'm sorry that there exist people dense enough to blindly presume things without reading the documentation, but it's quite obvious what copy allows for arguments.

      Well, sadly (or happily) the copy function also works with a directory as the 2nd parameter, copying the 1st parameter into the directory given by the 2nd - and not just file to file. Furthermore, the module gives the possibilty to export functions named 'cp' and 'mv', just like corresponding *nix functions.

      These factors make it (sadly!) more probable that copy is assumed to do something it does NOT do, i.e. copy @files, $dir.

      Btw, move has the same problems, simply moving its 1st onto its 2nd parameter, silently ignoring any other arguments.

      -- Hofmator

      Code written by Hofmator 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.

      All I'm saying that regardless what the documentation says, is that if people suggest that 'File::Copy::copy' is Perl's answer to 'cp', then people *will* treat it as 'cp'. It works well if you give it two arguments. It will erase your file if you give it three.

      IMO, that's very, very bad. It violates the principle of least surprise in one of the worst ways possible.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://582607]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (2)
As of 2024-04-19 18:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found