<> and filehandles in hashes

B

BZ

Hi!

Can anyone please explain to me why this code doesn't work? (it gives
a syntax error on the "print while..." line):

---- snip ----

#!/usr/bin/perl
use strict;
use warnings;

my %rec;
$rec{filename} = '/etc/fstab';
open($rec{fh},$rec{filename}) or die("$! while opening");

my @arr;
push @arr, { %rec };
# next line results in a syntax error:
print while (<$arr[0]->{fh}>);

close($rec{fh}) or die("$! while closing");

---- snip ----

If I change the <$arr[0]->{fh}> to readline($arr[0]->{fh}), it works
fine, and it also works fine if I put the filehandle in a scalar
variable first:

## this works fine again
my @arr;
push @arr, { %rec };
my $fh = $arr[0]->{fh};
print while (<$fh>);


Does anyone have a clue what's going on here?
 
D

Dave Weaver

On 8 Sep 2005 11:13:03 GMT said:
If I change the <$arr[0]->{fh}> to readline($arr[0]->{fh}), it works
fine, and it also works fine if I put the filehandle in a scalar
variable first:

## this works fine again
my @arr;
push @arr, { %rec };
my $fh = $arr[0]->{fh};
print while (<$fh>);

Does anyone have a clue what's going on here?

Taken from `perldoc perlop`:

If what the angle brackets contain is a simple scalar variable
(e.g., <$foo>), then that variable contains the name of the
filehandle to input from, or its typeglob, or a reference to the
same. For example:

$fh = \*STDIN;
$line = <$fh>;

If what's within the angle brackets is neither a filehandle nor a
simple scalar variable containing a filehandle name, typeglob,
or typeglob reference, it is interpreted as a filename pattern to
be globbed, and either a list of filenames or the next filename
in the list is returned, depending on context.
 
B

BZ

Dave Weaver wrote in comp.lang.perl.misc:
Taken from `perldoc perlop`:
If what the angle brackets contain is a simple scalar variable
(e.g., <$foo>), then that variable contains the name of the
filehandle to input from, or its typeglob, or a reference to the
same. For example:
$fh = \*STDIN;
$line = <$fh>;
If what's within the angle brackets is neither a filehandle nor a
simple scalar variable containing a filehandle name, typeglob,
or typeglob reference, it is interpreted as a filename pattern to
be globbed, and either a list of filenames or the next filename
in the list is returned, depending on context.

ahh, thanks. Must have overlooked that.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top