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.

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.