in reply to Re: Is validation of all subroutine arguments overkill?
in thread Is validation of all subroutine arguments overkill?

Hi stevieb,

Testing for warnings is a good idea, and ++ for the rest of your post, I just wanted to point out that I got burned by code similar to what you posted: You can't know how many warnings a piece of code will produce, especially over various Perl versions. Example: this CPAN Testers failure is caused by my test code expecting an exact number of warnings, but Perl 5.6 produced an extra warning in this case. Here's the code I currently use (in addition to Test::Fatal):

sub warns (&) { my $sub = shift; my @warns; { local $SIG{__WARN__} = sub { push @warns, shift }; $sub->() } return @warns; } # example usage use Test::More tests=>2; my @w = warns { is some_func(), "bar"; }; is grep({/\bfoo\b/} @w), 1; sub some_func { # could be any number of other warnings here warn "foo"; return "bar"; }

Regards,
-- Hauke D

Replies are listed 'Best First'.
Re^3: Is validation of all subroutine arguments overkill?
by stevieb (Canon) on May 21, 2016 at 14:32 UTC

    Yes, thanks for pointing that out, and in fact I usually do implement it with push() to an array. I'm in slow-mo this morning as it's a dreary Saturday to start a long weekend :)

    I'll keep my example as is so that future readers have context for your post, and can see the potential pitfall.