Redirecting STDERR

Redirecting STDERR

am 07.10.2005 21:10:09 von Ryan Frantz

Perlers,

I have a script where I redirect STDERR to a file so that I can capture
'die' messages like so:

use warnings;
use strict;

my $logfile =3D "/some/path/logfile.txt";

open STDERR, ">>$logfile";

something or die "Unable to do something()\n";

close STDERR;

Is it kosher to do this? Or is there a more preferred method to
redirect 'die' messages?

ry

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org

RE: Redirecting STDERR

am 07.10.2005 22:56:50 von Timothy Johnson

It partly depends on your operating system, I think. That's fine on
Windows, but on UNIX I think that under some if not all circumstances
your change will persist after the script finishes executing. I've
never had to do it, but I've seen one method that goes like this:

open(OLD_STDERR,">&STDERR") or die "Failed to save STDERR";
open(STDERR,">script.err") or die "Failed to redirect STDERR";

do something...

open(STDERR,">&OLD_STDERR") or die "Failed to restore STDERR";


-----Original Message-----
From: Ryan Frantz [mailto:RyanFrantz@informed-llc.com]=20
Sent: Friday, October 07, 2005 12:10 PM
To: beginners@perl.org
Subject: Redirecting STDERR

Perlers,

I have a script where I redirect STDERR to a file so that I can capture
'die' messages like so:

use warnings;
use strict;

my $logfile =3D "/some/path/logfile.txt";

open STDERR, ">>$logfile";

something or die "Unable to do something()\n";

close STDERR;

Is it kosher to do this? Or is there a more preferred method to
redirect 'die' messages?



--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org

Re: Redirecting STDERR

am 08.10.2005 03:11:51 von Jeff Peng

hi,
maybe u should redefined the __DIE__ handle for the reason of safety.
such as:

$SIG{__DIE__}=3D\&log_die;
sub log_die
{
open (HDW,">>",$err_log);
select HDW;$|=3D1;select STDOUT;
print HDW @_;
close HDW;
die @_;
}


2005/10/8, Timothy Johnson :
> It partly depends on your operating system, I think. That's fine on
> Windows, but on UNIX I think that under some if not all circumstances
> your change will persist after the script finishes executing. I've
> never had to do it, but I've seen one method that goes like this:
>
> open(OLD_STDERR,">&STDERR") or die "Failed to save STDERR";
> open(STDERR,">script.err") or die "Failed to redirect STDERR";
>
> do something...
>
> open(STDERR,">&OLD_STDERR") or die "Failed to restore STDERR";
>
>
> -----Original Message-----
> From: Ryan Frantz [mailto:RyanFrantz@informed-llc.com]
> Sent: Friday, October 07, 2005 12:10 PM
> To: beginners@perl.org
> Subject: Redirecting STDERR
>
> Perlers,
>
> I have a script where I redirect STDERR to a file so that I can capture
> 'die' messages like so:
>
> use warnings;
> use strict;
>
> my $logfile =3D "/some/path/logfile.txt";
>
> open STDERR, ">>$logfile";
>
> something or die "Unable to do something()\n";
>
> close STDERR;
>
> Is it kosher to do this? Or is there a more preferred method to
> redirect 'die' messages?
>
>
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
>
>
>
>

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org

Re: Redirecting STDERR

am 08.10.2005 06:04:45 von Stephen Kratzer

Opening the filehandle STDERR to a file will not persist beyond termination of
the script, nor will it affect other programs running at the same time as the
script. It affects only the running script. STDERR of the shell is different
from that of perl.

On Friday 07 October 2005 16:56, Timothy Johnson wrote:
> It partly depends on your operating system, I think. That's fine on
> Windows, but on UNIX I think that under some if not all circumstances
> your change will persist after the script finishes executing. I've
> never had to do it, but I've seen one method that goes like this:
>
> open(OLD_STDERR,">&STDERR") or die "Failed to save STDERR";
> open(STDERR,">script.err") or die "Failed to redirect STDERR";
>
> do something...
>
> open(STDERR,">&OLD_STDERR") or die "Failed to restore STDERR";
>
>
> -----Original Message-----
> From: Ryan Frantz [mailto:RyanFrantz@informed-llc.com]
> Sent: Friday, October 07, 2005 12:10 PM
> To: beginners@perl.org
> Subject: Redirecting STDERR
>
> Perlers,
>
> I have a script where I redirect STDERR to a file so that I can capture
> 'die' messages like so:
>
> use warnings;
> use strict;
>
> my $logfile = "/some/path/logfile.txt";
>
> open STDERR, ">>$logfile";
>
> something or die "Unable to do something()\n";
>
> close STDERR;
>
> Is it kosher to do this? Or is there a more preferred method to
> redirect 'die' messages?

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org