title: In Perl, if possible, how do I pass a generic diamond (<>) filehandle to a subroutine?
body: <p>I have a script that reads input serially on the fly as another job outputs it, and is expected to modify and return it.  To do more complex modifications, I'd like to be able to capture this input and pass it to a subroutine, which would then return an object to my base script, where I could modify the object as needed, and then write it back out.  Right now, my code looks something like this:</p>
  
  <pre><code>while (my $line = &lt;&gt;) {
      # Do stuff here to build object
      # When end of record, call writeObject
      # Start new object unless eof
  }
  
  sub writeObject {
      # Iterate over object and print to STDOUT
  }
  </code></pre>
  
  <p>I'd like to take the first while loop and convert it into a subroutine that I can then put, along with <code>writeObject</code>, into a small module that I can reuse.  What I think I need to do is somehow pass <code>&lt;&gt;</code> to this new subroutine, <code>buildObject</code>.  I'd then build new scripts that import <code>buildObject</code> and <code>writeObject</code>, and implement a <code>modifyObject</code> routine that will change from script to script.  The other two pieces should be completely reusable, as those parts of the process never change.  Optimally, the new routine would look something like this:</p>
  
  <pre><code>sub buildObjects {
      my ($input) = @_;
      my @objects = ();
      while (&lt;$input&gt;) {
          # Build object, push to @objects when done
      }
      return \@objects;
  }
  </code></pre>
  
  <p>A script using this would look something like this:</p>
  
  <pre><code>my $obj = buildObjects(&lt;&gt;);
  
  foreach my $object (@$obj) {
      $object = modifyObject($object);
      writeObject($object);
  }
  </code></pre>