warnings::everywhere - a way of ensuring consistent global warning settings


This is version 0.007.


 no warnings::anywhere qw(uninitialized);
 use Module::That::Spits::Out::Warnings;
 use Other::Unnecessarily::Chatty::Module;
 use warnings::everywhere qw(uninitialized);
 # Write your own bondage-and-discipline code that really, really
 # cares about the difference between undef and the empty string


Warnings are great - in your own code. Tools like prove, and libraries like Moose and Modern::Perl, turn them on for you so you can spot things like ambiguous syntax, variables you only used once, deprecated syntax and other useful things.

By default use warnings turns on all warnings, including some that you might not care about, like uninitialised variables. You could explicitly say

 use warnings;
 no warnings qw(uninitialized);

or you could use a module like common::sense which disables some warnings and makes others fatal, or you could roll your own system. Either way, for your own code, there are plenty of ways around unwanted warnings.

Not so for other code, though.

The test suite at $WORK produces a large number of 'use of uninitialized variable' warnings from (at the last count) four separate modules. Some of them are because warnings got switched on for that module, even though the module itself didn't say anything about warnings (probably because the test suite was run with prove). Others are there because the module explicitly said use warnings, and then proceeded to blithely throw around variables without checking whether they were defined first.

Either way, this isn't my code, and it's not something I'm going to fix. These warnings are just spam.

This is where warnings::everywhere comes in.


At its simplest, say

 use warnings::everywhere qw(all);

and all modules imported from there onwards will have all warnings switched on. Modules imported previously will be unaffected. You can turn specific warnings off by saying e.g.

 no warnings::everywhere qw(uninitialized);

or, depending on how frustrated and/or grammatically-sensitive you happen to be feeling,

 no warnings::anywhere qw(uninitialized);


 no goddamn::warnings::anywhere qw(uninitialized);

Parameters are the same as use warnings: a list of categories as per perllexwarn, where all means all warnings.


warnings::everywhere works by fiddling with the contents of the global hashes %warnings::Bits and %warnings::DeadBits. As such, there are limitations on what it can and cannot do:

It cannot affect modules that are already loaded.

If you say

 use Chatty::Module;
 no warnings::anywhere qw(uninitialized);

that's no good - Chatty::Module has already called use warnings and uninitialized variables was in the list of enabled warnings at that point, so it will still spam you.

Similarly, this is no help:

 use Module::That::Uses::Chatty::Module;
 no warnings::anywhere qw(uninitialized);
 use Chatty::Module;

Chatty::Module was pulled in by that other module already by the time perl gets to your use statement, so it's ignored.

It's vulnerable to anything that sets $^W

Any code that sets the global variable $^W, rather than saying use warnings or warnings-import>, will turn on all warnings everywhere, bypassing the changes warnings::everywhere makes. This also includes any code that sets -w via the shebang.

Any change to warnings by any of the warnings::anywhere code will turn off $^W again, whether it's a use statement or an explicit call to disable_warning_category or similar.

Any module that claims to enable warnings for you is potentially suspect - Moose is fine, but Dancer sets $^W to 1 as soon as it loads, even if your configuration subsequently disables import_warnings.

It cannot make all modules use warnings

All it does is fiddle with the exact behaviour of use warnings, so a module that doesn't say use warnings, or import a module that injects warnings like Moose, will be unaffected.

It's not lexical

While it looks like a pragma, it's not - it fiddles with global settings, after all. So you can't say

     no warnings::anywhere qw(uninitialized);

and expect to get a warning from the last line. That warning's been turned off for good.

It won't work for compile-time warnings

It works by fiddling with the (global) %warnings::Bits variable, and that's fine for run-time warnings, but compile-time warnings like e.g. experimental::smartmatch will still happen, even if you think you've disabled them.


warnings::anywhere provides the following functions, mostly for diagnostic use. They are not exported or exportable.

 Out: @categories

Returns a sorted list of warning categories enabled globally. Before you've fiddled with anything, this will be the list of warning categories from perllexwarn, minus all which isn't a category itself.

Fatal warnings are ignored for the purpose of this function.

 Out: @categories

Returns a sorted list of warning categories disabled globally. Before you've fiddled with anything, this will be the empty list.

Fatal warnings are ignored for the purpose of this function.

 In: $category

Supplied with a valid warning category, enables it for all future uses of use warnings.

 In: $category

Supplied with a valid warning category, disables it for future uses of use warnings - even calls to explicitly enable it.


Support for fatal warnings, possibly. It's possible it doesn't behave correctly when passed 'all'.


Unrecognised warning category $category

Your version of Perl doesn't recognise the warning category $category. Either you're using a different version of Perl than you thought, or a third-party module that defined that warning isn't loaded yet.




Sam Kington <>

The source code for this module is hosted on GitHub - this is probably the best place to look for suggestions and feedback.


Copyright (c) 2013 Sam Kington.


This library is free software and may be distributed under the same terms as perl itself.