[Israel.pm] Array of file handles

David Baird david.baird at cmcrossroads.com
Mon Dec 13 05:56:38 PST 2004


Can anyone explain why the following isn't valid:

my $files = 8;

my (@fharray);
(my $basename = $0) =~ s/\.[^.]*$//;

for my $h (0 .. ($files - 1)) {
   unlink "${basename}_$h.mif";
   open $fharray[$h], ">${basename}_$h.txt";
   print $fharray[$h] "file $h\n";
}

and yet, the following does work:

my $files = 8;

my (@fharray);
(my $basename = $0) =~ s/\.[^.]*$//;

for my $h (0 .. ($files - 1)) {
   unlink "${basename}_$h.mif";
   open $fharray[$h], ">${basename}_$h.txt";
   my $fh = $fharray[$h];
   print $fh "file $h\n";
}

The difference is that in the first case, I am trying to print to a
file handle defined as an element of an array, and in the second case,
I assign a scalar to the array element, and then print to it.

Using Perl 5.8.2

Thanks,
-David




More information about the Perl mailing list