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

what am i doing wrong? first program net.pl
my $count =0; for (1.10){ warn "Writing line $_\n"; #!/usr/bin/perl use strict; $SIG{PIPE} = 'IGNORE'; open (PIPE,"| net2.pl") or die "Cannot open pipe:$!"; select PIPE ; $ |=1; select STDOUT; my $count =0; for (1.10){ warn "Writing line $_\n"; if (print PIPE "This is line number $_\n") { $count ++; } else { warn " An error occured during writing :$!"; last; } sleep 1; } close PIPE or die " Cannot close pipe : $!"; print "Wrote $count lines of text \n"; ~
second program net2.pl
#!/usr/bin/perl use strict; for (1.3) { last unless defined (my $line = <>); warn " Read_three,got $line\n"; }
OUTPUT ./net.pl Writing line 1.1 Cannot close pipe : at ./net.pl line 19. 6 + Stopped (SIGTTIN) ./net2.pl &

Replies are listed 'Best First'.
Re: PIPE problem
by borisz (Canon) on Jan 15, 2004 at 18:47 UTC
    your program whould work, if you replace 1.3 with 1..3 and 1.10 with 1..10. .. is the range operator. And you may remove the first 3 lines from your example. Ahh and a typo on $ |= 1; it should read $| = 1; here the whole thing again.
    #!/usr/bin/perl use strict; $SIG{PIPE} = 'IGNORE'; open( PIPE, "|net2.pl" ) or die "Cannot open pipe:$!"; select PIPE; $| = 1; select STDOUT; my $count = 0; for ( 1 .. 10 ) { warn "Writing line $_\n"; if ( print PIPE "This is line number $_\n" ) { $count++; } else { warn " An error occured during writing :$!"; last; } sleep 1; } close PIPE or die " Cannot close pipe : $!"; print "Wrote $count lines of text \n";
    And net2.pl
    #!/usr/bin/perl use strict; for (1..3) { last unless defined (my $line = <>); warn " Read_three,got $line\n"; }
    Boris
Re: PIPE problem
by ysth (Canon) on Jan 15, 2004 at 18:45 UTC
    The range operator is .. , not . , so I think you you want 1..3 and 1..10.

    Also, in your close PIPE or die, be aware that close on a pipe doesn't always set $!. perldoc -f close gives example:

    close OUTPUT # wait for sort to finish or warn $! ? "Error closing sort pipe: $!" : "Exit status $? from sort";
    Update: changing both scripts to use .. instead of . and removing the extra stuff before #!/usr/bin/perl in net.pl gives this output:
    $ perl -w net1.pl Writing line 1 Read_three,got This is line number 1 Writing line 2 Read_three,got This is line number 2 Writing line 3 Read_three,got This is line number 3 Writing line 4 An error occured during writing :Broken pipe at net1.pl line 14. Wrote 3 lines of text
    which looks to me like what you should be expecting. If you have other expectations, please share them so others can help you further.

    Update: also added a -w to net2.pl's shebang line

Re: PIPE problem
by pg (Canon) on Jan 15, 2004 at 18:39 UTC

    Your net.pl missing bracket. It would be difficult for people to understand and help.

    By the way, I would suggest you to indent your code ;-)

    Update:

    A common situation for pipe close to fail is that, IO buffer is not flushed.