Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: What is a pipe and why would I want to use one?

by whiteperl (Scribe)
on Jul 10, 2002 at 06:10 UTC ( [id://180690]=note: print w/replies, xml ) Need Help??


in reply to What is a pipe and why would I want to use one?

Thanks for all the replys.
My only concern now is if I open a pipe to another program if the other program automatically opens.

Senario:

I open Program 1 - Daddy -> open(SEND,"|Kiddy.pl");
Program pipes its output to Program 2 - Kiddy
Does Kiddy automatically open?
Next where does Kiddy.pl get the data from Daddy.pl
  1. Is it in STDIN as in -> read(STDIN, $from_Daddy, $ENV{CONTENT_LENGTH});
  2. Is it in -> $from_Daddy = $ENV{QUERY_STRING}; #\ This seems unlikely but you never know /#
  3. or do i get it like open(GET,"Daddy.pl|");
whiteperl
  • Comment on Re: What is a pipe and why would I want to use one?

Replies are listed 'Best First'.
Re: Re: What is a pipe and why would I want to use one?
by rjray (Chaplain) on Jul 10, 2002 at 09:12 UTC

    Using open to create the pipe for you in this fashion does in fact execute the child program, yes.

    There are no environment variables involved, so none of your examples are exactly correct. When Kiddy.pl starts, it will have a STDIN that is not connected to the TTY, like Daddy.pl has. Instead, STDIN is like any other filehandle (which it is, anyway). You read from it with "$line = <STDIN>" just as you would with any other filehandle. Whatever the Daddy.pl writed to SEND, Kiddy.pl reads from STDIN.

    Your third example is close, though. What it would do is run a separate execution of Daddy.pl when you manually ran Kiddy.pl. Then, whatever Daddy.pl wrote to STDOUT, Kiddy.pl would read from GET.

    One last thing worth noting: if your program wants to know if a given filehandle is a pipe, you can test it with the -p file-test operator:

    my $is_a_pipe = -p STDIN;

    This is very useful when testing if the program is writing to a pipe-- if it is, you may choose to format data differently, or if you were pausing at page breaks you would choose not to, etc.

    --rjray

Re: Re: What is a pipe and why would I want to use one?
by fsn (Friar) on Jul 10, 2002 at 18:30 UTC
    This is a very very simple (and probably Linuxcentric) attempt to illustrate how you can send data from one program to another using a pipe:

    ./Daddy.pl:

    #!/usr/bin/perl $i=0; open SESAME,"|./Kiddy.pl"; # Open up the pipe while ( $i++<10) { print SESAME "Testing IPC with pipes. Line $i.\n"; # Prin +t to pipe } close SESAME; # This (or the termination of the program) # closes the pipe. The other side recieves this # as an EOF (End OF File).

    ./Kiddy.pl:

    #!/usr/bin/perl while ($in=<STDIN>) { # Read from <STDIN> until <EOF> chomp $in; # Remove <NL> print "By Kiddy.pl: $in\n"; # Print out what we read }

    There are some extra fluff in Kiddy.pl, the use of chomp and $in instead of $_ f.ex., but I kept it there to make it more clear, at least to me.

    If you run the above you would get this:

    $ ./Daddy.pl
    By Kiddy.pl: Testing IPC with pipes. Line 1.
    By Kiddy.pl: Testing IPC with pipes. Line 2.
    By Kiddy.pl: Testing IPC with pipes. Line 3.
    By Kiddy.pl: Testing IPC with pipes. Line 4.
    By Kiddy.pl: Testing IPC with pipes. Line 5.
    By Kiddy.pl: Testing IPC with pipes. Line 6.
    By Kiddy.pl: Testing IPC with pipes. Line 7.
    By Kiddy.pl: Testing IPC with pipes. Line 8.
    By Kiddy.pl: Testing IPC with pipes. Line 9.
    By Kiddy.pl: Testing IPC with pipes. Line 10.
    $
    

    Note that Daddy.pl is not printing to the screen at all, Kiddy.pl is doing all that.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://180690]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2025-01-14 16:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which URL do you most often use to access this site?












    Results (42 votes). Check out past polls.