1. Perl
  2. Module
  3. here

File::Copy - Move and copy files

The File::Copy module allows you to move and copy files.

# Loading modules and importing functions
use File::Copy qw/copy move/;

You can copy the file with the copy function and move the file with the move function. There is a builtin functions to move files called the rename function, but it does not support moving across file systems. If you want to move files, it's a good idea to use File::Copy 's move. Move can also be used when moving directories.

# File copy
copy ($from, $to);

# Move file
move ($from, $to)

This is an example to copy a file. Error processing is performed in case the file copy fails (* 1). Make sure to include the OS error message "$!" In the error message.

# File copy
my $from = 'xxx';
my $to = 'yyy';

copy ($from, $to)
  or die "Can't copy \" $from\"to \" $to\":$!";

This is an example to move files. Performs error handling in case the file move fails. Make sure to include the OS error message "$!" In the error message.

# Move file
my $from = 'xxx';
my $to = 'yyy';

move ($from, $to)
  or die "Can't move \" $from\"to \" $to\":$!";

The file copy will fail if the file does not exist, or if the destination directory does not have write permissions.

Related articles

Related Informatrion