carp() / STDERR

Discussion in 'Perl Misc' started by R McGlue, Aug 31, 2004.

  1. R McGlue

    R McGlue Guest

    Hi
    Im runnin a script which detatches from the tty and uses carp in
    certain places. Is there any way i can redirect the STDERR messages to a
    logfile instead of losing them to the ether??

    many thanks

    Ronan
     
    R McGlue, Aug 31, 2004
    #1
    1. Advertising

  2. R McGlue

    Paul Lalli Guest

    "R McGlue" <> wrote in message
    news:ch1ksm$nje$...
    > Hi
    > Im runnin a script which detatches from the tty and uses carp in
    > certain places. Is there any way i can redirect the STDERR messages to

    a
    > logfile instead of losing them to the ether??


    my $logfile = '/home/errs.txt';
    open STDERR, '>>', $logfile or die "Could not redirect STDERR: $!";

    (Note: Perl is smart enough to not lose track of the original STDERR if
    the open fails here, so the die message will still be printed to
    wherever STDERR is pointing if $logfile cannot be opened for appending.)

    Hope this helps,
    Paul Lalli
     
    Paul Lalli, Aug 31, 2004
    #2
    1. Advertising

  3. R McGlue

    R McGlue Guest

    Paul Lalli wrote:

    > "R McGlue" <> wrote in message
    > news:ch1ksm$nje$...
    >
    >>Hi
    >> Im runnin a script which detatches from the tty and uses carp in
    >>certain places. Is there any way i can redirect the STDERR messages to

    >
    > a
    >
    >>logfile instead of losing them to the ether??

    >
    >
    > my $logfile = '/home/errs.txt';
    > open STDERR, '>>', $logfile or die "Could not redirect STDERR: $!";
    >
    > (Note: Perl is smart enough to not lose track of the original STDERR if
    > the open fails here, so the die message will still be printed to
    > wherever STDERR is pointing if $logfile cannot be opened for appending.)
    >
    > Hope this helps,
    > Paul Lalli
    >
    >

    so if carp was to return and error message it would get written to
    /home/errs.txt
    ?
     
    R McGlue, Aug 31, 2004
    #3
  4. R McGlue

    Paul Lalli Guest

    "R McGlue" <> wrote in message
    news:ch21u2$omd$...
    > Paul Lalli wrote:
    >
    > > my $logfile = '/home/errs.txt';
    > > open STDERR, '>>', $logfile or die "Could not redirect STDERR: $!";
    > >
    > > (Note: Perl is smart enough to not lose track of the original STDERR

    if
    > > the open fails here, so the die message will still be printed to
    > > wherever STDERR is pointing if $logfile cannot be opened for

    appending.)
    > >

    > so if carp was to return and error message it would get written to
    > /home/errs.txt
    > ?


    Any message printed to STDERR, including those printed by carp(), would
    be printed to whatever file you named in $logfile (/home/errs.txt in
    this case), yes.

    Paul Lalli
     
    Paul Lalli, Aug 31, 2004
    #4
  5. R McGlue

    R McGlue Guest

    Paul Lalli wrote:
    > "R McGlue" <> wrote in message
    > news:ch21u2$omd$...
    >
    >>Paul Lalli wrote:
    >>
    >>
    >>>my $logfile = '/home/errs.txt';
    >>>open STDERR, '>>', $logfile or die "Could not redirect STDERR: $!";
    >>>
    >>>(Note: Perl is smart enough to not lose track of the original STDERR

    >
    > if
    >
    >>>the open fails here, so the die message will still be printed to
    >>>wherever STDERR is pointing if $logfile cannot be opened for

    >
    > appending.)
    >
    >>so if carp was to return and error message it would get written to
    >>/home/errs.txt
    >>?

    >
    >
    > Any message printed to STDERR, including those printed by carp(), would
    > be printed to whatever file you named in $logfile (/home/errs.txt in
    > this case), yes.
    >
    > Paul Lalli
    >
    >

    Right I'll elaborate a little more... I need to close STDERR if possible
    as the script spawns multiple clients and servers which communicate with
    each other via FIFO pipes. Its a fudged bit of code... but it works. I
    need to close STDERR to minimise disk leakage. Aside from opening STDERR
    and closing it around a carp or die command is there anyother way this
    can be implemted more..... 'cleanly'

    Ronan
     
    R McGlue, Sep 1, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Louis Erickson
    Replies:
    2
    Views:
    240
    James Willmore
    Sep 3, 2003
  2. Jo Oberman
    Replies:
    5
    Views:
    247
    Ilya Zakharevich
    Sep 14, 2003
  3. Gunnar Hjalmarsson

    CGI::Carp and "useless quotes"

    Gunnar Hjalmarsson, Apr 4, 2004, in forum: Perl Misc
    Replies:
    12
    Views:
    252
    Gunnar Hjalmarsson
    Apr 16, 2004
  4. joe
    Replies:
    2
    Views:
    407
    Peter J. Acklam
    Apr 10, 2004
  5. Irving Kimura

    Why Carp::croak over die?

    Irving Kimura, May 13, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    531
    Irving Kimura
    May 14, 2004
Loading...

Share This Page