title: Can I read and write to multiple filehandles simultaneously (Perl)?
body: <p>I'm trying to read from two files, and generate output in a third. I first wanted to edit the first one on the go but I didn't find a suitable method save for arrays.</p>
  
  <p>My problem is that the third file (output) is empty whenever I uncomment the "_ref_param_handling" function. <strong>BUT</strong> the following is what puzzles me the most: If I do a UNIX very basic `cat` system call on the output file at then end (see code below), it works just fine. If I open the filehandle just before and close it right after editing, it also works fine (around my <em>print FILEHANDLE LIST</em>). </p>
  
  <p>I undoubtedly am missing something here. Apart from a problem between my keyboard and my chair, what is it? A filehandle conflict? A scope problem?</p>
  
  <p>Every variable is declared and has the value I want it to have. </p>
  
  <p><strong>Edit (not applicable anymore).</strong> 
  Using IO::File on the three files didn't change anything.</p>
  
  <hr>
  
  <h2>Edit 2 : New full subroutine code</h2>
  
  <p>My code works (except when my ref already exists, but that's because of the "append" mode i think) but there might be some mistakes and <em>unperlish</em> ways of coding (sorry, Monks). I, however, <strong>use Strict and warnings</strong> !</p>
  
  <pre><code>sub _ref_edit($) {
      my $manda_def = "$dir/manda_def.list";
      my $newrefhandle;
      my $ref       = $_[0];
      (my $refout   = $ref) =~ s/empty//;
      my $refhandle;
      my $parname   = '';
      my $parvalue  = '';
      my @val;
  
      _printMan;
  
      my $flush = readline STDIN;    # Wait for &lt;enter&gt;
  
      # If one or both of the ref. and the default values are missing
      if ( !( -e $manda_def &amp;&amp; -e $ref ) ) {
          die "Cannot find $ref and/or $manda_def";
      }
  
      # Open needed files (ref &amp; default)
      open( $refhandle, "&lt;", $ref ) or die "Cannot open ref $ref : $!";
      open( $newrefhandle, "&gt;&gt;", $refout ) 
        or die "Cannot open new ref $refout : $!";
  
      # Read each line
      while ( my $refline = &lt;$refhandle&gt; ) {
      # If line read not an editable macro
          if ( $refline =~ /^define\({{(.+)}},\s+{{.*__VALUE__.*}}\)/ ){
          $parname = $1;         # $1 = parameter name captured in regexp
          # Prompt user
          $parvalue = _ref_param_handling( $parname, $manda_def );   
          # Substitution in ref
          $refline =~ s/__VALUE__/$parvalue/;
          # Param not specified and no default value
          $parvalue eq '' ? $refline=~s/__COM__/#/ : $refline=~s/__COM__//; 
          }
  
      print $newrefhandle $refline;
      }
      close $newrefhandle;
      close $refhandle;
  
      return $refout;
  }    # End ref edit  
  </code></pre>
  
  <p>the _ref_param_handle subroutine still is :</p>
  
  <pre><code>open( $mde, '&lt;', $_[1] )
        or die "Cannot open mandatory/default list $_[1] : $!";
  
      # Read default/mandatory file list 
      while (&lt;$mde&gt;) {       
         ( $name, $manda, $default, $match, $descript ) = split( /\s+/, $_, 5 ); 
         next if ( $name !~ $ref_param );  # If param read differs from parname
  
      (SOME IF/ELSE)
  
      } # End while &lt;MDE&gt;
      close $mde;
      return $input;
  }
  </code></pre>
  
  <p>Extract from manda_def file :</p>
  
  <pre><code>NAME  Mandatory? Default Match      Comm.
  PORT          y NULL  ^\d+$ Database port
  PROJECT       y NULL  \w{1,5}   Project name
  SERVER        y NULL  \w+           Server name
  modemRouting  n NULL  .+        
  modlib        y bin   .+        
  modules       y sms   .+
  </code></pre>
  
  <p>Extract from ref_file :</p>
  
  <pre><code>define({{PORT}},         {{__VALUE__}})dnl
  define({{PROJECT}},      {{__VALUE__}})dnl
  define({{SERVER}},       {{__VALUE__}})dnl
  define({{modemRouting}}, {{__COM__{{$0}} '__VALUE__'}})dnl
  define({{modlib}},       {{__COM__{{$0}} '__VALUE__'}})dnl
  define({{modules}},      {{__COM__{{$0}} '__VALUE__'}})dnl
  </code></pre>
  
  <p>Any help appreciated.</p>