unix pipes to perl scripts

Discussion in 'Perl Misc' started by trt., Mar 14, 2005.

  1. trt.

    trt. Guest

    Got a problem piping command output to a perl script, in the test below
    the loop accepts the piped files but it also interferes with my STDIN!

    ===test=============================
    $ls -1 *.cfg | ./pp
    file: hosts.cfg
    file: pseries.cfg

    type something : you typed :
    ===test=============================

    I do not get the chance to type something in after "type something"...

    Do i have to flush some variable inbetween the foreach loop and
    <STDIN>?
    Is looping trough <ARGV> or <> the best way to catch piped input?

    ===code=============================
    $cat ./pp
    #!/usr/bin/perl

    while ( <> ) {
    print "file: $_";

    };

    print "\ntype something : ";
    $answer = <STDIN>;
    print "you typed : $answer \n";
    ===code=============================

    nb: also tried open FH, "-|" ... close FH, no difference.

    thanks!
     
    trt., Mar 14, 2005
    #1
    1. Advertising

  2. trt.

    Guest

    trt. wrote:
    >
    > Got a problem piping command output to a perl script,
    > in the test below the loop accepts the piped files but
    > it also interferes with my STDIN!
    >
    > ===test=============================
    > $ls -1 *.cfg | ./pp
    > file: hosts.cfg
    > file: pseries.cfg
    >
    > type something : you typed :
    > ===test=============================
    >
    > I do not get the chance to type something in after
    > "type something"...
    >
    > Do i have to flush some variable inbetween the foreach
    > loop and <STDIN>?
    > Is looping trough <ARGV> or <> the best way to catch
    > piped input?
    >
    > ===code=============================
    > $cat ./pp
    > #!/usr/bin/perl
    >
    > while ( <> ) {
    > print "file: $_";
    >
    > };
    >
    > print "\ntype something : ";
    > $answer = <STDIN>;
    > print "you typed : $answer \n";
    > ===code=============================



    Try adding the lines:

    $| = 1; # autoflush
    open(STDIN, "</dev/tty") or die $!;

    right before the line that prints "type something". This will make any
    subsequent reads from STDIN read from the terminal (and not from the
    pipe).

    I hope this helps!

    -- Jean-Luc
     
    , Mar 14, 2005
    #2
    1. Advertising

  3. trt.

    trt. Guest

    I tried using $|=1 but opening the STDIN filehandle for /dev/tty did do
    the trick -- nice, thank you!
     
    trt., Mar 14, 2005
    #3
  4. trt. wrote:
    > Got a problem piping command output to a perl script, in the test below
    > the loop accepts the piped files but it also interferes with my STDIN!
    >
    > ===test=============================
    > $ls -1 *.cfg | ./pp
    > file: hosts.cfg
    > file: pseries.cfg
    >
    > type something : you typed :
    > ===test=============================
    >
    > I do not get the chance to type something in after "type something"...
    >
    > Do i have to flush some variable inbetween the foreach loop and
    > <STDIN>?
    > Is looping trough <ARGV> or <> the best way to catch piped input?


    The STDIN of your pp script is redirected from the keyboard to the
    STDOUT of the ls command. It is not automagically reconnected to the
    keyboard once the ls command has terminated.
    It would not be a good idea to reconnect to the keyboard, as quite a lot
    of scripts depend upon pipelines to perform certain tasks (this is a
    design feature of the UN*X/Linux architecture).

    If you must read some more input from the keyboard, you can open /dev/tty:

    #!/usr/bin/perl

    while ( <> ) {
    print "file: $_";

    };

    open STDIN, '<', '/dev/tty'; #################################
    print "\ntype something : ";
    $answer = <STDIN>;
    print "you typed : $answer \n";

    Note that you'll get problems with buffering, though.

    Josef
    --
    Josef Möllers (Pinguinpfleger bei FSC)
    If failure had no penalty success would not be a prize
    -- T. Pratchett
     
    Josef Moellers, Mar 14, 2005
    #4
  5. trt. wrote:

    > Got a problem piping command output to a perl script, in the test below
    > the loop accepts the piped files but it also interferes with my STDIN!
    >
    > ===test=============================
    > $ls -1 *.cfg | ./pp
    > file: hosts.cfg
    > file: pseries.cfg
    >
    > type something : you typed :
    > ===test=============================
    >
    > I do not get the chance to type something in after "type something"...


    Well, of course this interferes with your STDIN. You told it to. The
    pipeline you set up in the shell command made the output of "ls -l *.cfg"
    STDIN for pp, not your terminal. <> reads STDIN if you don't give the perl
    script any arguments, which you did not. So it reads STDIN to end of
    file (which happens when ls -l *.cfg is finished). Then when you try
    to read STDIN again, it's already reached end of file, so it just
    returns with no data.
    >
    > Do i have to flush some variable inbetween the foreach loop and
    > <STDIN>?
    > Is looping trough <ARGV> or <> the best way to catch piped input?
    >
    > ===code=============================
    > $cat ./pp
    > #!/usr/bin/perl
    >
    > while ( <> ) {
    > print "file: $_";
    >
    > };
    >
    > print "\ntype something : ";
    > $answer = <STDIN>;
    > print "you typed : $answer \n";
    > ===code=============================
    >
    > nb: also tried open FH, "-|" ... close FH, no difference.
    >
    > thanks!


    --
    Christopher Mattern

    "Which one you figure tracked us?"
    "The ugly one, sir."
    "...Could you be more specific?"
     
    Chris Mattern, Mar 14, 2005
    #5
    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. trt.
    Replies:
    1
    Views:
    618
    Michael Abootorab
    Mar 15, 2005
  2. Ganesh
    Replies:
    4
    Views:
    389
    Thomas Weidenfeller
    Jan 31, 2005
  3. potato

    Java and UNIX pipes

    potato, Jul 22, 2005, in forum: Java
    Replies:
    3
    Views:
    640
  4. Ren Patterson

    Perl scripts for Unix on my windows machine

    Ren Patterson, Oct 14, 2003, in forum: Perl Misc
    Replies:
    23
    Views:
    295
    Eric Bohlman
    Oct 17, 2003
  5. Replies:
    13
    Views:
    583
    Anno Siegel
    Sep 10, 2007
Loading...

Share This Page