On Sun, 9 Feb 2003 19:23:16 +0100, [EMAIL PROTECTED] (Anthony) wrote:

>Hi,
>
>But like i said on my second post i wanted to learn how to receive message
>from pipe.

OK, here are two simple scripts which should show you the idea.
One is a reader and one is a writer.  Always start and stop the
reader first, or you may get some blocking on perl56; but perl58
seems to work good either way on my machine. I don't know why
there is a difference.

So start 2 xterms side by side, have both scripts in the same
directory, then start the reader, then start the writer, and watch them
go. Hit control-c to stop them, stop the reader first.

Run them different ways, try to figure out why the scripts
hang if you manage to hang them.

Then look at how hard the code is to use for anything else than this
simple example. Then you will see why the modules in IPC
are mentioned as the way to go.

Don't keep asking for more help on this, because most of the
programmers never use 2 pipes for IPC.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#!/usr/bin/perl56
#this is the named-pipe reader
#start the reader first, since it makes the pipes
#the trick is to close the pipe after each write, so
#the scripts don't hang.
use warnings;
use strict;
use POSIX 'mkfifo';

$|=1;
mkfifo( 'b2a', 0666 ) unless -p 'b2a';
mkfifo( 'a2b', 0666 ) unless -p 'a2b';

while(1){
open(INB, "<b2a")  or die $!;
my $rv = read(INB, my $buffer, 4096) or die "Couldn't read from INB :
$!\n";
              # $rv is the number of bytes read,
              # $buffer holds the data read

print STDOUT "$buffer";

(my $num)= $buffer =~ /^(\d+).*/;

open(OUTA, ">a2b")  or die $!;
select OUTA; $|=1;
print OUTA "You sent $num\n";
close OUTA;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

#!/usr/bin/perl56
#this is the writer
use strict;
use warnings;
use POSIX 'mkfifo';

$|=1;
mkfifo( 'b2a', 0666 ) unless -p 'b2a';
mkfifo( 'a2b', 0666 ) unless -p 'a2b';

my $count=0;
while(1){
my $input = "$count : another bit of data";
open(OUTB, ">b2a") or die $!;
print OUTB "$input\n";
close OUTB;

open(INA, "<a2b") or die $!;
my $rv = read(INA, my $buffer, 4096) or die "Couldn't read from HANDLE :
$!\n";
        # $rv is the number of bytes read,
        # $buffer holds the data read

print STDOUT "$buffer";

sleep(1);
$count++;
}



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to