Hi,
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 |");
while (<CMD>)
{
lprint ($_)
}
close(CMD);
sub lprint ()
{
my ($line) = @_;
print LOG $line;
print $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.
Please advice.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.