title: Why does this program fail to copy files?
body: <p>this morning, my friend and I discussed and wrote the below code. The idea behind this Perl script is to create the directory structure and copy the files to the corresponding directory.</p>
  
  <pre><code>#!/usr/bin/perl
  use File::Path;
  use File::Copy;
  use Path::Class;
  use File::Basename qw/dirname/;
  my $src = "/Vijay/new.txt";
  unless (open(MYFILE, "file1")) {
      die ("cannot open input file file1\n");
  }
  $line = &lt;MYFILE&gt;;
  while ($line ne "") {
      print ($line);
      mkdir_and_copy($src,$line);
      $line = &lt;MYFILE&gt;;
  }
  sub mkdir_and_copy {
      my ($from, $to) = @_;
      my($directory, $filename) = $to =~ m/(.*\/)(.*)$/;
      print("creating dir $directory");
      system "mkdir -p $directory";
      print("copying file $from to $to");
      system "cp -f $from $to";
      return;
  }
  </code></pre>
  
  <p>The above piece of code creates the directory structure, but fails to copy the files to the corresponding directory. Could you please let us know, where exactly we are wrong?</p>
  
  <p>Contents of file1:</p>
  
  <pre><code>test/test1/test2/test.txt
  </code></pre>
  
  <p>Contents of new.txt:</p>
  
  <pre><code>Shell/Test/test1/test1.txt
  Shell/Test/test2/test2.txt
  Shell/Test/test3/test3.txt
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>&gt; ./mypgm.pl
  test/test1/test2/test.txt
  creating dir test/test1/test2/copying file /Vijay/new.txt to     test/test1/test2/test.txt
  cp: cannot access /Vijay/new.txt: No such file or directory
  &gt;
  </code></pre>
  
  <p>The directory <code>Vijay</code> has the file <code>new.txt</code> with the above mentioned content.</p>
  
  <p>Thanks in advance,</p>
  
  <p>Vijay</p>
  
  <hr>
  
  <p>Hello everyone,<br>
  I just modified my code. Please refer the below section of code.    </p>
  
  <pre><code>#!/usr/bin/perl        
  use File::Path;    
  use File::Copy;    
  use File::Basename qw/dirname/;    
  
  my $src = "./Vijay/new.txt";       
  unless (open(MYFILE, "file1"))    
  {    
  die ("cannot open input file file1\n");    
  }
  
  $line = ;
  while ($line ne "")
  {
  print ($line); print("\n");
  mkdir_and_copy($src,$line);
  $line = ""; }
  
  sub mkdir_and_copy        
  {      
  my ($from, $to) = @_;    
  my($directory, $filename) = $to =~ m/(.\/)(.)$/;    
  $temp = $directory.$filename;    
  print("Creating dirrectory $directory \n");    
  if(! -d $directory)    
  {    
  mkpath($directory) #or die "Failed to create path";    
  }    
  printf("From: $from \n");    
  printf("To: $temp \n");    
  copy($from,$temp) or die "Failed to Copy";    
  return;    
  }    
  </code></pre>
  
  <p>Now, it creates the exact directory structure and copies the file to the corresponding directory. Could you please tell me that, whether the above code is a proper one?</p>