Skip to Content
Programming Perl, 3rd Edition
book

Programming Perl, 3rd Edition

by Larry Wall, Tom Christiansen, Jon Orwant
July 2000
Intermediate to advanced
1104 pages
35h 1m
English
O'Reilly Media, Inc.
Content preview from Programming Perl, 3rd Edition

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] ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Programming Perl, 4th Edition

Programming Perl, 4th Edition

Tom Christiansen, brian d foy, Larry Wall, Jon Orwant
Learning Perl, 7th Edition

Learning Perl, 7th Edition

Randal L. Schwartz, brian d foy, Tom Phoenix
Programming the Perl DBI

Programming the Perl DBI

Tim Bunce, Alligator Descartes

Publisher Resources

ISBN: 0596000278Supplemental ContentErrata