What is the best way in Perl to copy files to a yet-to-be-created destination directory tree?
Something like
copy("test.txt","tardir/dest1/dest2/text.txt");
won't work since the directory tardir/dest1/dest2 does not yet exist. What is the best way to copy with directory creation in Perl?
use File::Path; use File::Copy; my $path = "tardir/dest1/dest2/"; my $file = "test.txt"; if (! -d $path) { my $dirs = eval { mkpath($path) }; die "Failed to create $path: $@\n" unless $dirs; } copy($file,$path) or die "Failed to copy $file: $!\n";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With