user1427023
user1427023

Reputation: 23

perl hash of files opening and reading

For whatever reason I'm compelled to try some big ordered files processing, I started my attempt using a hash of files in the following way:

my %fo=();#File operations hash
foreach my $fn("file1","file2","file3","file4"){
        open($fo{$fn}{"if"},"<","$fn") or die ("Error open input file $fn: $!");#Input file
        $fo{$fn}{"v"} = <$fo{$fn}{"if"}>;#read one record
}

So when print Dumper(\%fo) I get:

$VAR1 = {
      'file1' => {
                   'v' => undef,
                   'if' => \*{'::$__ANONIO__'}
                 },
      'file2' => {
                   'v' => 'GLOB(0x8f5f098)',
                   'if' => \*{'::$__ANONIO__'}
                 },
      'file3' => {
                   'v' => undef,
                   'if' => \*{'::$__ANONIO__'}
                 },
      'file4' => {
                   'v' => 'GLOB(0x8edf1e0)',
                   'if' => \*{'::$__ANONIO__'}
                 }
    };

My question is, how do I get the file being read correctly when the "pointer" is a hash? The file is being opened correctly and the files are not empty but I do not find the lines in the Dumper output and I'm not sure how/what to interpret from the GLOB(hash).

Thanks.

Upvotes: 2

Views: 192

Answers (1)

ikegami
ikegami

Reputation: 385565

<...> can be a shortcut for both readline(...) and for glob('...'). You want it to be the former, but it's the latter in this case.

You could use

readline( $fo{$fn}{"if"} )

or

<{$fo{$fn}{"if"}}>          # Add curlies around the expression.

or

use IO::Handle qw( );       # Not needed in 5.14+
$fo{$fn}{"if"}->getline()

to solve your problems.

Upvotes: 1

Related Questions