Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Order of output when switching filehandles

by Cirollo (Friar)
on Aug 14, 2000 at 22:20 UTC ( [id://27789]=perlquestion: print w/replies, xml ) Need Help??

Cirollo has asked for the wisdom of the Perl Monks concerning the following question:

I came across a strange problem earlier today: when switching to a new filehandle with select(), sometimes output will go to the new filehandle before it goes to the old one. Here is some relevant code to help explain:
# Create the TEE filehandle, which will print to both STDOUT and to a +file open (TEE, "| tee $name") or die "Teeing off: $!\n"; select(TEE); # Now print goes to the TEE handle by default $| = 1; # unbuffer TEE my $stmt = $dbh->prepare("$sql") or die $DBI::errstr; # $sql takes 30- +40 secs. to run $stmt->execute() or die $DBI::errstr; while (my @array = $stmt->fetchrow()) { print; } $stmt->finish(); select(STDOUT); &printfooter(); # prints a here-doc, about 10 lines close(STDOUT) or die "Error closing: $!\n";
The SQL query that gets printed to the TEE filehandle is pretty huge - it generally takes 35-40 seconds for that block to execute, according to a benchmark. And, it doesn't seem to matter whether the filehandle is buffered. So, can anyone explain why this is happening?

Replies are listed 'Best First'.
Re: Order of output when switching filehandles
by Shendal (Hermit) on Aug 14, 2000 at 22:44 UTC
    Instead of using tee, have you considered just printing directly to the file? I assume you are wanting the results to print to screen as well as to a log file. Why not do something like this:
    open(FILE, ">$name") or die "Failed file open: $!\n"; my $stmt = $dbh->prepare("$sql") or die $DBI::errstr; # $sql takes 30- +40 secs. to run $stmt->execute() or die $DBI::errstr; while (my @array = $stmt->fetchrow()) { print; print FILE "$_"; } $stmt->finish(); close(FILE);
    I didn't test this, but I'd bet that it's faster than running tee -- not to mention quite a bit easer to customize.
    Hope that helps,
    Shendal
      I ended up using tee because I use more print's than are in this example, and it becomes pretty unwieldy when you have to duplicate them all.
(crazyinsomniac) Re: Order of output when switching filehandles
by crazyinsomniac (Prior) on Aug 14, 2000 at 22:45 UTC
    I'm not sure, but you should close ( TEE ) when you're done with it.

    "cRaZy is co01, but sometimes cRaZy is cRaZy".
                                                          - crazyinsomniac

      Erk. That seems to have fixed it. Thanks.

      Just out of curiosity, can any of you gurus explain why that happens?

        I may be smoking something, but usually $| = 1; doesn't completely disable buffering, just most of Perl's buffering. You still need to close it to force the OS buffer to be flushed.
Re: Order of output when switching filehandles
by lhoward (Vicar) on Aug 14, 2000 at 23:27 UTC
    Have you considered using IO::Tee instead of launching tee as separate process?
    #!/usr/bin/perl -w use strict; use IO::Tee; open FH,">somefile" or die "error opening $!"; my $tee=IO::Tee->new(\*STDOUT,\*FH); select $tee; print "foo\n"; print "bar\n"; close FH;
      I considered that, but the machine I'm using does not have IO::Tee installed on it, so launching it as a seperate process is easier for all parties involved - it might not be the "right way" to do it, but theres no reason for it to not work and security is not a concern.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://27789]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (5)
As of 2024-04-25 16:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found