pipe perl output to a bash command error

Discussion in 'Perl Misc' started by Tarek Elganainy, Feb 2, 2009.

  1. 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
    Tarek Elganainy, Feb 2, 2009
    #1
    1. Advertising

  2. 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
    Josef Moellers, Feb 2, 2009
    #2
    1. Advertising

  3. Josef Moellers <> 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://toerring.de
    Jens Thoms Toerring, Feb 2, 2009
    #3
  4. Tarek Elganainy

    Guest

    On Feb 2, 3:18 pm, (Jens Thoms Toerring) wrote:
    > Josef Moellers <> 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  ___      
    >    \__________________________      http://toerring.de
    , Feb 3, 2009
    #4
  5. Tarek Elganainy

    Guest

    , Feb 3, 2009
    #5
  6. On Feb 3, 10:10 am, 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
    Tarek Elganainy, Feb 3, 2009
    #6
  7. Tarek Elganainy

    Guest

    You do not have to set it in each iteration of the while loop; you can
    just set it once before the loop.
    , Feb 3, 2009
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. William Park

    (patch for Bash) GTK+2 + Bash

    William Park, Jul 11, 2005, in forum: XML
    Replies:
    3
    Views:
    2,295
    William Park
    Jul 14, 2005
  2. j. del
    Replies:
    13
    Views:
    18,727
    Howard
    Mar 28, 2005
  3. hofer
    Replies:
    11
    Views:
    2,633
  4. Peng Yu
    Replies:
    1
    Views:
    247
    Alan Curry
    Jun 6, 2010
  5. Peng Yu
    Replies:
    3
    Views:
    358
    Rainer Weikusat
    May 13, 2013
Loading...

Share This Page