You are closing your pipe handles in the wrong place. Also if you read with angle brackets, you need the proper line terminator (default \n).

#!/usr/bin/perl # http://perlmonks.org/?node_id=1173754 use strict; use warnings; my ($reader, $writer); pipe $reader, $writer or die "$! on pipe"; $writer->autoflush; if( my $pid = fork ) # parent { close $reader; for (1 .. 5) { my $line = "-- $_ --"; print "PARENT: Going to transfer line $line\n"; print $writer "$line\n"; sleep 1; } close $writer; print "PARENT: closed writer, waiting for child to exit\n"; 1 while wait > 0; print "PARENT: exiting\n"; } elsif( defined $pid ) # child { close $writer; while( 1 ) { defined( my $line = <$reader> ) or last; print "CHILD: Received line $line"; } close $reader; print "CHILD: exiting\n"; exit; } else { die "$! on fork"; }

produces:

PARENT: Going to transfer line -- 1 -- CHILD: Received line -- 1 -- PARENT: Going to transfer line -- 2 -- CHILD: Received line -- 2 -- PARENT: Going to transfer line -- 3 -- CHILD: Received line -- 3 -- PARENT: Going to transfer line -- 4 -- CHILD: Received line -- 4 -- PARENT: Going to transfer line -- 5 -- CHILD: Received line -- 5 -- PARENT: closed writer, waiting for child to exit CHILD: exiting PARENT: exiting

In reply to Re: pipe used to send continuously to a TCP Socket does not work by tybalt89
in thread pipe used to send continuously to a TCP Socket does not work by Bloehdian

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.