#!/usr/bin/perl use strict; use warnings; use PerlIO::Util; *STDOUT->push_layer(tee => \my $capture_buf); print "hello world\n"; print STDERR "captured STDOUT: '$capture_buf'"; __END__ $ ./819527.pl hello world captured STDOUT: 'hello world '