# Redirect STDOUT. if ($filename) { open(STDOUT, '>', $filename) or die("Can't open redirect STDOUT: $!\n"); } print(...); # Goes to file if one was specified, else STDOUT. #### # Use select. if ($filename) { open(FILE, '>', $filename) or die("Can't open $filename: $!\n"); select(FILE); } print(...); # Goes to file if one was specified, else STDOUT. #### # If you want to output to STDOUT and to the file, # create a handle which may point to STDOUT. if ($filename) { open(FILE, '>', $filename) or die("Can't open $filename: $!\n"); } else { open(FILE, ">&STDOUT"); or die("Can't dup STDOUT: $!\n"); } print FILE (...); # Goes to file if one was specified, else STDOUT. print(...); # Goes to STDOUT.