use File::Temp qw(tempfile);
my $temp_fh = tempfile();
my $saved_fh = select($temp_fh);
eval "print 'Waka waka!'";
seek ($temp_fh, 0, 0);
select($saved_fh);
$text = join '', <$temp_fh>;
print "The text was '$text'\n";
####
use File::Temp;
use Carp;
# Takes code to be evaled, supresses printing and returns
# the text that would have been printed.
sub eval_print_trap {
my $code = shift;
my $temp_fh = tempfile();
my $saved_fh = select($temp_fh);
eval($code);
select($saved_fh);
if ($@) {
confess("Evaling \n'$code'\ngave error $@");
}
seek($temp_fh, 0, 0);
wantarray ? <$temp_fh>: join '', $temp_fh;
}
####
my $output = eval_print_trap("print 'Waka waka!'");