in reply to Re: Using Perl to launch another Perl script and generate error log if it fails
in thread Using Perl to launch another Perl script and generate error log if it fails

If I wanted to replace the warn functions with a function that would write the same output to an error log, what would that look like? Thanks.
  • Comment on Re: Re: Using Perl to launch another Perl script and generate error log if it fails

Replies are listed 'Best First'.
Re: Re: Re: Using Perl to launch another Perl script and generate error log if it fails
by TilRMan (Friar) on Mar 26, 2004 at 02:04 UTC

    If you aren't using your STDERR for anything else, just redirect it to the file and the warnings will show up there. You can do that in your shell script with foo.pl 2>> errlog.txt, or in Perl with:

    open STDERR, ">errlog.txt" or die "Whoa! I can't even log my errors: $!";

    Otherwise, you'll want the __WARN__ pseudo-signal:

    open ERRLOG, ">errlog.txt" or die; $SIG{__WARN__} = sub { my ($msg) = @_; $msg ||= q{Warning: something's wrong}; # Add the file and line number to $msg here # using the return values from caller(). print ERRLOG, $msg; };
    See also warn and caller in perlfunc and %SIG in perlvar.

    -- 
    LP^>