Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way in Perl to copy files into a yet-to-be-created directory tree?

Tags:

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?

like image 716
Chris N Avatar asked Oct 23 '08 11:10

Chris N


1 Answers

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"; 
like image 121
Robert Gamble Avatar answered Oct 03 '22 20:10

Robert Gamble