pipeline with [open r+] problem 
Author Message
 pipeline with [open r+] problem

Hello,

I am trying to use [open] as pipeline.

In my Tcl Script, the Wiki example:

        set channel [open |bc r+]
        puts $channel "1234567890 * 987654321"
        flush $channel
        puts [gets $channel]

works correctly.

But when I try to use my own filter
(which is a Perl script beautifier.pl)
in the same way:

        set channel [open |./beautifier.pl r+]
        puts $channel $line
        flush $channel
        puts [gets $channel]

it doesn't work, the Tcl script freezes, I think it can't
read from the channel and is waiting for input.

The filter beautifier.pl should work correctly, because
when I try either:

        set channel [open "|./beautifier.pl > out" w]
        puts $channel $line
        flush $channel

or:

        set channel [open |./beautifier.pl < in r]
        puts [gets $channel]

I get what I expected, it works correctly.
I can't understand why the pipeline channel works when use
only for reading it's output ([open r])
or only for writing to it ([open w])channel,
but doesn't work
when used it both for reading and writing from Tcl script ([open r+]).

I thought that it could be a problem of synchronization
(the Tcl script may have run faster then the filter)
so I tried:

     set channel [open "|./beautifier.pl" r+]
     fileevent $channel readable "set TV::done 1"
     puts $channel "$line"
     flush $channel

     vwait TV::done

     set bline [gets $channel]

and it seems that the fileevent readable doesn't occur at all.
Perhaps the filter doesn't flush his output (but the "[open r]" version
of pipeline works OK)?
Should I send EOF to the filter so as it flushes his output?
But how to send EOF?

Well, I am pretty confused,
please, where is the problem?

Thanks

Radek



Fri, 22 Dec 2006 04:52:33 GMT  
 pipeline with [open r+] problem

Quote:

> Well, I am pretty confused,
> please, where is the problem?

Not sure.  I'd try two things.  First, try open with "w+" instead of "r+"

   [open |./beautifier.pl w+]

If that doesn't work, I might try calling perl with the ./beautifier.pl
as an argument:

   [open "|/path/to/perl ./beautifier.pl" w+]

Do either of those make it work?

--
MKS



Fri, 22 Dec 2006 05:33:50 GMT  
 pipeline with [open r+] problem

Quote:

> But when I try to use my own filter (which is a Perl script
> beautifier.pl) in the same way:

>    set channel [open |./beautifier.pl r+]
>    puts $channel $line
>    flush $channel
>    puts [gets $channel]

> it doesn't work, the Tcl script freezes, I think it can't
> read from the channel and is waiting for input.

This is Perl's fault, or rather it is the fault of your Perl script
which is probably failing to turn off output buffering (Tcl can't, and
shouldn't, turn off buffering in Perl from outside).  The answer is to
add this at or near the start of your beautifier.pl:

  $| = 1;  # Make the output "piping hot", to quote the mnemonic

There are other ways of doing the same thing; the perlvar manual page
describes them.

The other way to work around this problem is to use Expect so Perl
thinks it is talking to a terminal and writes its output immediately
(either spawning directly or using the unbuffer script).  But since
you can change the Perl code, it's easier to do that instead.

Donal.



Fri, 22 Dec 2006 16:11:33 GMT  
 pipeline with [open r+] problem

| Should I send EOF to the filter so as it flushes his output?
| But how to send EOF?

You close the fd.  Of course, you cannot read the fd after that :-/

A possible solution is to redirect the pipe input to the string itself,
as described in the exec(n) manpage (look for the << redirector).

  set fd [open [list |script << $line] r]
  set output [read $fd]  

HTH
R'



Fri, 22 Dec 2006 20:01:57 GMT  
 pipeline with [open r+] problem

The Melissa's solution doesn;t work.
The Ralf's solution is the most suitable for me,
(how could I forget about "<<" redirecting).
The Donal's solution works too, but I don't want to
change the code of the Perl filter.
Anyway I've learned something.

Thank you all for help.



Sat, 23 Dec 2006 16:55:15 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Different behaviour of open command while opening with w or r+ mode

2. Problem opening a pipeline with quotes in an argument

3. opening file vs. opening pipeline on NT.

4. Opening file vs. opening pipeline on NT.

5. Tclvfs: why no r+ file open mode?

6. set fp [open "|program" r+]

7. open |tcl r+ (how?)

8. Need to close and re-open a process pipeline

9. open a command pipeline channel

10. opening a process pipeline question

11. open & command pipelines-HELP

12. open and command line pipeline question

 

 
Powered by phpBB® Forum Software