{ my $captured_stdout = ''; open(my $capturing_fh, '>', \$captured_stdout) or die $!; my $old_sel = select($capturing_fh); my $sentry = Object::Destroyer->new( sub { select($old_sel) } ); ... }