Couple of things :)

(1) You need to localize you global filehandle FILE. Otherwise all filehandles stored away in the array will refer to the most recently opened file...

local *FILE; open FILE, "<", "./$nbdc_dir/$file" or die ... ...

Or, even better, make use of the feature of more modern versions of Perl to accept a lexical variable in the open() statement:

open my $fh, "<", "./$nbdc_dir/$file" or die ... $nbdc_filehandles[$index] = $fh;

or even simply (if @nbdc_filehandles is lexical)

open $nbdc_filehandles[$index], "<", "./$nbdc_dir/$file" or die ...

The diamond operator is somewhat "special" syntactically, in that

(2) you need to make an explicit assignment if you use it outside of loops:

 my $line = <$fh>;

or

 $_ = <$fh>;

(3) no whitespace is allowed within the angular brackets:

$_ = <$fh>; # OK $_ = < $fh >; # not OK

(4) and, as jettero and BrowserUK pointed out, it doesn't accept array expressions, so you have to use an intermediate flat scalar, or readline():

my $fh = $nbdc_filehandles[$index]; $_ = <$fh>; # instead of # $_ = <$nbdc_filehandles[$index]>;

So, your code would look like

my @nbdc_filehandles; my @nbdc_data; my $index=0; foreach my $file (@nbdc_files) { open my $fh, "<", "./$nbdc_dir/$file" or die "Can't open ./$nbdc_d +ir/$file"; $nbdc_filehandles[$index] = $fh; #get the first set of data values $_ = <$fh>; $_ = <$fh>; chomp; #get rid of trailing \n $nbdc_data[$index] = [ split(/\t/, $_) ]; # ... $index++; } # then sometime later, to reuse the stored filehandles my $fh = $nbdc_filehandles[$idx]; my $line = <$fh>; # ...

In reply to Re: Using a filehandle tucked into an array by almut
in thread Using a filehandle tucked into an array by redbeard

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.