title: How to read multiple files in Perl and create different files containing data from those files
body: <p>I have bunch of 18MB text files which I need to read in Perl, extract specific information, and write it into a new file for each of those files.</p>
  
  <p>How can I do this?</p>
  
  <p>For example the following code applies just to one file and creates a new file for the file mentioned in the code, but how can I do it on multiple files as I have many of them?</p>
  
  <p>The following is my code which only works on a single file. I want to do the same for a lot of files. How can I do it?</p>
  
  <pre><code>use Regexp::Common qw/net/;
  use Regexp::Common qw/net/;
  
  system("clear");
  
  my $file2 = 'TR1';
  open my $in, '&lt;', $file2 or die $!;
  open my $out, '&gt;', 'Number_of_Hops_TR1_007' or die $!;
  
  my $var = 0;
  my $i   = 0;
  my $traceroute;
  my $line;
  
  while (my $line = &lt;$in&gt;) {
  
      if ($line =~ /^traceroute to (\S+)/) {
          $traceroute = $1;
          $var++;
  
          #print "$traceroute\n";
      }
      my ($ip) = $line =~ /(?: \d+ \s \s+) ($RE{net}{IPv4}) /msx;
  
      if ($traceroute eq $ip) {
  
          print $out $ip if defined, "\n";
          if ($ip ne undef) {
              { $i++; }
          }
      }
      else {
      }
  }
  
  print $out "Number of traceroutes - $var\n";
  
  print $out "Number of traceroutes reached destination - $i\n";
  
  my $subs = $var - $i;
  
  print $out
      "Number of traceroutes that did not reaach destination ($subs)\n";
  
  my $perc = ($i / $var) * 100;
  
  print $out "Percentage of sucessful traceroutes ($perc%)\n";
  </code></pre>