That doesn't make sense. Exceptions such as the one you provided are caught by eval and don't print anything.

>perl -we"$a=10; $a->isa('foo');" Can't call method "isa" without a package or object reference at -e li +ne 1. >perl -we"eval { $a=10; $a->isa('foo'); }" >

So you either used a bad example or you have a buggy __DIE__ handler. (It should check $^S.)

Assuming you just picked a bad example, eval doesn't affect warnings, which can be avoided with writing a warning handler

>perl -we"eval { $a='abc'; $a += 5; } Argument "abc" isn't numeric in addition (+) at -e line 1. >perl -we"eval { local $SIG{__WARN__} = sub {}; $a='abc'; $a += 5 }" >

Of course, that neither eval nor a warning handler prevents from writing to STDERR in general. local *STDERR; will handle most cases. The exception would be if you spawned a child process.

>perl -we"eval { print STDERR qq{foo\n}; }" Argument "abc" isn't numeric in addition (+) at -e line 1. >perl -we"eval { local *STDERR; print STDERR qq{foo\n}; }" print() on unopened filehandle STDERR at -e line 1. >perl -we"eval { local $SIG{__WARN__} = sub {}; local *STDERR; print S +TDERR qq{foo\n}; }" print() on unopened filehandle STDERR at -e line 1. >

By the way, you can avoid the need for any of shenanigans in this particular case by using isa as a function.

>perl -we"$a=10; UNIVERSAL::isa($a, 'foo');" >

In reply to Re: Prevent my STDERR logging in evals... by ikegami
in thread Prevent my STDERR logging in evals... by suaveant

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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.