{ my $captured_stdout = ''; local *STDOUT; open(STDOUT, '>', \$captured_stdout) or die $!; ... }