<> and filehandles in hashes

Discussion in 'Perl Misc' started by BZ, Sep 8, 2005.

  1. BZ

    BZ Guest

    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?

    --
    BZ
     
    BZ, Sep 8, 2005
    #1
    1. Advertising

  2. BZ

    Dave Weaver Guest

    On 8 Sep 2005 11:13:03 GMT, BZ <> wrote:
    ...
    > 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.
     
    Dave Weaver, Sep 8, 2005
    #2
    1. Advertising

  3. BZ

    BZ Guest

    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.

    --
    BZ
     
    BZ, Sep 8, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ben Holness

    Hashes of Hashes via subs

    Ben Holness, Oct 5, 2003, in forum: Perl
    Replies:
    8
    Views:
    588
    Ben Holness
    Oct 8, 2003
  2. Steven Arnold

    using hashes as keys in hashes

    Steven Arnold, Nov 23, 2005, in forum: Ruby
    Replies:
    3
    Views:
    183
    Mauricio Fernández
    Nov 23, 2005
  3. kazaam
    Replies:
    12
    Views:
    294
    Matthias Wächter
    Sep 13, 2007
  4. Tim O'Donovan

    Hash of hashes, of hashes, of arrays of hashes

    Tim O'Donovan, Oct 27, 2005, in forum: Perl Misc
    Replies:
    5
    Views:
    233
  5. Replies:
    3
    Views:
    233
Loading...

Share This Page