{
    "mode": "perldoc",
    "parameter": "warnings",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/perldoc/warnings/json",
    "generated": "2026-06-13T21:38:36Z",
    "synopsis": "use warnings;\nno warnings;\nuse warnings \"all\";\nno warnings \"uninitialized\";\n# or equivalent to those last two ...\nuse warnings qw(all -uninitialized);\nuse warnings::register;\nif (warnings::enabled()) {\nwarnings::warn(\"some warning\");\n}\nif (warnings::enabled(\"void\")) {\nwarnings::warn(\"void\", \"some warning\");\n}\nif (warnings::enabled($object)) {\nwarnings::warn($object, \"some warning\");\n}\nwarnings::warnif(\"some warning\");\nwarnings::warnif(\"void\", \"some warning\");\nwarnings::warnif($object, \"some warning\");",
    "sections": {
        "NAME": {
            "content": "warnings - Perl pragma to control optional warnings\n",
            "subsections": []
        },
        "SYNOPSIS": {
            "content": "use warnings;\nno warnings;\n\nuse warnings \"all\";\nno warnings \"uninitialized\";\n\n# or equivalent to those last two ...\nuse warnings qw(all -uninitialized);\n\nuse warnings::register;\nif (warnings::enabled()) {\nwarnings::warn(\"some warning\");\n}\n\nif (warnings::enabled(\"void\")) {\nwarnings::warn(\"void\", \"some warning\");\n}\n\nif (warnings::enabled($object)) {\nwarnings::warn($object, \"some warning\");\n}\n\nwarnings::warnif(\"some warning\");\nwarnings::warnif(\"void\", \"some warning\");\nwarnings::warnif($object, \"some warning\");\n",
            "subsections": []
        },
        "DESCRIPTION": {
            "content": "The \"warnings\" pragma gives control over which warnings are enabled in which parts of a Perl\nprogram. It's a more flexible alternative for both the command line flag -w and the equivalent\nPerl variable, $^W.\n\nThis pragma works just like the \"strict\" pragma. This means that the scope of the warning pragma\nis limited to the enclosing block. It also means that the pragma setting will not leak across\nfiles (via \"use\", \"require\" or \"do\"). This allows authors to independently define the degree of\nwarning checks that will be applied to their module.\n\nBy default, optional warnings are disabled, so any legacy code that doesn't attempt to control\nthe warnings will work unchanged.\n\nAll warnings are enabled in a block by either of these:\n\nuse warnings;\nuse warnings 'all';\n\nSimilarly all warnings are disabled in a block by either of these:\n\nno warnings;\nno warnings 'all';\n\nFor example, consider the code below:\n\nuse warnings;\nmy @x;\n{\nno warnings;\nmy $y = @x[0];\n}\nmy $z = @x[0];\n\nThe code in the enclosing block has warnings enabled, but the inner block has them disabled. In\nthis case that means the assignment to the scalar $z will trip the \"Scalar value @x[0] better\nwritten as $x[0]\" warning, but the assignment to the scalar $y will not.\n",
            "subsections": [
                {
                    "name": "Default Warnings and Optional Warnings",
                    "content": "Before the introduction of lexical warnings, Perl had two classes of warnings: mandatory and\noptional.\n\nAs its name suggests, if your code tripped a mandatory warning, you would get a warning whether\nyou wanted it or not. For example, the code below would always produce an \"isn't numeric\"\nwarning about the \"2:\".\n\nmy $x = \"2:\" + 3;\n\nWith the introduction of lexical warnings, mandatory warnings now become *default* warnings. The\ndifference is that although the previously mandatory warnings are still enabled by default, they\ncan then be subsequently enabled or disabled with the lexical warning pragma. For example, in\nthe code below, an \"isn't numeric\" warning will only be reported for the $x variable.\n\nmy $x = \"2:\" + 3;\nno warnings;\nmy $y = \"2:\" + 3;\n\nNote that neither the -w flag or the $^W can be used to disable/enable default warnings. They\nare still mandatory in this case.\n\n\"Negative warnings\"\nAs a convenience, you can (as of Perl 5.34) pass arguments to the \"import()\" method both\npositively and negatively. Negative warnings are those with a \"-\" sign prepended to their names;\npositive warnings are anything else. This lets you turn on some warnings and turn off others in\none command. So, assuming that you've already turned on a bunch of warnings but want to tweak\nthem a bit in some block, you can do this:\n\n{\nuse warnings qw(uninitialized -redefine);\n...\n}\n\nwhich is equivalent to:\n\n{\nuse warnings qw(uninitialized);\nno warnings qw(redefine);\n...\n}\n\nThe argument list is processed in the order you specify. So, for example, if you don't want to\nbe warned about use of experimental features, except for \"somefeature\" that you really dislike,\nyou can say this:\n\nuse warnings qw(all -experimental experimental::somefeature);\n\nwhich is equivalent to:\n\nuse warnings 'all';\nno warnings  'experimental';\nuse warnings 'experimental::somefeature';\n\nWhat's wrong with -w and $^W\nAlthough very useful, the big problem with using -w on the command line to enable warnings is\nthat it is all or nothing. Take the typical scenario when you are writing a Perl program. Parts\nof the code you will write yourself, but it's very likely that you will make use of pre-written\nPerl modules. If you use the -w flag in this case, you end up enabling warnings in pieces of\ncode that you haven't written.\n\nSimilarly, using $^W to either disable or enable blocks of code is fundamentally flawed. For a\nstart, say you want to disable warnings in a block of code. You might expect this to be enough\nto do the trick:\n\n{\nlocal ($^W) = 0;\nmy $x =+ 2;\nmy $y; chop $y;\n}\n\nWhen this code is run with the -w flag, a warning will be produced for the $x line: \"Reversed +=\noperator\".\n\nThe problem is that Perl has both compile-time and run-time warnings. To disable compile-time\nwarnings you need to rewrite the code like this:\n\n{\nBEGIN { $^W = 0 }\nmy $x =+ 2;\nmy $y; chop $y;\n}\n\nAnd note that unlike the first example, this will permanently set $^W since it cannot both run\nduring compile-time and be localized to a run-time block.\n\nThe other big problem with $^W is the way you can inadvertently change the warning setting in\nunexpected places in your code. For example, when the code below is run (without the -w flag),\nthe second call to \"doit\" will trip a \"Use of uninitialized value\" warning, whereas the first\nwill not.\n\nsub doit\n{\nmy $y; chop $y;\n}\n\ndoit();\n\n{\nlocal ($^W) = 1;\ndoit()\n}\n\nThis is a side-effect of $^W being dynamically scoped.\n\nLexical warnings get around these limitations by allowing finer control over where warnings can\nor can't be tripped.\n"
                },
                {
                    "name": "Controlling Warnings from the Command Line",
                    "content": "There are three Command Line flags that can be used to control when warnings are (or aren't)\nproduced:\n\n-w   This is the existing flag. If the lexical warnings pragma is not used in any of your code,\nor any of the modules that you use, this flag will enable warnings everywhere. See\n\"Backward Compatibility\" for details of how this flag interacts with lexical warnings.\n\n-W   If the -W flag is used on the command line, it will enable all warnings throughout the\nprogram regardless of whether warnings were disabled locally using \"no warnings\" or \"$^W\n=0\". This includes all files that get included via \"use\", \"require\" or \"do\". Think of it as\nthe Perl equivalent of the \"lint\" command.\n\n-X   Does the exact opposite to the -W flag, i.e. it disables all warnings.\n"
                },
                {
                    "name": "Backward Compatibility",
                    "content": "If you are used to working with a version of Perl prior to the introduction of lexically scoped\nwarnings, or have code that uses both lexical warnings and $^W, this section will describe how\nthey interact.\n\nHow Lexical Warnings interact with -w/$^W:\n\n1.   If none of the three command line flags (-w, -W or -X) that control warnings is used and\nneither $^W nor the \"warnings\" pragma are used, then default warnings will be enabled and\noptional warnings disabled. This means that legacy code that doesn't attempt to control the\nwarnings will work unchanged.\n\n2.   The -w flag just sets the global $^W variable as in 5.005. This means that any legacy code\nthat currently relies on manipulating $^W to control warning behavior will still work as\nis.\n\n3.   Apart from now being a boolean, the $^W variable operates in exactly the same horrible\nuncontrolled global way, except that it cannot disable/enable default warnings.\n\n4.   If a piece of code is under the control of the \"warnings\" pragma, both the $^W variable and\nthe -w flag will be ignored for the scope of the lexical warning.\n\n5.   The only way to override a lexical warnings setting is with the -W or -X command line\nflags.\n\nThe combined effect of 3 & 4 is that it will allow code which uses the \"warnings\" pragma to\ncontrol the warning behavior of $^W-type code (using a \"local $^W=0\") if it really wants to, but\nnot vice-versa.\n"
                },
                {
                    "name": "Category Hierarchy",
                    "content": "A hierarchy of \"categories\" have been defined to allow groups of warnings to be enabled/disabled\nin isolation.\n\nThe current hierarchy is:\n\nall -+\n|\n+- closure\n|\n+- deprecated\n|\n+- exiting\n|\n+- experimental --+\n|                 |\n|                 +- experimental::alphaassertions\n|                 |\n|                 +- experimental::bitwise\n|                 |\n|                 +- experimental::constattr\n|                 |\n|                 +- experimental::declaredrefs\n|                 |\n|                 +- experimental::isa\n|                 |\n|                 +- experimental::lexicalsubs\n|                 |\n|                 +- experimental::postderef\n|                 |\n|                 +- experimental::privateuse\n|                 |\n|                 +- experimental::restrict\n|                 |\n|                 +- experimental::refaliasing\n|                 |\n|                 +- experimental::regexsets\n|                 |\n|                 +- experimental::scriptrun\n|                 |\n|                 +- experimental::signatures\n|                 |\n|                 +- experimental::smartmatch\n|                 |\n|                 +- experimental::try\n|                 |\n|                 +- experimental::unipropwildcards\n|                 |\n|                 +- experimental::vlb\n|                 |\n|                 +- experimental::win32perlio\n|\n+- glob\n|\n+- imprecision\n|\n+- io ------------+\n|                 |\n|                 +- closed\n|                 |\n|                 +- exec\n|                 |\n|                 +- layer\n|                 |\n|                 +- newline\n|                 |\n|                 +- pipe\n|                 |\n|                 +- syscalls\n|                 |\n|                 +- unopened\n|\n+- locale\n|\n+- misc\n|\n+- missing\n|\n+- numeric\n|\n+- once\n|\n+- overflow\n|\n+- pack\n|\n+- portable\n|\n+- recursion\n|\n+- redefine\n|\n+- redundant\n|\n+- regexp\n|\n+- severe --------+\n|                 |\n|                 +- debugging\n|                 |\n|                 +- inplace\n|                 |\n|                 +- internal\n|                 |\n|                 +- malloc\n|\n+- shadow\n|\n+- signal\n|\n+- substr\n|\n+- syntax --------+\n|                 |\n|                 +- ambiguous\n|                 |\n|                 +- bareword\n|                 |\n|                 +- digit\n|                 |\n|                 +- illegalproto\n|                 |\n|                 +- parenthesis\n|                 |\n|                 +- precedence\n|                 |\n|                 +- printf\n|                 |\n|                 +- prototype\n|                 |\n|                 +- qw\n|                 |\n|                 +- reserved\n|                 |\n|                 +- semicolon\n|\n+- taint\n|\n+- threads\n|\n+- uninitialized\n|\n+- unpack\n|\n+- untie\n|\n+- utf8 ----------+\n|                 |\n|                 +- nonunicode\n|                 |\n|                 +- nonchar\n|                 |\n|                 +- surrogate\n|\n+- void\n\nJust like the \"strict\" pragma any of these categories can be combined\n\nuse warnings qw(void redefine);\nno warnings qw(io syntax untie);\n\nAlso like the \"strict\" pragma, if there is more than one instance of the \"warnings\" pragma in a\ngiven scope the cumulative effect is additive.\n\nuse warnings qw(void); # only \"void\" warnings enabled\n...\nuse warnings qw(io);   # only \"void\" & \"io\" warnings enabled\n...\nno warnings qw(void);  # only \"io\" warnings enabled\n\nTo determine which category a specific warning has been assigned to see perldiag.\n\nNote: Before Perl 5.8.0, the lexical warnings category \"deprecated\" was a sub-category of the\n\"syntax\" category. It is now a top-level category in its own right.\n\nNote: Before 5.21.0, the \"missing\" lexical warnings category was internally defined to be the\nsame as the \"uninitialized\" category. It is now a top-level category in its own right.\n"
                },
                {
                    "name": "Fatal Warnings",
                    "content": "The presence of the word \"FATAL\" in the category list will escalate warnings in those categories\ninto fatal errors in that lexical scope.\n\nNOTE: FATAL warnings should be used with care, particularly \"FATAL => 'all'\".\n\nLibraries using warnings::warn for custom warning categories generally don't expect\nwarnings::warn to be fatal and can wind up in an unexpected state as a result. For XS modules\nissuing categorized warnings, such unanticipated exceptions could also expose memory leak bugs.\n\nMoreover, the Perl interpreter itself has had serious bugs involving fatalized warnings. For a\nsummary of resolved and unresolved problems as of January 2015, please see this perl5-porters\npost <http://www.nntp.perl.org/group/perl.perl5.porters/2015/01/msg225235.html>.\n\nWhile some developers find fatalizing some warnings to be a useful defensive programming\ntechnique, using \"FATAL => 'all'\" to fatalize all possible warning categories -- including\ncustom ones -- is particularly risky. Therefore, the use of \"FATAL => 'all'\" is discouraged.\n\nThe strictures module on CPAN offers one example of a warnings subset that the module's authors\nbelieve is relatively safe to fatalize.\n\nNOTE: Users of FATAL warnings, especially those using \"FATAL => 'all'\", should be fully aware\nthat they are risking future portability of their programs by doing so. Perl makes absolutely no\ncommitments to not introduce new warnings or warnings categories in the future; indeed, we\nexplicitly reserve the right to do so. Code that may not warn now may warn in a future release\nof Perl if the Perl5 development team deems it in the best interests of the community to do so.\nShould code using FATAL warnings break due to the introduction of a new warning we will NOT\nconsider it an incompatible change. Users of FATAL warnings should take special caution during\nupgrades to check to see if their code triggers any new warnings and should pay particular\nattention to the fine print of the documentation of the features they use to ensure they do not\nexploit features that are documented as risky, deprecated, or unspecified, or where the\ndocumentation says \"so don't do that\", or anything with the same sense and spirit. Use of such\nfeatures in combination with FATAL warnings is ENTIRELY AT THE USER'S RISK.\n\nThe following documentation describes how to use FATAL warnings but the perl5 porters strongly\nrecommend that you understand the risks before doing so, especially for library code intended\nfor use by others, as there is no way for downstream users to change the choice of fatal\ncategories.\n\nIn the code below, the use of \"time\", \"length\" and \"join\" can all produce a \"Useless use of xxx\nin void context\" warning.\n\nuse warnings;\n\ntime;\n\n{\nuse warnings FATAL => qw(void);\nlength \"abc\";\n}\n\njoin \"\", 1,2,3;\n\nprint \"done\\n\";\n\nWhen run it produces this output\n\nUseless use of time in void context at fatal line 3.\nUseless use of length in void context at fatal line 7.\n\nThe scope where \"length\" is used has escalated the \"void\" warnings category into a fatal error,\nso the program terminates immediately when it encounters the warning.\n\nTo explicitly turn off a \"FATAL\" warning you just disable the warning it is associated with. So,\nfor example, to disable the \"void\" warning in the example above, either of these will do the\ntrick:\n\nno warnings qw(void);\nno warnings FATAL => qw(void);\n\nIf you want to downgrade a warning that has been escalated into a fatal error back to a normal\nwarning, you can use the \"NONFATAL\" keyword. For example, the code below will promote all\nwarnings into fatal errors, except for those in the \"syntax\" category.\n\nuse warnings FATAL => 'all', NONFATAL => 'syntax';\n\nAs of Perl 5.20, instead of \"use warnings FATAL => 'all';\" you can use:\n\nuse v5.20;       # Perl 5.20 or greater is required for the following\nuse warnings 'FATAL';  # short form of \"use warnings FATAL => 'all';\"\n\nHowever, you should still heed the guidance earlier in this section against using \"use warnings\nFATAL =\" 'all';>.\n\nIf you want your program to be compatible with versions of Perl before 5.20, you must use \"use\nwarnings FATAL => 'all';\" instead. (In previous versions of Perl, the behavior of the statements\n\"use warnings 'FATAL';\", \"use warnings 'NONFATAL';\" and \"no warnings 'FATAL';\" was unspecified;\nthey did not behave as if they included the \"=> 'all'\" portion. As of 5.20, they do.)\n"
                },
                {
                    "name": "Reporting Warnings from a Module",
                    "content": "The \"warnings\" pragma provides a number of functions that are useful for module authors. These\nare used when you want to report a module-specific warning to a calling module has enabled\nwarnings via the \"warnings\" pragma.\n\nConsider the module \"MyMod::Abc\" below.\n\npackage MyMod::Abc;\n\nuse warnings::register;\n\nsub open {\nmy $path = shift;\nif ($path !~ m#^/#) {\nwarnings::warn(\"changing relative path to /var/abc\")\nif warnings::enabled();\n$path = \"/var/abc/$path\";\n}\n}\n\n1;\n\nThe call to \"warnings::register\" will create a new warnings category called \"MyMod::Abc\", i.e.\nthe new category name matches the current package name. The \"open\" function in the module will\ndisplay a warning message if it gets given a relative path as a parameter. This warnings will\nonly be displayed if the code that uses \"MyMod::Abc\" has actually enabled them with the\n\"warnings\" pragma like below.\n\nuse MyMod::Abc;\nuse warnings 'MyMod::Abc';\n...\nabc::open(\"../fred.txt\");\n\nIt is also possible to test whether the pre-defined warnings categories are set in the calling\nmodule with the \"warnings::enabled\" function. Consider this snippet of code:\n\npackage MyMod::Abc;\n\nsub open {\nif (warnings::enabled(\"deprecated\")) {\nwarnings::warn(\"deprecated\",\n\"open is deprecated, use new instead\");\n}\nnew(@);\n}\n\nsub new\n...\n1;\n\nThe function \"open\" has been deprecated, so code has been included to display a warning message\nwhenever the calling module has (at least) the \"deprecated\" warnings category enabled. Something\nlike this, say.\n\nuse warnings 'deprecated';\nuse MyMod::Abc;\n...\nMyMod::Abc::open($filename);\n\nEither the \"warnings::warn\" or \"warnings::warnif\" function should be used to actually display\nthe warnings message. This is because they can make use of the feature that allows warnings to\nbe escalated into fatal errors. So in this case\n\nuse MyMod::Abc;\nuse warnings FATAL => 'MyMod::Abc';\n...\nMyMod::Abc::open('../fred.txt');\n\nthe \"warnings::warnif\" function will detect this and die after displaying the warning message.\n\nThe three warnings functions, \"warnings::warn\", \"warnings::warnif\" and \"warnings::enabled\" can\noptionally take an object reference in place of a category name. In this case the functions will\nuse the class name of the object as the warnings category.\n\nConsider this example:\n\npackage Original;\n\nno warnings;\nuse warnings::register;\n\nsub new\n{\nmy $class = shift;\nbless [], $class;\n}\n\nsub check\n{\nmy $self = shift;\nmy $value = shift;\n\nif ($value % 2 && warnings::enabled($self))\n{ warnings::warn($self, \"Odd numbers are unsafe\") }\n}\n\nsub doit\n{\nmy $self = shift;\nmy $value = shift;\n$self->check($value);\n# ...\n}\n\n1;\n\npackage Derived;\n\nuse warnings::register;\nuse Original;\nour @ISA = qw( Original );\nsub new\n{\nmy $class = shift;\nbless [], $class;\n}\n\n\n1;\n\nThe code below makes use of both modules, but it only enables warnings from \"Derived\".\n\nuse Original;\nuse Derived;\nuse warnings 'Derived';\nmy $x = Original->new();\n$x->doit(1);\nmy $y = Derived->new();\n$x->doit(1);\n\nWhen this code is run only the \"Derived\" object, $y, will generate a warning.\n\nOdd numbers are unsafe at main.pl line 7\n\nNotice also that the warning is reported at the line where the object is first used.\n\nWhen registering new categories of warning, you can supply more names to warnings::register like\nthis:\n\npackage MyModule;\nuse warnings::register qw(format precision);\n\n...\n\nwarnings::warnif('MyModule::format', '...');\n"
                }
            ]
        },
        "FUNCTIONS": {
            "content": "Note: The functions with names ending in \"atlevel\" were added in Perl 5.28.\n\nuse warnings::register\nCreates a new warnings category with the same name as the package where the call to the\npragma is used.\n\nwarnings::enabled()\nUse the warnings category with the same name as the current package.\n\nReturn TRUE if that warnings category is enabled in the calling module. Otherwise returns\nFALSE.\n\nwarnings::enabled($category)\nReturn TRUE if the warnings category, $category, is enabled in the calling module. Otherwise\nreturns FALSE.\n\nwarnings::enabled($object)\nUse the name of the class for the object reference, $object, as the warnings category.\n\nReturn TRUE if that warnings category is enabled in the first scope where the object is\nused. Otherwise returns FALSE.\n\nwarnings::enabledatlevel($category, $level)\nLike \"warnings::enabled\", but $level specifies the exact call frame, 0 being the immediate\ncaller.\n\nwarnings::fatalenabled()\nReturn TRUE if the warnings category with the same name as the current package has been set\nto FATAL in the calling module. Otherwise returns FALSE.\n\nwarnings::fatalenabled($category)\nReturn TRUE if the warnings category $category has been set to FATAL in the calling module.\nOtherwise returns FALSE.\n\nwarnings::fatalenabled($object)\nUse the name of the class for the object reference, $object, as the warnings category.\n\nReturn TRUE if that warnings category has been set to FATAL in the first scope where the\nobject is used. Otherwise returns FALSE.\n\nwarnings::fatalenabledatlevel($category, $level)\nLike \"warnings::fatalenabled\", but $level specifies the exact call frame, 0 being the\nimmediate caller.\n\nwarnings::warn($message)\nPrint $message to STDERR.\n\nUse the warnings category with the same name as the current package.\n\nIf that warnings category has been set to \"FATAL\" in the calling module then die. Otherwise\nreturn.\n\nwarnings::warn($category, $message)\nPrint $message to STDERR.\n\nIf the warnings category, $category, has been set to \"FATAL\" in the calling module then die.\nOtherwise return.\n\nwarnings::warn($object, $message)\nPrint $message to STDERR.\n\nUse the name of the class for the object reference, $object, as the warnings category.\n\nIf that warnings category has been set to \"FATAL\" in the scope where $object is first used\nthen die. Otherwise return.\n\nwarnings::warnatlevel($category, $level, $message)\nLike \"warnings::warn\", but $level specifies the exact call frame, 0 being the immediate\ncaller.\n\nwarnings::warnif($message)\nEquivalent to:\n\nif (warnings::enabled())\n{ warnings::warn($message) }\n\nwarnings::warnif($category, $message)\nEquivalent to:\n\nif (warnings::enabled($category))\n{ warnings::warn($category, $message) }\n\nwarnings::warnif($object, $message)\nEquivalent to:\n\nif (warnings::enabled($object))\n{ warnings::warn($object, $message) }\n\nwarnings::warnifatlevel($category, $level, $message)\nLike \"warnings::warnif\", but $level specifies the exact call frame, 0 being the immediate\ncaller.\n\nwarnings::registercategories(@names)\nThis registers warning categories for the given names and is primarily for use by the\nwarnings::register pragma.\n\nSee also \"Pragmatic Modules\" in perlmodlib and perldiag.\n",
            "subsections": []
        }
    },
    "summary": "warnings - Perl pragma to control optional warnings",
    "flags": [],
    "examples": [],
    "see_also": []
}