How to use File::Copy to copy files in Perl?
Question
How to use File::Copy to copy files in Perl?
The File::Copy module in Perl provides simple and efficient functions to copy files from one location to another. It’s part of Perl’s core modules, so you don’t need to install anything extra to start using it. This makes it a convenient choice for basic file copy operations.
Using File::Copy to Copy Files
The primary function provided by File::Copy is copy(). It takes two arguments — the source file path and the destination file path — and returns a true value on success or false on failure.
Because copy() is designed to work with file paths, it abstracts away the complexity of reading and writing file contents manually. Underneath, it attempts to use the most efficient system-level copy calls available, falling back to manual copying if needed.
Basic Syntax
use File::Copy;
my $source = "source.txt";
my $dest = "destination.txt";
if (copy($source, $dest)) {
print "File copied successfully.\n";
} else {
warn "Copy failed: $!\n";
}
Explanation of Perl Concepts
- use File::Copy; imports the module's copy function into your namespace.
copy($source, $dest)takes two filename strings. Both are scalar strings, denoted by the$sigil indicating scalars.- The function returns true (1) on success, false (
undef) on failure. The special variable$!holds the last OS error, useful for diagnostics. - Perl's TMTOWTDI ("There's More Than One Way To Do It") principle means you could manually copy using
open,read, andwrite, butFile::Copyabstracts this for you.
Common Pitfalls and Gotchas
- Ensure the source file exists and you have appropriate permissions; otherwise,
copy()fails. - If the destination file already exists, it will be overwritten without warning.
copy()does not handle copying directories — only files.- Symbolic links are copied as the file they point to, not as links — if you want link behavior, extra logic is needed.
Complete Runnable Example
use strict;
use warnings;
use File::Copy;
my $source = "test_source.txt";
my $dest = "test_destination.txt";
# Prepare a sample source file to demonstrate
open my $fh, '>', $source or die "Cannot create source file: $!";
print $fh "This is sample content.\n";
close $fh;
# Copy the file
if (copy($source, $dest)) {
print "File copied from '$source' to '$dest' successfully.\n";
} else {
warn "Failed to copy file: $!\n";
}
# Verify by reading destination
open my $fh2, '<', $dest or die "Cannot open destination file: $!";
print "Destination file content:\n";
while (<$fh2>) {
print $_;
}
close $fh2;
This script creates a sample file called test_source.txt, copies it to test_destination.txt using File::Copy::copy, and then prints the destination file’s content to confirm the copy succeeded.
Version Notes: The File::Copy module has been part of Perl’s core distribution since very early versions (Perl 5.002+). Its interface and behavior have remained stable, so it's safe and portable across Perl 5 versions.
Using File::Copy is the simplest and most idiomatic way to copy files in Perl, providing robust error detection and minimal code. For more complex needs like recursive directory copying or preserving metadata, look into modules like File::Copy::Recursive or File::Copy::Recursive.
Verified Code
Executed in a sandbox to capture real output. • v5.34.1 • 27ms
(empty)Copy failed: No such file or directory
Was this helpful?
Related Questions
- How to use File::Find to recursively search directories in Perl?
- How to use Path::Tiny for file operations in Perl?
- How to get file modification time in Perl?
- How to use File::Slurp to read files in Perl?
- How to open a file with specific encoding in Perl?
- How to change the current directory in Perl?