Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

How can I tell at run time that my script is writing to STDERR?

by Dave05 (Beadle)
on Oct 23, 2002 at 14:53 UTC ( #207408=perlquestion: print w/replies, xml ) Need Help??

Dave05 has asked for the wisdom of the Perl Monks concerning the following question: (debugging)

I have a fairly complex script, which pulls in a bunch of modules. The probability of warnings being issued is fairly high. I want to stop the script even for a warning. How can I escalate a 'warn' to a 'die'?

Originally posted as a Categorized Question.

  • Comment on How can I tell at run time that my script is writing to STDERR?

Replies are listed 'Best First'.
Re: How can I tell at run time that my script is writing to STDERR?
by zigdon (Deacon) on Oct 23, 2002 at 16:27 UTC
    use the $SIG{__WARN__}:
    $SIG{__WARN__} = sub { die $_[0] }; warn "This will be fatal\n"; print "You will never get to this line\n";
    See perlvar for more info.
Re: How can I tell at run time that my script is writing to STDERR?
by rir (Vicar) on Oct 23, 2002 at 17:54 UTC
    Zigdon's answer is perfectly correct.

    So this is offered as a more generalized solution. Just override the offending function like so:

    use subs qw( warn); sub warn { die @_} warn "This is a warning"; print "NOT REACHED\n";
Re: How can I tell at run time that my script is writing to STDERR?
by Dave05 (Beadle) on Oct 23, 2002 at 15:05 UTC
    To partially answer myself, for most warnings, saying

    use warnings FATAL => 'all';

    will have the desired effect. However, this won't kill my program if I issue a warning using 'warn'

Re: How can I tell at run time that my script is writing to STDERR?
by Dave05 (Beadle) on Oct 24, 2002 at 10:06 UTC
    Many thanks.

    I've tested both solutions. Note that zigdon's solution will trap warnings issued by included modules, whereas rir's solution will only affect calls to warn within the package where the sub is declared.

Re: How can I tell at run time that my script is writing to STDERR?
by mystik (Sexton) on Oct 24, 2002 at 15:21 UTC
    The title is confuzing -- I was about to state that the solutions listed don't really trap all of output to STDERR , one can still do:
    print STDERR "the one that got away\n";
    (Unfortunatly, I don't know a good solution to this instance...)
Re: How can I tell at run time that my script is writing to STDERR?
by .tom. (Initiate) on Oct 25, 2002 at 11:43 UTC
    What you can do to prevent anything from beeing to STDERR is close the file descriptor :
    print STDERR "prints...\n"; close(STDERR); print STDERR "doesn't print...\n" or print "this was bound to fail\n";
    But any entity that tries to write to STDERR will fail doing that, so you should also reopen STDERR to /dev/null to avoid that.
    open(STDERR,'>/dev/null'); print STDERR "doesn't fail so this..." or print "...won't print";

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://207408]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (7)
As of 2022-08-12 09:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?