I have a testing routine which, as part of the test, calls a function in a way that generates a warning message. I am expeting this warning message and would like to supress it in order to have clean test output.
However I only want to supress the warning message for this instance of the function call and I want to keep any other error messages that might occour.
I have tried using the 'no warnings qw/type/' pragma but have run into what seems to be a block nesting problem.
According to
perllexwarn the warnings pragma works on a block level which is fine. My problem seems to be when trying to control runtime warnings the scope doesn't work as I'm expecting:
#!/bin/perl
use warnings::register;
use warnings;
printf( "Perl Version: %vd\n",$^V);
test_warnings();
sub GenerateWarning {
warn "warnings are ",
warnings::enabled() ? "" : "not ",
"enabled\n";
}
sub test_warnings {
no warnings;
GenerateWarning();
AnotherSub();
}
sub AnotherSub {
GenerateWarning();
}
Produces the output:
Perl Version: 5.6.1
warnings are not enabled
warnings are enabled
What I would like is for warnings to be
not enabled both times.
Is there a way to do what I want to do? Should I be doing this a different way?
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.