#! perl use strict; use warnings; use autodie; use Capture::Tiny qw( tee_stdout ); my %test_hash = ( Foo => 1, Bar => 1, Baz => 1, Nix => 0 ); foreach (keys %test_hash) { if ($test_hash{$_} == 1) { my $logfile = $_ . '_log.txt'; print "the logfile is $logfile\n"; require "$_.pm"; my ($stdout, @result) = tee_stdout { $_->$_() }; open(my $out, '>', $logfile); print $out $stdout; close($out); } }