use feature "switch"; # Redirect STDERR open my $err_fh, '>', 'stderr.log'; *STDERR = $err_fh; # Redirect STDERR open my $out_fh, '>', 'stdout.log'; *STDOUT = $out_fh; my $i; given($i) { print "$i\n" when $i < 4; } # Prove STDERR is redirected my $error = 10 / 0;