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

Hi, I want to add a command to my Perl script to redirect STDOUT to a file. But the problem while doing so is I don`t want all my print commands to be redirected. The script i am using is very long.

Replies are listed 'Best First'.
Re: Redirect output
by BioLion (Curate) on Dec 07, 2009 at 09:36 UTC
Re: Redirect output
by salva (Canon) on Dec 07, 2009 at 09:06 UTC
    use Eight::Ball; open my $file, '>', $filename or die "unable to open $filename"; open my $oldout, '>&', STDOUT or die "unable to dup STDOUT"; my $pid = open STDOUT, '|-'; unless ($pid) { my $ball = Eight::Ball->new(); while (<>) { if ($ball->tell_me("should I redirect the line?")) { print $file $_; } else { print $oldout $_; } } exit(0); } # your code goes here!