I know a module like this already exists on CPAN, but I wanted to write my own simplified version that accepts wildcards in the input.
I don't write many Perl modules so I figured I would post the code here and see if anyone could give me advice on how to make this code more efficient or user friendly.
I also included some POD documentation so you can use perldoc for usage information.
Please let me know what you think and leave some feedback on how this code could be improved!
package File::Copy::Recursive;
# Use statements
use File::Basename;
use File::Copy;
use File::Path qw(make_path remove_tree);
use strict;
use warnings;
# Link the version number with CVS
our ($VERSION) = (q$Revision: 1.1 $ =~ m{([0-9.]+)});
# Exporter
require Exporter;
our @ISA = qw(
Exporter
);
# Declare exportable routines
our @EXPORT_OK = qw(
recursive_copy rcopy
recursive_move rmove
);
# Aliases to subroutines
sub rcopy { recursive_copy(@_); }
sub rmove { recursive_move(@_); }
# Recursive copy function
sub recursive_copy {
my ($source, $destination) = @_;
for my $path (glob $source) {
if (-d $path) {
make_path($destination.'/'.basename($path));
recursive_copy($path.'/*', $destination.'/'.basename($path));
}
else {
copy($path, $destination);
}
}
}
# Recursive move function
sub recursive_move {
my ($source, $destination) = @_;
for my $path (glob $source) {
if (-d $path) {
make_path($destination.'/'.basename($path));
recursive_move($path.'/*', $destination.'/'.basename($path));
remove_tree($path);
}
else {
move($path, $destination);
}
}
}
1;
__END__
=head1 NAME
File::Copy::Recursive - Recursive copy and move functions.
=head1 VERSION
$Revision: 1.1 $
=head1 SYNOPSIS
use File::Copy::Recursive;
# Recursively copy a directory
recursive_copy('input', 'output');
# Recursively copy all contents of a directory
recursive_copy('input/*', 'output');
# Recursively move a directory
recursive_move('input', 'output');
# Recursively move all contents of a directory
recursive_move('input/*', 'output');
=head1 DESCRIPTION
File::Copy::Recursive is designed to provide simple functions to recursively copy and move files.
These functions will accept wildcards (*) in the input.
=head1 SUBROUTINES/METHODS
=over
=item C<recursive_copy> or C<rcopy>
Recursive copy function.
# Recursively copy a directory
recursive_copy('input', 'output');
# Recursively copy all contents of a directory
recursive_copy('input/*', 'output');
=item C<recursive_move> or <rmove>
Recursive move function.
# Recursively move a directory
recursive_move('input', 'output');
# Recursively move all contents of a directory
recursive_move('input/*', 'output');
=back
mv
/cp
functions with the-r
recursive flag set. \$\endgroup\$