Hi,
usually when opening a file, we should check whether it can be opened
open(FILE,"textfile.txt") or die "Cannot open for reading:$!\n";
how can i also pipe a error message to a log file ? Something like "tee" ?
(1) Appending to a logfile from your Perl program:
sub handle_error {
# Try to append to the logfile. If it fails, show a msg on stderr.
if (open (my $of, ">>logfile")) {
print $of (@_);
close ($of);
} else {
print STDERR ("ERROR HANDLE: cannot append to logfile: $!\n");
}
# Now abort.
die (@_);
}
# Sample usage:
open (my $input, "input.txt")
or handle_error ("Cannot read input.txt: $!\n");
(2) Appending from the OS: use die() just as you would, but run your script
as:
perl myscript.pl 2>logfile
Unix-ish only. I don't think DOS allows for stderr-redirection while stdout
remains un-redirected.
Hope that helps.
--
Karel Kubat <
[email protected], (e-mail address removed)>
Phone: mobile (+31) 6 2956 4861, office (+31) (0)38 46 06 125
PGP fingerprint: D76E 86EC B457 627A 0A87 0B8D DB71 6BCD 1CF2 6CD5
From the Small Ads File:
Have several very old dresses from grandmother in beautiful condition.