in reply to Redirecting STDOUT

At the risk of taking too simplistic a view of your requirement, you can redirect the output of the whole perl program to log.txt and if desired STDERR to errlog.txt from the shell level using:
myprog.pl > log.txt 2> errlog.txt
or if ordinary and error output should be merged but appear in correct historic order...
myprog.pl > log.txt 2>&1

-M

Free your mind