By Alvin Alexander. Last updated: June 4, 2016
Perl file copying FAQ: How do I copy a file in Perl?
You can copy a file in Perl using the File::Copy module. Here's a quick example:
# a "perl file copy" example use File::Copy; # somewhere later in the code ... $original_file = 'original-filename.txt'; $new_file = 'new-filename.txt'; # do the perl file copy here: copy($original_file, $new_file);
Perl file copy - Adding in the "die" operator
You can also use the die
operator during the copy process, like this:
use File::Copy; # somewhere later in the code ... $original_file = 'original-filename.txt'; $new_file = 'new-filename.txt'; # perl file copy with the die operator copy($original_file, $new_file) or die "The copy operation failed: $!";
For more "Perl file copy" information
For more information, check out the File::Copy documentation.