Update: I misread the assignment. A single pipe-pair is all that's needed for the assignment, not two. The assignment asks for message(s) to come from the user, likely from STDIN. The demonstration is left intact and not changed in order to demonstrate bi-directional communication via pipes.

Greetings grjoe21, and welcome to the monastery.

A pipe is uni-directional whereas a socket is bi-directional. Therefore, the demonstration below requires two pipe pairs for bi-directional communication between the parent and child processes.

#!/usr/bin/env perl use strict; use warnings; # Also, see Interactive Client with IO::Socket for another # demonstration at: http://perldoc.perl.org/perlipc.html pipe( my $p_rdr, my $c_wtr ); pipe( my $c_rdr, my $p_wtr ); autoflush $c_wtr, 1; autoflush $p_wtr, 1; my $pid = fork() // die "can't fork: $!"; if ( $pid ) { # parent process my $reply; while ( defined ( $reply = <$p_rdr> ) ) { last if ( length $reply == 1 ); print $p_wtr "Hello, $reply"; } waitpid $pid, 0; } else { # child process my $reply; my @names = qw/ Sun Moon Wind Air /; for my $name ( @names ) { print $c_wtr "$name\n"; $reply = <$c_rdr>; print $reply; } print $c_wtr "\n"; exit 0; }

Output.

Hello, Sun Hello, Moon Hello, Wind Hello, Air

Regards, Mario


In reply to Re: Creating a Pipe by marioroy
in thread Creating a Pipe by grjoe21

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.