use 5.010; use strict; use warnings; my ($fh, $str); open ($fh, '>>', \$str) or die "$!"; my $oldSTDOUT = select $fh; $str .= qq[first line: \n]; say q[can we control the print from a sub? ]; sample( qw(1 2 3 ) ); $str .= sample( qw(4 5 6 ) ); say q[ well... ]; select $oldSTDOUT; close $fh; print_now( $str ); sub print_now { my $txt = shift; $txt =~ s/e/E/g; say "txt: $txt"; } sub sample { print 'in sample(): ', @_, "\n"; } __END__ ## output txt: first linE: can wE control thE print from a sub? in samplE(): 123 in samplE(): 456 1 wEll... ## the '1' from sample(456)