Iím writing a Perl script which should run (by detention) as stand alone mean without calling to external Perl modules (I Canít use external modules like: File::Tee, IO::Tee
and so on).
The program should be run on WIN and UNIX machine as well (Perl ).
I would like to print the STDOUT & STDERR handlers to log file and to the screen parallels.
If the program failed then the error message should be print in the log file and the console as well.
Iíve written the code below:
my $LOG_FILE = "$HOME/log/test.txt";
open(LOG, "> $LOG_FILE") || die "Can't redirect stdout";
open (CMD, "ls |");
sub lprint ()
my ($line) = @_;
print LOG $line;
The codes above prints the STDOUT to log file and to the screen as I need!!!
But how can I control the STDERR handler?
For example - If I change the CMD
command in order to get an error message:
From: open (CMD, "ls |");
To: open (CMD, "ls_non_command |");
Then I should fail and get an error message - I want to print the error message to the log file and to the screen.