how to redirect error to variable

how to redirect error to variable

am 27.10.2007 15:30:59 von Petr Vileta

I'm running script on server where sometime some service fail (mysql server,
user log is closed and reopened etc). I have not these processes under
control, this is virtual server hosting.
I need to redirect error messages to variable, send these messages vie
sendmail to user and exit from my script after I do needed actions. For
these reasons I wrote mu own sub for some SIG, say for SIG-PIPE.
This is my code

use Carp qw(cluck);
......
$SIG{'PIPE'}=sub {
local *STDERR;
my $errfile=$backups . '/err.txt';
open STDERR,"> $errfile";
cluck("SIG-PIPE ");
close STDERR;
open ERR,"< $errfile";
my $err='';
while ()
{
$err .= $_;
}
close STDERR;
print $err;
exit;
};

This work as expected. I tried to use IO::Scalar but without success. What
is wrong in this code?

use IO::Scalar;
$SIG{'PIPE'}=sub {
my $err='';
$SH=new IO::Scalar \$err;
open STDERR,,">&SH";
cluck("SIG-PIPE ");
print $err;
exit;
};

--

Petr Vileta, Czech republic
(My server rejects all messages from Yahoo and Hotmail. Send me your mail
from another non-spammer site please.)

Re: how to redirect error to variable

am 27.10.2007 17:02:30 von Michele Dondi

On Sat, 27 Oct 2007 15:30:59 +0200, "Petr Vileta"
wrote:

>I need to redirect error messages to variable, send these messages vie
>sendmail to user and exit from my script after I do needed actions. For

IIUC, then you can hook into $SIG{__DIE__}.


Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^ ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER 256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,

Re: how to redirect error to variable

am 27.10.2007 18:21:00 von paduille.4061.mumia.w+nospam

On 10/27/2007 08:30 AM, Petr Vileta wrote:
> [...]
> This work as expected. I tried to use IO::Scalar but without success.
> What is wrong in this code?
>
> use IO::Scalar;
> $SIG{'PIPE'}=sub {
> my $err='';
> $SH=new IO::Scalar \$err;
> open STDERR,,">&SH";
> cluck("SIG-PIPE ");
> print $err;
> exit;
> };
>

I don't think it'll work with IO::Scalar. Why not do it the right way?

my $err = '';

$SIG{PIPE} = sub {
$err .= Carp::shortmess('SIG-PIPE ');
print $err;
exit 1;
};

I doubt you want $err to be lexically scoped within the sub, and I think
you need to read "man Carp" again.

Re: how to redirect error to variable

am 28.10.2007 18:48:06 von Petr Vileta

Mumia W. wrote:
> On 10/27/2007 08:30 AM, Petr Vileta wrote:
>> [...]
>> This work as expected. I tried to use IO::Scalar but without success.
>> What is wrong in this code?
>>
>> use IO::Scalar;
>> $SIG{'PIPE'}=sub {
>> my $err='';
>> $SH=new IO::Scalar \$err;
>> open STDERR,,">&SH";
>> cluck("SIG-PIPE ");
>> print $err;
>> exit;
>> };
>>
>
> I don't think it'll work with IO::Scalar. Why not do it the right way?
>
> my $err = '';
>
> $SIG{PIPE} = sub {
> $err .= Carp::shortmess('SIG-PIPE ');
> print $err;
> exit 1;
> };
>
> I doubt you want $err to be lexically scoped within the sub, and I
> think you need to read "man Carp" again.

Hmm, thanks for direct me to right way. I use Perl 5.6.1 and my perldoc Carp
say nothing about shortmess() or longmess().

---CUT---
D:\Perl>perldoc Carp
NAME
carp - warn of errors (from perspective of caller)

cluck - warn of errors with stack backtrace (not exported by default)

croak - die of errors (from perspective of caller)

confess - die of errors with stack backtrace
---CUT---

and on CPAN these functions are documented for Perl 5.8.x and later. But it
works in my perl version too.
--

Petr Vileta, Czech republic
(My server rejects all messages from Yahoo and Hotmail. Send me your mail
from another non-spammer site please.)