use strict; use warnings; my $logfile = 'test_env.log'; open my $fh_log, ">", $logfile or die "Couldn't open $logfile for output: $!\n"; print $fh_log "Ran at: ", scalar localtime, "\n"; print "Ran at: ", scalar localtime, "\n"; foreach (sort keys %ENV) { print $fh_log "$_ $ENV{$_}\n"; }