title: Perl script.file handling issues
body: <p>I have written a Perl script:</p>
  
  <pre><code>#!/usr/bin/perl
  
  use strict;
  use warnings;
  
  my $file_name;
  my $ext = ".text";
  my $subnetwork2;
  my %files_list = ();
      opendir my $dir, "." or die "Cannot open directory: $!";
      my @files = readdir $dir;
  
  sub create_files() {
  
      my $subnetwork;
      open(MYFILE, 'file.txt');
      while (&lt;MYFILE&gt;) {
          if (/.subnetwork/) {
              my @string = split /[:,\s]+/, $_;
              $subnetwork = $string[2];
          }
          if (/.set/ &amp;&amp; (defined $subnetwork)) {
              my @string = split /[:,\s]+/, $_;
              my $file = $subnetwork . $string[1];
              open FILE, "&gt;", "$file.text" or die $!;
              close(FILE);
          }
      }
      close(MYFILE);
  }
  
  sub create_hash() {
      foreach (@files) {
          if (/.text/) {
  
              open($files_list{$_}, "&gt;&gt;$_") || die("This file will not open!");
  
          }
      }
  }
  
  sub init() {
      open(MYFILE3, 'file.txt');
      while (&lt;MYFILE3&gt;) {
          if (/.subnetwork/) {
              my @string3 = split /[:,\s]+/, $_;
              $subnetwork2 = $string3[2];
              last;
          }
      }
      close(MYFILE3);
  }
  
  sub main_process() {
  
      init;
      create_files;
      create_hash;
  
      open(MYFILE1, 'file.txt');
      while (&lt;MYFILE1&gt;) {
          if (/.subnetwork/) {
              my @string3 = split /[:,\s]+/, $_;
              $subnetwork2 = $string3[2];
          }
          if (/.set/) {
              my @string2 = split /[:,\s]+/, $_;
              $file_name = $subnetwork2 . $string2[1] . $ext;
          }
          if (/.domain/ || /.end/ || ($. &lt; 6)) {
              my $domain = $_;
              foreach (@files) {
                  if (/.text/ &amp;&amp; /$subnetwork2/) {
                      prnt { $files_list{$_} } "$domain";
                  }
              }
          }
          elsif ($. &gt;= 6) {
              print { $files_list{$file_name} } "$_";
          }
       }
      close(MYFILE1);
      foreach my $val (values %files_list) { close($val); }
      closedir $dir;
  }
  
  main_process;
  </code></pre>
  
  <p>This script creates files in the current directory based upon the content of <code>file.txt</code>, and then open those files again.</p>
  
  <p>Then it starts processing <code>file.txt</code> and redirects the lines according to the filename set dynamically.</p>
  
  <p>This setting of the file name is also based upon the data in the file <code>file.txt</code>.</p>
  
  <p>The problem that I am facing here is that the redirection is only to a single file. That means there is some problem with the file handle.</p>
  
  <p>All the files that are expected to be created are created perfectly but the data goes into only one of them.</p>
  
  <p>I doubt if there is a problem with the file handle that I am using while redirecting.</p>
  
  <p>Could anyone please help?
  Sample input file is below:</p>
  
  <pre><code>..cnai #Generated on Thu Aug 02 18:33:18 2012 by CNAI R21D06_EC01, user tcssrpi
  ..capabilities BASIC
  .utctime 2012-08-02 13:03:18
  .subnetwork ONRM_ROOT_MO:NETSim_BAG
  .domain BSC
  .set BAG01
  AFRVAMOS="OFF"
  AWBVAMOS="OFF"
  ALPHA=0
  AMRCSFR3MODE=1,3,4,7
  AMRCSFR3THR=12,21,21
  AMRCSFR3HYST=2,3,3
  AMRCSFR3ICM=
  AMRCSFR4ICM=
  USERDATA=""
  .set BAG02
  AFRVAMOS="OFF"
  AWBVAMOS="OFF"
  ALPHA=0
  AMRCSFR3MODE=1,3,4,7
  AMRCSFR3THR=12,21,21
  AMRCSFR3HYST=2,3,3
  ..end
  </code></pre>
  
  <p>The problem that i am facing is during execution:</p>
  
  <pre><code>&gt; process.pl 
  Use of uninitialized value in ref-to-glob cast at process.pl line 79, &lt;MYFILE1&gt; line 6.
  Can't use string ("") as a symbol ref while "strict refs" in use at process.pl line 79, &lt;MYFILE1&gt; line 6.
  </code></pre>
  
  <p>The problem i can understand is with this line:</p>
  
  <pre><code>print { $files_list{$_} } "$domain";
  </code></pre>
  
  <p>but i am unable to understand why!!</p>
  
  <p>The output i need is :</p>
  
  <pre><code>&gt; cat NETSim_BAGBAG01.text 
  .set BAG01
  AFRVAMOS="OFF"
  AWBVAMOS="OFF"
  ALPHA=0
  AMRCSFR3MODE=1,3,4,7
  AMRCSFR3THR=12,21,21
  AMRCSFR3HYST=2,3,3
  AMRCSFR3ICM=
  AMRCSFR4ICM=
  USERDATA=""
  
  
  &gt; cat NETSim_BAGBAG02.text
  .set BAG02
  AFRVAMOS="OFF"
  AWBVAMOS="OFF"
  ALPHA=0
  AMRCSFR3MODE=1,3,4,7
  AMRCSFR3THR=12,21,21
  AMRCSFR3HYST=2,3,3
  &gt; 
  </code></pre>