1

I have a hash (%filehandle) which stores filehandles as its values. But I can not print them.

My hash looks like this:

my %filehandle;
foreach my $output (@outputs){
  foreach my $fp (@fp_values_array){
    $filehandle{$output}{$fp}=undef;
    }
}
print Dumper \%filehandle;

$VAR1 = {

          'GMAF' => {
                      '0.05' => \*{'::$__ANONIO__'},
                      '1' => \*{'::$__ANONIO__'},
                      '0.001' => \*{'::$__ANONIO__'}
                    }
        };

I know that the problem is that the values are references but I'm starting with perl and I don't know how to acces to them...

I have tried something like this:

print "$_\n" for (keys ${$filehandle{GMAF}{$fp_value_array}});

But it doesn't work.

Type of argument to keys on reference must be unblessed hashref or arrayref at report.pl line 369, <INPUT> line 5000.

If someone can help me I would be very grateful.

Thanks in advanced!

ikegami
  • 367,544
  • 15
  • 269
  • 518
userbio
  • 115
  • 3
  • 14
  • What do you mean by 'you cannot print them'? Filehandles are references to an external file, socket, pipe, etc, that allow you to access them. You can not print them - but you can print TO them. Is that what you're asking? – Oesor May 15 '14 at 15:41
  • but these filehandles, as you said, are references to a filename output, aren't they? My goal is printing these output files name. – userbio May 15 '14 at 15:51
  • 1
    You use a name as one way to create a filehandle. A filehandle does not contain a name. It may be a reference to a socket, or a pipe, or something that does not have a filesystem name. Read http://stackoverflow.com/questions/2813092/can-i-find-a-filename-from-a-filehandle-in-perl - it can be done in some cases, but not always. – Oesor May 15 '14 at 15:52

2 Answers2

1

print "$_\n" for (keys ${$filehandle{GMAF}{$fp_value_array}});

Should be:

for my $fp_value_array (keys %{$filehandle{GMAF}}) {
    print $filehandle{GMAF}{$fp_value_array};
}

Which dereferences the hashref stored in $filehandle{GMAF}, uses keys to get the keys of it, and iterates over the keys to stringify and print the reference to the filehandle value stored in each key.

Oesor
  • 6,632
  • 2
  • 29
  • 56
0

File handles don't have file names associated with them. They wrap file descriptors. There are OS-specific ways of obtaining names from file descriptors (if any), but your best bet is to place the name in the hash along with the handle.

{
   GMAF => {
      '0.05' => {
         fn => ...,
         fh => ...,
      },
      '1' => {
         fn => ...,
         fh => ...,
      },
      '0.001' => {
         fn => ...,
         fh => ...,
      },
   },
}
ikegami
  • 367,544
  • 15
  • 269
  • 518