pipe perl output to a bash command error

  • Thread starter Tarek Elganainy
  • Start date
T

Tarek Elganainy

I was wondering why the first line give me output but the second
don't, any clue!!

1. while sleep 2;do printf "1\n1\n"|perl -e 'while ( <> )
{ print; }';done|perl -e 'while ( <> ) { print; }'

2. while sleep 2;do printf "1\n1\n"|perl -e 'while ( <> )
{ print; }';done|perl -e 'while ( <> ) { print; }'|cat
 
J

Josef Moellers

Tarek said:
I was wondering why the first line give me output but the second
don't, any clue!!

1. while sleep 2;do printf "1\n1\n"|perl -e 'while ( <> )
{ print; }';done|perl -e 'while ( <> ) { print; }'

2. while sleep 2;do printf "1\n1\n"|perl -e 'while ( <> )
{ print; }';done|perl -e 'while ( <> ) { print; }'|cat

Buffering?
In the first instance, the last perl is writing to the screen, so the
output is unbuffered. In the second instance, the last perl is writing
to a pipe, hence the output is buffered and will appear only when a
couple of kilobytes have accumulated.

The first perl instance in each pipeline only does 2 lines, then exits
and flushes its buffers.

Just an (un?)educated guess,

Josef
 
J

Jens Thoms Toerring

Buffering?
In the first instance, the last perl is writing to the screen, so the
output is unbuffered. In the second instance, the last perl is writing
to a pipe, hence the output is buffered and will appear only when a
couple of kilobytes have accumulated.
The first perl instance in each pipeline only does 2 lines, then exits
and flushes its buffers.
Just an (un?)educated guess,

s/\(un\?\)//

You can also test that this is the case. E.g. if you replace

while sleep 2

with

while [ -n "qqq" ]

or something similar that's always true then, after a delay needed
to fill the buffer, you start getting output from 'cat'.

Regards, Jens
 
S

snowsmash

Josef Moellers said:
TarekElganainywrote:
Buffering?
In the first instance, the last perl is writing to the screen, so the
output is unbuffered. In the second instance, the last perl is writing
to a pipe, hence the output is buffered and will appear only when a
couple of kilobytes have accumulated.
The first perl instance in each pipeline only does 2 lines, then exits
and flushes its buffers.
Just an (un?)educated guess,

s/\(un\?\)//

You can also test that this is the case. E.g. if you replace

while sleep 2

with

while [ -n "qqq" ]

or something similar that's always true then, after a delay needed
to fill the buffer, you start getting output from 'cat'.

                               Regards, Jens
 
T

Tarek Elganainy

Buffering indeed. Seehttp://perl.plover.com/FAQs/Buffering.html. To
work around, set $| = 1 in the perl that pipes to the cat.

Thanks a lot guys!
It worked now after setting $| = 1 to work around buffering issue

while sleep 2;do printf "1\n1\n"|perl -e 'while ( <> ) { $| = 1;
print; }';done|perl -e 'while ( <> ) { $| = 1; print; }'|cat
 
S

snowsmash

You do not have to set it in each iteration of the while loop; you can
just set it once before the loop.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top