Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Interactive programs & Teeing

Reply
Thread Tools

Interactive programs & Teeing

 
 
conrado.blasco@gmail.com
Guest
Posts: n/a
 
      11-27-2006
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;
}

 
Reply With Quote
 
 
 
 
boyd
Guest
Posts: n/a
 
      11-27-2006
In article <(E-Mail Removed) .com>,
http://www.velocityreviews.com/forums/(E-Mail Removed) 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
 
Reply With Quote
 
 
 
 
Mumia W. (reading news)
Guest
Posts: n/a
 
      11-28-2006
On 11/27/2006 04:09 PM, (E-Mail Removed) 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.


--
(E-Mail Removed)
 
Reply With Quote
 
conrado.blasco@gmail.com
Guest
Posts: n/a
 
      11-28-2006

boyd wrote:
> In article <(E-Mail Removed) .com>,
> (E-Mail Removed) 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

 
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
Interactive/Non-interactive ASPX ? WJ ASP .Net 2 02-26-2005 02:54 AM
non Interactive and Interactive AAH Computer Support 0 01-09-2005 04:09 PM
Interactive and non interactive AAH Computer Support 1 01-09-2005 04:01 PM
teeing parent+childs output -H C++ 3 08-19-2003 05:24 AM
teeing parent+childs output -H C Programming 3 08-19-2003 05:24 AM



Advertisments