use IO::Tee; my $tee = IO::Tee -> new(">stdout.txt", \*STDOUT); print $tee "Cheese!\n"; #### my $log = 0; print "Do you want to log to a file?"; my $resp = ; if ($resp =~ /^y/i) { $log = IO::Tee -> new(">stdout.txt", \*STDOUT); } ... if $log { print $log "What did I want to say?\n" } else { print "What did I want to say?\n" } #### my $tee; sub log_to_file { my ($on, $logfile) = @_; if ($on) { $tee = IO::Tee->new(">>$logfile", \*STDOUT); select $tee; print "Logging console window to $logfile\n"; } else { print "Console window log OFF.\n"; undef $tee; select STDOUT; close $logfile; } } #### sub log_to_file { my $on = $_[0]; if ($on) { Log::Log4perl->easy_init ({ level => $DEBUG, file => 'STDOUT', layout => '%m', }, {level => $DEBUG, file => ">> $console_log", layout => '%m', }); } else { Log::Log4perl->easy_init ({ level => $DEBUG, file => 'STDOUT', layout => '%m', }); } }