#!/usr/bin/perl use 5.014; # strict, //, s//r non-destructive use warnings; use cPanelUserConfig; { use autodie; my $fh_cgiout; my $memory; BEGIN { open $fh_cgiout, '>&', \*STDOUT; # duplicate real STDOUT to fh_cgiout: this means if you print to $fh_cgiout, it will actually go to the webserver close STDOUT; # must close STDOUT before reassigning it open STDOUT, '>', \$memory; # now when the script prints, it will "print" the text into memory, for later use } END { # at the very end, just before exiting, duplicate the contents # of the dupout_filename back to the CGI output filehandle (what was originally STDOUT); # start by closing the fake STDOUT and the fh_dupout, so that the dupout file is safe to read close STDOUT; # next, append the memory contents to the logfile open my $fh, '>>', 'dupout.log'; # append to your logfile my $prefix = sprintf "DEBUG %5d %s: ", $$, scalar gmtime; # I suggest a prefix in your logfile, to easily identify process ID and time for identifying the run print {$fh} ($memory =~ s/^/$prefix/gmr); # prepend every line of memory with the prefix, without editing the memory variable contents (/r regex option, v5.14) close $fh; # restore the original CGI STDOUT from $fh_cgiout open STDOUT, '>&', $fh_cgiout; # finally, print the memory contents to the original STDOUT, so that it will go to the webserver print $memory; # this time print the memory unedited } } print "Content-Type: text/plain;\n"; print "Hello World\n"; __END__