title: How do I execute a unix command containing a perl variable in perl
body: <p>In the following perl code, I am tring to copy a perl variable $file from one directory to another directory with:</p>
  
  <pre><code>"system("cp $file  $Output_Dir);
  </code></pre>
  
  <p>This command writes down the file name alright but then says:</p>
  
  <pre><code>cp: cannot stat 'tasmax_AFR-44_CNRM-CERFACS-CNRM-CM5_historical_r1i1p1_CLMcom-CCLM4-8-17_v1_day_19910101-19951231.nc': No such file or directory
  </code></pre>
  
  <p>The command</p>
  
  <pre><code>      system("@sixfiles = ls $Vars[$kk]}*");
  </code></pre>
  
  <p>gives me the error:
      sh: 1: =: not found
  I wonder what is wrong with this code. Assistance will be appreciated.</p>
  
  <pre><code>#!/usr/bin/perl -w
  use strict;
  use warnings;
  use File::Path;
  use File::Copy;
  
  my $debug = 1;
  
  my @Vars = ("pr","tasmin","tasmax");
  my $Vars;
  my @sixfiles;
  my $sixfiles;
  
  my $Input_Dir = "/home/zmumba/DATA/Input_Dir";
  my $Output_Dir = "/home/zmumba/DATA/Output_Dir";
  
  for (my $kk=0; $kk &lt; @Vars; ++$kk) {
      opendir my $in_dir, $Input_Dir or die "opendir failed on $Input_Dir: $! ($^E)";
      while (my $file=readdir $in_dir) {               
          next unless $file =~ /^$Vars[$kk]/;
          next if -d $file;
          print "$file\n";
          print "Copying $file\n" if $debug;
          my $cmd01 = "cp $file  $Output_Dir";
          print "Doing system ($cmd01)\n" if $debug;
          system ($cmd01);
          system("@sixfiles = ls $Vars[$kk]}*");
      }
  }
  </code></pre>