Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

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

( #207408=categorized question: print w/ replies, xml ) Need Help??
Contributed by Dave05 on Oct 23, 2002 at 14:53 UTC
Q&A  > debugging


Description:

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'?

Answer: How can I tell at run time that my script is writing to STDERR?
contributed by zigdon

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.
Answer: How can I tell at run time that my script is writing to STDERR?
contributed by rir

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";
Answer: How can I tell at run time that my script is writing to STDERR?
contributed by Dave05

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'

Answer: How can I tell at run time that my script is writing to STDERR?
contributed by Dave05

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.

Answer: How can I tell at run time that my script is writing to STDERR?
contributed by mystik

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...)
Answer: How can I tell at run time that my script is writing to STDERR?
contributed by .tom.

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";

Please (register and) log in if you wish to add an answer



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others taking refuge in the Monastery: (8)
    As of 2014-07-30 00:03 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My favorite superfluous repetitious redundant duplicative phrase is:









      Results (229 votes), past polls