Dear esteemed monks,

I feel a bit stupid on this issue. I was answering on a French Perl forum to a question in which the poster was trying to use symbolic references to store a number of file handles. I told the OP to use an array or hash of handles instead and outlined the basic syntax for it. I could not test it because it was too dependent on the OP's environment.

The OP said that it did not work. So I wrote a little demonstration program as follows:

my @in_fh; my $count = 0; while (my $line = <DATA>) { chomp $line; open my $IN, "<", $line or die "Cannot open file $line $!"; $in_fh[$count] = $IN; $count++; } my $i = 0; while ($i < $count) { while (my $line = <{$in_fh[$i]}> ) { print $line; } $i++; } __DATA__ one.txt two.txt
But the OP was right: this does not seem to be able to read anything from the two files.

I was able to work around the problem with the following syntax fix:

while (my $line = readline $in_fh[$i] ) {
but I am still puzzled at the fact that:
while (my $line = <{$in_fh[$i]}>) {
does not work properly.

When I check the content of $in_fh[$i], I get a glob: GLOB(0x60006f218), so this seems to be correct. But when trying to read the line, it returns the glob instead of the line from the file. I suspect that this is one case where the compiler is confused by the syntax and would need a hint about the syntax intent, but did not find a solution with the diamond <...> operator. I am fairly sure that I have already done something very similar with braces around the array element containing the file handle, but can't remember what it was exactly to get it to work. Any idea?

Update: fixed the off-by-one error mentioned by hdb, which was not in the original program, but added by mistake by me in the above code when slightly simplifying the code for the purpose of making it shorter for this post. (And, yes, I am using strictures.) Thanks hdb.


In reply to Using an array of file handles by Laurent_R

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.