my $str; my $io = tie *STDOUT, 'IO::Scalar', \$str; print_monks(); undef $io; untie *STDOUT; # $str now contains the output of the print_monks() function.