* This is a partial diff. I cleaned up a lot of whitespace issues. * In this answer, I show an example of storing a filehandle reference in a container and then using that with print.
-=head2 How can I make a filehandle local to a subroutine? How do I pass filehandles between subroutines? How do I make an array of filehandles? +=head2 How can I make a filehandle local to a subroutine? How do I pass filehandles between subroutines? How do I make an array of filehandles? X<filehandle, local> X<filehandle, passing> X<filehandle, reference> As of perl5.6, open() autovivifies file and directory handles @@ -228,6 +231,19 @@ process_file( $fh ); +If you like, you can store these filehandles in an array or a hash. +If you access them directly, they aren't simple scalars and you +need to give C<print> a little help by placing the filehandle +reference in braces. Perl can only figure it out on its own when +the filehandle reference is a simple scalar. + + my @fhs = ( $fh1, $fh2, $fh3 ); + + for( $i = 0; $i <= $#fhs; $i++ ) { + print {$fhs[$i]} "just another Perl answer, \n"; + } + + Before perl5.6, you had to deal with various typeglob idioms which you may see in older code.