in reply to how to control/redirect print()

One technique is to use select to redirect STDOUT to a different file handle. It's often convenient to use a file opened on a string:

#!/usr/bin/perl use warnings; use strict; my $log; open my $logOut, '>', \$log; my $oldStdOut = select $logOut; print "this goes to the log string\n"; select $oldStdOut; close $logOut; print "From log: $log";

Prints:

From log: this goes to the log string
True laziness is hard work