in reply to Re: Is there a better way to restore STDOUT ?
in thread Is there a better way to restore STDOUT ?

Hi all, Thanks for your help. I've incorporated your suggestions into the following test code.
#!/usr/bin/perl -w #First example: #Use select to redirect to FILE (which tees to STDOUT) or # to redirect to STDOUT $my_perlhome = "/home/privera/perl"; $log_file = "$my_perlhome/logfile"; $log_file2 = "$my_perlhome/logfile2"; $log_file3 = "$my_perlhome/logfile3"; $log_file4 = "$my_perlhome/logfile4"; $log_file5 = "$my_perlhome/logfile5"; #Flush buffers #$|=1; print "First example: select\n"; open(FILE, "| tee $log_file") or die "Can't open: $!\n"; select (FILE); print "Testing 1.2.3...\n"; close(FILE) or die "Can't close: $!\n"; select (STDOUT); print "done\n"; #Second example: #Uses local (notice braces) print "\nSecond example: local\n"; print "Should go to stdout.\n"; { local *STDOUT; open (STDOUT,"| tee $log_file2") or die "Can't open: $!\n"; print "Should go to second logfile and STDOUT\n"; } print "Should go to stdout.\n"; #Third example: #Uses SelectSaver #Info from perldoc.com: #"A SelectSaver object contains a reference to the file handle that #was selected when it was created. If its new method gets an extra par +ameter, #then that parameter is selected; otherwise, the selected file handle #remains unchanged. # #When a SelectSaver is destroyed, it re-selects the file handle #that was selected when it was created." print "\nThird example: SelectSaver\n"; use FileHandle; use SelectSaver; { my $newfh = new FileHandle("| tee $log_file3") or die "Can't open: $!\n"; my $saver = new SelectSaver($newfh); # <$newfh> is selected print "Should go to third logfile and STDOUT\n"; } # previous handle (STDOUT) is selected print "Should go to stdout.\n"; print "\nFourth example: select multiple\n"; open(FILE, "| tee $log_file4") or die "Can't open: $!\n"; select (FILE); print "This is written to both STDOUT and the fourth logfile\n"; select (STDOUT); print "This is written only to STDOUT\n"; select (FILE); print "This next message is written to both STDOUT and the fourth logf +ile\n"; close(FILE) or die "Can't close: $!\n"; select (STDOUT); print "done\n"; print "\nFifth example: select multiple object oriented\n"; my $newfh = new FileHandle ("| tee $log_file5") or die "Can't open: $! +\n"; select ($newfh); print "This is written to both STDOUT and the fifth logfile\n"; select (STDOUT); print "This is written only to STDOUT\n"; select ($newfh); print "This next message is written to both STDOUT and the fifth logfi +le\n"; close($newfh) or die "Can't close: $!\n"; select (STDOUT); print "done\n";
Here's a wrinkle I now face. The output from the above code is:
<privera> 121% select.pl First example: select Testing 1.2.3... done Second example: local Should go to stdout. Should go to second logfile and STDOUT Should go to stdout. Third example: SelectSaver Should go to third logfile and STDOUT Should go to stdout. Fourth example: select multiple This is written only to STDOUT This is written to both STDOUT and the fourth logfile This next message is written to both STDOUT and the fourth logfile done Fifth example: select multiple object oriented This is written only to STDOUT This is written to both STDOUT and the fifth logfile This next message is written to both STDOUT and the fifth logfile done
Why are the fourth and the fifth examples not written out in the same order as they were programmed? I tried setting $|=1 and setting autoflush to 1 with similar results. I even tried tossing in an extra "sleep 2" in to see if the order would change, but the results were the same. Thanks.