in reply to Using a filehandle tucked into an array

I think I would do this using IO::File from the outset and would store information in a ref. to a HoH keyed by filename rather than in an array. I would start with a list of files to be processed in an array and would populate the hash with "handle" and "buffer" key/value pairs accessed via subroutines to keep it all in step. This way I have the advantage (if needed) to work with files both by name and by their index in a list of files.

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

Replies are listed 'Best First'.
Re^2: Using a filehandle tucked into an array
by chromatic (Archbishop) on Dec 30, 2006 at 19:38 UTC

    It's off-topic, but I'm really curious about your use of quoting constructs in place of quoted strings. What's your reasoning here? I've never seen it before, and I assume there's an interesting reason.

      My background is *nix, specifically SunOS/Solaris, and I had been quite happy using normal quoted strings for years. Then I installed Active Perl on a PC at home but really struggled to write one-liners because of the quoting conventions on MS Windows. When I found out about q{...} and qq{...} and applied them in one-liners they made life easy again. I have now got into the habit of using them all the time so that I don't have to make adjustments when I move between systems.

      That's the only reason really. They involve a little more typing and they are not as familiar to most as quoted strings but, for me, they are more convenient.

      Cheers,

      JohnGG

      i do this too a lot. i just feel it's superior. for one, you so often need to use " or ' in your strings, and you're free to this way, your trade is that you can't use unbalanced forms of your quote marker (so, if you use [ as in qq[] you can't write qq[ :[ = sad face ], that's a syntax error), but in the rare case you must use unbalanced brackets in a in a string, it's probably only of one kind, so just qq with another. there's something good about forcing balance too, i'd use balanced double quotes if we had them (on the same level of accessibility)

      another argument is if you want to change your double or single quote status for some reason you just have to change the "front" of the string, usually easier to find, and obviously, also half as much to search for.


      It's not what you look like, when you're doin' what you’re doin'.
      It's what you’re doin' when you’re doin' what you look like you’re doin'!
           - Charles Wright & the Watts 103rd Street Rhythm Band, Express yourself