|
|||||
|
|
#1 |
|
|
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" ? thanks... |
|
|
#2 |
|
|
> 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" ? Have you already looked for the modules provided at cpan, e.g. http://search.cpan.org/search?query=tee especially the CPAN module IO::Tee looks like it would it solve your problem. Greetings, Janek |
|
|
#3 |
|
|
> how can i also pipe a error message to a log file ? Something like "tee" ? How do I print to more than one file at once? -- Tad McClellan SGML consulting tadmc@augustmail.com Perl programming Fort Worth, Texas |
|
|
#4 |
|
|
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 <karel@e-tunity.com, karel@qbat.org> 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. |
|
|
#5 |
|
|
Quoth karel@e-tunity.com: > (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. Not strictly Perl-related, but recent versions of NT (at least since win2k) with cmd.exe do allow this, with the usual Bourne shell syntax. Ben -- You poor take courage, you rich take care: The Earth was made a common treasury for everyone to share All things in common, all people one. 'We come in peace'---the order came to cut them down. [ben@morrow.me.uk] |
|
|
#6 |
|
|
Quoth and11@rol.ru: > justme wrote on 18 Ноябрь 2004 10:08: > > > 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" ? > > You may also try to catch __DIE__ signal and replace its handler with your custom one. > > { > local $SIG{__DIE__} = sub { open FH, ">>log.txt"; print FH "@_\n"; > close FH; print STDERR "@_\n"; } ^^ ; > open FH, "somefile" or die "err: $!" ^^ ; not strictly required, but I would anyway > } Please don't top-post. Please indent decently. Please wrap your lines at 76 characters. $SIG{__DIE__} has been broken since it was first introduced, in that it is called whenever die is called inside an eval, including for parse errors. This means you need to be careful of this: { local $SIG{__DIE__} = sub { die $_[0] if $^S or not defined $^S; # whetever else you want }; } This is, of course, mentioned in perlvar, under $SIG{__DIE__} and $^S. Ben -- It will be seen that the Erwhonians are a meek and long-suffering people, easily led by the nose, and quick to offer up common sense at the shrine of logic, when a philosopher convinces them that their institutions are not based on the strictest morality. [Samuel Butler, paraphrased] ben@morrow.me.uk |
|
|
#7 |
|
|
You may also try to catch __DIE__ signal and replace its handler with your custom one.
{ local $SIG{__DIE__} = sub { open FH, ">>log.txt"; print FH "@_\n"; close FH; print STDERR "@_\n"; } open FH, "somefile" or die "err: $!" } Regards, Andrew justme wrote on 18 Ноябрь 2004 10:08: > 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" ? > > thanks... -- Andrew |
|
|
#8 |
|
|
eight02645999@yahoo.com (justme) wrote in message news:<c0837966.0411180208.69ca8ac1@posting.google. com>...
> 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" ? > > thanks... ***uming the user can access the log file: unless(open FILE, "textfile.txt") { my $error = $!; appendErrorLog($error); } ################ sub appendErrorLog { my $error = @_; unless(open ERRORFILE ">>errorLog.txt") { #Cant even access the errorLog. } print ERRORFILE "$error\n"; close ERRORFILE; } You will want to customize this a bit. For example getting the userid would be a start. Also there are cpan things that will do the same thing. |