home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam    

Book Home Programming PerlSearch this book

32.20. File::Copy

use File::Copy;

copy("/tmp/fileA", "/tmp/fileA.orig") or die "copy failed: $!";
copy("/etc/motd", *STDOUT)            or die "copy failed: $!";
move("/tmp/fileA", "/tmp/fileB")      or die "move failed: $!";

use File::Copy qw/cp mv/;             # Get normal Unix names.
cp "/tmp/fileA", "/tmp/fileA.orig"    or die "copy failed: $!";
mv "/tmp/fileA", "/tmp/fileB"         or die "move failed: $!";
The File::Copy module exports two functions, copy and move, that respectively copy or rename their first argument to their second argument, similar to calling the Unix cp(1) and mv(1) commands (names you may use if you import them explicitly). The copy function also accepts filehandles as arguments. These functions return true when they work and false when they fail, setting $! ($OS_ERROR) as appropriate. (Unfortunately, you can't tell whether something like "Permission denied" applies to the first file or to the second one.) These functions are something of a compromise between convenience and precision. They do not support the numerous options and optimizations found in cp(1) and mv(1), such as recursive copying, automatic backups, retention of original timestamps and ownership information, and interactive confirmation. If you need any of those features, it's probably best to call your platform's versions of those commands.[2] Just realize that not all systems support the same commands or use the same options for them.
system("cp -R -pi /tmp/dir1 /tmp/dir2") == 0
    or die "external cp command status was $?";

[2]Or get the PPT versions if your platform is tool-challenged.

Library Navigation Links

Copyright © 2001 O'Reilly & Associates. All rights reserved.