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.
|