in reply to Collapsing multiple file open/close

The snippet doesn't compress nicely. Now, if you had more repetition, we might be able to do something. If this is just a sample of your code and you have more repetitiion in your real code, can you show it to us? I can't predict how this code would expand.

You could let Perl close the handles for you:

{ local *FH; open(FH, "< ${file}.txt") or die "Can't open ${file}. +txt: $!\n"; local *FH1; open(FH1, "> ${file}1.txt") or die "Can't create ${file +}1.txt: $!\n"; while(<FH>) { print FH1 $_; } }

or in newer versions of Perl:

{ open(my $fh, '<', "${file}.txt") or die "Can't open ${file}.txt: +$!\n"; open(my $fh1, '>', "${file}1.txt") or die "Can't create ${file}1.tx +t: $!\n"; while (<$fh>) { print $fh1 $_; } }

But here is the answer to the question you asked. As I've said, it's not very useful.

my @file_data = ( [ undef, "${file}.txt", 'r' ], [ undef, "${file}1.txt", 'w' ], ); foreach (@file_data) { my $name = $_->[1]; my $mode = $_->[2]; $_->[0] = IO::File->new($name, $mode) or die("Can't " . ($mode eq 'w' ? 'create' : 'open') . " file $name +: $!\n"); } while (<$file_data[0][0]>) { print $file_data[1][0] $_; } close($_->[0]) foreach @file_data;

Replies are listed 'Best First'.
Re^2: Collapsing multiple file open/close
by ady (Deacon) on Apr 13, 2005 at 16:35 UTC
    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

      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