In the script below I am reading four data files, spw592341.dataA to D, each containing a few lines that can easily be identified. Here's the script
use strict; use warnings; use Data::Dumper; use IO::File; print qq{Files to read\n}; my @dataFiles = glob q{spw592341.data*}; print qq{ $_\n} for @dataFiles; print qq{\n}; my $rhFiles = {}; foreach my $file (@dataFiles) { openFile($file); getLine($file); } showData(q{After reading first line}); foreach my $file (@dataFiles) { getLine($file); } showData(q{All files read again}); getLine($dataFiles[2]); showData(q{Read third file in list}); getLine(q{spw592341.dataD}); showData(q{Read spw592341.dataD}); foreach my $file (@dataFiles) { closeFile($file); } showData(q{After closing all files}); sub closeFile { my $file = shift; $rhFiles->{$file}->{handle}->close() or die qq{close: $file: $!\n}; delete $rhFiles->{$file}->{handle}; } sub getLine { my $file = shift; $rhFiles->{$file}->{buffer} = $rhFiles->{$file}->{handle}->getline(); chomp $rhFiles->{$file}->{buffer}; } sub openFile { my $file = shift; my $fh = IO::File->new($file, O_RDONLY) or die qq{open: $file: $!\n}; $rhFiles->{$file}->{handle} = $fh; } sub showData { my $msg = shift; print qq{\n$msg\n}; my $dd = Data::Dumper->new( [$rhFiles], [qw{rhFiles}])->Indent(1); print $dd->Dumpxs(); }
The output is just Data::Dumper output of the hash ref. used to hold the data.
Files to read spw592341.dataA spw592341.dataB spw592341.dataC spw592341.dataD After reading first line $rhFiles = { 'spw592341.dataB' => { 'handle' => bless( \*Symbol::GEN1, 'IO::File' ), 'buffer' => 'spw592341:dataB:line1' }, 'spw592341.dataD' => { 'handle' => bless( \*Symbol::GEN3, 'IO::File' ), 'buffer' => 'spw592341:dataD:line1' }, 'spw592341.dataA' => { 'handle' => bless( \*Symbol::GEN0, 'IO::File' ), 'buffer' => 'spw592341:dataA:line1' }, 'spw592341.dataC' => { 'handle' => bless( \*Symbol::GEN2, 'IO::File' ), 'buffer' => 'spw592341:dataC:line1' } }; All files read again $rhFiles = { 'spw592341.dataB' => { 'handle' => bless( \*Symbol::GEN1, 'IO::File' ), 'buffer' => 'spw592341:dataB:line2' }, 'spw592341.dataD' => { 'handle' => bless( \*Symbol::GEN3, 'IO::File' ), 'buffer' => 'spw592341:dataD:line2' }, 'spw592341.dataA' => { 'handle' => bless( \*Symbol::GEN0, 'IO::File' ), 'buffer' => 'spw592341:dataA:line2' }, 'spw592341.dataC' => { 'handle' => bless( \*Symbol::GEN2, 'IO::File' ), 'buffer' => 'spw592341:dataC:line2' } }; Read third file in list $rhFiles = { 'spw592341.dataB' => { 'handle' => bless( \*Symbol::GEN1, 'IO::File' ), 'buffer' => 'spw592341:dataB:line2' }, 'spw592341.dataD' => { 'handle' => bless( \*Symbol::GEN3, 'IO::File' ), 'buffer' => 'spw592341:dataD:line2' }, 'spw592341.dataA' => { 'handle' => bless( \*Symbol::GEN0, 'IO::File' ), 'buffer' => 'spw592341:dataA:line2' }, 'spw592341.dataC' => { 'handle' => bless( \*Symbol::GEN2, 'IO::File' ), 'buffer' => 'spw592341:dataC:line3' } }; Read spw592341.dataD $rhFiles = { 'spw592341.dataB' => { 'handle' => bless( \*Symbol::GEN1, 'IO::File' ), 'buffer' => 'spw592341:dataB:line2' }, 'spw592341.dataD' => { 'handle' => bless( \*Symbol::GEN3, 'IO::File' ), 'buffer' => 'spw592341:dataD:line3' }, 'spw592341.dataA' => { 'handle' => bless( \*Symbol::GEN0, 'IO::File' ), 'buffer' => 'spw592341:dataA:line2' }, 'spw592341.dataC' => { 'handle' => bless( \*Symbol::GEN2, 'IO::File' ), 'buffer' => 'spw592341:dataC:line3' } }; After closing all files $rhFiles = { 'spw592341.dataB' => { 'buffer' => 'spw592341:dataB:line2' }, 'spw592341.dataD' => { 'buffer' => 'spw592341:dataD:line3' }, 'spw592341.dataA' => { 'buffer' => 'spw592341:dataA:line2' }, 'spw592341.dataC' => { 'buffer' => 'spw592341:dataC:line3' } };
I hope this is of use.
Cheers,
JohnGG
In reply to Re: Using a filehandle tucked into an array
by johngg
in thread Using a filehandle tucked into an array
by redbeard
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |