Interactive programs & Teeing

Discussion in 'Perl Misc' started by conrado.blasco@gmail.com, Nov 27, 2006.

  1. Guest

    I'm trying to write an interactive script that gets commands from
    STDIN, prints the results to STDOUT, *and* tees the results to a log
    file.

    My problem is that when I add the teeing line to the program (see
    below), the script doesn't print anything to STDOUT anymore (not even
    the prompt). When I comment out that line, everything works fine
    (except that I don't get the log file).

    (Well, it actually still prints something, but only when the session is
    over.)

    It seems to me that I'm missing something very obvious, but I can't
    figure out... Any help appreciated.

    Relevant excerpt below:

    #!/usr/bin/env perl
    use warnings; # Needed since '-w' won't work on the shebang line
    use strict;
    use File::Basename;

    my $my_name = fileparse($0);

    # STDOUT prints on the screen as well as on to log file
    open (STDOUT, "| tee ./" . $my_name . ".log")
    or die "Teeing error: $!\n";

    my $prompt = "$my_name > ";

    # Main loop, read and parse standard input
    print $prompt;
    while (<STDIN>) {
    parse_cmdline($_);
    print $prompt;
    }
     
    , Nov 27, 2006
    #1
    1. Advertising

  2. boyd Guest

    In article <>,
    wrote:

    > I'm trying to write an interactive script that gets commands from
    > STDIN, prints the results to STDOUT, *and* tees the results to a log
    > file.
    >
    > My problem is that when I add the teeing line to the program (see
    > below), the script doesn't print anything to STDOUT anymore (not even
    > the prompt). When I comment out that line, everything works fine
    > (except that I don't get the log file).
    >
    > (Well, it actually still prints something, but only when the session is
    > over.)
    >
    > It seems to me that I'm missing something very obvious, but I can't
    > figure out... Any help appreciated.
    >
    > Relevant excerpt below:
    >
    > #!/usr/bin/env perl
    > use warnings; # Needed since '-w' won't work on the shebang line
    > use strict;
    > use File::Basename;
    >
    > my $my_name = fileparse($0);
    >
    > # STDOUT prints on the screen as well as on to log file
    > open (STDOUT, "| tee ./" . $my_name . ".log")
    > or die "Teeing error: $!\n";
    >
    > my $prompt = "$my_name > ";
    >
    > # Main loop, read and parse standard input
    > print $prompt;
    > while (<STDIN>) {
    > parse_cmdline($_);
    > print $prompt;
    > }


    It sounds like a buffering problem. Try adding $|++; near the top.

    Boyd
     
    boyd, Nov 27, 2006
    #2
    1. Advertising

  3. On 11/27/2006 04:09 PM, wrote:
    > I'm trying to write an interactive script that gets commands from
    > STDIN, prints the results to STDOUT, *and* tees the results to a log
    > file. [...]
    >
    > (Well, it actually still prints something, but only when the session is
    > over.)
    > [...]


    It's best just to open the log file and print twice: once to STDOUT and
    once to the log file.

    The combination of Perl's buffering and tee's buffering result in your
    inability to get immediate output if STDOUT is sending to tee.


    --
     
    Mumia W. (reading news), Nov 28, 2006
    #3
  4. Guest

    boyd wrote:
    > In article <>,
    > wrote:
    >
    > > I'm trying to write an interactive script that gets commands from
    > > STDIN, prints the results to STDOUT, *and* tees the results to a log
    > > file.
    > >
    > > My problem is that when I add the teeing line to the program (see
    > > below), the script doesn't print anything to STDOUT anymore (not even
    > > the prompt). When I comment out that line, everything works fine
    > > (except that I don't get the log file).
    > >
    > > (Well, it actually still prints something, but only when the session is
    > > over.)
    > >
    > > It seems to me that I'm missing something very obvious, but I can't
    > > figure out... Any help appreciated.
    > >
    > > Relevant excerpt below:
    > >
    > > #!/usr/bin/env perl
    > > use warnings; # Needed since '-w' won't work on the shebang line
    > > use strict;
    > > use File::Basename;
    > >
    > > my $my_name = fileparse($0);
    > >
    > > # STDOUT prints on the screen as well as on to log file
    > > open (STDOUT, "| tee ./" . $my_name . ".log")
    > > or die "Teeing error: $!\n";
    > >
    > > my $prompt = "$my_name > ";
    > >
    > > # Main loop, read and parse standard input
    > > print $prompt;
    > > while (<STDIN>) {
    > > parse_cmdline($_);
    > > print $prompt;
    > > }

    >
    > It sounds like a buffering problem. Try adding $|++; near the top.
    >
    > Boyd


    That worked, thanks,

    Conrado
     
    , Nov 28, 2006
    #4
    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. WJ
    Replies:
    2
    Views:
    526
  2. -H
    Replies:
    3
    Views:
    359
    Jack Klein
    Aug 19, 2003
  3. Replies:
    12
    Views:
    1,684
    Dave Thompson
    Jan 10, 2005
  4. -H

    teeing parent+childs output

    -H, Aug 18, 2003, in forum: C Programming
    Replies:
    3
    Views:
    330
    Jack Klein
    Aug 19, 2003
  5. Allan Adler

    system with several interactive programs

    Allan Adler, Apr 5, 2005, in forum: C Programming
    Replies:
    2
    Views:
    267
    Allan Adler
    Apr 5, 2005
Loading...

Share This Page