Home > Archive > PERL Miscellaneous > October 2007 > how to redirect error to variable
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
how to redirect error to variable
|
|
| Petr Vileta 2007-10-27, 7:07 pm |
| 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> )
{
$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.)
| |
| Michele Dondi 2007-10-27, 7:07 pm |
| On Sat, 27 Oct 2007 15:30:59 +0200, "Petr Vileta"
<stoupa@practisoft.cz> 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{po
p^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
| |
| Mumia W. 2007-10-27, 7:07 pm |
| 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.
| |
| Petr Vileta 2007-10-28, 7:09 pm |
| Mumia W. wrote:
> On 10/27/2007 08:30 AM, Petr Vileta wrote:
>
> 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.)
|
|
|
|
|