use IO::Handle; my $file = 'output.dat'; open(OUTPUT, ">$file") || die $!; STDOUT->fdopen(\*OUTPUT, "w") || die $!; open STDERR, ">&STDOUT"; print "This is STDOUT\n"; print STDERR "This is STDERR\n"; warn "This is a warning\n"; #### my $file = 'output.dat'; { open(my $fh_out, '>', $file) or die("Unable to create output file \"$file\": $!\n"); local *STDOUT = $fh_out; local *STDERR = $fh_out; print "This is STDOUT (1)\n"; print STDERR "This is STDERR (1)\n"; warn "This is a warning (1)\n"; } print "This is STDOUT (2)\n"; print STDERR "This is STDERR (2)\n"; warn "This is a warning (2)\n"; #### use v5.8.0; my $file = 'output.dat'; { open(my $fh_out, '>', $file) or die("Unable to create output file \"$file\": $!\n"); local *STDOUT; open(*STDOUT, '>&', $fh_out) or die("Unable to redirect STDOUT: $!\n"); local *STDERR; open(*STDERR, '>&', $fh_out) or die("Unable to redirect STDERR: $!\n"); print "This is STDOUT (1)\n"; print STDERR "This is STDERR (1)\n"; warn "This is a warning (1)\n"; } print "This is STDOUT (2)\n"; print STDERR "This is STDERR (2)\n"; warn "This is a warning (2)\n";