CleanupTask: Restore STDERR and STDOUT to what they were when the object was created

This commit is contained in:
Brian Fraser
2012-08-15 14:00:52 -03:00
parent 2e38f936a6
commit d2b854cd9d
2 changed files with 18 additions and 0 deletions

View File

@@ -42,6 +42,10 @@ sub new {
my $self = {
task => $task,
};
open $self->{stdout_copy}, ">&=", *STDOUT
or die "Cannot dup stdout: $OS_ERROR";
open $self->{stderr_copy}, ">&=", *STDERR
or die "Cannot dup stderr: $OS_ERROR";
PTDEBUG && _d('Created cleanup task', $task);
return bless $self, $class;
}
@@ -51,6 +55,12 @@ sub DESTROY {
my $task = $self->{task};
if ( ref $task ) {
PTDEBUG && _d('Calling cleanup task', $task);
# Temporarily restore STDOUT and STDERR to what they were
# when the object was created
open local *STDOUT, ">&=", $self->{stdout_copy}
if $self->{stdout_copy};
open local *STDERR, ">&=", $self->{stderr_copy}
if $self->{stderr_copy};
$task->();
}
else {