[Israel.pm] file handle in hash?

Mikhael Goikhman migo at homemail.com
Mon Oct 22 12:31:30 PDT 2007


On 22 Oct 2007 21:16:21 +0200, Shmuel Fomberg wrote:
> 
> What I meant to write is that this code works:
> ----------
> use strict;
> use warnings;
> open my $fh, ">", "foofile.txt";
> my %hh = (handle => $fh);
> my $f2 = $hh{handle};
> print $f2 "something";
> -----------
> 
> while this code does not:
> ----------
> use strict;
> use warnings;
> open my $fh, ">", "foofile.txt";
> my %hh = (handle => $fh);
> print $hh{handle} "something";
> -----------
> 
> why?

This is the way perl parses FILEHANDLE syntax. Read "perldoc -f print"
for the explanation. The correct syntax is:

  print { $hh{handle} } "something";

Or you may write it this way:

  use IO::Handle;
  $hh{handle}->print("something");

Rehards,
Mikhael.

-- 
perl -e 'print+chr(64+hex)for+split//,d9b815c07f9b8d1e'



More information about the Perl mailing list