Gangabass has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks

I have little program which gives me error:

syntax error at pipe.pl line 25, near "<$result->{fh"

Here is code:

#!/usr/bin/perl use strict; use warnings; my ( $program, $arguments ) = ( "netstat", "-n" ); my @results; for ( my $n = 0; $n < 5; $n++ ){ print "Opening FH for $n..."; my $pid = open my $fh, "-|", "$program $arguments" or die $!; push @results, { pid => $pid, fh => $fh }; print " Done!\n"; } foreach my $result (@results) { print "=" x 5, $result->{pid}, "=" x 5, "\n"; my $fh = $result->{fh}; while ( my $line = <$result->{fh}> ) { print $line; } close $result->{fh}; }

At this moment i change it to:

my $fh = $result->{fh}; while ( my $line = <$fh> ) {

but i have a question why perl doesn't like that (and what to do)?

Update: This is perl, v5.8.9 built for MSWin32-x86-multi-thread.

Replies are listed 'Best First'.
Re: <> and $hash_ref->{key} construct
by Anonymous Monk on Jan 29, 2009 at 04:09 UTC
    unbalanced angle brackets, think about it
    1 2 3 | | | <$result->{fh}> \--------^
    just like unbalanced (), or {}
    D:\>perl -e"<>>" syntax error at -e line 1, at EOF Execution of -e aborted due to compilation errors. D:\>perl -e"())" syntax error at -e line 1, next char ) Execution of -e aborted due to compilation errors. D:\>perl -e"{}}" Unmatched right curly bracket at -e line 1, at end of line syntax error at -e line 1, near "}}" Execution of -e aborted due to compilation errors.
    If you were to change it to
    <$$result{fh}>
    it would be equivalent of glob , not readline

      Thanks!

Re: <> and $hash_ref->{key} construct
by johngg (Canon) on Jan 29, 2009 at 09:33 UTC

    IIRC, the expression inside the angle brackets has to be either a package filehandle (e.g. <FH>) or a simple scalar as you have used in your working code. To use a filehandle held in a data structure you can use readline, I think, so this should work.

    ... while ( my $line = readline $result->{fh} ) { ...

    I hope I've got that right and this useful to you.

    Cheers,

    JohnGG

Re: <> and $hash_ref->{key} construct
by Anonymous Monk on Jan 29, 2009 at 02:50 UTC
    Read "I/O Operators" in perlop