in reply to Re: Collapsing multiple file open/close
in thread Collapsing multiple file open/close

Rightoh!
Yes, i like the auto-close solution here!
What i was probing was something like :
foreach my $i ("" "1") { open (FH$i, "<${file}$i.txt") or die "${file}$i.txt"; }

but of course file handle names don't work that way.

I like your general "foreach solution" too, but, yes, that's overkill for this specific snippet.
Thanks for lightening up my understanding
best regards, allan

Replies are listed 'Best First'.
Re^3: Collapsing multiple file open/close
by ikegami (Patriarch) on Apr 13, 2005 at 16:44 UTC

    There are two problems with that:

    1) Creating symbols dynamically (FH$i) is pretty bad. I think you could use $FH{$i} instead (since Perl 5.6?), but your loop would become
    while (<$FH{''}>) { print $FH{1} $_; }

    2) You're opening both files for reading.

      Ahhhh, you're right!
      I'll leave it at that. I walk away with a better understanding of this issue.
      Interesting how clearity and beauty of code is in general correlated to compactness and efficiency, but there's that lower threshold, where the direct approach (however redundant) is preferable.
      Thanks, allan