-   Programming (
-   -   Stdout And Stdin: Can't get it to do it "on the fly"... (

Eskild 07-15-2005 05:11 AM

Stdout And Stdin: Can't get it to do it "on the fly"...
I have this very annoying problem with STDIN and STDOUT... But first let me explain my problem.

When I type: cat somefile | grep something
each line is processed, in other words I get a "live feed" from the "cat" command.

Now if I write a perl script which looks like this:


print <STDIN>;
and do the following I would expect it to "live feed" my script with the STDOUT

cat somefile |

What happens is that the "cat" command store the STDOUT in the buffer and when the cat program is done it sends the STDOUT buffer to my script.

So my question is; is there a way to force linux (or the program) to send the STDOUT in a "live feed" alike manner?

Quigi 07-15-2005 12:16 PM

Looks like a Perl issue. The function 'print' establishes array context for the expression '<STDIN>'. Your code requests that all lines from STDIN are read first, and then the resulting array is passed to print. Obviously evaluation of '<STDIN>' has to wait till it's sure no more lines are coming in, i.e., till EOF.

You might want to process each line as soon as it arrives -- the 'while (<>)' construct achieves this. Compare

perl -e 'print <STDIN>'

perl -e 'print while (<STDIN>)'
Type a few lines to each, then C-d to signal end-of-file. (BTW, that space between '>' and ')' was inserted courtesy of LinuxQuestions :) )

Eskild 07-18-2005 03:21 AM

Looks like I'm trying to do the almost impossible.
Cause it looks like that perl does handle the STDIN on the fly, because the following is processed:

$ find /* |
print while(<>);

So the problem is the program that send the STDOUT, I think...

What I wanted to do was:

$ program |

Then make the feed a mysql db with the output without having to start the program more than once, but looks like I have to make the program auto TERM and then make a cron job or something to get it to work.

But still thanks for all your help.

keefaz 07-18-2005 11:52 AM

Anything is possible...

How do you know that 'program' use STDOUT,
it may use STDERR instead to produce output

Quigi 07-19-2005 01:41 PM


Can you tell us some more about your goal? What happens when you run 'program |'? What would you want to happen? When you say "the following is processed", does it work as intended, or not?

Regarding your original question, if you run

cat somefile | perl -we 'print <STDIN>'
indeed the <> Perl construct does gather all the lines. But how can you tell the difference -- you get all of "somefile" anyway? Just the delay, if it's a long file?

To see if 'program' writes to STDERR, you could run 'program > out'. What you see was STDERR, what goes into 'out' was STDOUT.

All times are GMT -5. The time now is 01:40 PM.