in reply to STDOUT redirect in Perl

The most simple way is to use select().
open ( OUTFILE, '>myfilename.txt' ); print "This goes to the screen\n"; select ( OUTFILE ); print "This goes to the file\n";

The other method that works is using typeglobs.

open ( OUTFILE, '>myfilename.txt' ); print "This goes to the screen\n"; *STDOUT = *OUTFILE; print "This goes to the file\n";

Personally, I prefer the select method.

Replies are listed 'Best First'.
Re: Re: STDOUT redirect in Perl
by Ven'Tatsu (Deacon) on Nov 14, 2001 at 08:58 UTC
    You don't need to mess with typeglobs to do the second.
    open(STDOUT, '> myfilename.txt'); #reopen STDOUT as a file print "This goes to the file\n";

    Or
    open(OUTFILE, '> myfilename.txt'); print "This goes to the screen\n"; open(STDOUT, '>&OUTFILE'); #dup STDOUT to OUTFILE print "This goes to the file\n";