Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > pipe perl output to a bash command error

Reply
Thread Tools

pipe perl output to a bash command error

 
 
Tarek Elganainy
Guest
Posts: n/a
 
      02-02-2009
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
 
Reply With Quote
 
 
 
 
Josef Moellers
Guest
Posts: n/a
 
      02-02-2009
Tarek Elganainy wrote:
> 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
--
These are my personal views and not those of Fujitsu Siemens Computers!
Josef Möllers (Pinguinpfleger bei FSC)
If failure had no penalty success would not be a prize (T. Pratchett)
Company Details: http://www.fujitsu-siemens.com/imprint.html
 
Reply With Quote
 
 
 
 
Jens Thoms Toerring
Guest
Posts: n/a
 
      02-02-2009
Josef Moellers <(E-Mail Removed)> wrote:
> Tarek Elganainy wrote:
> > 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,


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
--
\ Jens Thoms Toerring ___ http://www.velocityreviews.com/forums/(E-Mail Removed)
\__________________________ http://toerring.de
 
Reply With Quote
 
snowsmash@gmail.com
Guest
Posts: n/a
 
      02-03-2009
On Feb 2, 3:18*pm, (E-Mail Removed) (Jens Thoms Toerring) wrote:
> Josef Moellers <(E-Mail Removed)> wrote:
> > TarekElganainywrote:
> > > 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,

>
> 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
> --
> * \ * Jens Thoms Toerring *___ * * *(E-Mail Removed)
> * *\__________________________ * * *http://toerring.de


 
Reply With Quote
 
snowsmash@gmail.com
Guest
Posts: n/a
 
      02-03-2009
Buffering indeed. See http://perl.plover.com/FAQs/Buffering.html. To
work around, set $| = 1 in the perl that pipes to the cat.
 
Reply With Quote
 
Tarek Elganainy
Guest
Posts: n/a
 
      02-03-2009
On Feb 3, 10:10*am, (E-Mail Removed) wrote:
> 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
 
Reply With Quote
 
snowsmash@gmail.com
Guest
Posts: n/a
 
      02-03-2009
You do not have to set it in each iteration of the while loop; you can
just set it once before the loop.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
using bash process substitution in perl pipe? Peng Yu Perl Misc 1 06-05-2010 11:24 PM
converting a sed / grep / awk / . . . bash pipe line into python hofer Python 11 09-03-2008 09:56 PM
executing bash comands with pipe Fred Perl Misc 4 06-26-2007 06:52 PM
(patch for Bash) GTK+2 + Bash William Park XML 3 07-14-2005 12:09 AM
How to read data from a bash pipe j. del C++ 13 03-28-2005 04:00 PM



Advertisments